Set tradfri entities to non-available when hub is not available (#59278)
* Set available when needed. Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
7b9715bec3
commit
2b68b9292c
3 changed files with 26 additions and 2 deletions
|
@ -15,6 +15,7 @@ from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.helpers.event import Event, async_track_time_interval
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
|
@ -34,6 +35,7 @@ from .const import (
|
|||
GROUPS,
|
||||
KEY_API,
|
||||
PLATFORMS,
|
||||
SIGNAL_GW,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -137,10 +139,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
if hass.is_stopping:
|
||||
return
|
||||
|
||||
gw_status = True
|
||||
try:
|
||||
await api(gateway.get_gateway_info())
|
||||
except RequestError:
|
||||
_LOGGER.error("Keep-alive failed")
|
||||
gw_status = False
|
||||
|
||||
async_dispatcher_send(hass, SIGNAL_GW, gw_status)
|
||||
|
||||
listeners.append(
|
||||
async_track_time_interval(hass, async_keep_alive, timedelta(seconds=60))
|
||||
|
|
|
@ -20,9 +20,10 @@ from pytradfri.device.socket_control import SocketControl
|
|||
from pytradfri.error import PytradfriError
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity import DeviceInfo, Entity
|
||||
|
||||
from .const import DOMAIN
|
||||
from .const import DOMAIN, SIGNAL_GW
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -122,8 +123,24 @@ class TradfriBaseDevice(TradfriBaseClass):
|
|||
) -> None:
|
||||
"""Initialize a device."""
|
||||
self._attr_available = device.reachable
|
||||
self._hub_available = True
|
||||
super().__init__(device, api, gateway_id)
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Start thread when added to hass."""
|
||||
# Only devices shall receive SIGNAL_GW
|
||||
self.async_on_remove(
|
||||
async_dispatcher_connect(self.hass, SIGNAL_GW, self.set_hub_available)
|
||||
)
|
||||
await super().async_added_to_hass()
|
||||
|
||||
@callback
|
||||
def set_hub_available(self, available: bool) -> None:
|
||||
"""Set status of hub."""
|
||||
if available != self._hub_available:
|
||||
self._hub_available = available
|
||||
self._refresh(self._device)
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Return the device info."""
|
||||
|
@ -142,5 +159,5 @@ class TradfriBaseDevice(TradfriBaseClass):
|
|||
# The base class _refresh cannot be used, because
|
||||
# there are devices (group) that do not have .reachable
|
||||
# so set _attr_available here and let the base class do the rest.
|
||||
self._attr_available = device.reachable
|
||||
self._attr_available = device.reachable and self._hub_available
|
||||
super()._refresh(device, write_ha)
|
||||
|
|
|
@ -21,6 +21,7 @@ DOMAIN = "tradfri"
|
|||
KEY_API = "tradfri_api"
|
||||
DEVICES = "tradfri_devices"
|
||||
GROUPS = "tradfri_groups"
|
||||
SIGNAL_GW = "tradfri.gw_status"
|
||||
KEY_SECURITY_CODE = "security_code"
|
||||
SUPPORTED_GROUP_FEATURES = SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION
|
||||
SUPPORTED_LIGHT_FEATURES = SUPPORT_TRANSITION
|
||||
|
|
Loading…
Add table
Reference in a new issue