From 587ba3db9a37184528798f439373429b4bb78f23 Mon Sep 17 00:00:00 2001 From: AlexSperka Date: Sun, 29 Nov 2026 19:19:36 +0100 Subject: [PATCH] Discover the Eltako FAM-USB in EnOcean (#195024) --- .../components/enocean/config_flow.py | 14 +++- .../components/enocean/manifest.json | 6 ++ homeassistant/generated/usb.py | 7 ++ tests/components/enocean/test_config_flow.py | 80 +++++++++++++++++-- 4 files changed, 98 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/enocean/config_flow.py b/homeassistant/components/enocean/config_flow.py index 86aea6f543fd..c3f3db5aedbb 100644 --- a/homeassistant/components/enocean/config_flow.py +++ b/homeassistant/components/enocean/config_flow.py @@ -68,6 +68,16 @@ class EnOceanFlowHandler(ConfigFlow, domain=DOMAIN): @override async def async_step_usb(self, discovery_info: UsbServiceInfo) -> ConfigFlowResult: """Handle usb discovery.""" + discovery_info.device = await self.hass.async_add_executor_job( + usb.get_serial_by_id, discovery_info.device + ) + + # A dongle can show up as several serial ports with the same USB identity: a + # FAM-USB has two, of which one is connected to the radio module. Only the + # port on which a dongle answers may claim that identity. + if await detect_serial_protocol(discovery_info.device) is None: + return self.async_abort(reason="invalid_dongle_path") + unique_id = usb_unique_id_from_service_info(discovery_info) await self.async_set_unique_id(unique_id) @@ -75,10 +85,6 @@ class EnOceanFlowHandler(ConfigFlow, domain=DOMAIN): updates={CONF_DEVICE: discovery_info.device} ) - discovery_info.device = await self.hass.async_add_executor_job( - usb.get_serial_by_id, discovery_info.device - ) - self.data[CONF_DEVICE] = discovery_info.device self.context["title_placeholders"] = { "name": human_readable_device_name( diff --git a/homeassistant/components/enocean/manifest.json b/homeassistant/components/enocean/manifest.json index d8617398cfaf..0010d6734c43 100644 --- a/homeassistant/components/enocean/manifest.json +++ b/homeassistant/components/enocean/manifest.json @@ -18,6 +18,12 @@ "manufacturer": "*enocean*", "pid": "6001", "vid": "0403" + }, + { + "description": "*enocean programmer*", + "manufacturer": "*enocean*", + "pid": "6010", + "vid": "0403" } ] } diff --git a/homeassistant/generated/usb.py b/homeassistant/generated/usb.py index 3f3bf687bf97..46e6e29fd975 100644 --- a/homeassistant/generated/usb.py +++ b/homeassistant/generated/usb.py @@ -35,6 +35,13 @@ USB = [ "pid": "6001", "vid": "0403", }, + { + "description": "*enocean programmer*", + "domain": "enocean", + "manufacturer": "*enocean*", + "pid": "6010", + "vid": "0403", + }, { "description": "*zbt-2*", "domain": "homeassistant_connect_zbt2", diff --git a/tests/components/enocean/test_config_flow.py b/tests/components/enocean/test_config_flow.py index 617f3e66ac7c..1ef796525215 100644 --- a/tests/components/enocean/test_config_flow.py +++ b/tests/components/enocean/test_config_flow.py @@ -296,11 +296,12 @@ async def test_usb_discovery( ) device = "/dev/enocean0" # test discovery step - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_USB}, - data=usb_discovery_info, - ) + with patch(DETECT_METHOD, return_value=SerialProtocol.ESP3): + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USB}, + data=usb_discovery_info, + ) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "usb_confirm" @@ -357,3 +358,72 @@ async def test_usb_discovery_already_configured_updates_path( assert result["type"] is FlowResultType.ABORT assert result["reason"] == "single_instance_allowed" + + +FAM_USB_RADIO_PORT = "/dev/serial/by-id/usb-EnOcean_GmbH_EnOcean_Programmer-if01-port0" +FAM_USB_OTHER_PORT = "/dev/serial/by-id/usb-EnOcean_GmbH_EnOcean_Programmer-if00-port0" + + +def _fam_usb_discovery_info(device: str) -> UsbServiceInfo: + """Return what the USB discovery reports for a port of an Eltako FAM-USB.""" + return UsbServiceInfo( + device=device, + pid="6010", + vid="0403", + serial_number="1234", + description="EnOcean Programmer V3.2 - EnOcean Programmer V3.2", + manufacturer="EnOcean GmbH", + ) + + +async def _detect_fam_usb(path: str) -> SerialProtocol | None: + """Answer as a FAM-USB does: its dongle speaks ESP2 on one of its two ports.""" + return SerialProtocol.ESP2 if path == FAM_USB_RADIO_PORT else None + + +async def test_usb_discovery_of_a_dongle_with_two_ports(hass: HomeAssistant) -> None: + """Test that the port on which the dongle answers is the one that is set up.""" + with ( + patch(DETECT_METHOD, side_effect=_detect_fam_usb), + patch(SETUP_ENTRY_METHOD, AsyncMock(return_value=True)), + ): + other_port = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USB}, + data=_fam_usb_discovery_info(FAM_USB_OTHER_PORT), + ) + radio_port = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USB}, + data=_fam_usb_discovery_info(FAM_USB_RADIO_PORT), + ) + + assert other_port["type"] is FlowResultType.ABORT + assert other_port["reason"] == "invalid_dongle_path" + assert radio_port["type"] is FlowResultType.FORM + assert radio_port["step_id"] == "usb_confirm" + + result = await hass.config_entries.flow.async_configure( + radio_port["flow_id"], {} + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["data"] == {"device": FAM_USB_RADIO_PORT, "protocol": "esp2"} + assert ( + result["context"]["unique_id"] + == "0403:6010_1234_EnOcean GmbH_EnOcean Programmer V3.2 - EnOcean Programmer V3.2" + ) + + +async def test_usb_discovery_without_an_answering_dongle(hass: HomeAssistant) -> None: + """Test that a port on which no dongle answers is not offered.""" + with patch(DETECT_METHOD, return_value=None): + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USB}, + data=_fam_usb_discovery_info(FAM_USB_OTHER_PORT), + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "invalid_dongle_path" + assert not hass.config_entries.flow.async_progress()