diff --git a/tests/components/deconz/conftest.py b/tests/components/deconz/conftest.py index 9a50e3cb621..80dbb09b643 100644 --- a/tests/components/deconz/conftest.py +++ b/tests/components/deconz/conftest.py @@ -2,7 +2,7 @@ from __future__ import annotations -from collections.abc import Callable +from collections.abc import Callable, Generator from types import MappingProxyType from typing import Any from unittest.mock import patch @@ -20,6 +20,9 @@ from tests.components.light.conftest import mock_light_profiles # noqa: F401 from tests.test_util.aiohttp import AiohttpClientMocker type ConfigEntryFactoryType = Callable[[ConfigEntry | None], ConfigEntry] +type WebsocketDataType = Callable[[dict[str, Any]], None] +type WebsocketStateType = Callable[[str], None] +type _WebsocketMock = Generator[Any, Any, Callable[[dict[str, Any] | None, str], None]] # Config entry fixtures @@ -217,22 +220,46 @@ async def fixture_config_entry_setup( # Websocket fixtures -@pytest.fixture(autouse=True) -def mock_deconz_websocket(): +@pytest.fixture(autouse=True, name="_mock_websocket") +def fixture_websocket() -> _WebsocketMock: """No real websocket allowed.""" with patch("pydeconz.gateway.WSClient") as mock: - async def make_websocket_call(data: dict | None = None, state: str = ""): + async def make_websocket_call( + data: dict[str, Any] | None = None, state: str = "" + ) -> None: """Generate a websocket call.""" pydeconz_gateway_session_handler = mock.call_args[0][3] + signal: Signal if data: mock.return_value.data = data - await pydeconz_gateway_session_handler(signal=Signal.DATA) + signal = Signal.DATA elif state: mock.return_value.state = state - await pydeconz_gateway_session_handler(signal=Signal.CONNECTION_STATE) - else: - raise NotImplementedError + signal = Signal.CONNECTION_STATE + await pydeconz_gateway_session_handler(signal) yield make_websocket_call + + +@pytest.fixture(name="mock_websocket_data") +def fixture_websocket_data(_mock_websocket: _WebsocketMock) -> WebsocketDataType: + """Fixture to send websocket data.""" + + async def change_websocket_data(data: dict[str, Any]) -> None: + """Provide new data on the websocket.""" + await _mock_websocket(data=data) + + return change_websocket_data + + +@pytest.fixture(name="mock_websocket_state") +def fixture_websocket_state(_mock_websocket: _WebsocketMock) -> WebsocketStateType: + """Fixture to set websocket state.""" + + async def change_websocket_state(state: str) -> None: + """Simulate a change to the websocket connection state.""" + await _mock_websocket(state=state) + + return change_websocket_state diff --git a/tests/components/deconz/test_alarm_control_panel.py b/tests/components/deconz/test_alarm_control_panel.py index 1f1b65aff23..15c4d26f4c5 100644 --- a/tests/components/deconz/test_alarm_control_panel.py +++ b/tests/components/deconz/test_alarm_control_panel.py @@ -28,6 +28,8 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant +from .conftest import WebsocketDataType + from tests.test_util.aiohttp import AiohttpClientMocker @@ -102,7 +104,7 @@ async def test_alarm_control_panel( aioclient_mock: AiohttpClientMocker, config_entry_setup: ConfigEntry, mock_put_request: Callable[[str, str], AiohttpClientMocker], - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, ) -> None: """Test successful creation of alarm control panel entities.""" assert len(hass.states.async_all()) == 4 @@ -117,7 +119,7 @@ async def test_alarm_control_panel( "id": "0", "state": {"panel": AncillaryControlPanel.ARMED_AWAY}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert hass.states.get("alarm_control_panel.keypad").state == STATE_ALARM_ARMED_AWAY @@ -131,7 +133,7 @@ async def test_alarm_control_panel( "id": "0", "state": {"panel": AncillaryControlPanel.ARMED_NIGHT}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert ( @@ -147,7 +149,7 @@ async def test_alarm_control_panel( "id": "0", "state": {"panel": AncillaryControlPanel.ARMED_STAY}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert hass.states.get("alarm_control_panel.keypad").state == STATE_ALARM_ARMED_HOME @@ -161,7 +163,7 @@ async def test_alarm_control_panel( "id": "0", "state": {"panel": AncillaryControlPanel.DISARMED}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert hass.states.get("alarm_control_panel.keypad").state == STATE_ALARM_DISARMED @@ -180,7 +182,7 @@ async def test_alarm_control_panel( "id": "0", "state": {"panel": arming_event}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert hass.states.get("alarm_control_panel.keypad").state == STATE_ALARM_ARMING @@ -198,7 +200,7 @@ async def test_alarm_control_panel( "id": "0", "state": {"panel": pending_event}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert ( @@ -214,7 +216,7 @@ async def test_alarm_control_panel( "id": "0", "state": {"panel": AncillaryControlPanel.IN_ALARM}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert hass.states.get("alarm_control_panel.keypad").state == STATE_ALARM_TRIGGERED @@ -228,7 +230,7 @@ async def test_alarm_control_panel( "id": "0", "state": {"panel": AncillaryControlPanel.NOT_READY}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert hass.states.get("alarm_control_panel.keypad").state == STATE_ALARM_TRIGGERED diff --git a/tests/components/deconz/test_binary_sensor.py b/tests/components/deconz/test_binary_sensor.py index 76962ec8f46..58c357628a0 100644 --- a/tests/components/deconz/test_binary_sensor.py +++ b/tests/components/deconz/test_binary_sensor.py @@ -24,6 +24,8 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er +from .conftest import WebsocketDataType + TEST_DATA = [ ( # Alarm binary sensor { @@ -458,7 +460,7 @@ async def test_binary_sensors( device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, config_entry_setup: ConfigEntry, - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, expected: dict[str, Any], ) -> None: """Test successful creation of binary sensor entities.""" @@ -497,7 +499,7 @@ async def test_binary_sensors( "id": "1", "state": expected["websocket_event"], } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert hass.states.get(expected["entity_id"]).state == expected["next_state"] @@ -598,7 +600,8 @@ async def test_allow_clip_sensor(hass: HomeAssistant, config_entry_setup) -> Non @pytest.mark.usefixtures("config_entry_setup") async def test_add_new_binary_sensor( - hass: HomeAssistant, mock_deconz_websocket + hass: HomeAssistant, + mock_websocket_data: WebsocketDataType, ) -> None: """Test that adding a new binary sensor works.""" assert len(hass.states.async_all()) == 0 @@ -617,7 +620,7 @@ async def test_add_new_binary_sensor( "uniqueid": "00:00:00:00:00:00:00:00-00", }, } - await mock_deconz_websocket(data=event_added_sensor) + await mock_websocket_data(event_added_sensor) await hass.async_block_till_done() assert len(hass.states.async_all()) == 1 @@ -633,7 +636,7 @@ async def test_add_new_binary_sensor_ignored_load_entities_on_service_call( config_entry_setup: ConfigEntry, deconz_payload: dict[str, Any], mock_requests: Callable[[str], None], - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, ) -> None: """Test that adding a new binary sensor is not allowed.""" sensor = { @@ -653,7 +656,7 @@ async def test_add_new_binary_sensor_ignored_load_entities_on_service_call( assert len(hass.states.async_all()) == 0 - await mock_deconz_websocket(data=event_added_sensor) + await mock_websocket_data(event_added_sensor) await hass.async_block_till_done() assert len(hass.states.async_all()) == 0 @@ -687,7 +690,7 @@ async def test_add_new_binary_sensor_ignored_load_entities_on_options_change( config_entry_setup: ConfigEntry, deconz_payload: dict[str, Any], mock_requests: Callable[[str], None], - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, ) -> None: """Test that adding a new binary sensor is not allowed.""" sensor = { @@ -707,7 +710,7 @@ async def test_add_new_binary_sensor_ignored_load_entities_on_options_change( assert len(hass.states.async_all()) == 0 - await mock_deconz_websocket(data=event_added_sensor) + await mock_websocket_data(event_added_sensor) await hass.async_block_till_done() assert len(hass.states.async_all()) == 0 diff --git a/tests/components/deconz/test_climate.py b/tests/components/deconz/test_climate.py index 63d1badc7bc..de24469257e 100644 --- a/tests/components/deconz/test_climate.py +++ b/tests/components/deconz/test_climate.py @@ -45,6 +45,8 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceValidationError +from .conftest import WebsocketDataType + from tests.test_util.aiohttp import AiohttpClientMocker @@ -89,7 +91,7 @@ from tests.test_util.aiohttp import AiohttpClientMocker async def test_simple_climate_device( hass: HomeAssistant, mock_put_request: Callable[[str, str], AiohttpClientMocker], - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, ) -> None: """Test successful creation of climate entities. @@ -117,7 +119,7 @@ async def test_simple_climate_device( "id": "0", "state": {"on": False}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert hass.states.get("climate.thermostat").state == STATE_OFF @@ -135,7 +137,7 @@ async def test_simple_climate_device( "id": "0", "state": {"on": True}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert hass.states.get("climate.thermostat").state == HVACMode.HEAT @@ -203,7 +205,7 @@ async def test_climate_device_without_cooling_support( hass: HomeAssistant, config_entry_setup: ConfigEntry, mock_put_request: Callable[[str, str], AiohttpClientMocker], - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, ) -> None: """Test successful creation of sensor entities.""" assert len(hass.states.async_all()) == 2 @@ -234,7 +236,7 @@ async def test_climate_device_without_cooling_support( "id": "1", "config": {"mode": "off"}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert hass.states.get("climate.thermostat").state == STATE_OFF @@ -253,7 +255,7 @@ async def test_climate_device_without_cooling_support( "config": {"mode": "other"}, "state": {"on": True}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert hass.states.get("climate.thermostat").state == HVACMode.HEAT @@ -271,7 +273,7 @@ async def test_climate_device_without_cooling_support( "id": "1", "state": {"on": False}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert hass.states.get("climate.thermostat").state == STATE_OFF @@ -396,7 +398,7 @@ async def test_climate_device_without_cooling_support( async def test_climate_device_with_cooling_support( hass: HomeAssistant, mock_put_request: Callable[[str, str], AiohttpClientMocker], - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, ) -> None: """Test successful creation of sensor entities.""" assert len(hass.states.async_all()) == 2 @@ -424,7 +426,7 @@ async def test_climate_device_with_cooling_support( "id": "0", "config": {"mode": "cool"}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() await hass.async_block_till_done() @@ -443,7 +445,7 @@ async def test_climate_device_with_cooling_support( "id": "0", "state": {"on": True}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert hass.states.get("climate.zen_01").state == HVACMode.COOL @@ -503,7 +505,7 @@ async def test_climate_device_with_cooling_support( async def test_climate_device_with_fan_support( hass: HomeAssistant, mock_put_request: Callable[[str, str], AiohttpClientMocker], - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, ) -> None: """Test successful creation of sensor entities.""" assert len(hass.states.async_all()) == 2 @@ -532,7 +534,7 @@ async def test_climate_device_with_fan_support( "id": "0", "config": {"fanmode": "unsupported"}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert hass.states.get("climate.zen_01").attributes["fan_mode"] == FAN_OFF @@ -550,7 +552,7 @@ async def test_climate_device_with_fan_support( "config": {"fanmode": "unsupported"}, "state": {"on": True}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert hass.states.get("climate.zen_01").attributes["fan_mode"] == FAN_ON @@ -568,7 +570,7 @@ async def test_climate_device_with_fan_support( "id": "0", "config": {"fanmode": "unsupported"}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert hass.states.get("climate.zen_01").attributes["fan_mode"] == FAN_ON @@ -649,7 +651,7 @@ async def test_climate_device_with_fan_support( async def test_climate_device_with_preset( hass: HomeAssistant, mock_put_request: Callable[[str, str], AiohttpClientMocker], - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, ) -> None: """Test successful creation of sensor entities.""" assert len(hass.states.async_all()) == 2 @@ -681,7 +683,7 @@ async def test_climate_device_with_preset( "id": "0", "config": {"preset": "manual"}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert ( @@ -698,7 +700,7 @@ async def test_climate_device_with_preset( "id": "0", "config": {"preset": "unsupported"}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert hass.states.get("climate.zen_01").attributes["preset_mode"] is None @@ -823,7 +825,10 @@ async def test_clip_climate_device( ], ) @pytest.mark.usefixtures("config_entry_setup") -async def test_verify_state_update(hass: HomeAssistant, mock_deconz_websocket) -> None: +async def test_verify_state_update( + hass: HomeAssistant, + mock_websocket_data: WebsocketDataType, +) -> None: """Test that state update properly.""" assert hass.states.get("climate.thermostat").state == HVACMode.AUTO assert ( @@ -838,7 +843,7 @@ async def test_verify_state_update(hass: HomeAssistant, mock_deconz_websocket) - "id": "1", "state": {"on": False}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert hass.states.get("climate.thermostat").state == HVACMode.AUTO @@ -850,7 +855,8 @@ async def test_verify_state_update(hass: HomeAssistant, mock_deconz_websocket) - @pytest.mark.usefixtures("config_entry_setup") async def test_add_new_climate_device( - hass: HomeAssistant, mock_deconz_websocket + hass: HomeAssistant, + mock_websocket_data: WebsocketDataType, ) -> None: """Test that adding a new climate device works.""" event_added_sensor = { @@ -876,7 +882,7 @@ async def test_add_new_climate_device( assert len(hass.states.async_all()) == 0 - await mock_deconz_websocket(data=event_added_sensor) + await mock_websocket_data(event_added_sensor) await hass.async_block_till_done() assert len(hass.states.async_all()) == 2 @@ -988,7 +994,10 @@ async def test_no_mode_no_state(hass: HomeAssistant) -> None: ], ) @pytest.mark.usefixtures("config_entry_setup") -async def test_boost_mode(hass: HomeAssistant, mock_deconz_websocket) -> None: +async def test_boost_mode( + hass: HomeAssistant, + mock_websocket_data: WebsocketDataType, +) -> None: """Test that a climate device with boost mode and different state works.""" assert len(hass.states.async_all()) == 3 @@ -1010,7 +1019,7 @@ async def test_boost_mode(hass: HomeAssistant, mock_deconz_websocket) -> None: "state": {"valve": 100}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() climate_thermostat = hass.states.get("climate.thermostat") diff --git a/tests/components/deconz/test_cover.py b/tests/components/deconz/test_cover.py index ec7c41d628c..991a3318cc4 100644 --- a/tests/components/deconz/test_cover.py +++ b/tests/components/deconz/test_cover.py @@ -28,6 +28,8 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant +from .conftest import WebsocketDataType + from tests.test_util.aiohttp import AiohttpClientMocker @@ -55,7 +57,7 @@ async def test_cover( hass: HomeAssistant, config_entry_setup: ConfigEntry, mock_put_request: Callable[[str, str], AiohttpClientMocker], - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, ) -> None: """Test that all supported cover entities are created.""" assert len(hass.states.async_all()) == 2 @@ -73,7 +75,7 @@ async def test_cover( "id": "1", "state": {"lift": 0, "open": True}, } - await mock_deconz_websocket(data=event_changed_light) + await mock_websocket_data(event_changed_light) await hass.async_block_till_done() cover = hass.states.get("cover.window_covering_device") diff --git a/tests/components/deconz/test_deconz_event.py b/tests/components/deconz/test_deconz_event.py index 7a56d318844..ce83b8fa742 100644 --- a/tests/components/deconz/test_deconz_event.py +++ b/tests/components/deconz/test_deconz_event.py @@ -28,6 +28,8 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr +from .conftest import WebsocketDataType + from tests.common import async_capture_events @@ -77,7 +79,7 @@ async def test_deconz_events( hass: HomeAssistant, device_registry: dr.DeviceRegistry, config_entry_setup: ConfigEntry, - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, ) -> None: """Test successful creation of deconz events.""" assert len(hass.states.async_all()) == 3 @@ -103,7 +105,7 @@ async def test_deconz_events( "id": "1", "state": {"buttonevent": 2000}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() device = device_registry.async_get_device( @@ -125,7 +127,7 @@ async def test_deconz_events( "id": "3", "state": {"buttonevent": 2000}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() device = device_registry.async_get_device( @@ -148,7 +150,7 @@ async def test_deconz_events( "id": "4", "state": {"gesture": 0}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() device = device_registry.async_get_device( @@ -171,7 +173,7 @@ async def test_deconz_events( "id": "5", "state": {"buttonevent": 6002, "angle": 110, "xy": [0.5982, 0.3897]}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() device = device_registry.async_get_device( @@ -197,7 +199,7 @@ async def test_deconz_events( "id": "1", "name": "other name", } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert len(captured_events) == 4 @@ -284,7 +286,7 @@ async def test_deconz_alarm_events( hass: HomeAssistant, device_registry: dr.DeviceRegistry, config_entry_setup: ConfigEntry, - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, ) -> None: """Test successful creation of deconz alarm events.""" assert len(hass.states.async_all()) == 4 @@ -309,7 +311,7 @@ async def test_deconz_alarm_events( "id": "1", "state": {"action": AncillaryControlAction.EMERGENCY}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() device = device_registry.async_get_device( @@ -333,7 +335,7 @@ async def test_deconz_alarm_events( "id": "1", "state": {"action": AncillaryControlAction.FIRE}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() device = device_registry.async_get_device( @@ -357,7 +359,7 @@ async def test_deconz_alarm_events( "id": "1", "state": {"action": AncillaryControlAction.INVALID_CODE}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() device = device_registry.async_get_device( @@ -381,7 +383,7 @@ async def test_deconz_alarm_events( "id": "1", "state": {"action": AncillaryControlAction.PANIC}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() device = device_registry.async_get_device( @@ -405,7 +407,7 @@ async def test_deconz_alarm_events( "id": "1", "state": {"action": AncillaryControlAction.ARMED_AWAY}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert len(captured_events) == 4 @@ -419,7 +421,7 @@ async def test_deconz_alarm_events( "id": "1", "state": {"panel": AncillaryControlPanel.ARMED_AWAY}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert len(captured_events) == 4 @@ -470,7 +472,7 @@ async def test_deconz_presence_events( hass: HomeAssistant, device_registry: dr.DeviceRegistry, config_entry_setup: ConfigEntry, - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, ) -> None: """Test successful creation of deconz presence events.""" assert len(hass.states.async_all()) == 5 @@ -506,7 +508,7 @@ async def test_deconz_presence_events( "id": "1", "state": {"presenceevent": presence_event}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert len(captured_events) == 1 @@ -527,7 +529,7 @@ async def test_deconz_presence_events( "id": "1", "state": {"presenceevent": PresenceStatePresenceEvent.NINE}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert len(captured_events) == 0 @@ -577,7 +579,7 @@ async def test_deconz_relative_rotary_events( hass: HomeAssistant, device_registry: dr.DeviceRegistry, config_entry_setup: ConfigEntry, - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, ) -> None: """Test successful creation of deconz relative rotary events.""" assert len(hass.states.async_all()) == 1 @@ -608,7 +610,7 @@ async def test_deconz_relative_rotary_events( "expectedrotation": rotation, }, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert len(captured_events) == 1 @@ -631,7 +633,7 @@ async def test_deconz_relative_rotary_events( "id": "1", "name": "123", } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert len(captured_events) == 0 diff --git a/tests/components/deconz/test_device_trigger.py b/tests/components/deconz/test_device_trigger.py index 9daf34f6665..7734fad7c7b 100644 --- a/tests/components/deconz/test_device_trigger.py +++ b/tests/components/deconz/test_device_trigger.py @@ -33,6 +33,8 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers.trigger import async_initialize_triggers from homeassistant.setup import async_setup_component +from .conftest import WebsocketDataType + from tests.common import async_get_device_automations @@ -318,7 +320,7 @@ async def test_functional_device_trigger( hass: HomeAssistant, device_registry: dr.DeviceRegistry, service_calls: list[ServiceCall], - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, ) -> None: """Test proper matching and attachment of device trigger automation.""" device = device_registry.async_get_device( @@ -356,7 +358,7 @@ async def test_functional_device_trigger( "id": "1", "state": {"buttonevent": 1002}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert len(service_calls) == 1 diff --git a/tests/components/deconz/test_diagnostics.py b/tests/components/deconz/test_diagnostics.py index 64e0f417387..615cce03ec2 100644 --- a/tests/components/deconz/test_diagnostics.py +++ b/tests/components/deconz/test_diagnostics.py @@ -6,6 +6,8 @@ from syrupy import SnapshotAssertion from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant +from .conftest import WebsocketStateType + from tests.components.diagnostics import get_diagnostics_for_config_entry from tests.typing import ClientSessionGenerator @@ -14,11 +16,11 @@ async def test_entry_diagnostics( hass: HomeAssistant, hass_client: ClientSessionGenerator, config_entry_setup: ConfigEntry, - mock_deconz_websocket, + mock_websocket_state: WebsocketStateType, snapshot: SnapshotAssertion, ) -> None: """Test config entry diagnostics.""" - await mock_deconz_websocket(state=State.RUNNING) + await mock_websocket_state(State.RUNNING) await hass.async_block_till_done() assert ( diff --git a/tests/components/deconz/test_fan.py b/tests/components/deconz/test_fan.py index 351ec798909..fa0191b7f65 100644 --- a/tests/components/deconz/test_fan.py +++ b/tests/components/deconz/test_fan.py @@ -15,6 +15,8 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, STATE_UNAVAILABLE from homeassistant.core import HomeAssistant +from .conftest import WebsocketDataType + from tests.test_util.aiohttp import AiohttpClientMocker @@ -47,7 +49,7 @@ async def test_fans( aioclient_mock: AiohttpClientMocker, config_entry_setup: ConfigEntry, mock_put_request: Callable[[str, str], AiohttpClientMocker], - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, ) -> None: """Test that all supported fan entities are created.""" assert len(hass.states.async_all()) == 2 # Light and fan @@ -63,7 +65,7 @@ async def test_fans( "id": "1", "state": {"speed": 1}, } - await mock_deconz_websocket(data=event_changed_light) + await mock_websocket_data(event_changed_light) await hass.async_block_till_done() assert hass.states.get("fan.ceiling_fan").state == STATE_ON @@ -76,7 +78,7 @@ async def test_fans( "id": "1", "state": {"speed": 2}, } - await mock_deconz_websocket(data=event_changed_light) + await mock_websocket_data(event_changed_light) await hass.async_block_till_done() assert hass.states.get("fan.ceiling_fan").state == STATE_ON @@ -89,7 +91,7 @@ async def test_fans( "id": "1", "state": {"speed": 3}, } - await mock_deconz_websocket(data=event_changed_light) + await mock_websocket_data(event_changed_light) await hass.async_block_till_done() assert hass.states.get("fan.ceiling_fan").state == STATE_ON @@ -102,7 +104,7 @@ async def test_fans( "id": "1", "state": {"speed": 4}, } - await mock_deconz_websocket(data=event_changed_light) + await mock_websocket_data(event_changed_light) await hass.async_block_till_done() assert hass.states.get("fan.ceiling_fan").state == STATE_ON @@ -115,7 +117,7 @@ async def test_fans( "id": "1", "state": {"speed": 0}, } - await mock_deconz_websocket(data=event_changed_light) + await mock_websocket_data(event_changed_light) await hass.async_block_till_done() assert hass.states.get("fan.ceiling_fan").state == STATE_OFF @@ -214,7 +216,7 @@ async def test_fans( "id": "1", "state": {"speed": 5}, } - await mock_deconz_websocket(data=event_changed_light) + await mock_websocket_data(event_changed_light) await hass.async_block_till_done() assert hass.states.get("fan.ceiling_fan").state == STATE_ON diff --git a/tests/components/deconz/test_gateway.py b/tests/components/deconz/test_gateway.py index 1e3f09ecec5..cafead19d69 100644 --- a/tests/components/deconz/test_gateway.py +++ b/tests/components/deconz/test_gateway.py @@ -133,17 +133,17 @@ async def test_gateway_device_configuration_url_when_addon( ) @pytest.mark.usefixtures("config_entry_setup") async def test_connection_status_signalling( - hass: HomeAssistant, mock_deconz_websocket + hass: HomeAssistant, mock_websocket_state ) -> None: """Make sure that connection status triggers a dispatcher send.""" assert hass.states.get("binary_sensor.presence").state == STATE_OFF - await mock_deconz_websocket(state=State.RETRYING) + await mock_websocket_state(State.RETRYING) await hass.async_block_till_done() assert hass.states.get("binary_sensor.presence").state == STATE_UNAVAILABLE - await mock_deconz_websocket(state=State.RUNNING) + await mock_websocket_state(State.RUNNING) await hass.async_block_till_done() assert hass.states.get("binary_sensor.presence").state == STATE_OFF diff --git a/tests/components/deconz/test_light.py b/tests/components/deconz/test_light.py index b97237eda41..dc3ffb6035e 100644 --- a/tests/components/deconz/test_light.py +++ b/tests/components/deconz/test_light.py @@ -40,7 +40,7 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant -from .conftest import ConfigEntryFactoryType +from .conftest import ConfigEntryFactoryType, WebsocketDataType from tests.test_util.aiohttp import AiohttpClientMocker @@ -471,7 +471,10 @@ async def test_lights( ], ) @pytest.mark.usefixtures("config_entry_setup") -async def test_light_state_change(hass: HomeAssistant, mock_deconz_websocket) -> None: +async def test_light_state_change( + hass: HomeAssistant, + mock_websocket_data: WebsocketDataType, +) -> None: """Verify light can change state on websocket event.""" assert hass.states.get("light.hue_go").state == STATE_ON @@ -482,7 +485,7 @@ async def test_light_state_change(hass: HomeAssistant, mock_deconz_websocket) -> "id": "0", "state": {"on": False}, } - await mock_deconz_websocket(data=event_changed_light) + await mock_websocket_data(event_changed_light) await hass.async_block_till_done() assert hass.states.get("light.hue_go").state == STATE_OFF @@ -1295,7 +1298,8 @@ async def test_disable_light_groups( ) @pytest.mark.usefixtures("config_entry_setup") async def test_non_color_light_reports_color( - hass: HomeAssistant, mock_deconz_websocket + hass: HomeAssistant, + mock_websocket_data: WebsocketDataType, ) -> None: """Verify hs_color does not crash when a group gets updated with a bad color value. @@ -1331,7 +1335,7 @@ async def test_non_color_light_reports_color( "t": "event", "uniqueid": "ec:1b:bd:ff:fe:ee:ed:dd-01", } - await mock_deconz_websocket(data=event_changed_light) + await mock_websocket_data(event_changed_light) await hass.async_block_till_done() group = hass.states.get("light.group") @@ -1510,15 +1514,16 @@ async def test_verify_group_supported_features(hass: HomeAssistant) -> None: ) @pytest.mark.usefixtures("config_entry_setup") async def test_verify_group_color_mode_fallback( - hass: HomeAssistant, mock_deconz_websocket + hass: HomeAssistant, + mock_websocket_data: WebsocketDataType, ) -> None: """Test that group supported features reflect what included lights support.""" group_state = hass.states.get("light.opbergruimte") assert group_state.state == STATE_OFF assert group_state.attributes[ATTR_COLOR_MODE] is None - await mock_deconz_websocket( - data={ + await mock_websocket_data( + { "e": "changed", "id": "13", "r": "lights", @@ -1533,8 +1538,8 @@ async def test_verify_group_color_mode_fallback( "uniqueid": "00:17:88:01:08:11:22:33-01", } ) - await mock_deconz_websocket( - data={ + await mock_websocket_data( + { "e": "changed", "id": "43", "r": "groups", diff --git a/tests/components/deconz/test_lock.py b/tests/components/deconz/test_lock.py index cbb3ad92cb2..7601e4ff725 100644 --- a/tests/components/deconz/test_lock.py +++ b/tests/components/deconz/test_lock.py @@ -18,6 +18,8 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant +from .conftest import WebsocketDataType + from tests.test_util.aiohttp import AiohttpClientMocker @@ -45,7 +47,7 @@ async def test_lock_from_light( hass: HomeAssistant, config_entry_setup: ConfigEntry, mock_put_request: Callable[[str, str], AiohttpClientMocker], - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, ) -> None: """Test that all supported lock entities based on lights are created.""" assert len(hass.states.async_all()) == 1 @@ -58,7 +60,7 @@ async def test_lock_from_light( "id": "1", "state": {"on": True}, } - await mock_deconz_websocket(data=event_changed_light) + await mock_websocket_data(event_changed_light) await hass.async_block_till_done() assert hass.states.get("lock.door_lock").state == STATE_LOCKED @@ -131,7 +133,7 @@ async def test_lock_from_sensor( hass: HomeAssistant, config_entry_setup: ConfigEntry, mock_put_request: Callable[[str, str], AiohttpClientMocker], - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, ) -> None: """Test that all supported lock entities based on sensors are created.""" assert len(hass.states.async_all()) == 2 @@ -144,7 +146,7 @@ async def test_lock_from_sensor( "id": "1", "state": {"lockstate": "locked"}, } - await mock_deconz_websocket(data=event_changed_light) + await mock_websocket_data(event_changed_light) await hass.async_block_till_done() assert hass.states.get("lock.door_lock").state == STATE_LOCKED diff --git a/tests/components/deconz/test_number.py b/tests/components/deconz/test_number.py index c2ec7203ac2..6d081cc5b1a 100644 --- a/tests/components/deconz/test_number.py +++ b/tests/components/deconz/test_number.py @@ -16,6 +16,8 @@ from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceValidationError from homeassistant.helpers import device_registry as dr, entity_registry as er +from .conftest import WebsocketDataType + from tests.test_util.aiohttp import AiohttpClientMocker TEST_DATA = [ @@ -104,7 +106,7 @@ async def test_number_entities( device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, config_entry_setup: ConfigEntry, - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, mock_put_request: Callable[[str, str], AiohttpClientMocker], expected: dict[str, Any], ) -> None: @@ -142,7 +144,7 @@ async def test_number_entities( "r": "sensors", "id": "0", } | expected["websocket_event"] - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert hass.states.get(expected["entity_id"]).state == expected["next_state"] diff --git a/tests/components/deconz/test_scene.py b/tests/components/deconz/test_scene.py index c168bbcdd3a..8d5531d5e9c 100644 --- a/tests/components/deconz/test_scene.py +++ b/tests/components/deconz/test_scene.py @@ -11,6 +11,8 @@ from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er +from .conftest import WebsocketDataType + from tests.test_util.aiohttp import AiohttpClientMocker TEST_DATA = [ @@ -117,7 +119,8 @@ async def test_scenes( ) @pytest.mark.usefixtures("config_entry_setup") async def test_only_new_scenes_are_created( - hass: HomeAssistant, mock_deconz_websocket + hass: HomeAssistant, + mock_websocket_data: WebsocketDataType, ) -> None: """Test that scenes works.""" assert len(hass.states.async_all()) == 2 @@ -129,7 +132,7 @@ async def test_only_new_scenes_are_created( "id": "1", "scenes": [{"id": "1", "name": "Scene"}], } - await mock_deconz_websocket(data=event_changed_group) + await mock_websocket_data(event_changed_group) await hass.async_block_till_done() assert len(hass.states.async_all()) == 2 diff --git a/tests/components/deconz/test_sensor.py b/tests/components/deconz/test_sensor.py index e22a26983db..6ebd7692e00 100644 --- a/tests/components/deconz/test_sensor.py +++ b/tests/components/deconz/test_sensor.py @@ -24,7 +24,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.util import dt as dt_util -from .conftest import ConfigEntryFactoryType +from .conftest import ConfigEntryFactoryType, WebsocketDataType from tests.common import async_fire_time_changed @@ -906,7 +906,7 @@ async def test_sensors( device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, config_entry_setup: ConfigEntry, - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, expected: dict[str, Any], ) -> None: """Test successful creation of sensor entities.""" @@ -954,7 +954,7 @@ async def test_sensors( event_changed_sensor = {"t": "event", "e": "changed", "r": "sensors", "id": "1"} event_changed_sensor |= expected["websocket_event"] - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert hass.states.get(expected["entity_id"]).state == expected["next_state"] @@ -1057,7 +1057,10 @@ async def test_allow_clip_sensors( @pytest.mark.usefixtures("config_entry_setup") -async def test_add_new_sensor(hass: HomeAssistant, mock_deconz_websocket) -> None: +async def test_add_new_sensor( + hass: HomeAssistant, + mock_websocket_data: WebsocketDataType, +) -> None: """Test that adding a new sensor works.""" event_added_sensor = { "t": "event", @@ -1076,7 +1079,7 @@ async def test_add_new_sensor(hass: HomeAssistant, mock_deconz_websocket) -> Non assert len(hass.states.async_all()) == 0 - await mock_deconz_websocket(data=event_added_sensor) + await mock_websocket_data(event_added_sensor) await hass.async_block_till_done() assert len(hass.states.async_all()) == 2 @@ -1168,7 +1171,10 @@ async def test_air_quality_sensor_without_ppb(hass: HomeAssistant) -> None: ], ) @pytest.mark.usefixtures("config_entry_setup") -async def test_add_battery_later(hass: HomeAssistant, mock_deconz_websocket) -> None: +async def test_add_battery_later( + hass: HomeAssistant, + mock_websocket_data: WebsocketDataType, +) -> None: """Test that a battery sensor can be created later on. Without an initial battery state a battery sensor @@ -1183,7 +1189,7 @@ async def test_add_battery_later(hass: HomeAssistant, mock_deconz_websocket) -> "id": "2", "config": {"battery": 50}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert len(hass.states.async_all()) == 0 @@ -1195,7 +1201,7 @@ async def test_add_battery_later(hass: HomeAssistant, mock_deconz_websocket) -> "id": "1", "config": {"battery": 50}, } - await mock_deconz_websocket(data=event_changed_sensor) + await mock_websocket_data(event_changed_sensor) await hass.async_block_till_done() assert len(hass.states.async_all()) == 1 diff --git a/tests/components/deconz/test_siren.py b/tests/components/deconz/test_siren.py index 3db345a6ad2..d345b6e315c 100644 --- a/tests/components/deconz/test_siren.py +++ b/tests/components/deconz/test_siren.py @@ -16,6 +16,8 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant +from .conftest import WebsocketDataType + from tests.test_util.aiohttp import AiohttpClientMocker @@ -41,7 +43,7 @@ from tests.test_util.aiohttp import AiohttpClientMocker async def test_sirens( hass: HomeAssistant, config_entry_setup: ConfigEntry, - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, mock_put_request: Callable[[str, str], AiohttpClientMocker], ) -> None: """Test that siren entities are created.""" @@ -56,7 +58,7 @@ async def test_sirens( "id": "1", "state": {"alert": None}, } - await mock_deconz_websocket(data=event_changed_light) + await mock_websocket_data(event_changed_light) await hass.async_block_till_done() assert hass.states.get("siren.warning_device").state == STATE_OFF diff --git a/tests/components/deconz/test_switch.py b/tests/components/deconz/test_switch.py index 7b01751c748..578370948b3 100644 --- a/tests/components/deconz/test_switch.py +++ b/tests/components/deconz/test_switch.py @@ -16,7 +16,7 @@ from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, STATE_UNAVA from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er -from .conftest import ConfigEntryFactoryType +from .conftest import ConfigEntryFactoryType, WebsocketDataType from tests.test_util.aiohttp import AiohttpClientMocker @@ -56,7 +56,7 @@ async def test_power_plugs( hass: HomeAssistant, config_entry_setup: ConfigEntry, mock_put_request: Callable[[str, str], AiohttpClientMocker], - mock_deconz_websocket, + mock_websocket_data: WebsocketDataType, ) -> None: """Test that all supported switch entities are created.""" assert len(hass.states.async_all()) == 4 @@ -72,7 +72,7 @@ async def test_power_plugs( "id": "1", "state": {"on": False}, } - await mock_deconz_websocket(data=event_changed_light) + await mock_websocket_data(event_changed_light) await hass.async_block_till_done() assert hass.states.get("switch.on_off_switch").state == STATE_OFF