diff --git a/custom_components/huesensor/__init__.py b/custom_components/huesensor/__init__.py index 5f0a9fb..eb53e8d 100644 --- a/custom_components/huesensor/__init__.py +++ b/custom_components/huesensor/__init__.py @@ -1 +1,26 @@ """The huesensors component.""" +import asyncio +import async_timeout + + +def get_bridges(hass): + from homeassistant.components import hue + from homeassistant.components.hue.bridge import HueBridge + + return [ + entry + for entry in hass.data[hue.DOMAIN].values() + if isinstance(entry, HueBridge) and entry.api + ] + + +async def update_api(api): + import aiohue + + try: + with async_timeout.timeout(10): + await api.update() + except (asyncio.TimeoutError, aiohue.AiohueException) as err: + _LOGGER.debug("Failed to fetch sensors: %s", err) + return False + return True diff --git a/custom_components/huesensor/binary_sensor.py b/custom_components/huesensor/binary_sensor.py index 9fa5fe6..3fe34f3 100644 --- a/custom_components/huesensor/binary_sensor.py +++ b/custom_components/huesensor/binary_sensor.py @@ -12,6 +12,8 @@ from homeassistant.components.binary_sensor import BinarySensorDevice from homeassistant.helpers.event import async_track_time_interval +from . import get_bridges, update_api + DEPENDENCIES = ["hue"] @@ -113,29 +115,6 @@ def parse_sml(response): return data -def get_bridges(hass): - from homeassistant.components import hue - from homeassistant.components.hue.bridge import HueBridge - - return [ - entry - for entry in hass.data[hue.DOMAIN].values() - if isinstance(entry, HueBridge) and entry.api - ] - - -async def update_api(api): - import aiohue - - try: - with async_timeout.timeout(10): - await api.update() - except (asyncio.TimeoutError, aiohue.AiohueException) as err: - _LOGGER.debug("Failed to fetch sensors: %s", err) - return False - return True - - async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Initialise Hue Bridge connection.""" data = HueSensorData(hass, async_add_entities) diff --git a/custom_components/huesensor/device_tracker.py b/custom_components/huesensor/device_tracker.py index 50eaf58..fa542c4 100644 --- a/custom_components/huesensor/device_tracker.py +++ b/custom_components/huesensor/device_tracker.py @@ -26,6 +26,7 @@ from homeassistant.util import slugify from homeassistant.components import zone +from . import get_bridges, update_api DEPENDENCIES = ["hue"] @@ -35,29 +36,6 @@ DEFAULT_SCAN_INTERVAL = timedelta(seconds=30) -def get_bridges(hass): - from homeassistant.components import hue - from homeassistant.components.hue.bridge import HueBridge - - return [ - entry - for entry in hass.data[hue.DOMAIN].values() - if isinstance(entry, HueBridge) and entry.api - ] - - -async def update_api(api): - import aiohue - - try: - with async_timeout.timeout(10): - await api.update() - except (asyncio.TimeoutError, aiohue.AiohueException) as err: - _LOGGER.debug("Failed to fetch sensors: %s", err) - return False - return True - - async def async_setup_scanner(hass, config, async_see, discovery_info=None): interval = config.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL) scanner = HueDeviceScanner(hass, async_see) diff --git a/custom_components/huesensor/remote.py b/custom_components/huesensor/remote.py index 3a52535..c86f78a 100644 --- a/custom_components/huesensor/remote.py +++ b/custom_components/huesensor/remote.py @@ -18,6 +18,8 @@ from homeassistant.helpers.event import async_track_time_interval +from . import get_bridges, update_api + DEPENDENCIES = ["hue"] @@ -211,29 +213,6 @@ def parse_z3_switch(response): return data -def get_bridges(hass): - from homeassistant.components import hue - from homeassistant.components.hue.bridge import HueBridge - - return [ - entry - for entry in hass.data[hue.DOMAIN].values() - if isinstance(entry, HueBridge) and entry.api - ] - - -async def update_api(api): - import aiohue - - try: - with async_timeout.timeout(10): - await api.update() - except (asyncio.TimeoutError, aiohue.AiohueException) as err: - _LOGGER.debug("Failed to fetch sensors: %s", err) - return False - return True - - async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Initialise Hue Bridge connection.""" data = HueRemoteData(hass, async_add_entities)