diff --git a/homeassistant/components/enocean/__init__.py b/homeassistant/components/enocean/__init__.py index 646ff303c002..11170fbac979 100644 --- a/homeassistant/components/enocean/__init__.py +++ b/homeassistant/components/enocean/__init__.py @@ -57,7 +57,7 @@ async def async_setup_entry( try: await gateway.start() except ConnectionError as err: - gateway.stop() + await gateway.stop() raise ConfigEntryNotReady(f"Failed to start EnOcean gateway: {err}") from err config_entry.runtime_data = gateway @@ -72,4 +72,4 @@ async def async_unload_entry( ) -> None: """Unload EnOcean config entry: stop the gateway.""" - config_entry.runtime_data.stop() + await config_entry.runtime_data.stop() diff --git a/homeassistant/components/enocean/binary_sensor.py b/homeassistant/components/enocean/binary_sensor.py index 66e7cf732e0b..22d4cf40530a 100644 --- a/homeassistant/components/enocean/binary_sensor.py +++ b/homeassistant/components/enocean/binary_sensor.py @@ -2,7 +2,7 @@ from typing import override -from enocean_async import ERP1Telegram +from enocean_async.protocol.erp1.telegram import ERP1Telegram import probatio from homeassistant.components.binary_sensor import ( @@ -111,7 +111,7 @@ class EnOceanBinarySensor(EnOceanEntity, BinarySensorEntity): self.hass.bus.fire( EVENT_BUTTON_PRESSED, { - "id": self.address.to_bytelist(), + "id": self.address.bytelist, "pushed": pushed, "which": self.which, "onoff": self.onoff, diff --git a/homeassistant/components/enocean/config_flow.py b/homeassistant/components/enocean/config_flow.py index 8aa4e7ca9a0f..9bf9ff55d83b 100644 --- a/homeassistant/components/enocean/config_flow.py +++ b/homeassistant/components/enocean/config_flow.py @@ -174,7 +174,7 @@ class EnOceanFlowHandler(ConfigFlow, domain=DOMAIN): LOGGER.warning("Dongle path %s is invalid: %s", dongle_path, str(exception)) return False finally: - gateway.stop() + await gateway.stop() return True diff --git a/homeassistant/components/enocean/entity.py b/homeassistant/components/enocean/entity.py index 978367f8a987..669b0a9e6015 100644 --- a/homeassistant/components/enocean/entity.py +++ b/homeassistant/components/enocean/entity.py @@ -2,8 +2,10 @@ from typing import override -from enocean_async import EURID, Address, BaseAddress, ERP1Telegram, SenderAddress -from enocean_async.esp3.packet import ESP3Packet, ESP3PacketType +from enocean_async import EURID, BaseAddress, SenderAddress +from enocean_async.address import Address +from enocean_async.protocol.erp1.telegram import ERP1Telegram +from enocean_async.protocol.esp3.packet import ESP3Packet, ESP3PacketType from homeassistant.helpers.dispatcher import async_dispatcher_connect, dispatcher_send from homeassistant.helpers.entity import Entity @@ -32,11 +34,11 @@ class EnOceanEntity(Entity): self.address: SenderAddress | None = None try: - address = Address.from_bytelist(dev_id) - if address.is_eurid(): - self.address = EURID.from_number(address.to_number()) - elif address.is_base_address(): - self.address = BaseAddress.from_number(address.to_number()) + address = Address(dev_id) + if address.is_eurid: + self.address = EURID(int(address)) + elif address.is_base_address: + self.address = BaseAddress(int(address)) except ValueError: self.address = None diff --git a/homeassistant/components/enocean/light.py b/homeassistant/components/enocean/light.py index f547c9e37a86..29f9c81bce19 100644 --- a/homeassistant/components/enocean/light.py +++ b/homeassistant/components/enocean/light.py @@ -3,8 +3,8 @@ import math from typing import Any, override -from enocean_async import ERP1Telegram -from enocean_async.esp3.packet import ESP3PacketType +from enocean_async.protocol.erp1.telegram import ERP1Telegram +from enocean_async.protocol.esp3.packet import ESP3PacketType import probatio from homeassistant.components.light import ( diff --git a/homeassistant/components/enocean/manifest.json b/homeassistant/components/enocean/manifest.json index a4a842fb5eb7..effe33e31f85 100644 --- a/homeassistant/components/enocean/manifest.json +++ b/homeassistant/components/enocean/manifest.json @@ -10,7 +10,7 @@ "integration_type": "hub", "iot_class": "local_push", "loggers": ["enocean_async"], - "requirements": ["enocean-async==0.4.2"], + "requirements": ["enocean-async==0.16.0"], "single_config_entry": true, "usb": [ { diff --git a/homeassistant/components/enocean/sensor.py b/homeassistant/components/enocean/sensor.py index aa8c76df09b9..81f80ac91487 100644 --- a/homeassistant/components/enocean/sensor.py +++ b/homeassistant/components/enocean/sensor.py @@ -4,7 +4,10 @@ from collections.abc import Callable from dataclasses import dataclass from typing import override -from enocean_async import EEP, EEP_SPECIFICATIONS, EEPHandler, EEPMessage, ERP1Telegram +from enocean_async import EEP, EEP_SPECIFICATIONS +from enocean_async.eep.handler import EEPHandler +from enocean_async.eep.message import EEPMessage +from enocean_async.protocol.erp1.telegram import ERP1Telegram import probatio from homeassistant.components.sensor import ( @@ -186,14 +189,14 @@ class EnOceanPowerSensor(EnOceanSensor): if telegram.rorg != 0xA5: return - if (eep := EEP_SPECIFICATIONS.get(EEP(0xA5, 0x12, 0x01))) is None: + if (eep := EEP_SPECIFICATIONS.get(EEP("A5-12-01"))) is None: return msg: EEPMessage = EEPHandler(eep).decode(telegram) - if "DT" in msg.values and msg.values["DT"].raw == 1: + if "DT" in msg.raw and msg.raw["DT"] == 1: # this packet reports the current value - raw_val = msg.values["MR"].raw - divisor = msg.values["DIV"].raw + raw_val = msg.raw["MR"] + divisor = msg.raw["DIV"] self._attr_native_value = raw_val / (10**divisor) self.schedule_update_ha_state() diff --git a/homeassistant/components/enocean/switch.py b/homeassistant/components/enocean/switch.py index c355f520f6d5..4d0835cf8166 100644 --- a/homeassistant/components/enocean/switch.py +++ b/homeassistant/components/enocean/switch.py @@ -2,8 +2,11 @@ from typing import Any, override -from enocean_async import EEP, EEP_SPECIFICATIONS, EEPHandler, EEPMessage, ERP1Telegram -from enocean_async.esp3.packet import ESP3PacketType +from enocean_async import EEP, EEP_SPECIFICATIONS +from enocean_async.eep.handler import EEPHandler +from enocean_async.eep.message import EEPMessage +from enocean_async.protocol.erp1.telegram import ERP1Telegram +from enocean_async.protocol.esp3.packet import ESP3PacketType import probatio from homeassistant.components.switch import ( @@ -98,7 +101,7 @@ class EnOceanSwitch(EnOceanEntity, SwitchEntity): return optional = [0x03] - optional.extend(self.address.to_bytelist()) + optional.extend(self.address.bytelist) optional.extend([0xFF, 0x00]) self.send_command( data=[0xD2, 0x01, self.channel & 0xFF, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00], @@ -113,7 +116,7 @@ class EnOceanSwitch(EnOceanEntity, SwitchEntity): if not self.address: return optional = [0x03] - optional.extend(self.address.to_bytelist()) + optional.extend(self.address.bytelist) optional.extend([0xFF, 0x00]) self.send_command( data=[0xD2, 0x01, self.channel & 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], @@ -127,16 +130,16 @@ class EnOceanSwitch(EnOceanEntity, SwitchEntity): """Update the internal state of the switch.""" if telegram.rorg == 0xA5: # power meter telegram, turn on if > 1 watts - if (eep := EEP_SPECIFICATIONS.get(EEP(0xA5, 0x12, 0x01))) is None: + if (eep := EEP_SPECIFICATIONS.get(EEP("A5-12-01"))) is None: LOGGER.warning("EEP A5-12-01 cannot be decoded") return msg: EEPMessage = EEPHandler(eep).decode(telegram) - if "DT" in msg.values and msg.values["DT"].raw == 1: + if "DT" in msg.raw and msg.raw["DT"] == 1: # this packet reports the current value - raw_val = msg.values["MR"].raw - divisor = msg.values["DIV"].raw + raw_val = msg.raw["MR"] + divisor = msg.raw["DIV"] watts = raw_val / (10**divisor) if watts > 1: self._attr_is_on = True @@ -144,14 +147,14 @@ class EnOceanSwitch(EnOceanEntity, SwitchEntity): elif telegram.rorg == 0xD2: # actuator status telegram - if (eep := EEP_SPECIFICATIONS.get(EEP(0xD2, 0x01, 0x01))) is None: + if (eep := EEP_SPECIFICATIONS.get(EEP("D2-01-01"))) is None: LOGGER.warning("EEP D2-01-01 cannot be decoded") return msg = EEPHandler(eep).decode(telegram) - if msg.values["CMD"].raw == 4: - channel = msg.values["I/O"].raw - output = msg.values["OV"].raw + if msg.raw["CMD"] == 4: + channel = msg.raw["I/O"] + output = msg.raw["OV"] if channel == self.channel: self._attr_is_on = output > 0 self.schedule_update_ha_state() diff --git a/requirements_all.txt b/requirements_all.txt index 261507ec5c0b..03b34e334151 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -990,7 +990,7 @@ energyid-webhooks==0.0.17 energyzero==5.1.1 # homeassistant.components.enocean -enocean-async==0.4.2 +enocean-async==0.16.0 # homeassistant.components.entur_public_transport enturclient==0.2.4 diff --git a/tests/components/enocean/__init__.py b/tests/components/enocean/__init__.py index 81771694abf8..a6dd12c59a39 100644 --- a/tests/components/enocean/__init__.py +++ b/tests/components/enocean/__init__.py @@ -1 +1,7 @@ """Tests of the EnOcean integration.""" + +from collections.abc import Awaitable, Callable + +from enocean_async.protocol.erp1.telegram import ERP1Telegram + +type TelegramReceiver = Callable[[ERP1Telegram], Awaitable[None]] diff --git a/tests/components/enocean/conftest.py b/tests/components/enocean/conftest.py index e4f914df4667..9b84170d32da 100644 --- a/tests/components/enocean/conftest.py +++ b/tests/components/enocean/conftest.py @@ -1,12 +1,18 @@ """Fixtures for EnOcean integration tests.""" +from collections.abc import Generator from typing import Final +from unittest.mock import Mock, patch +from enocean_async.protocol.erp1.telegram import ERP1Telegram import pytest from pytest_homeassistant.common import MockConfigEntry from homeassistant.components.enocean.const import DOMAIN from homeassistant.const import CONF_DEVICE +from homeassistant.core import HomeAssistant + +from . import TelegramReceiver ENTRY_CONFIG: Final[dict[str, str]] = { CONF_DEVICE: "/dev/ttyUSB0", @@ -21,3 +27,28 @@ def mock_config_entry() -> MockConfigEntry: unique_id="device_chip_id", data=ENTRY_CONFIG, ) + + +@pytest.fixture +def mock_gateway() -> Generator[Mock]: + """Return a mocked EnOcean gateway.""" + with patch( + "homeassistant.components.enocean.Gateway", autospec=True + ) as gateway_class: + yield gateway_class.return_value + + +@pytest.fixture +async def receive_telegram( + hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_gateway: Mock +) -> TelegramReceiver: + """Set up the integration and return a function simulating a received telegram.""" + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + callback = mock_gateway.add_erp1_received_callback.call_args.args[0] + + async def _receive(telegram: ERP1Telegram) -> None: + callback(telegram) + await hass.async_block_till_done() + + return _receive diff --git a/tests/components/enocean/test_binary_sensor.py b/tests/components/enocean/test_binary_sensor.py new file mode 100644 index 000000000000..17dc6b65d9f4 --- /dev/null +++ b/tests/components/enocean/test_binary_sensor.py @@ -0,0 +1,58 @@ +"""Tests for the EnOcean binary sensor platform.""" + +from enocean_async import EURID +from enocean_async.protocol.erp1.rorg import RORG +from enocean_async.protocol.erp1.telegram import ERP1Telegram +import pytest +from pytest_homeassistant.common import async_capture_events + +from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN +from homeassistant.components.enocean import DOMAIN +from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component + +from . import TelegramReceiver + +DEVICE_ID = [0xDE, 0xAD, 0xBE, 0xEF] + + +@pytest.mark.parametrize( + ("data", "status", "expected"), + [ + pytest.param( + b"\x70", 0x30, {"pushed": 1, "which": 0, "onoff": 0}, id="pressed" + ), + pytest.param( + b"\x10", 0x30, {"pushed": 1, "which": 1, "onoff": 1}, id="other_button" + ), + pytest.param( + b"\x00", 0x20, {"pushed": 0, "which": -1, "onoff": -1}, id="released" + ), + ], +) +async def test_button_event( + hass: HomeAssistant, + receive_telegram: TelegramReceiver, + data: bytes, + status: int, + expected: dict[str, int], +) -> None: + """Test that a rocker switch telegram fires a button event.""" + assert await async_setup_component( + hass, + BINARY_SENSOR_DOMAIN, + { + BINARY_SENSOR_DOMAIN: [ + {"platform": DOMAIN, "id": DEVICE_ID, "name": "button"} + ] + }, + ) + await hass.async_block_till_done() + events = async_capture_events(hass, "button_pressed") + + await receive_telegram( + ERP1Telegram(RORG.RORG_RPS, data, EURID("DE:AD:BE:EF"), status) + ) + + assert len(events) == 1 + assert events[0].data == {"id": DEVICE_ID, **expected} diff --git a/tests/components/enocean/test_config_flow.py b/tests/components/enocean/test_config_flow.py index 146fe1fa8e50..250969911484 100644 --- a/tests/components/enocean/test_config_flow.py +++ b/tests/components/enocean/test_config_flow.py @@ -70,7 +70,7 @@ async def test_detection_flow_with_valid_path(hass: HomeAssistant) -> None: with patch( GATEWAY_CLASS, - return_value=Mock(start=AsyncMock(), stop=Mock()), + return_value=Mock(start=AsyncMock(), stop=AsyncMock()), ): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": "detect"}, data={CONF_DEVICE: USER_PROVIDED_PATH} @@ -102,7 +102,8 @@ async def test_detection_flow_with_invalid_path(hass: HomeAssistant) -> None: with patch( GATEWAY_CLASS, return_value=Mock( - start=AsyncMock(side_effect=ConnectionError("invalid path")), stop=Mock() + start=AsyncMock(side_effect=ConnectionError("invalid path")), + stop=AsyncMock(), ), ): result = await hass.config_entries.flow.async_init( @@ -116,7 +117,7 @@ async def test_detection_flow_with_invalid_path(hass: HomeAssistant) -> None: assert CONF_DEVICE in result["errors"] with ( - patch(GATEWAY_CLASS, return_value=Mock(start=AsyncMock(), stop=Mock())), + patch(GATEWAY_CLASS, return_value=Mock(start=AsyncMock(), stop=AsyncMock())), patch(SETUP_ENTRY_METHOD, AsyncMock(return_value=None)), ): result = await hass.config_entries.flow.async_configure( @@ -132,7 +133,7 @@ async def test_manual_flow_with_valid_path(hass: HomeAssistant) -> None: with patch( GATEWAY_CLASS, - return_value=Mock(start=AsyncMock(), stop=Mock()), + return_value=Mock(start=AsyncMock(), stop=AsyncMock()), ): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": "manual"}, data={CONF_DEVICE: USER_PROVIDED_PATH} @@ -150,7 +151,8 @@ async def test_manual_flow_with_invalid_path(hass: HomeAssistant) -> None: with patch( GATEWAY_CLASS, return_value=Mock( - start=AsyncMock(side_effect=ConnectionError("invalid path")), stop=Mock() + start=AsyncMock(side_effect=ConnectionError("invalid path")), + stop=AsyncMock(), ), ): result = await hass.config_entries.flow.async_init( @@ -162,7 +164,7 @@ async def test_manual_flow_with_invalid_path(hass: HomeAssistant) -> None: assert CONF_DEVICE in result["errors"] with ( - patch(GATEWAY_CLASS, return_value=Mock(start=AsyncMock(), stop=Mock())), + patch(GATEWAY_CLASS, return_value=Mock(start=AsyncMock(), stop=AsyncMock())), patch(SETUP_ENTRY_METHOD, AsyncMock(return_value=None)), ): result = await hass.config_entries.flow.async_configure( @@ -178,7 +180,7 @@ async def test_import_flow_with_valid_path(hass: HomeAssistant) -> None: with patch( GATEWAY_CLASS, - return_value=Mock(start=AsyncMock(), stop=Mock()), + return_value=Mock(start=AsyncMock(), stop=AsyncMock()), ): result = await hass.config_entries.flow.async_init( DOMAIN, @@ -198,7 +200,8 @@ async def test_import_flow_with_invalid_path(hass: HomeAssistant) -> None: with patch( GATEWAY_CLASS, return_value=Mock( - start=AsyncMock(side_effect=ConnectionError("invalid path")), stop=Mock() + start=AsyncMock(side_effect=ConnectionError("invalid path")), + stop=AsyncMock(), ), ): result = await hass.config_entries.flow.async_init( @@ -239,7 +242,7 @@ async def test_usb_discovery( with ( patch( GATEWAY_CLASS, - return_value=Mock(start=AsyncMock(), stop=Mock()), + return_value=Mock(start=AsyncMock(), stop=AsyncMock()), ), patch(SETUP_ENTRY_METHOD, AsyncMock(return_value=None)), patch( diff --git a/tests/components/enocean/test_init.py b/tests/components/enocean/test_init.py index 3aac1b18fe51..3fcc8cc21ce1 100644 --- a/tests/components/enocean/test_init.py +++ b/tests/components/enocean/test_init.py @@ -1,6 +1,6 @@ """Test the EnOcean integration.""" -from unittest.mock import patch +from unittest.mock import Mock, patch from pytest_homeassistant.common import MockConfigEntry @@ -22,3 +22,17 @@ async def test_device_not_connected( await hass.async_block_till_done() assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY + + +async def test_unload_stops_gateway( + hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_gateway: Mock +) -> None: + """Test that unloading the config entry stops the gateway.""" + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + assert mock_config_entry.state is ConfigEntryState.LOADED + + await hass.config_entries.async_unload(mock_config_entry.entry_id) + + assert mock_config_entry.state is ConfigEntryState.NOT_LOADED + mock_gateway.stop.assert_awaited_once() diff --git a/tests/components/enocean/test_light.py b/tests/components/enocean/test_light.py new file mode 100644 index 000000000000..8599c597e12f --- /dev/null +++ b/tests/components/enocean/test_light.py @@ -0,0 +1,44 @@ +"""Tests for the EnOcean light platform.""" + +from enocean_async import BaseAddress +from enocean_async.protocol.erp1.rorg import RORG +from enocean_async.protocol.erp1.telegram import ERP1Telegram + +from homeassistant.components.enocean import DOMAIN +from homeassistant.components.light import ATTR_BRIGHTNESS, DOMAIN as LIGHT_DOMAIN +from homeassistant.const import STATE_ON +from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component + +from . import TelegramReceiver + + +async def test_dimmer_status( + hass: HomeAssistant, receive_telegram: TelegramReceiver +) -> None: + """Test that a dimming telegram from a base address updates the light.""" + assert await async_setup_component( + hass, + LIGHT_DOMAIN, + { + LIGHT_DOMAIN: [ + { + "platform": DOMAIN, + "id": [0xFF, 0x80, 0x00, 0x02], + "sender_id": [0xFF, 0x80, 0x00, 0x01], + "name": "dimmer", + } + ] + }, + ) + await hass.async_block_till_done() + + await receive_telegram( + ERP1Telegram( + RORG.RORG_4BS, bytes.fromhex("02320009"), BaseAddress("FF:80:00:02") + ) + ) + + state = hass.states.get("light.dimmer") + assert state.state == STATE_ON + assert state.attributes[ATTR_BRIGHTNESS] == 128 diff --git a/tests/components/enocean/test_sensor.py b/tests/components/enocean/test_sensor.py new file mode 100644 index 000000000000..5f77fd80cccc --- /dev/null +++ b/tests/components/enocean/test_sensor.py @@ -0,0 +1,51 @@ +"""Tests for the EnOcean sensor platform.""" + +from enocean_async import EURID +from enocean_async.protocol.erp1.rorg import RORG +from enocean_async.protocol.erp1.telegram import ERP1Telegram +import pytest + +from homeassistant.components.enocean import DOMAIN +from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN +from homeassistant.const import STATE_UNKNOWN +from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component + +from . import TelegramReceiver + + +@pytest.mark.parametrize( + ("data", "expected_state"), + [ + pytest.param("0000640c", "100.0", id="current_value"), + pytest.param("00006408", STATE_UNKNOWN, id="cumulative_value_ignored"), + ], +) +async def test_power_sensor( + hass: HomeAssistant, + receive_telegram: TelegramReceiver, + data: str, + expected_state: str, +) -> None: + """Test that a meter reading telegram updates the power sensor.""" + assert await async_setup_component( + hass, + SENSOR_DOMAIN, + { + SENSOR_DOMAIN: [ + { + "platform": DOMAIN, + "id": [0xDE, 0xAD, 0xBE, 0xEF], + "name": "meter", + "device_class": "powersensor", + } + ] + }, + ) + await hass.async_block_till_done() + + await receive_telegram( + ERP1Telegram(RORG.RORG_4BS, bytes.fromhex(data), EURID("DE:AD:BE:EF")) + ) + + assert hass.states.get("sensor.power_meter").state == expected_state diff --git a/tests/components/enocean/test_switch.py b/tests/components/enocean/test_switch.py index 9c75ca0f6d8e..ba5739d9e9c0 100644 --- a/tests/components/enocean/test_switch.py +++ b/tests/components/enocean/test_switch.py @@ -1,14 +1,29 @@ """Tests for the EnOcean switch platform.""" +from unittest.mock import Mock + +from enocean_async import EURID +from enocean_async.protocol.erp1.rorg import RORG +from enocean_async.protocol.erp1.telegram import ERP1Telegram +import pytest from pytest_homeassistant.common import MockConfigEntry, assert_setup_component from homeassistant.components.enocean import DOMAIN from homeassistant.components.enocean.entity import combine_hex from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN +from homeassistant.const import ( + ATTR_ENTITY_ID, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, + STATE_OFF, + STATE_ON, +) from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er from homeassistant.setup import async_setup_component +from . import TelegramReceiver + SWITCH_CONFIG = { "switch": [ { @@ -71,3 +86,55 @@ async def test_unique_id_migration( entity_registry.async_get_entity_id(SWITCH_DOMAIN, DOMAIN, old_unique_id) is None ) + + +@pytest.mark.parametrize( + ("rorg", "data", "expected_state"), + [ + pytest.param(RORG.RORG_VLD, "046164", STATE_ON, id="actuator_status_on"), + pytest.param(RORG.RORG_VLD, "046100", STATE_OFF, id="actuator_status_off"), + pytest.param(RORG.RORG_VLD, "046264", STATE_OFF, id="other_channel"), + pytest.param(RORG.RORG_4BS, "0000640c", STATE_ON, id="power_reading"), + ], +) +async def test_status_telegram( + hass: HomeAssistant, + receive_telegram: TelegramReceiver, + rorg: RORG, + data: str, + expected_state: str, +) -> None: + """Test that telegrams of the actuator update the switch state.""" + assert await async_setup_component(hass, SWITCH_DOMAIN, SWITCH_CONFIG) + await hass.async_block_till_done() + + await receive_telegram( + ERP1Telegram(rorg, bytes.fromhex(data), EURID("DE:AD:BE:EF")) + ) + + assert hass.states.get("switch.room0").state == expected_state + + +@pytest.mark.usefixtures("receive_telegram") +@pytest.mark.parametrize( + ("service", "expected_data"), + [ + pytest.param(SERVICE_TURN_ON, "d20101640000000000", id="turn_on"), + pytest.param(SERVICE_TURN_OFF, "d20101000000000000", id="turn_off"), + ], +) +async def test_switch_command( + hass: HomeAssistant, mock_gateway: Mock, service: str, expected_data: str +) -> None: + """Test that switching sends a command to the actuator.""" + assert await async_setup_component(hass, SWITCH_DOMAIN, SWITCH_CONFIG) + await hass.async_block_till_done() + + await hass.services.async_call( + SWITCH_DOMAIN, service, {ATTR_ENTITY_ID: "switch.room0"}, blocking=True + ) + await hass.async_block_till_done() + + packet = mock_gateway.send_esp3_packet.call_args.args[0] + assert packet.data == bytes.fromhex(expected_data) + assert packet.optional == bytes.fromhex("03deadbeefff00")