diff --git a/homeassistant/components/fressnapf_tracker/__init__.py b/homeassistant/components/fressnapf_tracker/__init__.py index d1c23370b21948..fa8ad628f7fa6d 100644 --- a/homeassistant/components/fressnapf_tracker/__init__.py +++ b/homeassistant/components/fressnapf_tracker/__init__.py @@ -1,12 +1,13 @@ """The Fressnapf Tracker integration.""" -from fressnapftracker import AuthClient +from fressnapftracker import AuthClient, FressnapfTrackerAuthenticationError from homeassistant.const import CONF_ACCESS_TOKEN, Platform from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.helpers.httpx_client import get_async_client -from .const import CONF_USER_ID +from .const import CONF_USER_ID, DOMAIN from .coordinator import ( FressnapfTrackerConfigEntry, FressnapfTrackerDataUpdateCoordinator, @@ -26,10 +27,16 @@ async def async_setup_entry( ) -> bool: """Set up Fressnapf Tracker from a config entry.""" auth_client = AuthClient(client=get_async_client(hass)) - devices = await auth_client.get_devices( - user_id=entry.data[CONF_USER_ID], - user_access_token=entry.data[CONF_ACCESS_TOKEN], - ) + try: + devices = await auth_client.get_devices( + user_id=entry.data[CONF_USER_ID], + user_access_token=entry.data[CONF_ACCESS_TOKEN], + ) + except FressnapfTrackerAuthenticationError as exception: + raise ConfigEntryAuthFailed( + translation_domain=DOMAIN, + translation_key="invalid_auth", + ) from exception coordinators: list[FressnapfTrackerDataUpdateCoordinator] = [] for device in devices: diff --git a/homeassistant/components/fressnapf_tracker/config_flow.py b/homeassistant/components/fressnapf_tracker/config_flow.py index 0906b0182ab52c..3823246308ee09 100644 --- a/homeassistant/components/fressnapf_tracker/config_flow.py +++ b/homeassistant/components/fressnapf_tracker/config_flow.py @@ -1,5 +1,6 @@ """Config flow for the Fressnapf Tracker integration.""" +from collections.abc import Mapping import logging from typing import Any @@ -10,7 +11,12 @@ ) import voluptuous as vol -from homeassistant.config_entries import ConfigFlow, ConfigFlowResult +from homeassistant.config_entries import ( + SOURCE_REAUTH, + SOURCE_RECONFIGURE, + ConfigFlow, + ConfigFlowResult, +) from homeassistant.const import CONF_ACCESS_TOKEN from homeassistant.helpers.httpx_client import get_async_client @@ -136,40 +142,43 @@ async def async_step_sms_code( errors=errors, ) - async def async_step_reconfigure( - self, user_input: dict[str, Any] | None = None + async def _async_reauth_reconfigure( + self, + user_input: dict[str, Any] | None, + entry: Any, + step_id: str, ) -> ConfigFlowResult: - """Handle reconfiguration of the integration.""" + """Request a new sms code for reauth or reconfigure flows.""" errors: dict[str, str] = {} - reconfigure_entry = self._get_reconfigure_entry() if user_input is not None: errors, success = await self._async_request_sms_code( user_input[CONF_PHONE_NUMBER] ) if success: - if reconfigure_entry.data[CONF_USER_ID] != self._context[CONF_USER_ID]: + if entry.data[CONF_USER_ID] != self._context[CONF_USER_ID]: errors["base"] = "account_change_not_allowed" - else: + elif self.source == SOURCE_REAUTH: + return await self.async_step_reauth_sms_code() + elif self.source == SOURCE_RECONFIGURE: return await self.async_step_reconfigure_sms_code() return self.async_show_form( - step_id="reconfigure", - data_schema=vol.Schema( - { - vol.Required( - CONF_PHONE_NUMBER, - default=reconfigure_entry.data.get(CONF_PHONE_NUMBER), - ): str, - } + step_id=step_id, + data_schema=self.add_suggested_values_to_schema( + STEP_USER_DATA_SCHEMA, + {CONF_PHONE_NUMBER: entry.data.get(CONF_PHONE_NUMBER)}, ), errors=errors, ) - async def async_step_reconfigure_sms_code( - self, user_input: dict[str, Any] | None = None + async def _async_reauth_reconfigure_sms_code( + self, + user_input: dict[str, Any] | None, + entry: Any, + step_id: str, ) -> ConfigFlowResult: - """Handle the SMS code step during reconfiguration.""" + """Verify SMS code for reauth or reconfigure flows.""" errors: dict[str, str] = {} if user_input is not None: @@ -178,16 +187,61 @@ async def async_step_reconfigure_sms_code( ) if access_token: return self.async_update_reload_and_abort( - self._get_reconfigure_entry(), - data={ + entry, + data_updates={ CONF_PHONE_NUMBER: self._context[CONF_PHONE_NUMBER], - CONF_USER_ID: self._context[CONF_USER_ID], CONF_ACCESS_TOKEN: access_token, }, ) return self.async_show_form( - step_id="reconfigure_sms_code", + step_id=step_id, data_schema=STEP_SMS_CODE_DATA_SCHEMA, errors=errors, ) + + async def async_step_reauth( + self, entry_data: Mapping[str, Any] + ) -> ConfigFlowResult: + """Handle configuration by re-auth.""" + return await self.async_step_reauth_confirm() + + async def async_step_reauth_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle reauth confirmation step.""" + return await self._async_reauth_reconfigure( + user_input, + self._get_reauth_entry(), + "reauth_confirm", + ) + + async def async_step_reauth_sms_code( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle the SMS code step during reauth.""" + return await self._async_reauth_reconfigure_sms_code( + user_input, + self._get_reauth_entry(), + "reauth_sms_code", + ) + + async def async_step_reconfigure( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle reconfiguration of the integration.""" + return await self._async_reauth_reconfigure( + user_input, + self._get_reconfigure_entry(), + "reconfigure", + ) + + async def async_step_reconfigure_sms_code( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle the SMS code step during reconfiguration.""" + return await self._async_reauth_reconfigure_sms_code( + user_input, + self._get_reconfigure_entry(), + "reconfigure_sms_code", + ) diff --git a/homeassistant/components/fressnapf_tracker/coordinator.py b/homeassistant/components/fressnapf_tracker/coordinator.py index c80d5dcce877f7..a51ee6658701d3 100644 --- a/homeassistant/components/fressnapf_tracker/coordinator.py +++ b/homeassistant/components/fressnapf_tracker/coordinator.py @@ -3,10 +3,17 @@ from datetime import timedelta import logging -from fressnapftracker import ApiClient, Device, FressnapfTrackerError, Tracker +from fressnapftracker import ( + ApiClient, + Device, + FressnapfTrackerError, + FressnapfTrackerInvalidDeviceTokenError, + Tracker, +) from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.helpers.httpx_client import get_async_client from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed @@ -46,5 +53,10 @@ def __init__( async def _async_update_data(self) -> Tracker: try: return await self.client.get_tracker() + except FressnapfTrackerInvalidDeviceTokenError as exception: + raise ConfigEntryAuthFailed( + translation_domain=DOMAIN, + translation_key="invalid_auth", + ) from exception except FressnapfTrackerError as exception: raise UpdateFailed(exception) from exception diff --git a/homeassistant/components/fressnapf_tracker/quality_scale.yaml b/homeassistant/components/fressnapf_tracker/quality_scale.yaml index 03a41e0324a1d9..f4d24e577c7f3c 100644 --- a/homeassistant/components/fressnapf_tracker/quality_scale.yaml +++ b/homeassistant/components/fressnapf_tracker/quality_scale.yaml @@ -34,7 +34,7 @@ rules: integration-owner: done log-when-unavailable: done parallel-updates: done - reauthentication-flow: todo + reauthentication-flow: done test-coverage: done # Gold diff --git a/homeassistant/components/fressnapf_tracker/strings.json b/homeassistant/components/fressnapf_tracker/strings.json index cb5c886c9532db..2cc88af8a8fa09 100644 --- a/homeassistant/components/fressnapf_tracker/strings.json +++ b/homeassistant/components/fressnapf_tracker/strings.json @@ -2,6 +2,7 @@ "config": { "abort": { "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", + "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]", "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]" }, "error": { @@ -11,7 +12,7 @@ "unknown": "[%key:common::config_flow::error::unknown%]" }, "step": { - "reconfigure": { + "reauth_confirm": { "data": { "phone_number": "[%key:component::fressnapf_tracker::config::step::user::data::phone_number%]" }, @@ -20,6 +21,23 @@ }, "description": "Re-authenticate with your Fressnapf Tracker account to refresh your credentials." }, + "reauth_sms_code": { + "data": { + "sms_code": "[%key:component::fressnapf_tracker::config::step::sms_code::data::sms_code%]" + }, + "data_description": { + "sms_code": "[%key:component::fressnapf_tracker::config::step::sms_code::data_description::sms_code%]" + } + }, + "reconfigure": { + "data": { + "phone_number": "[%key:component::fressnapf_tracker::config::step::user::data::phone_number%]" + }, + "data_description": { + "phone_number": "[%key:component::fressnapf_tracker::config::step::user::data_description::phone_number%]" + }, + "description": "Update your Fressnapf Tracker account configuration." + }, "reconfigure_sms_code": { "data": { "sms_code": "[%key:component::fressnapf_tracker::config::step::sms_code::data::sms_code%]" @@ -62,6 +80,9 @@ "charging": { "message": "The flashlight cannot be activated while charging." }, + "invalid_auth": { + "message": "Your authentication with the Fressnapf Tracker API expired. Please re-authenticate to refresh your credentials." + }, "low_battery": { "message": "The flashlight cannot be activated due to low battery." }, diff --git a/homeassistant/components/kostal_plenticore/sensor.py b/homeassistant/components/kostal_plenticore/sensor.py index aafd6bb1ff6bd9..317e6e03cef2e1 100644 --- a/homeassistant/components/kostal_plenticore/sensor.py +++ b/homeassistant/components/kostal_plenticore/sensor.py @@ -245,6 +245,7 @@ class PlenticoreSensorEntityDescription(SensorEntityDescription): name="Battery SoC", native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.BATTERY, + state_class=SensorStateClass.MEASUREMENT, formatter="format_round", ), PlenticoreSensorEntityDescription( diff --git a/homeassistant/components/sonos/helpers.py b/homeassistant/components/sonos/helpers.py index 1fb3bb3d5e7420..e83b0132a0e6bd 100644 --- a/homeassistant/components/sonos/helpers.py +++ b/homeassistant/components/sonos/helpers.py @@ -15,6 +15,7 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import CALLBACK_TYPE +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.dispatcher import dispatcher_send from .const import SONOS_SPEAKER_ACTIVITY @@ -135,6 +136,7 @@ class UnjoinData: speakers: list[SonosSpeaker] = field(default_factory=list) event: asyncio.Event = field(default_factory=asyncio.Event) + exception: HomeAssistantError | OSError | SoCoException | None = None @dataclass diff --git a/homeassistant/components/sonos/media_player.py b/homeassistant/components/sonos/media_player.py index f7d72587c5b030..cb48037524e402 100644 --- a/homeassistant/components/sonos/media_player.py +++ b/homeassistant/components/sonos/media_player.py @@ -15,6 +15,7 @@ PLAY_MODES, ) from soco.data_structures import DidlFavorite, DidlMusicTrack +from soco.exceptions import SoCoException from soco.ms_data_structures import MusicServiceItem from sonos_websocket.exception import SonosWebsocketError @@ -853,10 +854,14 @@ async def async_process_unjoin(now: datetime.datetime) -> None: _LOGGER.debug( "Processing unjoins for %s", [x.zone_name for x in unjoin_data.speakers] ) - await SonosSpeaker.unjoin_multi( - self.hass, self.config_entry, unjoin_data.speakers - ) - unjoin_data.event.set() + try: + await SonosSpeaker.unjoin_multi( + self.hass, self.config_entry, unjoin_data.speakers + ) + except (HomeAssistantError, SoCoException, OSError) as err: + unjoin_data.exception = err + finally: + unjoin_data.event.set() if unjoin_data := sonos_data.unjoin_data.get(household_id): unjoin_data.speakers.append(self.speaker) @@ -868,3 +873,7 @@ async def async_process_unjoin(now: datetime.datetime) -> None: _LOGGER.debug("Requesting unjoin for %s", self.speaker.zone_name) await unjoin_data.event.wait() + + # Re-raise any exception that occurred during processing + if unjoin_data.exception: + raise unjoin_data.exception diff --git a/homeassistant/components/sonos/speaker.py b/homeassistant/components/sonos/speaker.py index ed2d4add7ba948..e760b8db3e8bb1 100644 --- a/homeassistant/components/sonos/speaker.py +++ b/homeassistant/components/sonos/speaker.py @@ -1060,7 +1060,10 @@ def _unjoin_all(speakers: list[SonosSpeaker]) -> None: async with config_entry.runtime_data.topology_condition: await hass.async_add_executor_job(_unjoin_all, speakers) await SonosSpeaker.wait_for_groups( - hass, config_entry, [[s] for s in speakers] + hass, + config_entry, + [[s] for s in speakers], + action="unjoin", ) @soco_error() @@ -1204,6 +1207,7 @@ async def wait_for_groups( hass: HomeAssistant, config_entry: SonosConfigEntry, groups: list[list[SonosSpeaker]], + action: str = "join", ) -> None: """Wait until all groups are present, or timeout.""" @@ -1228,14 +1232,17 @@ def _test_groups(groups: list[list[SonosSpeaker]]) -> bool: while not _test_groups(groups): await config_entry.runtime_data.topology_condition.wait() except TimeoutError: - group_description = [ + group_description = "; ".join( f"{group[0].zone_name}: {', '.join(speaker.zone_name for speaker in group)}" for group in groups - ] + ) raise HomeAssistantError( translation_domain=DOMAIN, translation_key="timeout_join", - translation_placeholders={"group_description": str(group_description)}, + translation_placeholders={ + "group_description": group_description, + "action": action, + }, ) from TimeoutError any_speaker = next(iter(config_entry.runtime_data.discovered.values())) any_speaker.soco.zone_group_state.clear_cache() diff --git a/homeassistant/components/sonos/strings.json b/homeassistant/components/sonos/strings.json index 71b6ffe6c63fb7..902aed2b7b72aa 100644 --- a/homeassistant/components/sonos/strings.json +++ b/homeassistant/components/sonos/strings.json @@ -125,7 +125,7 @@ "message": "{entity_id} is not a known Sonos speaker." }, "timeout_join": { - "message": "Timeout while waiting for Sonos player to join the group {group_description}" + "message": "Timeout while waiting for Sonos player to {action} the group {group_description}" } }, "issues": { diff --git a/tests/components/fressnapf_tracker/test_config_flow.py b/tests/components/fressnapf_tracker/test_config_flow.py index d247f96a44a0af..295476ba6e90a7 100644 --- a/tests/components/fressnapf_tracker/test_config_flow.py +++ b/tests/components/fressnapf_tracker/test_config_flow.py @@ -1,5 +1,6 @@ """Test the Fressnapf Tracker config flow.""" +from collections.abc import Callable from unittest.mock import AsyncMock, MagicMock from fressnapftracker import ( @@ -198,21 +199,41 @@ async def test_user_flow_duplicate_phone_number( assert result["reason"] == "already_configured" -@pytest.mark.usefixtures("mock_api_client") -@pytest.mark.usefixtures("mock_auth_client") -async def test_reconfigure_flow( +@pytest.mark.parametrize( + ("flow_starter", "expected_step_id", "expected_sms_step_id", "expected_reason"), + [ + ( + lambda entry, hass: entry.start_reauth_flow(hass), + "reauth_confirm", + "reauth_sms_code", + "reauth_successful", + ), + ( + lambda entry, hass: entry.start_reconfigure_flow(hass), + "reconfigure", + "reconfigure_sms_code", + "reconfigure_successful", + ), + ], +) +@pytest.mark.usefixtures("mock_api_client", "mock_auth_client") +async def test_reauth_reconfigure_flow( hass: HomeAssistant, mock_config_entry: MockConfigEntry, + flow_starter: Callable, + expected_step_id: str, + expected_sms_step_id: str, + expected_reason: str, ) -> None: - """Test the reconfigure flow.""" + """Test the reauth and reconfigure flows.""" mock_config_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() - result = await mock_config_entry.start_reconfigure_flow(hass) + result = await flow_starter(mock_config_entry, hass) assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reconfigure" + assert result["step_id"] == expected_step_id # Submit phone number result = await hass.config_entries.flow.async_configure( @@ -220,7 +241,7 @@ async def test_reconfigure_flow( {CONF_PHONE_NUMBER: MOCK_PHONE_NUMBER}, ) assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reconfigure_sms_code" + assert result["step_id"] == expected_sms_step_id # Submit SMS code result = await hass.config_entries.flow.async_configure( @@ -229,21 +250,42 @@ async def test_reconfigure_flow( ) assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "reconfigure_successful" + assert result["reason"] == expected_reason +@pytest.mark.parametrize( + ("flow_starter", "expected_step_id", "expected_sms_step_id", "expected_reason"), + [ + ( + lambda entry, hass: entry.start_reauth_flow(hass), + "reauth_confirm", + "reauth_sms_code", + "reauth_successful", + ), + ( + lambda entry, hass: entry.start_reconfigure_flow(hass), + "reconfigure", + "reconfigure_sms_code", + "reconfigure_successful", + ), + ], +) @pytest.mark.usefixtures("mock_api_client") -async def test_reconfigure_flow_invalid_phone_number( +async def test_reauth_reconfigure_flow_invalid_phone_number( hass: HomeAssistant, mock_auth_client: MagicMock, mock_config_entry: MockConfigEntry, + flow_starter: Callable, + expected_step_id: str, + expected_sms_step_id: str, + expected_reason: str, ) -> None: - """Test reconfigure flow with invalid phone number.""" + """Test reauth and reconfigure flows with invalid phone number.""" mock_config_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() - result = await mock_config_entry.start_reconfigure_flow(hass) + result = await flow_starter(mock_config_entry, hass) mock_auth_client.request_sms_code.side_effect = ( FressnapfTrackerInvalidPhoneNumberError @@ -255,7 +297,7 @@ async def test_reconfigure_flow_invalid_phone_number( ) assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reconfigure" + assert result["step_id"] == expected_step_id assert result["errors"] == {"base": "invalid_phone_number"} # Recover from error @@ -265,7 +307,7 @@ async def test_reconfigure_flow_invalid_phone_number( {CONF_PHONE_NUMBER: MOCK_PHONE_NUMBER}, ) assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reconfigure_sms_code" + assert result["step_id"] == expected_sms_step_id result = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -273,21 +315,39 @@ async def test_reconfigure_flow_invalid_phone_number( ) assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "reconfigure_successful" + assert result["reason"] == expected_reason +@pytest.mark.parametrize( + ("flow_starter", "expected_sms_step_id", "expected_reason"), + [ + ( + lambda entry, hass: entry.start_reauth_flow(hass), + "reauth_sms_code", + "reauth_successful", + ), + ( + lambda entry, hass: entry.start_reconfigure_flow(hass), + "reconfigure_sms_code", + "reconfigure_successful", + ), + ], +) @pytest.mark.usefixtures("mock_api_client") -async def test_reconfigure_flow_invalid_sms_code( +async def test_reauth_reconfigure_flow_invalid_sms_code( hass: HomeAssistant, mock_auth_client: MagicMock, mock_config_entry: MockConfigEntry, + flow_starter: Callable, + expected_sms_step_id: str, + expected_reason: str, ) -> None: - """Test reconfigure flow with invalid SMS code.""" + """Test reauth and reconfigure flows with invalid SMS code.""" mock_config_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() - result = await mock_config_entry.start_reconfigure_flow(hass) + result = await flow_starter(mock_config_entry, hass) result = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -302,7 +362,7 @@ async def test_reconfigure_flow_invalid_sms_code( ) assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reconfigure_sms_code" + assert result["step_id"] == expected_sms_step_id assert result["errors"] == {"base": "invalid_sms_code"} # Recover from error @@ -313,21 +373,42 @@ async def test_reconfigure_flow_invalid_sms_code( ) assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "reconfigure_successful" + assert result["reason"] == expected_reason +@pytest.mark.parametrize( + ("flow_starter", "expected_step_id", "expected_sms_step_id", "expected_reason"), + [ + ( + lambda entry, hass: entry.start_reauth_flow(hass), + "reauth_confirm", + "reauth_sms_code", + "reauth_successful", + ), + ( + lambda entry, hass: entry.start_reconfigure_flow(hass), + "reconfigure", + "reconfigure_sms_code", + "reconfigure_successful", + ), + ], +) @pytest.mark.usefixtures("mock_api_client") -async def test_reconfigure_flow_invalid_user_id( +async def test_reauth_reconfigure_flow_invalid_user_id( hass: HomeAssistant, mock_auth_client: MagicMock, mock_config_entry: MockConfigEntry, + flow_starter: Callable, + expected_step_id: str, + expected_sms_step_id: str, + expected_reason: str, ) -> None: - """Test reconfigure flow does not allow to reconfigure to another account.""" + """Test reauth and reconfigure flows do not allow changing to another account.""" mock_config_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() - result = await mock_config_entry.start_reconfigure_flow(hass) + result = await flow_starter(mock_config_entry, hass) mock_auth_client.request_sms_code = AsyncMock( return_value=SmsCodeResponse(id=MOCK_USER_ID + 1) @@ -339,7 +420,7 @@ async def test_reconfigure_flow_invalid_user_id( ) assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reconfigure" + assert result["step_id"] == expected_step_id assert result["errors"] == {"base": "account_change_not_allowed"} # Recover from error @@ -351,7 +432,7 @@ async def test_reconfigure_flow_invalid_user_id( {CONF_PHONE_NUMBER: MOCK_PHONE_NUMBER}, ) assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reconfigure_sms_code" + assert result["step_id"] == expected_sms_step_id result = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -359,4 +440,4 @@ async def test_reconfigure_flow_invalid_user_id( ) assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "reconfigure_successful" + assert result["reason"] == expected_reason diff --git a/tests/components/sonos/test_services.py b/tests/components/sonos/test_services.py index a94a03b95a031b..3bc85d3b166253 100644 --- a/tests/components/sonos/test_services.py +++ b/tests/components/sonos/test_services.py @@ -13,6 +13,7 @@ SERVICE_JOIN, SERVICE_UNJOIN, ) +from homeassistant.const import ATTR_ENTITY_ID from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import entity_registry as er @@ -131,7 +132,7 @@ async def test_media_player_join_timeout( expected = ( "Timeout while waiting for Sonos player to join the " - "group ['Living Room: Living Room, Bedroom']" + "group Living Room: Living Room, Bedroom" ) with ( patch( @@ -153,6 +154,37 @@ async def test_media_player_join_timeout( assert soco_living_room.join.call_count == 0 +async def test_media_player_unjoin_timeout( + hass: HomeAssistant, + sonos_setup_two_speakers: list[MockSoCo], +) -> None: + """Test unjoining of speaker with timeout error.""" + + soco_living_room = sonos_setup_two_speakers[0] + soco_bedroom = sonos_setup_two_speakers[1] + + # First group the speakers together + group_speakers(soco_living_room, soco_bedroom) + await hass.async_block_till_done(wait_background_tasks=True) + + expected = ( + "Timeout while waiting for Sonos player to unjoin the group Bedroom: Bedroom" + ) + with ( + patch( + "homeassistant.components.sonos.speaker.asyncio.timeout", instant_timeout + ), + pytest.raises(HomeAssistantError, match=re.escape(expected)), + ): + await hass.services.async_call( + MP_DOMAIN, + SERVICE_UNJOIN, + {ATTR_ENTITY_ID: "media_player.bedroom"}, + blocking=True, + ) + assert soco_bedroom.unjoin.call_count == 1 + + async def test_media_player_unjoin( hass: HomeAssistant, sonos_setup_two_speakers: list[MockSoCo],