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-27 23:02:48 +01:00 committed by GitHub
parent 53fdcf1b6a
commit 1af3177466
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 9 deletions

View File

@ -25,6 +25,7 @@ from .const import (
DOMAIN,
ERROR_STATES,
)
from .helpers import parse_id
_LOGGER = logging.getLogger(__name__)
@ -53,6 +54,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,

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__)
@ -65,7 +66,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()
@ -110,7 +111,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(
@ -139,11 +142,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(

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()

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)

View File

@ -39,7 +39,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,
@ -169,7 +169,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,