Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 32 additions & 31 deletions homeassistant/components/zwave_js/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1501,41 +1501,42 @@ async def async_step_esphome(
if not is_hassio(self.hass):
return self.async_abort(reason="not_hassio")

if (
discovery_info.zwave_home_id
and (
current_config_entries := self._async_current_entries(
include_ignore=False
if discovery_info.zwave_home_id:
if (
(
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,
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)
# And use the add-on
and existing_entry.data.get(CONF_USE_ADDON)
):
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")
Comment on lines +1505 to +1532
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This diff is big because I split out if discovery_info.zwave_home_id and so it got indented.


# We are not aborting if home ID configured here, we just want to make sure that it's set
# We will update a USB based config entry automatically in `async_step_finish_addon_setup_user`
await self.async_set_unique_id(
str(discovery_info.zwave_home_id), raise_on_progress=False
)
# Only update existing entries that are configured via sockets
and existing_entry.data.get(CONF_SOCKET_PATH)
# And use the add-on
and existing_entry.data.get(CONF_USE_ADDON)
):
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")

# We are not aborting if home ID configured here, we just want to make sure that it's set
# We will update a USB based config entry automatically in `async_step_finish_addon_setup_user`
await self.async_set_unique_id(
str(discovery_info.zwave_home_id), raise_on_progress=False
)
self.socket_path = discovery_info.socket_path
self.context["title_placeholders"] = {
CONF_NAME: f"{discovery_info.name} via ESPHome"
Expand Down
44 changes: 28 additions & 16 deletions tests/components/zwave_js/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,20 @@
"port": 3001,
}


ESPHOME_DISCOVERY_INFO = ESPHomeServiceInfo(
name="mock-name",
zwave_home_id=1234,
ip_address="192.168.1.100",
port=6053,
)

ESPHOME_DISCOVERY_INFO_CLEAN = ESPHomeServiceInfo(
name="mock-name",
zwave_home_id=None,
ip_address="192.168.1.100",
port=6053,
)

USB_DISCOVERY_INFO = UsbServiceInfo(
device="/dev/zwave",
pid="AAAA",
Expand Down Expand Up @@ -1167,31 +1173,22 @@ async def mock_restore_nvm(data: bytes, options: dict[str, bool] | None = None):
assert "keep_old_devices" in entry.data


@pytest.mark.parametrize(
"service_info", [ESPHOME_DISCOVERY_INFO, ESPHOME_DISCOVERY_INFO_CLEAN]
)
@pytest.mark.usefixtures("supervisor", "addon_not_installed", "addon_info")
async def test_esphome_discovery_intent_custom(
hass: HomeAssistant,
install_addon: AsyncMock,
set_addon_options: AsyncMock,
start_addon: AsyncMock,
service_info: ESPHomeServiceInfo,
) -> None:
"""Test ESPHome discovery success path."""
# Make sure it works only on hassio
with patch(
"homeassistant.components.zwave_js.config_flow.is_hassio", return_value=False
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_ESPHOME},
data=ESPHOME_DISCOVERY_INFO,
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "not_hassio"

# Test working version
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_ESPHOME},
data=ESPHOME_DISCOVERY_INFO,
data=service_info,
)

assert result["type"] is FlowResultType.MENU
Expand Down Expand Up @@ -1272,7 +1269,7 @@ async def test_esphome_discovery_intent_custom(

assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == TITLE
assert result["result"].unique_id == str(ESPHOME_DISCOVERY_INFO.zwave_home_id)
assert result["result"].unique_id == "1234"
assert result["data"] == {
"url": "ws://host1:3001",
"usb_path": None,
Expand Down Expand Up @@ -1524,6 +1521,21 @@ async def test_esphome_discovery_usb_same_home_id(
}


@pytest.mark.usefixtures("supervisor")
async def test_esphome_discovery_not_hassio(hass: HomeAssistant) -> None:
"""Test ESPHome discovery aborts when not hassio."""
with patch(
"homeassistant.components.zwave_js.config_flow.is_hassio", return_value=False
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_ESPHOME},
data=ESPHOME_DISCOVERY_INFO,
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "not_hassio"


@pytest.mark.usefixtures("supervisor", "addon_installed")
async def test_discovery_addon_not_running(
hass: HomeAssistant,
Expand Down
Loading