|
| 1 | +"""Tests for the switch module.""" |
| 2 | + |
| 3 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 4 | + |
| 5 | +from eheimdigital.types import EheimDeviceType |
| 6 | +import pytest |
| 7 | +from syrupy.assertion import SnapshotAssertion |
| 8 | + |
| 9 | +from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN |
| 10 | +from homeassistant.const import ( |
| 11 | + ATTR_ENTITY_ID, |
| 12 | + SERVICE_TURN_OFF, |
| 13 | + SERVICE_TURN_ON, |
| 14 | + STATE_OFF, |
| 15 | + STATE_ON, |
| 16 | + Platform, |
| 17 | +) |
| 18 | +from homeassistant.core import HomeAssistant |
| 19 | +from homeassistant.helpers import entity_registry as er |
| 20 | + |
| 21 | +from .conftest import init_integration |
| 22 | + |
| 23 | +from tests.common import MockConfigEntry, snapshot_platform |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.usefixtures("classic_vario_mock") |
| 27 | +async def test_setup_classic_vario( |
| 28 | + hass: HomeAssistant, |
| 29 | + eheimdigital_hub_mock: MagicMock, |
| 30 | + mock_config_entry: MockConfigEntry, |
| 31 | + entity_registry: er.EntityRegistry, |
| 32 | + snapshot: SnapshotAssertion, |
| 33 | +) -> None: |
| 34 | + """Test switch platform setup for the filter.""" |
| 35 | + mock_config_entry.add_to_hass(hass) |
| 36 | + |
| 37 | + with ( |
| 38 | + patch("homeassistant.components.eheimdigital.PLATFORMS", [Platform.SWITCH]), |
| 39 | + patch( |
| 40 | + "homeassistant.components.eheimdigital.coordinator.asyncio.Event", |
| 41 | + new=AsyncMock, |
| 42 | + ), |
| 43 | + ): |
| 44 | + await hass.config_entries.async_setup(mock_config_entry.entry_id) |
| 45 | + |
| 46 | + await eheimdigital_hub_mock.call_args.kwargs["device_found_callback"]( |
| 47 | + "00:00:00:00:00:03", EheimDeviceType.VERSION_EHEIM_CLASSIC_VARIO |
| 48 | + ) |
| 49 | + await hass.async_block_till_done() |
| 50 | + |
| 51 | + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.parametrize( |
| 55 | + ("service", "active"), [(SERVICE_TURN_OFF, False), (SERVICE_TURN_ON, True)] |
| 56 | +) |
| 57 | +async def test_turn_on_off( |
| 58 | + hass: HomeAssistant, |
| 59 | + eheimdigital_hub_mock: MagicMock, |
| 60 | + mock_config_entry: MockConfigEntry, |
| 61 | + classic_vario_mock: MagicMock, |
| 62 | + service: str, |
| 63 | + active: bool, |
| 64 | +) -> None: |
| 65 | + """Test turning on/off the switch.""" |
| 66 | + await init_integration(hass, mock_config_entry) |
| 67 | + |
| 68 | + await eheimdigital_hub_mock.call_args.kwargs["device_found_callback"]( |
| 69 | + "00:00:00:00:00:03", EheimDeviceType.VERSION_EHEIM_CLASSIC_VARIO |
| 70 | + ) |
| 71 | + await hass.async_block_till_done() |
| 72 | + |
| 73 | + await hass.services.async_call( |
| 74 | + SWITCH_DOMAIN, |
| 75 | + service, |
| 76 | + {ATTR_ENTITY_ID: "switch.mock_classicvario"}, |
| 77 | + blocking=True, |
| 78 | + ) |
| 79 | + |
| 80 | + classic_vario_mock.set_active.assert_awaited_once_with(active=active) |
| 81 | + |
| 82 | + |
| 83 | +async def test_state_update( |
| 84 | + hass: HomeAssistant, |
| 85 | + eheimdigital_hub_mock: MagicMock, |
| 86 | + mock_config_entry: MockConfigEntry, |
| 87 | + classic_vario_mock: MagicMock, |
| 88 | +) -> None: |
| 89 | + """Test the switch state update.""" |
| 90 | + await init_integration(hass, mock_config_entry) |
| 91 | + |
| 92 | + await eheimdigital_hub_mock.call_args.kwargs["device_found_callback"]( |
| 93 | + "00:00:00:00:00:03", EheimDeviceType.VERSION_EHEIM_CLASSIC_VARIO |
| 94 | + ) |
| 95 | + await hass.async_block_till_done() |
| 96 | + |
| 97 | + assert (state := hass.states.get("switch.mock_classicvario")) |
| 98 | + assert state.state == STATE_ON |
| 99 | + |
| 100 | + classic_vario_mock.is_active = False |
| 101 | + |
| 102 | + await eheimdigital_hub_mock.call_args.kwargs["receive_callback"]() |
| 103 | + |
| 104 | + assert (state := hass.states.get("switch.mock_classicvario")) |
| 105 | + assert state.state == STATE_OFF |
0 commit comments