Fix unique_id of nuki config entry (#62840)

* fix(nuki): fixed naming of nuki integration

* parse_id function

* migration path

* fixes from ci runs

* don't update title if it was changed

* move to dedicated helper

* use dict of params

* Update homeassistant/components/nuki/__init__.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Pascal Reeb
2021-12-29 15:59:00 +01:00
committed by Franck Nijhof
co-authored by Martin Hjelmare
parent e54e353676
commit d62d013fb6
5 changed files with 27 additions and 9 deletions
@@ -27,6 +27,7 @@ from .const import (
DOMAIN,
ERROR_STATES,
)
from .helpers import parse_id
_LOGGER = logging.getLogger(__name__)
@@ -80,6 +81,14 @@ async def async_setup_entry(hass, entry):
hass.data.setdefault(DOMAIN, {})
# Migration of entry unique_id
if isinstance(entry.unique_id, int):
new_id = parse_id(entry.unique_id)
params = {"unique_id": new_id}
if entry.title == entry.unique_id:
params["title"] = new_id
hass.config_entries.async_update_entry(entry, **params)
try:
bridge = await hass.async_add_executor_job(
NukiBridge,
+8 -6
View File
@@ -12,6 +12,7 @@ from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TOKEN
from homeassistant.data_entry_flow import FlowResult
from .const import DEFAULT_PORT, DEFAULT_TIMEOUT, DOMAIN
from .helpers import parse_id
_LOGGER = logging.getLogger(__name__)
@@ -69,7 +70,7 @@ class NukiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
"""Prepare configuration for a DHCP discovered Nuki bridge."""
await self.async_set_unique_id(int(discovery_info.hostname[12:], 16))
await self.async_set_unique_id(discovery_info.hostname[12:].upper())
self._abort_if_unique_id_configured()
@@ -114,7 +115,9 @@ class NukiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
errors["base"] = "unknown"
if not errors:
existing_entry = await self.async_set_unique_id(info["ids"]["hardwareId"])
existing_entry = await self.async_set_unique_id(
parse_id(info["ids"]["hardwareId"])
)
if existing_entry:
self.hass.config_entries.async_update_entry(existing_entry, data=conf)
self.hass.async_create_task(
@@ -143,11 +146,10 @@ class NukiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
errors["base"] = "unknown"
if "base" not in errors:
await self.async_set_unique_id(info["ids"]["hardwareId"])
bridge_id = parse_id(info["ids"]["hardwareId"])
await self.async_set_unique_id(bridge_id)
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=info["ids"]["hardwareId"], data=user_input
)
return self.async_create_entry(title=bridge_id, data=user_input)
data_schema = self.discovery_schema or USER_SCHEMA
return self.async_show_form(
+6
View File
@@ -0,0 +1,6 @@
"""nuki integration helpers."""
def parse_id(hardware_id):
"""Parse Nuki ID."""
return hex(hardware_id).split("x")[-1].upper()
+2 -1
View File
@@ -7,6 +7,7 @@ HOST = "1.1.1.1"
MAC = "01:23:45:67:89:ab"
HW_ID = 123456789
ID_HEX = "75BCD15"
MOCK_INFO = {"ids": {"hardwareId": HW_ID}}
@@ -16,7 +17,7 @@ async def setup_nuki_integration(hass):
entry = MockConfigEntry(
domain="nuki",
unique_id=HW_ID,
unique_id=ID_HEX,
data={"host": HOST, "port": 8080, "token": "test-token"},
)
entry.add_to_hass(hass)
+2 -2
View File
@@ -41,7 +41,7 @@ async def test_form(hass):
await hass.async_block_till_done()
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result2["title"] == 123456789
assert result2["title"] == "75BCD15"
assert result2["data"] == {
"host": "1.1.1.1",
"port": 8080,
@@ -204,7 +204,7 @@ async def test_dhcp_flow(hass):
)
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result2["title"] == 123456789
assert result2["title"] == "75BCD15"
assert result2["data"] == {
"host": "1.1.1.1",
"port": 8080,