2021-05-25 16:32:25 +00:00
|
|
|
"""Tests for the Sonos Alarm switch platform."""
|
2021-06-16 15:30:05 +00:00
|
|
|
from copy import copy
|
2021-10-29 19:43:59 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
from unittest.mock import patch
|
2021-06-16 15:30:05 +00:00
|
|
|
|
2021-10-29 19:43:59 +00:00
|
|
|
from homeassistant.components.sonos.const import DATA_SONOS_DISCOVERY_MANAGER
|
2021-05-25 16:32:25 +00:00
|
|
|
from homeassistant.components.sonos.switch import (
|
|
|
|
ATTR_DURATION,
|
|
|
|
ATTR_ID,
|
|
|
|
ATTR_INCLUDE_LINKED_ZONES,
|
|
|
|
ATTR_PLAY_MODE,
|
|
|
|
ATTR_RECURRENCE,
|
|
|
|
ATTR_VOLUME,
|
|
|
|
)
|
2021-10-29 19:43:59 +00:00
|
|
|
from homeassistant.config_entries import RELOAD_AFTER_UPDATE_DELAY
|
2021-12-03 15:05:40 +00:00
|
|
|
from homeassistant.const import ATTR_TIME, STATE_OFF, STATE_ON
|
2021-12-08 18:28:27 +00:00
|
|
|
from homeassistant.helpers import entity_registry as ent_reg
|
2021-10-29 19:43:59 +00:00
|
|
|
from homeassistant.util import dt
|
|
|
|
|
|
|
|
from .conftest import SonosMockEvent
|
|
|
|
|
|
|
|
from tests.common import async_fire_time_changed
|
2021-05-25 16:32:25 +00:00
|
|
|
|
|
|
|
|
2021-12-08 18:28:27 +00:00
|
|
|
async def test_entity_registry(hass, async_autosetup_sonos):
|
2021-05-25 16:32:25 +00:00
|
|
|
"""Test sonos device with alarm registered in the device registry."""
|
2021-12-08 18:28:27 +00:00
|
|
|
entity_registry = ent_reg.async_get(hass)
|
2021-05-25 16:32:25 +00:00
|
|
|
|
|
|
|
assert "media_player.zone_a" in entity_registry.entities
|
|
|
|
assert "switch.sonos_alarm_14" in entity_registry.entities
|
2021-10-23 21:11:27 +00:00
|
|
|
assert "switch.sonos_zone_a_status_light" in entity_registry.entities
|
|
|
|
assert "switch.sonos_zone_a_night_sound" in entity_registry.entities
|
|
|
|
assert "switch.sonos_zone_a_speech_enhancement" in entity_registry.entities
|
2021-12-03 15:05:40 +00:00
|
|
|
assert "switch.sonos_zone_a_subwoofer_enabled" in entity_registry.entities
|
|
|
|
assert "switch.sonos_zone_a_surround_enabled" in entity_registry.entities
|
2021-10-23 21:11:27 +00:00
|
|
|
assert "switch.sonos_zone_a_touch_controls" in entity_registry.entities
|
2021-05-25 16:32:25 +00:00
|
|
|
|
|
|
|
|
2021-12-08 18:28:27 +00:00
|
|
|
async def test_switch_attributes(hass, async_autosetup_sonos, soco):
|
2021-10-23 21:11:27 +00:00
|
|
|
"""Test for correct Sonos switch states."""
|
2021-12-08 18:28:27 +00:00
|
|
|
entity_registry = ent_reg.async_get(hass)
|
2021-05-25 16:32:25 +00:00
|
|
|
|
|
|
|
alarm = entity_registry.entities["switch.sonos_alarm_14"]
|
|
|
|
alarm_state = hass.states.get(alarm.entity_id)
|
|
|
|
assert alarm_state.state == STATE_ON
|
|
|
|
assert alarm_state.attributes.get(ATTR_TIME) == "07:00:00"
|
|
|
|
assert alarm_state.attributes.get(ATTR_ID) == "14"
|
|
|
|
assert alarm_state.attributes.get(ATTR_DURATION) == "02:00:00"
|
|
|
|
assert alarm_state.attributes.get(ATTR_RECURRENCE) == "DAILY"
|
|
|
|
assert alarm_state.attributes.get(ATTR_VOLUME) == 0.25
|
|
|
|
assert alarm_state.attributes.get(ATTR_PLAY_MODE) == "SHUFFLE_NOREPEAT"
|
|
|
|
assert not alarm_state.attributes.get(ATTR_INCLUDE_LINKED_ZONES)
|
2021-05-27 17:56:59 +00:00
|
|
|
|
2021-10-23 21:11:27 +00:00
|
|
|
night_sound = entity_registry.entities["switch.sonos_zone_a_night_sound"]
|
|
|
|
night_sound_state = hass.states.get(night_sound.entity_id)
|
|
|
|
assert night_sound_state.state == STATE_ON
|
|
|
|
|
|
|
|
speech_enhancement = entity_registry.entities[
|
|
|
|
"switch.sonos_zone_a_speech_enhancement"
|
|
|
|
]
|
|
|
|
speech_enhancement_state = hass.states.get(speech_enhancement.entity_id)
|
|
|
|
assert speech_enhancement_state.state == STATE_ON
|
|
|
|
|
|
|
|
crossfade = entity_registry.entities["switch.sonos_zone_a_crossfade"]
|
|
|
|
crossfade_state = hass.states.get(crossfade.entity_id)
|
|
|
|
assert crossfade_state.state == STATE_ON
|
|
|
|
|
2021-10-29 19:43:59 +00:00
|
|
|
# Ensure switches are disabled
|
2021-10-23 21:11:27 +00:00
|
|
|
status_light = entity_registry.entities["switch.sonos_zone_a_status_light"]
|
2021-10-29 19:43:59 +00:00
|
|
|
assert hass.states.get(status_light.entity_id) is None
|
|
|
|
|
|
|
|
touch_controls = entity_registry.entities["switch.sonos_zone_a_touch_controls"]
|
|
|
|
assert hass.states.get(touch_controls.entity_id) is None
|
|
|
|
|
2021-12-03 15:05:40 +00:00
|
|
|
sub_switch = entity_registry.entities["switch.sonos_zone_a_subwoofer_enabled"]
|
|
|
|
sub_switch_state = hass.states.get(sub_switch.entity_id)
|
|
|
|
assert sub_switch_state.state == STATE_OFF
|
|
|
|
|
|
|
|
surround_switch = entity_registry.entities["switch.sonos_zone_a_surround_enabled"]
|
|
|
|
surround_switch_state = hass.states.get(surround_switch.entity_id)
|
|
|
|
assert surround_switch_state.state == STATE_ON
|
|
|
|
|
2021-10-29 19:43:59 +00:00
|
|
|
# Enable disabled switches
|
|
|
|
for entity in (status_light, touch_controls):
|
|
|
|
entity_registry.async_update_entity(
|
|
|
|
entity_id=entity.entity_id, disabled_by=None
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Fire event to cancel poll timer and avoid triggering errors during time jump
|
|
|
|
service = soco.contentDirectory
|
|
|
|
empty_event = SonosMockEvent(soco, service, {})
|
|
|
|
subscription = service.subscribe.return_value
|
|
|
|
subscription.callback(event=empty_event)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Mock shutdown calls during config entry reload
|
|
|
|
with patch.object(hass.data[DATA_SONOS_DISCOVERY_MANAGER], "async_shutdown") as m:
|
|
|
|
async_fire_time_changed(
|
|
|
|
hass,
|
|
|
|
dt.utcnow() + timedelta(seconds=RELOAD_AFTER_UPDATE_DELAY + 1),
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert m.called
|
|
|
|
|
2021-10-23 21:11:27 +00:00
|
|
|
status_light_state = hass.states.get(status_light.entity_id)
|
|
|
|
assert status_light_state.state == STATE_ON
|
|
|
|
|
|
|
|
touch_controls = entity_registry.entities["switch.sonos_zone_a_touch_controls"]
|
|
|
|
touch_controls_state = hass.states.get(touch_controls.entity_id)
|
|
|
|
assert touch_controls_state.state == STATE_ON
|
|
|
|
|
2021-05-27 17:56:59 +00:00
|
|
|
|
|
|
|
async def test_alarm_create_delete(
|
2021-12-08 18:28:27 +00:00
|
|
|
hass, async_setup_sonos, soco, alarm_clock, alarm_clock_extended, alarm_event
|
2021-05-27 17:56:59 +00:00
|
|
|
):
|
|
|
|
"""Test for correct creation and deletion of alarms during runtime."""
|
2021-12-08 18:28:27 +00:00
|
|
|
entity_registry = ent_reg.async_get(hass)
|
2021-06-16 15:30:05 +00:00
|
|
|
|
|
|
|
one_alarm = copy(alarm_clock.ListAlarms.return_value)
|
|
|
|
two_alarms = copy(alarm_clock_extended.ListAlarms.return_value)
|
2021-05-27 17:56:59 +00:00
|
|
|
|
2021-12-08 18:28:27 +00:00
|
|
|
await async_setup_sonos()
|
2021-05-27 17:56:59 +00:00
|
|
|
|
2021-06-16 15:30:05 +00:00
|
|
|
assert "switch.sonos_alarm_14" in entity_registry.entities
|
|
|
|
assert "switch.sonos_alarm_15" not in entity_registry.entities
|
|
|
|
|
|
|
|
subscription = alarm_clock.subscribe.return_value
|
2021-05-27 17:56:59 +00:00
|
|
|
sub_callback = subscription.callback
|
|
|
|
|
2021-06-16 15:30:05 +00:00
|
|
|
alarm_clock.ListAlarms.return_value = two_alarms
|
|
|
|
|
2021-09-28 07:49:32 +00:00
|
|
|
alarm_event.variables["alarm_list_version"] = two_alarms["CurrentAlarmListVersion"]
|
|
|
|
|
2021-05-27 17:56:59 +00:00
|
|
|
sub_callback(event=alarm_event)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert "switch.sonos_alarm_14" in entity_registry.entities
|
|
|
|
assert "switch.sonos_alarm_15" in entity_registry.entities
|
|
|
|
|
2021-09-28 07:49:32 +00:00
|
|
|
one_alarm["CurrentAlarmListVersion"] = alarm_event.increment_variable(
|
|
|
|
"alarm_list_version"
|
|
|
|
)
|
2021-05-27 17:56:59 +00:00
|
|
|
|
2021-06-16 15:30:05 +00:00
|
|
|
alarm_clock.ListAlarms.return_value = one_alarm
|
|
|
|
|
2021-05-27 17:56:59 +00:00
|
|
|
sub_callback(event=alarm_event)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert "switch.sonos_alarm_14" in entity_registry.entities
|
|
|
|
assert "switch.sonos_alarm_15" not in entity_registry.entities
|