Use ZeroconfServiceInfo in shelly (#60098)

This commit is contained in:
epenet 2021-11-21 23:33:20 +01:00 committed by GitHub
parent fa9465d003
commit 4555820987
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 8 deletions

View File

@ -189,16 +189,17 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
self, discovery_info: zeroconf.ZeroconfServiceInfo self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> FlowResult: ) -> FlowResult:
"""Handle zeroconf discovery.""" """Handle zeroconf discovery."""
host = discovery_info[zeroconf.ATTR_HOST]
try: try:
self.info = await self._async_get_info(discovery_info["host"]) self.info = await self._async_get_info(host)
except HTTP_CONNECT_ERRORS: except HTTP_CONNECT_ERRORS:
return self.async_abort(reason="cannot_connect") return self.async_abort(reason="cannot_connect")
except aioshelly.exceptions.FirmwareUnsupported: except aioshelly.exceptions.FirmwareUnsupported:
return self.async_abort(reason="unsupported_firmware") return self.async_abort(reason="unsupported_firmware")
await self.async_set_unique_id(self.info["mac"]) await self.async_set_unique_id(self.info["mac"])
self._abort_if_unique_id_configured({CONF_HOST: discovery_info["host"]}) self._abort_if_unique_id_configured({CONF_HOST: host})
self.host = discovery_info["host"] self.host = host
self.context["title_placeholders"] = { self.context["title_placeholders"] = {
"name": discovery_info.get("name", "").split(".")[0] "name": discovery_info.get("name", "").split(".")[0]

View File

@ -8,6 +8,7 @@ import aioshelly
import pytest import pytest
from homeassistant import config_entries, data_entry_flow from homeassistant import config_entries, data_entry_flow
from homeassistant.components import zeroconf
from homeassistant.components.shelly.const import DOMAIN from homeassistant.components.shelly.const import DOMAIN
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@ -16,11 +17,11 @@ MOCK_SETTINGS = {
"name": "Test name", "name": "Test name",
"device": {"mac": "test-mac", "hostname": "test-host", "type": "SHSW-1"}, "device": {"mac": "test-mac", "hostname": "test-host", "type": "SHSW-1"},
} }
DISCOVERY_INFO = { DISCOVERY_INFO = zeroconf.ZeroconfServiceInfo(
"host": "1.1.1.1", host="1.1.1.1",
"name": "shelly1pm-12345", name="shelly1pm-12345",
"properties": {"id": "shelly1pm-12345"}, properties={"id": "shelly1pm-12345"},
} )
MOCK_CONFIG = { MOCK_CONFIG = {
"wifi": {"ap": {"ssid": "Test name"}}, "wifi": {"ap": {"ssid": "Test name"}},
} }