diff --git a/homeassistant/components/russound_rio/__init__.py b/homeassistant/components/russound_rio/__init__.py index e36cedecfe3..8627c636ef2 100644 --- a/homeassistant/components/russound_rio/__init__.py +++ b/homeassistant/components/russound_rio/__init__.py @@ -7,7 +7,7 @@ from aiorussound import Russound from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_HOST, CONF_PORT, Platform -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryNotReady from .const import CONNECT_TIMEOUT, RUSSOUND_RIO_EXCEPTIONS @@ -26,6 +26,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: RussoundConfigEntry) -> port = entry.data[CONF_PORT] russ = Russound(hass.loop, host, port) + @callback + def is_connected_updated(connected: bool) -> None: + if connected: + _LOGGER.warning("Reconnected to controller at %s:%s", host, port) + else: + _LOGGER.warning( + "Disconnected from controller at %s:%s", + host, + port, + ) + + russ.add_connection_callback(is_connected_updated) + try: async with asyncio.timeout(CONNECT_TIMEOUT): await russ.connect() diff --git a/homeassistant/components/russound_rio/entity.py b/homeassistant/components/russound_rio/entity.py index 3430a77108b..150c4e285d1 100644 --- a/homeassistant/components/russound_rio/entity.py +++ b/homeassistant/components/russound_rio/entity.py @@ -6,6 +6,7 @@ from typing import Any, Concatenate from aiorussound import Controller +from homeassistant.core import callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo from homeassistant.helpers.entity import Entity @@ -68,3 +69,17 @@ class RussoundBaseEntity(Entity): self._attr_device_info["connections"] = { (CONNECTION_NETWORK_MAC, controller.mac_address) } + + @callback + def _is_connected_updated(self, connected: bool) -> None: + """Update the state when the device is ready to receive commands or is unavailable.""" + self._attr_available = connected + self.async_write_ha_state() + + async def async_added_to_hass(self) -> None: + """Register callbacks.""" + self._instance.add_connection_callback(self._is_connected_updated) + + async def async_will_remove_from_hass(self) -> None: + """Remove callbacks.""" + self._instance.remove_connection_callback(self._is_connected_updated) diff --git a/homeassistant/components/russound_rio/media_player.py b/homeassistant/components/russound_rio/media_player.py index 5f11227ef53..20aaf0f3c08 100644 --- a/homeassistant/components/russound_rio/media_player.py +++ b/homeassistant/components/russound_rio/media_player.py @@ -140,8 +140,14 @@ class RussoundZoneDevice(RussoundBaseEntity, MediaPlayerEntity): async def async_added_to_hass(self) -> None: """Register callback handlers.""" + await super().async_added_to_hass() self._zone.add_callback(self._callback_handler) + async def async_will_remove_from_hass(self) -> None: + """Remove callbacks.""" + await super().async_will_remove_from_hass() + self._zone.remove_callback(self._callback_handler) + def _current_source(self) -> Source: return self._zone.fetch_current_source()