mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 04:07:08 +00:00
Refactor Tradfri light group (#27714)
* Set same manufacturer name of gateway as of devices * Refactor Tradfri light group * Restore should_poll and async_update
This commit is contained in:
parent
a0c50f4794
commit
86347a3d5f
@ -18,7 +18,6 @@ class TradfriBaseClass(Entity):
|
|||||||
|
|
||||||
def __init__(self, device, api, gateway_id):
|
def __init__(self, device, api, gateway_id):
|
||||||
"""Initialize a device."""
|
"""Initialize a device."""
|
||||||
self._available = True
|
|
||||||
self._api = api
|
self._api = api
|
||||||
self._device = None
|
self._device = None
|
||||||
self._device_control = None
|
self._device_control = None
|
||||||
@ -33,7 +32,6 @@ class TradfriBaseClass(Entity):
|
|||||||
def _async_start_observe(self, exc=None):
|
def _async_start_observe(self, exc=None):
|
||||||
"""Start observation of device."""
|
"""Start observation of device."""
|
||||||
if exc:
|
if exc:
|
||||||
self._available = False
|
|
||||||
self.async_schedule_update_ha_state()
|
self.async_schedule_update_ha_state()
|
||||||
_LOGGER.warning("Observation failed for %s", self._name, exc_info=exc)
|
_LOGGER.warning("Observation failed for %s", self._name, exc_info=exc)
|
||||||
|
|
||||||
@ -52,11 +50,6 @@ class TradfriBaseClass(Entity):
|
|||||||
"""Start thread when added to hass."""
|
"""Start thread when added to hass."""
|
||||||
self._async_start_observe()
|
self._async_start_observe()
|
||||||
|
|
||||||
@property
|
|
||||||
def available(self):
|
|
||||||
"""Return True if entity is available."""
|
|
||||||
return self._available
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the display name of this device."""
|
"""Return the display name of this device."""
|
||||||
@ -82,7 +75,6 @@ class TradfriBaseClass(Entity):
|
|||||||
"""Refresh the device data."""
|
"""Refresh the device data."""
|
||||||
self._device = device
|
self._device = device
|
||||||
self._name = device.name
|
self._name = device.name
|
||||||
self._available = device.reachable
|
|
||||||
|
|
||||||
|
|
||||||
class TradfriBaseDevice(TradfriBaseClass):
|
class TradfriBaseDevice(TradfriBaseClass):
|
||||||
@ -91,6 +83,16 @@ class TradfriBaseDevice(TradfriBaseClass):
|
|||||||
All devices should inherit from this class.
|
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
|
@property
|
||||||
def device_info(self):
|
def device_info(self):
|
||||||
"""Return the device info."""
|
"""Return the device info."""
|
||||||
@ -104,3 +106,8 @@ class TradfriBaseDevice(TradfriBaseClass):
|
|||||||
"sw_version": info.firmware_version,
|
"sw_version": info.firmware_version,
|
||||||
"via_device": (DOMAIN, self._gateway_id),
|
"via_device": (DOMAIN, self._gateway_id),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def _refresh(self, device):
|
||||||
|
"""Refresh the device data."""
|
||||||
|
super()._refresh(device)
|
||||||
|
self._available = device.reachable
|
||||||
|
@ -7,7 +7,7 @@ ATTR_HUE = "hue"
|
|||||||
ATTR_SAT = "saturation"
|
ATTR_SAT = "saturation"
|
||||||
ATTR_TRADFRI_GATEWAY = "Gateway"
|
ATTR_TRADFRI_GATEWAY = "Gateway"
|
||||||
ATTR_TRADFRI_GATEWAY_MODEL = "E1526"
|
ATTR_TRADFRI_GATEWAY_MODEL = "E1526"
|
||||||
ATTR_TRADFRI_MANUFACTURER = "IKEA"
|
ATTR_TRADFRI_MANUFACTURER = "IKEA of Sweden"
|
||||||
ATTR_TRANSITION_TIME = "transition_time"
|
ATTR_TRANSITION_TIME = "transition_time"
|
||||||
CONF_ALLOW_TRADFRI_GROUPS = "allow_tradfri_groups"
|
CONF_ALLOW_TRADFRI_GROUPS = "allow_tradfri_groups"
|
||||||
CONF_IDENTITY = "identity"
|
CONF_IDENTITY = "identity"
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
"""Support for IKEA Tradfri lights."""
|
"""Support for IKEA Tradfri lights."""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from pytradfri.error import PytradfriError
|
|
||||||
|
|
||||||
import homeassistant.util.color as color_util
|
import homeassistant.util.color as color_util
|
||||||
from homeassistant.components.light import (
|
from homeassistant.components.light import (
|
||||||
ATTR_BRIGHTNESS,
|
ATTR_BRIGHTNESS,
|
||||||
@ -14,8 +12,7 @@ from homeassistant.components.light import (
|
|||||||
SUPPORT_COLOR,
|
SUPPORT_COLOR,
|
||||||
SUPPORT_COLOR_TEMP,
|
SUPPORT_COLOR_TEMP,
|
||||||
)
|
)
|
||||||
from homeassistant.core import callback
|
from .base_class import TradfriBaseDevice, TradfriBaseClass
|
||||||
from .base_class import TradfriBaseDevice
|
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_DIMMER,
|
ATTR_DIMMER,
|
||||||
ATTR_HUE,
|
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)
|
async_add_entities(TradfriGroup(group, api, gateway_id) for group in groups)
|
||||||
|
|
||||||
|
|
||||||
class TradfriGroup(Light):
|
class TradfriGroup(TradfriBaseClass, Light):
|
||||||
"""The platform class required by hass."""
|
"""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."""
|
"""Initialize a Group."""
|
||||||
self._api = api
|
super().__init__(device, api, gateway_id)
|
||||||
self._unique_id = f"group-{gateway_id}-{group.id}"
|
|
||||||
self._group = group
|
|
||||||
self._name = group.name
|
|
||||||
|
|
||||||
self._refresh(group)
|
self._unique_id = f"group-{gateway_id}-{device.id}"
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
self._refresh(device)
|
||||||
"""Start thread when added to hass."""
|
|
||||||
self._async_start_observe()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def should_poll(self):
|
||||||
"""Return unique ID for this group."""
|
"""Poll needed for tradfri groups."""
|
||||||
return self._unique_id
|
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
|
@property
|
||||||
def supported_features(self):
|
def supported_features(self):
|
||||||
"""Flag supported features."""
|
"""Flag supported features."""
|
||||||
return SUPPORTED_GROUP_FEATURES
|
return SUPPORTED_GROUP_FEATURES
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the display name of this group."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return true if group lights are on."""
|
"""Return true if group lights are on."""
|
||||||
return self._group.state
|
return self._device.state
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def brightness(self):
|
def brightness(self):
|
||||||
"""Return the brightness of the group lights."""
|
"""Return the brightness of the group lights."""
|
||||||
return self._group.dimmer
|
return self._device.dimmer
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs):
|
async def async_turn_off(self, **kwargs):
|
||||||
"""Instruct the group lights to turn off."""
|
"""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):
|
async def async_turn_on(self, **kwargs):
|
||||||
"""Instruct the group lights to turn on, or dim."""
|
"""Instruct the group lights to turn on, or dim."""
|
||||||
@ -106,41 +100,9 @@ class TradfriGroup(Light):
|
|||||||
if kwargs[ATTR_BRIGHTNESS] == 255:
|
if kwargs[ATTR_BRIGHTNESS] == 255:
|
||||||
kwargs[ATTR_BRIGHTNESS] = 254
|
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:
|
else:
|
||||||
await self._api(self._group.set_state(1))
|
await self._api(self._device.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())
|
|
||||||
|
|
||||||
|
|
||||||
class TradfriLight(TradfriBaseDevice, Light):
|
class TradfriLight(TradfriBaseDevice, Light):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user