Squeezebox add alarms support - switch platform. Part 1 (#141055)

* initial

* remove dupe name definition

* snapshot update

* name def updates

* test update for new entity name

* remove attributes

* icon translations

* merge fixes

* Snapshot update post merge

* update to class initialisation

* move entity delete to coordinator

* remove some comments

* move known_alarms to coordinator

* test_switch update for syrupy change

* listener and sets

* check self.available

* remove refresh from conftest

* test update

* test tweak

* move listener to switch platform

* updates revew

* SWITCH_DOMAIN
This commit is contained in:
peteS-UK
2025-05-26 17:41:28 +02:00
committed by GitHub
parent 3dc7b75e4b
commit 8623d96deb
9 changed files with 505 additions and 3 deletions
+43 -1
View File
@@ -32,7 +32,6 @@ from homeassistant.const import CONF_HOST, CONF_PORT, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import format_mac
# from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
CONF_VOLUME_STEP = "volume_step"
@@ -48,6 +47,7 @@ SERVER_UUIDS = [
TEST_MAC = ["aa:bb:cc:dd:ee:ff", "ff:ee:dd:cc:bb:aa"]
TEST_PLAYER_NAME = "Test Player"
TEST_SERVER_NAME = "Test Server"
TEST_ALARM_ID = "1"
FAKE_VALID_ITEM_ID = "1234"
FAKE_INVALID_ITEM_ID = "4321"
@@ -294,6 +294,7 @@ def mock_pysqueezebox_player(uuid: str) -> MagicMock:
mock_player.image_url = None
mock_player.model = "SqueezeLite"
mock_player.creator = "Ralph Irving & Adrian Smith"
mock_player.alarms_enabled = True
return mock_player
@@ -363,6 +364,47 @@ async def configure_squeezebox_media_player_button_platform(
await hass.async_block_till_done(wait_background_tasks=True)
async def configure_squeezebox_switch_platform(
hass: HomeAssistant,
config_entry: MockConfigEntry,
lms: MagicMock,
) -> None:
"""Configure a squeezebox config entry with appropriate mocks for switch."""
with (
patch(
"homeassistant.components.squeezebox.PLATFORMS",
[Platform.SWITCH],
),
patch("homeassistant.components.squeezebox.Server", return_value=lms),
):
# Set up the switch platform.
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done(wait_background_tasks=True)
@pytest.fixture
async def mock_alarms_player(
hass: HomeAssistant,
config_entry: MockConfigEntry,
lms: MagicMock,
) -> MagicMock:
"""Mock the alarms of a configured player."""
players = await lms.async_get_players()
players[0].alarms = [
{
"id": TEST_ALARM_ID,
"enabled": True,
"time": "07:00",
"dow": [0, 1, 2, 3, 4, 5, 6],
"repeat": False,
"url": "CURRENT_PLAYLIST",
"volume": 50,
},
]
await configure_squeezebox_switch_platform(hass, config_entry, lms)
return players[0]
@pytest.fixture
async def configured_player(
hass: HomeAssistant, config_entry: MockConfigEntry, lms: MagicMock
@@ -0,0 +1,96 @@
# serializer version: 1
# name: test_entity_registry[switch.test_player_alarm_1-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'switch.test_player_alarm_1',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Alarm (1)',
'platform': 'squeezebox',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'alarm',
'unique_id': 'aa:bb:cc:dd:ee:ff_alarm_1',
'unit_of_measurement': None,
})
# ---
# name: test_entity_registry[switch.test_player_alarm_1-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'alarm_id': '1',
'friendly_name': 'Test Player Alarm (1)',
}),
'context': <ANY>,
'entity_id': 'switch.test_player_alarm_1',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
# name: test_entity_registry[switch.test_player_alarms_enabled-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'switch.test_player_alarms_enabled',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Alarms enabled',
'platform': 'squeezebox',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'alarms_enabled',
'unique_id': 'aa:bb:cc:dd:ee:ff_alarms_enabled',
'unit_of_measurement': None,
})
# ---
# name: test_entity_registry[switch.test_player_alarms_enabled-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Test Player Alarms enabled',
}),
'context': <ANY>,
'entity_id': 'switch.test_player_alarms_enabled',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
+135
View File
@@ -0,0 +1,135 @@
"""Tests for the Squeezebox alarm switch platform."""
from datetime import timedelta
from unittest.mock import MagicMock
from freezegun.api import FrozenDateTimeFactory
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.squeezebox.const import PLAYER_UPDATE_INTERVAL
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.const import CONF_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_registry import EntityRegistry
from .conftest import TEST_ALARM_ID
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
async def test_entity_registry(
hass: HomeAssistant,
entity_registry: EntityRegistry,
mock_alarms_player: MagicMock,
snapshot: SnapshotAssertion,
config_entry: MockConfigEntry,
) -> None:
"""Test squeezebox media_player entity registered in the entity registry."""
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
async def test_switch_state(
hass: HomeAssistant,
mock_alarms_player: MagicMock,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test the state of the switch."""
assert hass.states.get(f"switch.test_player_alarm_{TEST_ALARM_ID}").state == "on"
mock_alarms_player.alarms[0]["enabled"] = False
freezer.tick(timedelta(seconds=PLAYER_UPDATE_INTERVAL))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert hass.states.get(f"switch.test_player_alarm_{TEST_ALARM_ID}").state == "off"
async def test_switch_deleted(
hass: HomeAssistant,
mock_alarms_player: MagicMock,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test detecting switch deleted."""
assert hass.states.get(f"switch.test_player_alarm_{TEST_ALARM_ID}").state == "on"
mock_alarms_player.alarms = []
freezer.tick(timedelta(seconds=PLAYER_UPDATE_INTERVAL))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert hass.states.get(f"switch.test_player_alarm_{TEST_ALARM_ID}") is None
async def test_turn_on(
hass: HomeAssistant,
mock_alarms_player: MagicMock,
) -> None:
"""Test turning on the switch."""
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{CONF_ENTITY_ID: f"switch.test_player_alarm_{TEST_ALARM_ID}"},
blocking=True,
)
mock_alarms_player.async_update_alarm.assert_called_once_with(
TEST_ALARM_ID, enabled=True
)
async def test_turn_off(
hass: HomeAssistant,
mock_alarms_player: MagicMock,
) -> None:
"""Test turning on the switch."""
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{CONF_ENTITY_ID: f"switch.test_player_alarm_{TEST_ALARM_ID}"},
blocking=True,
)
mock_alarms_player.async_update_alarm.assert_called_once_with(
TEST_ALARM_ID, enabled=False
)
async def test_alarms_enabled_state(
hass: HomeAssistant,
mock_alarms_player: MagicMock,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test the alarms enabled switch."""
assert hass.states.get("switch.test_player_alarms_enabled").state == "on"
mock_alarms_player.alarms_enabled = False
freezer.tick(timedelta(seconds=PLAYER_UPDATE_INTERVAL))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert hass.states.get("switch.test_player_alarms_enabled").state == "off"
async def test_alarms_enabled_turn_on(
hass: HomeAssistant,
mock_alarms_player: MagicMock,
) -> None:
"""Test turning on the alarms enabled switch."""
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{CONF_ENTITY_ID: "switch.test_player_alarms_enabled"},
blocking=True,
)
mock_alarms_player.async_set_alarms_enabled.assert_called_once_with(True)
async def test_alarms_enabled_turn_off(
hass: HomeAssistant,
mock_alarms_player: MagicMock,
) -> None:
"""Test turning off the alarms enabled switch."""
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{CONF_ENTITY_ID: "switch.test_player_alarms_enabled"},
blocking=True,
)
mock_alarms_player.async_set_alarms_enabled.assert_called_once_with(False)