Change fix property to _attr for tradfri (#55691)

This commit is contained in:
jan iversen 2021-09-06 08:49:00 +02:00 committed by GitHub
parent 1b3530a3f8
commit 0dd128af77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 64 deletions

View File

@ -32,6 +32,8 @@ class TradfriBaseClass(Entity):
All devices and groups should ultimately inherit from this class. All devices and groups should ultimately inherit from this class.
""" """
_attr_should_poll = False
def __init__(self, device, api, gateway_id): def __init__(self, device, api, gateway_id):
"""Initialize a device.""" """Initialize a device."""
self._api = handle_error(api) self._api = handle_error(api)
@ -39,9 +41,6 @@ class TradfriBaseClass(Entity):
self._device_control = None self._device_control = None
self._device_data = None self._device_data = None
self._gateway_id = gateway_id self._gateway_id = gateway_id
self._name = None
self._unique_id = None
self._refresh(device) self._refresh(device)
@callback @callback
@ -49,7 +48,7 @@ class TradfriBaseClass(Entity):
"""Start observation of device.""" """Start observation of device."""
if exc: if exc:
self.async_write_ha_state() self.async_write_ha_state()
_LOGGER.warning("Observation failed for %s", self._name, exc_info=exc) _LOGGER.warning("Observation failed for %s", self._attr_name, exc_info=exc)
try: try:
cmd = self._device.observe( cmd = self._device.observe(
@ -66,21 +65,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 name(self):
"""Return the display name of this device."""
return self._name
@property
def should_poll(self):
"""No polling needed for tradfri device."""
return False
@property
def unique_id(self):
"""Return unique ID for device."""
return self._unique_id
@callback @callback
def _observe_update(self, device): def _observe_update(self, device):
"""Receive new state data for this device.""" """Receive new state data for this device."""
@ -90,7 +74,7 @@ class TradfriBaseClass(Entity):
def _refresh(self, device): def _refresh(self, device):
"""Refresh the device data.""" """Refresh the device data."""
self._device = device self._device = device
self._name = device.name self._attr_name = device.name
class TradfriBaseDevice(TradfriBaseClass): class TradfriBaseDevice(TradfriBaseClass):
@ -99,16 +83,6 @@ 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."""
@ -118,7 +92,7 @@ class TradfriBaseDevice(TradfriBaseClass):
"identifiers": {(DOMAIN, self._device.id)}, "identifiers": {(DOMAIN, self._device.id)},
"manufacturer": info.manufacturer, "manufacturer": info.manufacturer,
"model": info.model_number, "model": info.model_number,
"name": self._name, "name": self._attr_name,
"sw_version": info.firmware_version, "sw_version": info.firmware_version,
"via_device": (DOMAIN, self._gateway_id), "via_device": (DOMAIN, self._gateway_id),
} }
@ -126,4 +100,4 @@ class TradfriBaseDevice(TradfriBaseClass):
def _refresh(self, device): def _refresh(self, device):
"""Refresh the device data.""" """Refresh the device data."""
super()._refresh(device) super()._refresh(device)
self._available = device.reachable self._attr_available = device.reachable

View File

@ -24,7 +24,7 @@ class TradfriCover(TradfriBaseDevice, CoverEntity):
def __init__(self, device, api, gateway_id): def __init__(self, device, api, gateway_id):
"""Initialize a cover.""" """Initialize a cover."""
super().__init__(device, api, gateway_id) super().__init__(device, api, gateway_id)
self._unique_id = f"{gateway_id}-{device.id}" self._attr_unique_id = f"{gateway_id}-{device.id}"
self._refresh(device) self._refresh(device)

View File

@ -48,19 +48,16 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class TradfriGroup(TradfriBaseClass, LightEntity): class TradfriGroup(TradfriBaseClass, LightEntity):
"""The platform class for light groups required by hass.""" """The platform class for light groups required by hass."""
_attr_supported_features = SUPPORTED_GROUP_FEATURES
def __init__(self, device, api, gateway_id): def __init__(self, device, api, gateway_id):
"""Initialize a Group.""" """Initialize a Group."""
super().__init__(device, api, gateway_id) super().__init__(device, api, gateway_id)
self._unique_id = f"group-{gateway_id}-{device.id}" self._attr_unique_id = f"group-{gateway_id}-{device.id}"
self._attr_should_poll = True
self._refresh(device) self._refresh(device)
@property
def should_poll(self):
"""Poll needed for tradfri groups."""
return True
async def async_update(self): async def async_update(self):
"""Fetch new state data for the group. """Fetch new state data for the group.
@ -68,11 +65,6 @@ class TradfriGroup(TradfriBaseClass, LightEntity):
""" """
await self._api(self._device.update()) await self._api(self._device.update())
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORTED_GROUP_FEATURES
@property @property
def is_on(self): def is_on(self):
"""Return true if group lights are on.""" """Return true if group lights are on."""
@ -108,7 +100,7 @@ class TradfriLight(TradfriBaseDevice, LightEntity):
def __init__(self, device, api, gateway_id): def __init__(self, device, api, gateway_id):
"""Initialize a Light.""" """Initialize a Light."""
super().__init__(device, api, gateway_id) super().__init__(device, api, gateway_id)
self._unique_id = f"light-{gateway_id}-{device.id}" self._attr_unique_id = f"light-{gateway_id}-{device.id}"
self._hs_color = None self._hs_color = None
# Calculate supported features # Calculate supported features
@ -119,24 +111,11 @@ class TradfriLight(TradfriBaseDevice, LightEntity):
_features |= SUPPORT_COLOR | SUPPORT_COLOR_TEMP _features |= SUPPORT_COLOR | SUPPORT_COLOR_TEMP
if device.light_control.can_set_temp: if device.light_control.can_set_temp:
_features |= SUPPORT_COLOR_TEMP _features |= SUPPORT_COLOR_TEMP
self._features = _features self._attr_supported_features = _features
self._refresh(device) self._refresh(device)
self._attr_min_mireds = self._device_control.min_mireds
@property self._attr_max_mireds = self._device_control.max_mireds
def min_mireds(self):
"""Return the coldest color_temp that this light supports."""
return self._device_control.min_mireds
@property
def max_mireds(self):
"""Return the warmest color_temp that this light supports."""
return self._device_control.max_mireds
@property
def supported_features(self):
"""Flag supported features."""
return self._features
@property @property
def is_on(self): def is_on(self):

View File

@ -35,7 +35,7 @@ class TradfriSensor(TradfriBaseDevice, SensorEntity):
def __init__(self, device, api, gateway_id): def __init__(self, device, api, gateway_id):
"""Initialize the device.""" """Initialize the device."""
super().__init__(device, api, gateway_id) super().__init__(device, api, gateway_id)
self._unique_id = f"{gateway_id}-{device.id}" self._attr_unique_id = f"{gateway_id}-{device.id}"
@property @property
def native_value(self): def native_value(self):

View File

@ -25,7 +25,7 @@ class TradfriSwitch(TradfriBaseDevice, SwitchEntity):
def __init__(self, device, api, gateway_id): def __init__(self, device, api, gateway_id):
"""Initialize a switch.""" """Initialize a switch."""
super().__init__(device, api, gateway_id) super().__init__(device, api, gateway_id)
self._unique_id = f"{gateway_id}-{device.id}" self._attr_unique_id = f"{gateway_id}-{device.id}"
def _refresh(self, device): def _refresh(self, device):
"""Refresh the switch data.""" """Refresh the switch data."""