Skip to content

Commit 0a44682

Browse files
balloobfrenck
authored andcommitted
Push ESPHome discovery to ZJS addon (home-assistant#153004)
1 parent 06a5747 commit 0a44682

File tree

2 files changed

+73
-11
lines changed

2 files changed

+73
-11
lines changed

homeassistant/components/zwave_js/config_flow.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,10 @@ async def _async_set_addon_config(self, config_updates: dict) -> None:
376376

377377
new_addon_config = addon_config | config_updates
378378

379-
if not new_addon_config[CONF_ADDON_DEVICE]:
380-
new_addon_config.pop(CONF_ADDON_DEVICE)
381-
if not new_addon_config[CONF_ADDON_SOCKET]:
382-
new_addon_config.pop(CONF_ADDON_SOCKET)
379+
if new_addon_config.get(CONF_ADDON_DEVICE) is None:
380+
new_addon_config.pop(CONF_ADDON_DEVICE, None)
381+
if new_addon_config.get(CONF_ADDON_SOCKET) is None:
382+
new_addon_config.pop(CONF_ADDON_SOCKET, None)
383383

384384
if new_addon_config == addon_config:
385385
return
@@ -1470,14 +1470,33 @@ async def async_step_esphome(
14701470
if not is_hassio(self.hass):
14711471
return self.async_abort(reason="not_hassio")
14721472

1473-
if discovery_info.zwave_home_id:
1474-
await self.async_set_unique_id(str(discovery_info.zwave_home_id))
1475-
self._abort_if_unique_id_configured(
1476-
{
1477-
CONF_USB_PATH: None,
1478-
CONF_SOCKET_PATH: discovery_info.socket_path,
1479-
}
1473+
if (
1474+
discovery_info.zwave_home_id
1475+
and (
1476+
current_config_entries := self._async_current_entries(
1477+
include_ignore=False
1478+
)
14801479
)
1480+
and (home_id := str(discovery_info.zwave_home_id))
1481+
and (
1482+
existing_entry := next(
1483+
(
1484+
entry
1485+
for entry in current_config_entries
1486+
if entry.unique_id == home_id
1487+
),
1488+
None,
1489+
)
1490+
)
1491+
# Only update existing entries that are configured via sockets
1492+
and existing_entry.data.get(CONF_SOCKET_PATH)
1493+
):
1494+
await self._async_set_addon_config(
1495+
{CONF_ADDON_SOCKET: discovery_info.socket_path}
1496+
)
1497+
# Reloading will sync add-on options to config entry data
1498+
self.hass.config_entries.async_schedule_reload(existing_entry.entry_id)
1499+
return self.async_abort(reason="already_configured")
14811500

14821501
self.socket_path = discovery_info.socket_path
14831502
self.context["title_placeholders"] = {

tests/components/zwave_js/test_config_flow.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,49 @@ async def test_esphome_discovery(
12901290
assert len(mock_setup_entry.mock_calls) == 1
12911291

12921292

1293+
@pytest.mark.usefixtures("supervisor", "addon_installed", "addon_info")
1294+
async def test_esphome_discovery_already_configured(
1295+
hass: HomeAssistant,
1296+
set_addon_options: AsyncMock,
1297+
addon_options: dict[str, Any],
1298+
) -> None:
1299+
"""Test ESPHome discovery success path."""
1300+
addon_options[CONF_ADDON_SOCKET] = "esphome://existing-device:6053"
1301+
addon_options["another_key"] = "should_not_be_touched"
1302+
1303+
entry = MockConfigEntry(
1304+
entry_id="mock-entry-id",
1305+
domain=DOMAIN,
1306+
data={CONF_SOCKET_PATH: "esphome://existing-device:6053"},
1307+
title=TITLE,
1308+
unique_id="1234",
1309+
)
1310+
entry.add_to_hass(hass)
1311+
1312+
with patch.object(hass.config_entries, "async_schedule_reload") as mock_reload:
1313+
result = await hass.config_entries.flow.async_init(
1314+
DOMAIN,
1315+
context={"source": config_entries.SOURCE_ESPHOME},
1316+
data=ESPHOME_DISCOVERY_INFO,
1317+
)
1318+
1319+
mock_reload.assert_called_once_with(entry.entry_id)
1320+
1321+
assert result["type"] is FlowResultType.ABORT
1322+
assert result["reason"] == "already_configured"
1323+
1324+
# Addon got updated
1325+
assert set_addon_options.call_args == call(
1326+
"core_zwave_js",
1327+
AddonsOptions(
1328+
config={
1329+
"socket": "esphome://192.168.1.100:6053",
1330+
"another_key": "should_not_be_touched",
1331+
}
1332+
),
1333+
)
1334+
1335+
12931336
@pytest.mark.usefixtures("supervisor", "addon_installed")
12941337
async def test_discovery_addon_not_running(
12951338
hass: HomeAssistant,

0 commit comments

Comments
 (0)