mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 02:07:09 +00:00
When tradfri device goes offline set attr_available false (#58487)
This commit is contained in:
parent
39998f5387
commit
55534cfedd
@ -59,31 +59,38 @@ class TradfriBaseClass(Entity):
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize a device."""
|
"""Initialize a device."""
|
||||||
self._api = handle_error(api)
|
self._api = handle_error(api)
|
||||||
|
self._attr_name = device.name
|
||||||
|
self._attr_available = device.reachable
|
||||||
self._device: Device = device
|
self._device: Device = device
|
||||||
self._device_control: BlindControl | LightControl | SocketControl | SignalRepeaterControl | AirPurifierControl | None = (
|
self._device_control: BlindControl | LightControl | SocketControl | SignalRepeaterControl | AirPurifierControl | None = (
|
||||||
None
|
None
|
||||||
)
|
)
|
||||||
self._device_data: Socket | Light | Blind | AirPurifier | None = None
|
self._device_data: Socket | Light | Blind | AirPurifier | None = None
|
||||||
self._gateway_id = gateway_id
|
self._gateway_id = gateway_id
|
||||||
self._refresh(device)
|
|
||||||
|
async def _async_run_observe(self, cmd: Command) -> None:
|
||||||
|
"""Run observe in a coroutine."""
|
||||||
|
try:
|
||||||
|
await self._api(cmd)
|
||||||
|
except PytradfriError as err:
|
||||||
|
self._attr_available = False
|
||||||
|
self.async_write_ha_state()
|
||||||
|
_LOGGER.warning("Observation failed, trying again", exc_info=err)
|
||||||
|
self._async_start_observe()
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_start_observe(self, exc: Exception | None = None) -> None:
|
def _async_start_observe(self, exc: Exception | None = None) -> None:
|
||||||
"""Start observation of device."""
|
"""Start observation of device."""
|
||||||
if exc:
|
if exc:
|
||||||
|
self._attr_available = False
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
_LOGGER.warning("Observation failed for %s", self._attr_name, exc_info=exc)
|
_LOGGER.warning("Observation failed for %s", self._attr_name, exc_info=exc)
|
||||||
|
|
||||||
try:
|
|
||||||
cmd = self._device.observe(
|
cmd = self._device.observe(
|
||||||
callback=self._observe_update,
|
callback=self._observe_update,
|
||||||
err_callback=self._async_start_observe,
|
err_callback=self._async_start_observe,
|
||||||
duration=0,
|
duration=0,
|
||||||
)
|
)
|
||||||
self.hass.async_create_task(self._api(cmd))
|
self.hass.async_create_task(self._async_run_observe(cmd))
|
||||||
except PytradfriError as err:
|
|
||||||
_LOGGER.warning("Observation failed, trying again", exc_info=err)
|
|
||||||
self._async_start_observe()
|
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Start thread when added to hass."""
|
"""Start thread when added to hass."""
|
||||||
@ -93,12 +100,14 @@ class TradfriBaseClass(Entity):
|
|||||||
def _observe_update(self, device: Device) -> None:
|
def _observe_update(self, device: Device) -> None:
|
||||||
"""Receive new state data for this device."""
|
"""Receive new state data for this device."""
|
||||||
self._refresh(device)
|
self._refresh(device)
|
||||||
self.async_write_ha_state()
|
|
||||||
|
|
||||||
def _refresh(self, device: Device) -> None:
|
def _refresh(self, device: Device, write_ha: bool = True) -> None:
|
||||||
"""Refresh the device data."""
|
"""Refresh the device data."""
|
||||||
self._device = device
|
self._device = device
|
||||||
self._attr_name = device.name
|
self._attr_name = device.name
|
||||||
|
self._attr_available = device.reachable
|
||||||
|
if write_ha:
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
|
||||||
class TradfriBaseDevice(TradfriBaseClass):
|
class TradfriBaseDevice(TradfriBaseClass):
|
||||||
@ -119,8 +128,3 @@ 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: Device) -> None:
|
|
||||||
"""Refresh the device data."""
|
|
||||||
super()._refresh(device)
|
|
||||||
self._attr_available = device.reachable
|
|
||||||
|
@ -41,10 +41,9 @@ class TradfriCover(TradfriBaseDevice, CoverEntity):
|
|||||||
gateway_id: str,
|
gateway_id: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize a cover."""
|
"""Initialize a cover."""
|
||||||
super().__init__(device, api, gateway_id)
|
|
||||||
self._attr_unique_id = f"{gateway_id}-{device.id}"
|
self._attr_unique_id = f"{gateway_id}-{device.id}"
|
||||||
|
super().__init__(device, api, gateway_id)
|
||||||
self._refresh(device)
|
self._refresh(device, write_ha=False)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self) -> dict[str, str] | None:
|
def extra_state_attributes(self) -> dict[str, str] | None:
|
||||||
@ -90,11 +89,10 @@ class TradfriCover(TradfriBaseDevice, CoverEntity):
|
|||||||
"""Return if the cover is closed or not."""
|
"""Return if the cover is closed or not."""
|
||||||
return self.current_cover_position == 0
|
return self.current_cover_position == 0
|
||||||
|
|
||||||
def _refresh(self, device: Command) -> None:
|
def _refresh(self, device: Command, write_ha: bool = True) -> None:
|
||||||
"""Refresh the cover data."""
|
"""Refresh the cover data."""
|
||||||
super()._refresh(device)
|
|
||||||
self._device = device
|
|
||||||
|
|
||||||
# Caching of BlindControl and cover object
|
# Caching of BlindControl and cover object
|
||||||
|
self._device = device
|
||||||
self._device_control = device.blind_control
|
self._device_control = device.blind_control
|
||||||
self._device_data = device.blind_control.blinds[0]
|
self._device_data = device.blind_control.blinds[0]
|
||||||
|
super()._refresh(device, write_ha=write_ha)
|
||||||
|
@ -166,10 +166,9 @@ class TradfriAirPurifierFan(TradfriBaseDevice, FanEntity):
|
|||||||
return
|
return
|
||||||
await self._api(self._device_control.set_mode(0))
|
await self._api(self._device_control.set_mode(0))
|
||||||
|
|
||||||
def _refresh(self, device: Command) -> None:
|
def _refresh(self, device: Command, write_ha: bool = True) -> None:
|
||||||
"""Refresh the purifier data."""
|
"""Refresh the purifier data."""
|
||||||
super()._refresh(device)
|
|
||||||
|
|
||||||
# Caching of air purifier control and purifier object
|
# Caching of air purifier control and purifier object
|
||||||
self._device_control = device.air_purifier_control
|
self._device_control = device.air_purifier_control
|
||||||
self._device_data = device.air_purifier_control.air_purifiers[0]
|
self._device_data = device.air_purifier_control.air_purifiers[0]
|
||||||
|
super()._refresh(device, write_ha=write_ha)
|
||||||
|
@ -73,7 +73,7 @@ class TradfriGroup(TradfriBaseClass, LightEntity):
|
|||||||
|
|
||||||
self._attr_unique_id = f"group-{gateway_id}-{device.id}"
|
self._attr_unique_id = f"group-{gateway_id}-{device.id}"
|
||||||
self._attr_should_poll = True
|
self._attr_should_poll = True
|
||||||
self._refresh(device)
|
self._refresh(device, write_ha=False)
|
||||||
|
|
||||||
async def async_update(self) -> None:
|
async def async_update(self) -> None:
|
||||||
"""Fetch new state data for the group.
|
"""Fetch new state data for the group.
|
||||||
@ -134,8 +134,7 @@ class TradfriLight(TradfriBaseDevice, LightEntity):
|
|||||||
if device.light_control.can_set_temp:
|
if device.light_control.can_set_temp:
|
||||||
_features |= SUPPORT_COLOR_TEMP
|
_features |= SUPPORT_COLOR_TEMP
|
||||||
self._attr_supported_features = _features
|
self._attr_supported_features = _features
|
||||||
|
self._refresh(device, write_ha=False)
|
||||||
self._refresh(device)
|
|
||||||
if self._device_control:
|
if self._device_control:
|
||||||
self._attr_min_mireds = self._device_control.min_mireds
|
self._attr_min_mireds = self._device_control.min_mireds
|
||||||
self._attr_max_mireds = self._device_control.max_mireds
|
self._attr_max_mireds = self._device_control.max_mireds
|
||||||
@ -270,10 +269,9 @@ class TradfriLight(TradfriBaseDevice, LightEntity):
|
|||||||
if command is not None:
|
if command is not None:
|
||||||
await self._api(command)
|
await self._api(command)
|
||||||
|
|
||||||
def _refresh(self, device: Command) -> None:
|
def _refresh(self, device: Command, write_ha: bool = True) -> None:
|
||||||
"""Refresh the light data."""
|
"""Refresh the light data."""
|
||||||
super()._refresh(device)
|
|
||||||
|
|
||||||
# Caching of LightControl and light object
|
# Caching of LightControl and light object
|
||||||
self._device_control = device.light_control
|
self._device_control = device.light_control
|
||||||
self._device_data = device.light_control.lights[0]
|
self._device_data = device.light_control.lights[0]
|
||||||
|
super()._refresh(device, write_ha=write_ha)
|
||||||
|
@ -46,13 +46,12 @@ class TradfriSwitch(TradfriBaseDevice, SwitchEntity):
|
|||||||
super().__init__(device, api, gateway_id)
|
super().__init__(device, api, gateway_id)
|
||||||
self._attr_unique_id = f"{gateway_id}-{device.id}"
|
self._attr_unique_id = f"{gateway_id}-{device.id}"
|
||||||
|
|
||||||
def _refresh(self, device: Command) -> None:
|
def _refresh(self, device: Command, write_ha: bool = True) -> None:
|
||||||
"""Refresh the switch data."""
|
"""Refresh the switch data."""
|
||||||
super()._refresh(device)
|
|
||||||
|
|
||||||
# Caching of switch control and switch object
|
# Caching of switch control and switch object
|
||||||
self._device_control = device.socket_control
|
self._device_control = device.socket_control
|
||||||
self._device_data = device.socket_control.sockets[0]
|
self._device_data = device.socket_control.sockets[0]
|
||||||
|
super()._refresh(device, write_ha=write_ha)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user