From 86347a3d5f0f8b73e9fc519989524fcf53a66189 Mon Sep 17 00:00:00 2001 From: Patrik <21142447+ggravlingen@users.noreply.github.com> Date: Mon, 21 Oct 2019 21:42:17 +0200 Subject: [PATCH] Refactor Tradfri light group (#27714) * Set same manufacturer name of gateway as of devices * Refactor Tradfri light group * Restore should_poll and async_update --- .../components/tradfri/base_class.py | 23 ++++-- homeassistant/components/tradfri/const.py | 2 +- homeassistant/components/tradfri/light.py | 82 +++++-------------- 3 files changed, 38 insertions(+), 69 deletions(-) diff --git a/homeassistant/components/tradfri/base_class.py b/homeassistant/components/tradfri/base_class.py index 632ce6b164e..ba90fe05d1e 100644 --- a/homeassistant/components/tradfri/base_class.py +++ b/homeassistant/components/tradfri/base_class.py @@ -18,7 +18,6 @@ class TradfriBaseClass(Entity): def __init__(self, device, api, gateway_id): """Initialize a device.""" - self._available = True self._api = api self._device = None self._device_control = None @@ -33,7 +32,6 @@ class TradfriBaseClass(Entity): def _async_start_observe(self, exc=None): """Start observation of device.""" if exc: - self._available = False self.async_schedule_update_ha_state() _LOGGER.warning("Observation failed for %s", self._name, exc_info=exc) @@ -52,11 +50,6 @@ class TradfriBaseClass(Entity): """Start thread when added to hass.""" self._async_start_observe() - @property - def available(self): - """Return True if entity is available.""" - return self._available - @property def name(self): """Return the display name of this device.""" @@ -82,7 +75,6 @@ class TradfriBaseClass(Entity): """Refresh the device data.""" self._device = device self._name = device.name - self._available = device.reachable class TradfriBaseDevice(TradfriBaseClass): @@ -91,6 +83,16 @@ class TradfriBaseDevice(TradfriBaseClass): All devices should inherit from this class. """ + def __init__(self, device, api, gateway_id): + """Initialize a device.""" + super().__init__(device, api, gateway_id) + self._available = True + + @property + def available(self): + """Return True if entity is available.""" + return self._available + @property def device_info(self): """Return the device info.""" @@ -104,3 +106,8 @@ class TradfriBaseDevice(TradfriBaseClass): "sw_version": info.firmware_version, "via_device": (DOMAIN, self._gateway_id), } + + def _refresh(self, device): + """Refresh the device data.""" + super()._refresh(device) + self._available = device.reachable diff --git a/homeassistant/components/tradfri/const.py b/homeassistant/components/tradfri/const.py index a7acfcbf876..038f0e91c76 100644 --- a/homeassistant/components/tradfri/const.py +++ b/homeassistant/components/tradfri/const.py @@ -7,7 +7,7 @@ ATTR_HUE = "hue" ATTR_SAT = "saturation" ATTR_TRADFRI_GATEWAY = "Gateway" ATTR_TRADFRI_GATEWAY_MODEL = "E1526" -ATTR_TRADFRI_MANUFACTURER = "IKEA" +ATTR_TRADFRI_MANUFACTURER = "IKEA of Sweden" ATTR_TRANSITION_TIME = "transition_time" CONF_ALLOW_TRADFRI_GROUPS = "allow_tradfri_groups" CONF_IDENTITY = "identity" diff --git a/homeassistant/components/tradfri/light.py b/homeassistant/components/tradfri/light.py index 089f80223e8..9ee3c5d6a8c 100644 --- a/homeassistant/components/tradfri/light.py +++ b/homeassistant/components/tradfri/light.py @@ -1,8 +1,6 @@ """Support for IKEA Tradfri lights.""" import logging -from pytradfri.error import PytradfriError - import homeassistant.util.color as color_util from homeassistant.components.light import ( ATTR_BRIGHTNESS, @@ -14,8 +12,7 @@ from homeassistant.components.light import ( SUPPORT_COLOR, SUPPORT_COLOR_TEMP, ) -from homeassistant.core import callback -from .base_class import TradfriBaseDevice +from .base_class import TradfriBaseDevice, TradfriBaseClass from .const import ( ATTR_DIMMER, ATTR_HUE, @@ -51,50 +48,47 @@ async def async_setup_entry(hass, config_entry, async_add_entities): async_add_entities(TradfriGroup(group, api, gateway_id) for group in groups) -class TradfriGroup(Light): - """The platform class required by hass.""" +class TradfriGroup(TradfriBaseClass, Light): + """The platform class for light groups required by hass.""" - def __init__(self, group, api, gateway_id): + def __init__(self, device, api, gateway_id): """Initialize a Group.""" - self._api = api - self._unique_id = f"group-{gateway_id}-{group.id}" - self._group = group - self._name = group.name + super().__init__(device, api, gateway_id) - self._refresh(group) + self._unique_id = f"group-{gateway_id}-{device.id}" - async def async_added_to_hass(self): - """Start thread when added to hass.""" - self._async_start_observe() + self._refresh(device) @property - def unique_id(self): - """Return unique ID for this group.""" - return self._unique_id + def should_poll(self): + """Poll needed for tradfri groups.""" + return True + + async def async_update(self): + """Fetch new state data for the group. + + This method is required for groups to update properly. + """ + await self._api(self._device.update()) @property def supported_features(self): """Flag supported features.""" return SUPPORTED_GROUP_FEATURES - @property - def name(self): - """Return the display name of this group.""" - return self._name - @property def is_on(self): """Return true if group lights are on.""" - return self._group.state + return self._device.state @property def brightness(self): """Return the brightness of the group lights.""" - return self._group.dimmer + return self._device.dimmer async def async_turn_off(self, **kwargs): """Instruct the group lights to turn off.""" - await self._api(self._group.set_state(0)) + await self._api(self._device.set_state(0)) async def async_turn_on(self, **kwargs): """Instruct the group lights to turn on, or dim.""" @@ -106,41 +100,9 @@ class TradfriGroup(Light): if kwargs[ATTR_BRIGHTNESS] == 255: kwargs[ATTR_BRIGHTNESS] = 254 - await self._api(self._group.set_dimmer(kwargs[ATTR_BRIGHTNESS], **keys)) + await self._api(self._device.set_dimmer(kwargs[ATTR_BRIGHTNESS], **keys)) else: - await self._api(self._group.set_state(1)) - - @callback - def _async_start_observe(self, exc=None): - """Start observation of light.""" - if exc: - _LOGGER.warning("Observation failed for %s", self._name, exc_info=exc) - - try: - cmd = self._group.observe( - callback=self._observe_update, - err_callback=self._async_start_observe, - duration=0, - ) - self.hass.async_create_task(self._api(cmd)) - except PytradfriError as err: - _LOGGER.warning("Observation failed, trying again", exc_info=err) - self._async_start_observe() - - def _refresh(self, group): - """Refresh the light data.""" - self._group = group - self._name = group.name - - @callback - def _observe_update(self, tradfri_device): - """Receive new state data for this light.""" - self._refresh(tradfri_device) - self.async_schedule_update_ha_state() - - async def async_update(self): - """Fetch new state data for the group.""" - await self._api(self._group.update()) + await self._api(self._device.set_state(1)) class TradfriLight(TradfriBaseDevice, Light):