mirror of
https://github.com/home-assistant/core.git
synced 2025-07-07 21:37:07 +00:00
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
@ -15,6 +15,7 @@ from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
import homeassistant.helpers.config_validation as cv
|
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.event import Event, async_track_time_interval
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
@ -34,6 +35,7 @@ from .const import (
|
|||||||
GROUPS,
|
GROUPS,
|
||||||
KEY_API,
|
KEY_API,
|
||||||
PLATFORMS,
|
PLATFORMS,
|
||||||
|
SIGNAL_GW,
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -137,10 +139,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
if hass.is_stopping:
|
if hass.is_stopping:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
gw_status = True
|
||||||
try:
|
try:
|
||||||
await api(gateway.get_gateway_info())
|
await api(gateway.get_gateway_info())
|
||||||
except RequestError:
|
except RequestError:
|
||||||
_LOGGER.error("Keep-alive failed")
|
_LOGGER.error("Keep-alive failed")
|
||||||
|
gw_status = False
|
||||||
|
|
||||||
|
async_dispatcher_send(hass, SIGNAL_GW, gw_status)
|
||||||
|
|
||||||
listeners.append(
|
listeners.append(
|
||||||
async_track_time_interval(hass, async_keep_alive, timedelta(seconds=60))
|
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 pytradfri.error import PytradfriError
|
||||||
|
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity import DeviceInfo, Entity
|
from homeassistant.helpers.entity import DeviceInfo, Entity
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN, SIGNAL_GW
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -122,8 +123,24 @@ class TradfriBaseDevice(TradfriBaseClass):
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize a device."""
|
"""Initialize a device."""
|
||||||
self._attr_available = device.reachable
|
self._attr_available = device.reachable
|
||||||
|
self._hub_available = True
|
||||||
super().__init__(device, api, gateway_id)
|
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
|
@property
|
||||||
def device_info(self) -> DeviceInfo:
|
def device_info(self) -> DeviceInfo:
|
||||||
"""Return the device info."""
|
"""Return the device info."""
|
||||||
@ -142,5 +159,5 @@ class TradfriBaseDevice(TradfriBaseClass):
|
|||||||
# The base class _refresh cannot be used, because
|
# The base class _refresh cannot be used, because
|
||||||
# there are devices (group) that do not have .reachable
|
# there are devices (group) that do not have .reachable
|
||||||
# so set _attr_available here and let the base class do the rest.
|
# 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)
|
super()._refresh(device, write_ha)
|
||||||
|
@ -21,6 +21,7 @@ DOMAIN = "tradfri"
|
|||||||
KEY_API = "tradfri_api"
|
KEY_API = "tradfri_api"
|
||||||
DEVICES = "tradfri_devices"
|
DEVICES = "tradfri_devices"
|
||||||
GROUPS = "tradfri_groups"
|
GROUPS = "tradfri_groups"
|
||||||
|
SIGNAL_GW = "tradfri.gw_status"
|
||||||
KEY_SECURITY_CODE = "security_code"
|
KEY_SECURITY_CODE = "security_code"
|
||||||
SUPPORTED_GROUP_FEATURES = SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION
|
SUPPORTED_GROUP_FEATURES = SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION
|
||||||
SUPPORTED_LIGHT_FEATURES = SUPPORT_TRANSITION
|
SUPPORTED_LIGHT_FEATURES = SUPPORT_TRANSITION
|
||||||
|
Loading…
x
Reference in New Issue
Block a user