From e3ee19d0c432fcb96876a297f7f3050ff8616fd6 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 19 Nov 2021 17:16:28 +0100 Subject: [PATCH] Use ZeroconfServiceInfo in kodi (#59984) Co-authored-by: epenet --- homeassistant/components/kodi/config_flow.py | 17 +++++----- tests/components/kodi/util.py | 33 ++++++++++---------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/kodi/config_flow.py b/homeassistant/components/kodi/config_flow.py index 404540d47aa..6e7ff6bb2a4 100644 --- a/homeassistant/components/kodi/config_flow.py +++ b/homeassistant/components/kodi/config_flow.py @@ -7,6 +7,7 @@ from pykodi import CannotConnectError, InvalidAuthError, Kodi, get_kodi_connecti import voluptuous as vol from homeassistant import config_entries, core, exceptions +from homeassistant.components import zeroconf from homeassistant.const import ( CONF_HOST, CONF_NAME, @@ -17,8 +18,8 @@ from homeassistant.const import ( CONF_USERNAME, ) from homeassistant.core import callback +from homeassistant.data_entry_flow import FlowResult from homeassistant.helpers.aiohttp_client import async_get_clientsession -from homeassistant.helpers.typing import DiscoveryInfoType from .const import ( CONF_WS_PORT, @@ -99,15 +100,17 @@ class KodiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): self._ssl: bool | None = DEFAULT_SSL self._discovery_name: str | None = None - async def async_step_zeroconf(self, discovery_info: DiscoveryInfoType): + async def async_step_zeroconf( + self, discovery_info: zeroconf.ZeroconfServiceInfo + ) -> FlowResult: """Handle zeroconf discovery.""" - self._host = discovery_info["host"] - self._port = int(discovery_info["port"]) - self._name = discovery_info["hostname"][: -len(".local.")] - if not (uuid := discovery_info["properties"].get("uuid")): + self._host = discovery_info[zeroconf.ATTR_HOST] + self._port = int(discovery_info[zeroconf.ATTR_PORT]) + self._name = discovery_info[zeroconf.ATTR_HOSTNAME][: -len(".local.")] + if not (uuid := discovery_info[zeroconf.ATTR_PROPERTIES].get("uuid")): return self.async_abort(reason="no_uuid") - self._discovery_name = discovery_info["name"] + self._discovery_name = discovery_info[zeroconf.ATTR_NAME] await self.async_set_unique_id(uuid) self._abort_if_unique_id_configured( diff --git a/tests/components/kodi/util.py b/tests/components/kodi/util.py index edd6950d76e..c3aaca16d5a 100644 --- a/tests/components/kodi/util.py +++ b/tests/components/kodi/util.py @@ -1,4 +1,5 @@ """Test the Kodi config flow.""" +from homeassistant.components import zeroconf from homeassistant.components.kodi.const import DEFAULT_SSL TEST_HOST = { @@ -14,24 +15,24 @@ TEST_CREDENTIALS = {"username": "username", "password": "password"} TEST_WS_PORT = {"ws_port": 9090} UUID = "11111111-1111-1111-1111-111111111111" -TEST_DISCOVERY = { - "host": "1.1.1.1", - "port": 8080, - "hostname": "hostname.local.", - "type": "_xbmc-jsonrpc-h._tcp.local.", - "name": "hostname._xbmc-jsonrpc-h._tcp.local.", - "properties": {"uuid": UUID}, -} +TEST_DISCOVERY = zeroconf.ZeroconfServiceInfo( + host="1.1.1.1", + port=8080, + hostname="hostname.local.", + type="_xbmc-jsonrpc-h._tcp.local.", + name="hostname._xbmc-jsonrpc-h._tcp.local.", + properties={"uuid": UUID}, +) -TEST_DISCOVERY_WO_UUID = { - "host": "1.1.1.1", - "port": 8080, - "hostname": "hostname.local.", - "type": "_xbmc-jsonrpc-h._tcp.local.", - "name": "hostname._xbmc-jsonrpc-h._tcp.local.", - "properties": {}, -} +TEST_DISCOVERY_WO_UUID = zeroconf.ZeroconfServiceInfo( + host="1.1.1.1", + port=8080, + hostname="hostname.local.", + type="_xbmc-jsonrpc-h._tcp.local.", + name="hostname._xbmc-jsonrpc-h._tcp.local.", + properties={}, +) TEST_IMPORT = {