|
1 | 1 | """Test binary sensors of APCUPSd integration.""" |
2 | 2 |
|
3 | | -from unittest.mock import patch |
| 3 | +from unittest.mock import AsyncMock |
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 | from syrupy.assertion import SnapshotAssertion |
|
10 | 10 | from homeassistant.helpers import entity_registry as er |
11 | 11 | from homeassistant.util import slugify |
12 | 12 |
|
13 | | -from . import MOCK_STATUS, async_init_integration |
| 13 | +from . import MOCK_STATUS |
14 | 14 |
|
15 | | -from tests.common import snapshot_platform |
| 15 | +from tests.common import MockConfigEntry, snapshot_platform |
| 16 | + |
| 17 | +pytestmark = pytest.mark.usefixtures( |
| 18 | + "entity_registry_enabled_by_default", "init_integration" |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def platforms() -> list[Platform]: |
| 24 | + """Overridden fixture to specify platforms to test.""" |
| 25 | + return [Platform.BINARY_SENSOR] |
16 | 26 |
|
17 | 27 |
|
18 | 28 | async def test_binary_sensor( |
19 | 29 | hass: HomeAssistant, |
20 | 30 | entity_registry: er.EntityRegistry, |
21 | 31 | snapshot: SnapshotAssertion, |
| 32 | + mock_config_entry: MockConfigEntry, |
22 | 33 | ) -> None: |
23 | | - """Test states of binary sensors.""" |
24 | | - with patch("homeassistant.components.apcupsd.PLATFORMS", [Platform.BINARY_SENSOR]): |
25 | | - config_entry = await async_init_integration(hass, status=MOCK_STATUS) |
26 | | - await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) |
| 34 | + """Test states of binary sensor entities.""" |
| 35 | + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) |
27 | 36 |
|
28 | 37 |
|
29 | | -async def test_no_binary_sensor(hass: HomeAssistant) -> None: |
| 38 | +@pytest.mark.parametrize( |
| 39 | + "mock_request_status", |
| 40 | + [{k: v for k, v in MOCK_STATUS.items() if k != "STATFLAG"}], |
| 41 | + indirect=True, |
| 42 | +) |
| 43 | +async def test_no_binary_sensor( |
| 44 | + hass: HomeAssistant, |
| 45 | + mock_request_status: AsyncMock, |
| 46 | +) -> None: |
30 | 47 | """Test binary sensor when STATFLAG is not available.""" |
31 | | - status = MOCK_STATUS.copy() |
32 | | - status.pop("STATFLAG") |
33 | | - await async_init_integration(hass, status=status) |
34 | | - |
35 | | - device_slug = slugify(MOCK_STATUS["UPSNAME"]) |
| 48 | + device_slug = slugify(mock_request_status.return_value["UPSNAME"]) |
36 | 49 | state = hass.states.get(f"binary_sensor.{device_slug}_online_status") |
37 | 50 | assert state is None |
38 | 51 |
|
39 | 52 |
|
40 | 53 | @pytest.mark.parametrize( |
41 | | - ("override", "expected"), |
| 54 | + ("mock_request_status", "expected"), |
42 | 55 | [ |
43 | | - ("0x008", "on"), |
44 | | - ("0x02040010 Status Flag", "off"), |
| 56 | + (MOCK_STATUS | {"STATFLAG": "0x008"}, "on"), |
| 57 | + (MOCK_STATUS | {"STATFLAG": "0x02040010 Status Flag"}, "off"), |
45 | 58 | ], |
| 59 | + indirect=["mock_request_status"], |
46 | 60 | ) |
47 | | -async def test_statflag(hass: HomeAssistant, override: str, expected: str) -> None: |
| 61 | +async def test_statflag( |
| 62 | + hass: HomeAssistant, |
| 63 | + mock_request_status: AsyncMock, |
| 64 | + expected: str, |
| 65 | +) -> None: |
48 | 66 | """Test binary sensor for different STATFLAG values.""" |
49 | | - status = MOCK_STATUS.copy() |
50 | | - status["STATFLAG"] = override |
51 | | - await async_init_integration(hass, status=status) |
52 | | - |
53 | | - device_slug = slugify(MOCK_STATUS["UPSNAME"]) |
54 | | - assert ( |
55 | | - hass.states.get(f"binary_sensor.{device_slug}_online_status").state == expected |
56 | | - ) |
| 67 | + device_slug = slugify(mock_request_status.return_value["UPSNAME"]) |
| 68 | + state = hass.states.get(f"binary_sensor.{device_slug}_online_status") |
| 69 | + assert state.state == expected |
0 commit comments