From c62961f40ca255bc58401636a18ea91b52f99ee7 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 2 Mar 2020 18:10:38 -0800 Subject: [PATCH] Add unique ID to coronavirus (#32423) --- homeassistant/components/coronavirus/__init__.py | 3 +++ homeassistant/components/coronavirus/config_flow.py | 2 ++ homeassistant/components/coronavirus/strings.json | 3 +++ tests/components/coronavirus/test_config_flow.py | 2 +- tests/components/coronavirus/test_init.py | 3 +++ 5 files changed, 12 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/coronavirus/__init__.py b/homeassistant/components/coronavirus/__init__.py index d5dbcd9f3f4..04976a1e4c5 100644 --- a/homeassistant/components/coronavirus/__init__.py +++ b/homeassistant/components/coronavirus/__init__.py @@ -42,6 +42,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): hass, entry.entry_id, _async_migrator ) + if not entry.unique_id: + hass.config_entries.async_update_entry(entry, unique_id=entry.data["country"]) + for component in PLATFORMS: hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, component) diff --git a/homeassistant/components/coronavirus/config_flow.py b/homeassistant/components/coronavirus/config_flow.py index 4a313a6837f..49183dd028e 100644 --- a/homeassistant/components/coronavirus/config_flow.py +++ b/homeassistant/components/coronavirus/config_flow.py @@ -32,6 +32,8 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): self._options[case.country] = case.country if user_input is not None: + await self.async_set_unique_id(user_input["country"]) + self._abort_if_unique_id_configured() return self.async_create_entry( title=self._options[user_input["country"]], data=user_input ) diff --git a/homeassistant/components/coronavirus/strings.json b/homeassistant/components/coronavirus/strings.json index 13cd5f04012..fd4873c808c 100644 --- a/homeassistant/components/coronavirus/strings.json +++ b/homeassistant/components/coronavirus/strings.json @@ -8,6 +8,9 @@ "country": "Country" } } + }, + "abort": { + "already_configured": "This country is already configured." } } } diff --git a/tests/components/coronavirus/test_config_flow.py b/tests/components/coronavirus/test_config_flow.py index 6d940d8e53d..ef04d0df07a 100644 --- a/tests/components/coronavirus/test_config_flow.py +++ b/tests/components/coronavirus/test_config_flow.py @@ -22,9 +22,9 @@ async def test_form(hass): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], {"country": OPTION_WORLDWIDE}, ) - assert result2["type"] == "create_entry" assert result2["title"] == "Worldwide" + assert result2["result"].unique_id == OPTION_WORLDWIDE assert result2["data"] == { "country": OPTION_WORLDWIDE, } diff --git a/tests/components/coronavirus/test_init.py b/tests/components/coronavirus/test_init.py index 05a14f2f296..57293635570 100644 --- a/tests/components/coronavirus/test_init.py +++ b/tests/components/coronavirus/test_init.py @@ -53,3 +53,6 @@ async def test_migration(hass): assert hass.states.get("sensor.netherlands_confirmed").state == "10" assert hass.states.get("sensor.worldwide_confirmed").state == "11" + + assert nl_entry.unique_id == "Netherlands" + assert worldwide_entry.unique_id == OPTION_WORLDWIDE