From b2c9bd2ca67df277093790e3fc0def34d9f7fb04 Mon Sep 17 00:00:00 2001 From: On Freund Date: Sat, 21 Nov 2020 21:47:57 +0200 Subject: [PATCH] Gracefully handle no uuid in kodi discovery (#43494) --- homeassistant/components/kodi/config_flow.py | 5 ++++- homeassistant/components/kodi/strings.json | 3 ++- tests/components/kodi/test_config_flow.py | 11 +++++++++++ tests/components/kodi/util.py | 10 ++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/kodi/config_flow.py b/homeassistant/components/kodi/config_flow.py index c11255aba87..c48e4564f92 100644 --- a/homeassistant/components/kodi/config_flow.py +++ b/homeassistant/components/kodi/config_flow.py @@ -104,7 +104,10 @@ class KodiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): self._host = discovery_info["host"] self._port = int(discovery_info["port"]) self._name = discovery_info["hostname"][: -len(".local.")] - uuid = discovery_info["properties"]["uuid"] + uuid = discovery_info["properties"].get("uuid") + if not uuid: + return self.async_abort(reason="no_uuid") + self._discovery_name = discovery_info["name"] await self.async_set_unique_id(uuid) diff --git a/homeassistant/components/kodi/strings.json b/homeassistant/components/kodi/strings.json index cf2f265f577..f1bb5342903 100644 --- a/homeassistant/components/kodi/strings.json +++ b/homeassistant/components/kodi/strings.json @@ -37,7 +37,8 @@ "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", - "unknown": "[%key:common::config_flow::error::unknown%]" + "unknown": "[%key:common::config_flow::error::unknown%]", + "no_uuid": "Kodi instance does not have a unique id. This is most likely due to an old Kodi version (17.x or below). You can configure the integration manually or upgrade to a more recent Kodi version." } }, "device_automation": { diff --git a/tests/components/kodi/test_config_flow.py b/tests/components/kodi/test_config_flow.py index 9b010f3ed42..9e892033786 100644 --- a/tests/components/kodi/test_config_flow.py +++ b/tests/components/kodi/test_config_flow.py @@ -11,6 +11,7 @@ from homeassistant.components.kodi.const import DEFAULT_TIMEOUT, DOMAIN from .util import ( TEST_CREDENTIALS, TEST_DISCOVERY, + TEST_DISCOVERY_WO_UUID, TEST_HOST, TEST_IMPORT, TEST_WS_PORT, @@ -573,6 +574,16 @@ async def test_discovery_updates_unique_id(hass): assert entry.data["name"] == "hostname" +async def test_discovery_without_unique_id(hass): + """Test a discovery flow with no unique id aborts.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": "zeroconf"}, data=TEST_DISCOVERY_WO_UUID + ) + + assert result["type"] == "abort" + assert result["reason"] == "no_uuid" + + async def test_form_import(hass): """Test we get the form with import source.""" with patch( diff --git a/tests/components/kodi/util.py b/tests/components/kodi/util.py index 5a47ea88631..edd6950d76e 100644 --- a/tests/components/kodi/util.py +++ b/tests/components/kodi/util.py @@ -24,6 +24,16 @@ TEST_DISCOVERY = { } +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_IMPORT = { "name": "name", "host": "1.1.1.1",