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
17 changes: 10 additions & 7 deletions homeassistant/components/esphome/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,22 @@ async def async_step_zeroconf(
) -> FlowResult:
"""Handle zeroconf discovery."""
# Hostname is format: livingroom.local.
local_name = discovery_info["hostname"][:-1]
local_name = discovery_info[zeroconf.ATTR_HOSTNAME][:-1]
node_name = local_name[: -len(".local")]
address = discovery_info["properties"].get("address", local_name)
address = discovery_info[zeroconf.ATTR_PROPERTIES].get("address", local_name)

# Check if already configured
await self.async_set_unique_id(node_name)
self._abort_if_unique_id_configured(
updates={CONF_HOST: discovery_info[CONF_HOST]}
updates={CONF_HOST: discovery_info[zeroconf.ATTR_HOST]}
)

for entry in self._async_current_entries():
already_configured = False

if CONF_HOST in entry.data and entry.data[CONF_HOST] in (
address,
discovery_info[CONF_HOST],
discovery_info[zeroconf.ATTR_HOST],
):
# Is this address or IP address already configured?
already_configured = True
Expand All @@ -174,14 +174,17 @@ async def async_step_zeroconf(
if not entry.unique_id:
self.hass.config_entries.async_update_entry(
entry,
data={**entry.data, CONF_HOST: discovery_info[CONF_HOST]},
data={
**entry.data,
CONF_HOST: discovery_info[zeroconf.ATTR_HOST],
},
unique_id=node_name,
)

return self.async_abort(reason="already_configured")

self._host = discovery_info[CONF_HOST]
self._port = discovery_info[CONF_PORT]
self._host = discovery_info[zeroconf.ATTR_HOST]
self._port = discovery_info[zeroconf.ATTR_PORT]
self._name = node_name

return await self.async_step_discovery_confirm()
Expand Down
73 changes: 37 additions & 36 deletions tests/components/esphome/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import pytest

from homeassistant import config_entries
from homeassistant.components import zeroconf
from homeassistant.components.esphome import CONF_NOISE_PSK, DOMAIN, DomainData
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT
from homeassistant.data_entry_flow import (
Expand Down Expand Up @@ -215,12 +216,12 @@ async def test_discovery_initiation(hass, mock_client, mock_zeroconf):
"""Test discovery importing works."""
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(False, "test8266"))

service_info = {
"host": "192.168.43.183",
"port": 6053,
"hostname": "test8266.local.",
"properties": {},
}
service_info = zeroconf.ZeroconfServiceInfo(
host="192.168.43.183",
port=6053,
hostname="test8266.local.",
properties={},
)
flow = await hass.config_entries.flow.async_init(
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
)
Expand All @@ -247,12 +248,12 @@ async def test_discovery_already_configured_hostname(hass, mock_client):

entry.add_to_hass(hass)

service_info = {
"host": "192.168.43.183",
"port": 6053,
"hostname": "test8266.local.",
"properties": {},
}
service_info = zeroconf.ZeroconfServiceInfo(
host="192.168.43.183",
port=6053,
hostname="test8266.local.",
properties={},
)
result = await hass.config_entries.flow.async_init(
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
)
Expand All @@ -272,12 +273,12 @@ async def test_discovery_already_configured_ip(hass, mock_client):

entry.add_to_hass(hass)

service_info = {
"host": "192.168.43.183",
"port": 6053,
"hostname": "test8266.local.",
"properties": {"address": "192.168.43.183"},
}
service_info = zeroconf.ZeroconfServiceInfo(
host="192.168.43.183",
port=6053,
hostname="test8266.local.",
properties={"address": "192.168.43.183"},
)
result = await hass.config_entries.flow.async_init(
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
)
Expand All @@ -301,12 +302,12 @@ async def test_discovery_already_configured_name(hass, mock_client):
domain_data = DomainData.get(hass)
domain_data.set_entry_data(entry, mock_entry_data)

service_info = {
"host": "192.168.43.184",
"port": 6053,
"hostname": "test8266.local.",
"properties": {"address": "test8266.local"},
}
service_info = zeroconf.ZeroconfServiceInfo(
host="192.168.43.184",
port=6053,
hostname="test8266.local.",
properties={"address": "test8266.local"},
)
result = await hass.config_entries.flow.async_init(
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
)
Expand All @@ -320,12 +321,12 @@ async def test_discovery_already_configured_name(hass, mock_client):

async def test_discovery_duplicate_data(hass, mock_client):
"""Test discovery aborts if same mDNS packet arrives."""
service_info = {
"host": "192.168.43.183",
"port": 6053,
"hostname": "test8266.local.",
"properties": {"address": "test8266.local"},
}
service_info = zeroconf.ZeroconfServiceInfo(
host="192.168.43.183",
port=6053,
hostname="test8266.local.",
properties={"address": "test8266.local"},
)

mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(False, "test8266"))

Expand All @@ -351,12 +352,12 @@ async def test_discovery_updates_unique_id(hass, mock_client):

entry.add_to_hass(hass)

service_info = {
"host": "192.168.43.183",
"port": 6053,
"hostname": "test8266.local.",
"properties": {"address": "test8266.local"},
}
service_info = zeroconf.ZeroconfServiceInfo(
host="192.168.43.183",
port=6053,
hostname="test8266.local.",
properties={"address": "test8266.local"},
)
result = await hass.config_entries.flow.async_init(
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
)
Expand Down