|
6 | 6 |
|
7 | 7 | import pytest
|
8 | 8 |
|
| 9 | +from homeassistant.components.sonos import DOMAIN |
9 | 10 | from homeassistant.components.sonos.const import (
|
10 | 11 | DATA_SONOS_DISCOVERY_MANAGER,
|
11 | 12 | MODEL_SONOS_ARC_ULTRA,
|
|
31 | 32 | STATE_ON,
|
32 | 33 | )
|
33 | 34 | from homeassistant.core import HomeAssistant
|
34 |
| -from homeassistant.helpers import entity_registry as er |
| 35 | +from homeassistant.helpers import device_registry as dr, entity_registry as er |
| 36 | +from homeassistant.setup import async_setup_component |
35 | 37 | from homeassistant.util import dt as dt_util
|
36 | 38 |
|
37 |
| -from .conftest import MockSoCo, SonosMockEvent, create_rendering_control_event |
| 39 | +from .conftest import ( |
| 40 | + MockSoCo, |
| 41 | + SoCoMockFactory, |
| 42 | + SonosMockEvent, |
| 43 | + SonosMockService, |
| 44 | + create_rendering_control_event, |
| 45 | +) |
38 | 46 |
|
39 | 47 | from tests.common import async_fire_time_changed
|
40 | 48 |
|
@@ -259,3 +267,75 @@ async def test_alarm_create_delete(
|
259 | 267 |
|
260 | 268 | assert "switch.sonos_alarm_14" in entity_registry.entities
|
261 | 269 | assert "switch.sonos_alarm_15" not in entity_registry.entities
|
| 270 | + |
| 271 | + |
| 272 | +async def test_alarm_change_device( |
| 273 | + hass: HomeAssistant, |
| 274 | + async_setup_sonos, |
| 275 | + soco: MockSoCo, |
| 276 | + alarm_clock: SonosMockService, |
| 277 | + alarm_clock_extended: SonosMockService, |
| 278 | + alarm_event: SonosMockEvent, |
| 279 | + entity_registry: er.EntityRegistry, |
| 280 | + device_registry: dr.DeviceRegistry, |
| 281 | + soco_factory: SoCoMockFactory, |
| 282 | +) -> None: |
| 283 | + """Test Sonos Alarm being moved to a different speaker. |
| 284 | +
|
| 285 | + This test simulates a scenario where an alarm is created on one speaker |
| 286 | + and then moved to another speaker. It checks that the entity is correctly |
| 287 | + created on the new speaker and removed from the old one. |
| 288 | + """ |
| 289 | + |
| 290 | + # Create the alarm on the soco_lr speaker |
| 291 | + soco_factory.mock_zones = True |
| 292 | + soco_lr = soco_factory.cache_mock(MockSoCo(), "10.10.10.1", "Living Room") |
| 293 | + alarm_dict = copy(alarm_clock.ListAlarms.return_value) |
| 294 | + alarm_dict["CurrentAlarmList"] = alarm_dict["CurrentAlarmList"].replace( |
| 295 | + "RINCON_test", f"{soco_lr.uid}" |
| 296 | + ) |
| 297 | + alarm_dict["CurrentAlarmListVersion"] = f"{soco_lr.uid}:900" |
| 298 | + soco_lr.alarmClock.ListAlarms.return_value = alarm_dict |
| 299 | + soco_br = soco_factory.cache_mock(MockSoCo(), "10.10.10.2", "Bedroom") |
| 300 | + await async_setup_component( |
| 301 | + hass, |
| 302 | + DOMAIN, |
| 303 | + { |
| 304 | + DOMAIN: { |
| 305 | + "media_player": { |
| 306 | + "interface_addr": "127.0.0.1", |
| 307 | + "hosts": ["10.10.10.1", "10.10.10.2"], |
| 308 | + } |
| 309 | + } |
| 310 | + }, |
| 311 | + ) |
| 312 | + await hass.async_block_till_done() |
| 313 | + |
| 314 | + entity_id = "switch.sonos_alarm_14" |
| 315 | + |
| 316 | + # Verify the alarm is created on the soco_lr speaker |
| 317 | + assert entity_id in entity_registry.entities |
| 318 | + entity = entity_registry.async_get(entity_id) |
| 319 | + device = device_registry.async_get(entity.device_id) |
| 320 | + assert device.name == soco_lr.get_speaker_info()["zone_name"] |
| 321 | + |
| 322 | + # Simulate the alarm being moved to the soco_br speaker |
| 323 | + alarm_update = copy(alarm_clock_extended.ListAlarms.return_value) |
| 324 | + alarm_update["CurrentAlarmList"] = alarm_update["CurrentAlarmList"].replace( |
| 325 | + "RINCON_test", f"{soco_br.uid}" |
| 326 | + ) |
| 327 | + alarm_clock.ListAlarms.return_value = alarm_update |
| 328 | + |
| 329 | + # Update the alarm_list_version so it gets processed. |
| 330 | + alarm_event.variables["alarm_list_version"] = f"{soco_br.uid}:1000" |
| 331 | + alarm_update["CurrentAlarmListVersion"] = alarm_event.increment_variable( |
| 332 | + "alarm_list_version" |
| 333 | + ) |
| 334 | + |
| 335 | + alarm_clock.subscribe.return_value.callback(event=alarm_event) |
| 336 | + await hass.async_block_till_done(wait_background_tasks=True) |
| 337 | + |
| 338 | + assert entity_id in entity_registry.entities |
| 339 | + alarm_14 = entity_registry.async_get(entity_id) |
| 340 | + device = device_registry.async_get(alarm_14.device_id) |
| 341 | + assert device.name == soco_br.get_speaker_info()["zone_name"] |
0 commit comments