Set available property in russound base entity (#123933)

* Set available property in Russound base entity

* Fix

* Fix
This commit is contained in:
Noah Husby 2024-08-14 10:38:40 -04:00 committed by GitHub
parent bb88961968
commit faacfe3f90
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 1 deletions

View File

@ -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()

View File

@ -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)

View File

@ -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()