|
1 | 1 | """Test squeezebox binary sensors.""" |
2 | 2 |
|
3 | 3 | from copy import deepcopy |
4 | | -from unittest.mock import patch |
| 4 | +from datetime import timedelta |
| 5 | +from unittest.mock import MagicMock, patch |
5 | 6 |
|
6 | | -from homeassistant.const import Platform |
| 7 | +from freezegun.api import FrozenDateTimeFactory |
| 8 | +import pytest |
| 9 | + |
| 10 | +from homeassistant.components.binary_sensor import BinarySensorDeviceClass |
| 11 | +from homeassistant.components.squeezebox.const import PLAYER_UPDATE_INTERVAL |
| 12 | +from homeassistant.const import STATE_OFF, STATE_ON, Platform |
7 | 13 | from homeassistant.core import HomeAssistant |
8 | 14 |
|
9 | 15 | from .conftest import FAKE_QUERY_RESPONSE |
10 | 16 |
|
11 | | -from tests.common import MockConfigEntry |
| 17 | +from tests.common import MockConfigEntry, async_fire_time_changed |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture(autouse=True) |
| 21 | +def squeezebox_binary_sensor_platform(): |
| 22 | + """Only set up the binary_sensor platform for squeezebox tests.""" |
| 23 | + with patch( |
| 24 | + "homeassistant.components.squeezebox.PLATFORMS", [Platform.BINARY_SENSOR] |
| 25 | + ): |
| 26 | + yield |
12 | 27 |
|
13 | 28 |
|
14 | | -async def test_binary_sensor( |
| 29 | +async def test_binary_server_sensor( |
15 | 30 | hass: HomeAssistant, |
16 | 31 | config_entry: MockConfigEntry, |
17 | 32 | ) -> None: |
18 | 33 | """Test binary sensor states and attributes.""" |
19 | | - with ( |
20 | | - patch( |
21 | | - "homeassistant.components.squeezebox.PLATFORMS", |
22 | | - [Platform.BINARY_SENSOR], |
23 | | - ), |
24 | | - patch( |
25 | | - "homeassistant.components.squeezebox.Server.async_query", |
26 | | - return_value=deepcopy(FAKE_QUERY_RESPONSE), |
27 | | - ), |
| 34 | + with patch( |
| 35 | + "homeassistant.components.squeezebox.Server.async_query", |
| 36 | + return_value=deepcopy(FAKE_QUERY_RESPONSE), |
28 | 37 | ): |
29 | 38 | await hass.config_entries.async_setup(config_entry.entry_id) |
30 | 39 | await hass.async_block_till_done(wait_background_tasks=True) |
31 | 40 |
|
32 | 41 | state = hass.states.get("binary_sensor.fakelib_needs_restart") |
33 | 42 |
|
34 | 43 | assert state is not None |
35 | | - assert state.state == "off" |
| 44 | + assert state.state == STATE_OFF |
| 45 | + |
| 46 | + |
| 47 | +@pytest.fixture |
| 48 | +async def mock_player( |
| 49 | + hass: HomeAssistant, |
| 50 | + config_entry: MockConfigEntry, |
| 51 | + lms: MagicMock, |
| 52 | +) -> MagicMock: |
| 53 | + """Set up the squeezebox integration and return the mocked player.""" |
| 54 | + |
| 55 | + # Mock server status data for coordinator update |
| 56 | + # called on update, return something != None to not raise |
| 57 | + lms.async_prepared_status.return_value = { |
| 58 | + "dummy": False, |
| 59 | + } |
| 60 | + with patch("homeassistant.components.squeezebox.Server", return_value=lms): |
| 61 | + await hass.config_entries.async_setup(config_entry.entry_id) |
| 62 | + await hass.async_block_till_done(wait_background_tasks=True) |
| 63 | + |
| 64 | + # Return the player mock |
| 65 | + return (await lms.async_get_players())[0] |
| 66 | + |
| 67 | + |
| 68 | +async def test_player_alarm_sensors_device_class( |
| 69 | + hass: HomeAssistant, |
| 70 | + mock_player: MagicMock, |
| 71 | +) -> None: |
| 72 | + """Test player alarm binary sensors have correct device class.""" |
| 73 | + |
| 74 | + # Test alarm upcoming sensor device class |
| 75 | + upcoming_state = hass.states.get("binary_sensor.none_alarm_upcoming") |
| 76 | + assert upcoming_state is not None |
| 77 | + assert upcoming_state.attributes.get("device_class") is None |
| 78 | + |
| 79 | + # Test alarm active sensor device class |
| 80 | + active_state = hass.states.get("binary_sensor.none_alarm_active") |
| 81 | + assert active_state is not None |
| 82 | + assert ( |
| 83 | + active_state.attributes.get("device_class") == BinarySensorDeviceClass.RUNNING |
| 84 | + ) |
| 85 | + |
| 86 | + # Test alarm snooze sensor device class |
| 87 | + snooze_state = hass.states.get("binary_sensor.none_alarm_snoozed") |
| 88 | + assert snooze_state is not None |
| 89 | + assert ( |
| 90 | + snooze_state.attributes.get("device_class") == BinarySensorDeviceClass.RUNNING |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +async def test_player_alarm_sensors_state( |
| 95 | + hass: HomeAssistant, |
| 96 | + mock_player: MagicMock, |
| 97 | + freezer: FrozenDateTimeFactory, |
| 98 | +) -> None: |
| 99 | + """Test player alarm binary sensors with default states.""" |
| 100 | + |
| 101 | + player = mock_player |
| 102 | + |
| 103 | + # Test alarm upcoming sensor |
| 104 | + upcoming_state = hass.states.get("binary_sensor.none_alarm_upcoming") |
| 105 | + assert upcoming_state is not None |
| 106 | + assert upcoming_state.state == STATE_ON |
| 107 | + |
| 108 | + # Test alarm active sensor |
| 109 | + active_state = hass.states.get("binary_sensor.none_alarm_active") |
| 110 | + assert active_state is not None |
| 111 | + assert active_state.state == STATE_OFF |
| 112 | + |
| 113 | + # Test alarm snooze sensor |
| 114 | + snooze_state = hass.states.get("binary_sensor.none_alarm_snoozed") |
| 115 | + assert snooze_state is not None |
| 116 | + assert snooze_state.state == STATE_OFF |
| 117 | + |
| 118 | + # Toggle alarm states and verify sensors update |
| 119 | + player.alarm_upcoming = False |
| 120 | + player.alarm_active = True |
| 121 | + player.alarm_snooze = True |
| 122 | + freezer.tick(timedelta(seconds=PLAYER_UPDATE_INTERVAL)) |
| 123 | + async_fire_time_changed(hass) |
| 124 | + await hass.async_block_till_done() |
| 125 | + |
| 126 | + upcoming_state = hass.states.get("binary_sensor.none_alarm_upcoming") |
| 127 | + assert upcoming_state is not None |
| 128 | + assert upcoming_state.state == STATE_OFF |
| 129 | + |
| 130 | + active_state = hass.states.get("binary_sensor.none_alarm_active") |
| 131 | + assert active_state is not None |
| 132 | + assert active_state.state == STATE_ON |
0 commit comments