Push ESPHome discovery to ZJS addon (#153004)

This commit is contained in:
Paulus Schoutsen
2025-09-26 10:12:56 -04:00
committed by GitHub
parent b724176b23
commit 8051f78d10
2 changed files with 73 additions and 11 deletions

View File

@@ -376,10 +376,10 @@ class ZWaveJSConfigFlow(ConfigFlow, domain=DOMAIN):
new_addon_config = addon_config | config_updates
if not new_addon_config[CONF_ADDON_DEVICE]:
new_addon_config.pop(CONF_ADDON_DEVICE)
if not new_addon_config[CONF_ADDON_SOCKET]:
new_addon_config.pop(CONF_ADDON_SOCKET)
if new_addon_config.get(CONF_ADDON_DEVICE) is None:
new_addon_config.pop(CONF_ADDON_DEVICE, None)
if new_addon_config.get(CONF_ADDON_SOCKET) is None:
new_addon_config.pop(CONF_ADDON_SOCKET, None)
if new_addon_config == addon_config:
return
@@ -1470,14 +1470,33 @@ class ZWaveJSConfigFlow(ConfigFlow, domain=DOMAIN):
if not is_hassio(self.hass):
return self.async_abort(reason="not_hassio")
if discovery_info.zwave_home_id:
await self.async_set_unique_id(str(discovery_info.zwave_home_id))
self._abort_if_unique_id_configured(
{
CONF_USB_PATH: None,
CONF_SOCKET_PATH: discovery_info.socket_path,
}
if (
discovery_info.zwave_home_id
and (
current_config_entries := self._async_current_entries(
include_ignore=False
)
)
and (home_id := str(discovery_info.zwave_home_id))
and (
existing_entry := next(
(
entry
for entry in current_config_entries
if entry.unique_id == home_id
),
None,
)
)
# Only update existing entries that are configured via sockets
and existing_entry.data.get(CONF_SOCKET_PATH)
):
await self._async_set_addon_config(
{CONF_ADDON_SOCKET: discovery_info.socket_path}
)
# Reloading will sync add-on options to config entry data
self.hass.config_entries.async_schedule_reload(existing_entry.entry_id)
return self.async_abort(reason="already_configured")
self.socket_path = discovery_info.socket_path
self.context["title_placeholders"] = {

View File

@@ -1290,6 +1290,49 @@ async def test_esphome_discovery(
assert len(mock_setup_entry.mock_calls) == 1
@pytest.mark.usefixtures("supervisor", "addon_installed", "addon_info")
async def test_esphome_discovery_already_configured(
hass: HomeAssistant,
set_addon_options: AsyncMock,
addon_options: dict[str, Any],
) -> None:
"""Test ESPHome discovery success path."""
addon_options[CONF_ADDON_SOCKET] = "esphome://existing-device:6053"
addon_options["another_key"] = "should_not_be_touched"
entry = MockConfigEntry(
entry_id="mock-entry-id",
domain=DOMAIN,
data={CONF_SOCKET_PATH: "esphome://existing-device:6053"},
title=TITLE,
unique_id="1234",
)
entry.add_to_hass(hass)
with patch.object(hass.config_entries, "async_schedule_reload") as mock_reload:
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_ESPHOME},
data=ESPHOME_DISCOVERY_INFO,
)
mock_reload.assert_called_once_with(entry.entry_id)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
# Addon got updated
assert set_addon_options.call_args == call(
"core_zwave_js",
AddonsOptions(
config={
"socket": "esphome://192.168.1.100:6053",
"another_key": "should_not_be_touched",
}
),
)
@pytest.mark.usefixtures("supervisor", "addon_installed")
async def test_discovery_addon_not_running(
hass: HomeAssistant,