Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump aiorussound to 4.0.5 #126774

Merged
merged 5 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions homeassistant/components/russound_rio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import logging

from aiorussound import RussoundClient, RussoundTcpConnectionHandler
from aiorussound.models import CallbackType

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PORT, Platform
from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady

from .const import CONNECT_TIMEOUT, RUSSOUND_RIO_EXCEPTIONS
Expand All @@ -24,26 +25,26 @@ async def async_setup_entry(hass: HomeAssistant, entry: RussoundConfigEntry) ->

host = entry.data[CONF_HOST]
port = entry.data[CONF_PORT]
russ = RussoundClient(RussoundTcpConnectionHandler(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.connection_handler.add_connection_callback(is_connected_updated)
client = RussoundClient(RussoundTcpConnectionHandler(host, port))

async def _connection_update_callback(
_client: RussoundClient, _callback_type: CallbackType
) -> None:
"""Call when the device is notified of changes."""
if _callback_type == CallbackType.CONNECTION:
if _client.is_connected():
_LOGGER.warning("Reconnected to device at %s", entry.data[CONF_HOST])
else:
_LOGGER.warning("Disconnected from device at %s", entry.data[CONF_HOST])

await client.register_state_update_callbacks(_connection_update_callback)

try:
async with asyncio.timeout(CONNECT_TIMEOUT):
await russ.connect()
await client.connect()
except RUSSOUND_RIO_EXCEPTIONS as err:
raise ConfigEntryNotReady(f"Error while connecting to {host}:{port}") from err
entry.runtime_data = russ
entry.runtime_data = client

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

Expand All @@ -53,6 +54,6 @@ def is_connected_updated(connected: bool) -> None:
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
await entry.runtime_data.close()
await entry.runtime_data.disconnect()

return unload_ok
59 changes: 16 additions & 43 deletions homeassistant/components/russound_rio/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@
import logging
from typing import Any

from aiorussound import Controller, RussoundClient, RussoundTcpConnectionHandler
from aiorussound import RussoundClient, RussoundTcpConnectionHandler
import voluptuous as vol

from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.helpers import config_validation as cv

from .const import (
CONNECT_TIMEOUT,
DOMAIN,
RUSSOUND_RIO_EXCEPTIONS,
NoPrimaryControllerException,
)
from .const import CONNECT_TIMEOUT, DOMAIN, RUSSOUND_RIO_EXCEPTIONS

DATA_SCHEMA = vol.Schema(
{
Expand All @@ -30,16 +25,6 @@
_LOGGER = logging.getLogger(__name__)


def find_primary_controller_metadata(
controllers: dict[int, Controller],
) -> tuple[str, str]:
"""Find the mac address of the primary Russound controller."""
if 1 in controllers:
c = controllers[1]
return c.mac_address, c.controller_type
raise NoPrimaryControllerException


class FlowHandler(ConfigFlow, domain=DOMAIN):
"""Russound RIO configuration flow."""

Expand All @@ -54,28 +39,22 @@ async def async_step_user(
host = user_input[CONF_HOST]
port = user_input[CONF_PORT]

russ = RussoundClient(
RussoundTcpConnectionHandler(self.hass.loop, host, port)
)
client = RussoundClient(RussoundTcpConnectionHandler(host, port))
try:
async with asyncio.timeout(CONNECT_TIMEOUT):
await russ.connect()
controllers = await russ.enumerate_controllers()
metadata = find_primary_controller_metadata(controllers)
await russ.close()
await client.connect()
controller = client.controllers[1]
await client.disconnect()
except RUSSOUND_RIO_EXCEPTIONS:
_LOGGER.exception("Could not connect to Russound RIO")
errors["base"] = "cannot_connect"
except NoPrimaryControllerException:
_LOGGER.exception(
"Russound RIO device doesn't have a primary controller",
)
errors["base"] = "no_primary_controller"
else:
await self.async_set_unique_id(metadata[0])
await self.async_set_unique_id(controller.mac_address)
self._abort_if_unique_id_configured()
data = {CONF_HOST: host, CONF_PORT: port}
return self.async_create_entry(title=metadata[1], data=data)
return self.async_create_entry(
title=controller.controller_type, data=data
)

return self.async_show_form(
step_id="user", data_schema=DATA_SCHEMA, errors=errors
Expand All @@ -88,25 +67,19 @@ async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResu
port = import_data.get(CONF_PORT, 9621)

# Connection logic is repeated here since this method will be removed in future releases
russ = RussoundClient(RussoundTcpConnectionHandler(self.hass.loop, host, port))
client = RussoundClient(RussoundTcpConnectionHandler(host, port))
try:
async with asyncio.timeout(CONNECT_TIMEOUT):
await russ.connect()
controllers = await russ.enumerate_controllers()
metadata = find_primary_controller_metadata(controllers)
await russ.close()
await client.connect()
controller = client.controllers[1]
await client.disconnect()
except RUSSOUND_RIO_EXCEPTIONS:
_LOGGER.exception("Could not connect to Russound RIO")
return self.async_abort(
reason="cannot_connect", description_placeholders={}
)
except NoPrimaryControllerException:
_LOGGER.exception("Russound RIO device doesn't have a primary controller")
return self.async_abort(
reason="no_primary_controller", description_placeholders={}
)
else:
await self.async_set_unique_id(metadata[0])
await self.async_set_unique_id(controller.mac_address)
self._abort_if_unique_id_configured()
data = {CONF_HOST: host, CONF_PORT: port}
return self.async_create_entry(title=metadata[1], data=data)
return self.async_create_entry(title=controller.controller_type, data=data)
4 changes: 0 additions & 4 deletions homeassistant/components/russound_rio/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
)


class NoPrimaryControllerException(Exception):
"""Thrown when the Russound device is not the primary unit in the RNET stack."""


CONNECT_TIMEOUT = 5

MP_FEATURES_BY_FLAG = {
Expand Down
33 changes: 18 additions & 15 deletions homeassistant/components/russound_rio/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from functools import wraps
from typing import Any, Concatenate

from aiorussound import Controller, RussoundTcpConnectionHandler
from aiorussound import Controller, RussoundClient, RussoundTcpConnectionHandler
from aiorussound.models import CallbackType

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
Expand Down Expand Up @@ -46,7 +46,7 @@ def __init__(
self._client = controller.client
self._controller = controller
self._primary_mac_address = (
controller.mac_address or controller.parent_controller.mac_address
controller.mac_address or self._client.controllers[1].mac_address
)
self._device_identifier = (
self._controller.mac_address
Expand All @@ -64,30 +64,33 @@ def __init__(
self._attr_device_info["configuration_url"] = (
f"http://{self._client.connection_handler.host}"
)
if controller.parent_controller:
if controller.controller_id != 1:
assert self._client.controllers[1].mac_address
self._attr_device_info["via_device"] = (
DOMAIN,
controller.parent_controller.mac_address,
self._client.controllers[1].mac_address,
)
else:
assert controller.mac_address
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
async def _state_update_callback(
self, _client: RussoundClient, _callback_type: CallbackType
) -> None:
"""Call when the device is notified of changes."""
if _callback_type == CallbackType.CONNECTION:
self._attr_available = _client.is_connected()
self._controller = _client.controllers[self._controller.controller_id]
self.async_write_ha_state()

async def async_added_to_hass(self) -> None:
"""Register callbacks."""
self._client.connection_handler.add_connection_callback(
self._is_connected_updated
)
"""Register callback handlers."""
await self._client.register_state_update_callbacks(self._state_update_callback)

async def async_will_remove_from_hass(self) -> None:
"""Remove callbacks."""
self._client.connection_handler.remove_connection_callback(
self._is_connected_updated
await self._client.unregister_state_update_callbacks(
self._state_update_callback
)
2 changes: 1 addition & 1 deletion homeassistant/components/russound_rio/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"iot_class": "local_push",
"loggers": ["aiorussound"],
"quality_scale": "silver",
"requirements": ["aiorussound==3.1.5"]
"requirements": ["aiorussound==4.0.5"]
}
Loading
Loading