Skip to content

Commit 57c6973

Browse files
Migrate Satel Integra entities unique_id to use config flow entry_id (#154187)
1 parent fb1f258 commit 57c6973

File tree

9 files changed

+307
-86
lines changed

9 files changed

+307
-86
lines changed

homeassistant/components/satel_integra/__init__.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from homeassistant.exceptions import ConfigEntryNotReady
2020
from homeassistant.helpers import config_validation as cv, issue_registry as ir
2121
from homeassistant.helpers.dispatcher import async_dispatcher_send
22+
from homeassistant.helpers.entity_registry import RegistryEntry, async_migrate_entries
2223
from homeassistant.helpers.typing import ConfigType
2324

2425
from .const import (
@@ -257,10 +258,11 @@ async def async_migrate_entry(
257258
config_entry.minor_version,
258259
)
259260

260-
if config_entry.version > 1:
261+
if config_entry.version > 2:
261262
# This means the user has downgraded from a future version
262263
return False
263264

265+
# 1.2 Migrate subentries to include configured numbers to title
264266
if config_entry.version == 1 and config_entry.minor_version == 1:
265267
for subentry in config_entry.subentries.values():
266268
property_map = {
@@ -278,6 +280,21 @@ async def async_migrate_entry(
278280

279281
hass.config_entries.async_update_entry(config_entry, minor_version=2)
280282

283+
# 2.1 Migrate all entity unique IDs to replace "satel" prefix with config entry ID, allows multiple entries to be configured
284+
if config_entry.version == 1:
285+
286+
@callback
287+
def migrate_unique_id(entity_entry: RegistryEntry) -> dict[str, str]:
288+
"""Migrate the unique ID to a new format."""
289+
return {
290+
"new_unique_id": entity_entry.unique_id.replace(
291+
"satel", config_entry.entry_id
292+
)
293+
}
294+
295+
await async_migrate_entries(hass, config_entry.entry_id, migrate_unique_id)
296+
hass.config_entries.async_update_entry(config_entry, version=2, minor_version=1)
297+
281298
_LOGGER.debug(
282299
"Migration to configuration version %s.%s successful",
283300
config_entry.version,

homeassistant/components/satel_integra/alarm_control_panel.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ async def async_setup_entry(
5252
async_add_entities(
5353
[
5454
SatelIntegraAlarmPanel(
55-
controller, zone_name, arm_home_mode, partition_num
55+
controller,
56+
zone_name,
57+
arm_home_mode,
58+
partition_num,
59+
config_entry.entry_id,
5660
)
5761
],
5862
config_subentry_id=subentry.subentry_id,
@@ -69,10 +73,12 @@ class SatelIntegraAlarmPanel(AlarmControlPanelEntity):
6973
| AlarmControlPanelEntityFeature.ARM_AWAY
7074
)
7175

72-
def __init__(self, controller, name, arm_home_mode, partition_id) -> None:
76+
def __init__(
77+
self, controller, name, arm_home_mode, partition_id, config_entry_id
78+
) -> None:
7379
"""Initialize the alarm panel."""
7480
self._attr_name = name
75-
self._attr_unique_id = f"satel_alarm_panel_{partition_id}"
81+
self._attr_unique_id = f"{config_entry_id}_alarm_panel_{partition_id}"
7682
self._arm_home_mode = arm_home_mode
7783
self._partition_id = partition_id
7884
self._satel = controller

homeassistant/components/satel_integra/binary_sensor.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ async def async_setup_entry(
5353
zone_type,
5454
CONF_ZONES,
5555
SIGNAL_ZONES_UPDATED,
56+
config_entry.entry_id,
5657
)
5758
],
5859
config_subentry_id=subentry.subentry_id,
@@ -77,6 +78,7 @@ async def async_setup_entry(
7778
ouput_type,
7879
CONF_OUTPUTS,
7980
SIGNAL_OUTPUTS_UPDATED,
81+
config_entry.entry_id,
8082
)
8183
],
8284
config_subentry_id=subentry.subentry_id,
@@ -96,10 +98,11 @@ def __init__(
9698
zone_type,
9799
sensor_type,
98100
react_to_signal,
101+
config_entry_id,
99102
):
100103
"""Initialize the binary_sensor."""
101104
self._device_number = device_number
102-
self._attr_unique_id = f"satel_{sensor_type}_{device_number}"
105+
self._attr_unique_id = f"{config_entry_id}_{sensor_type}_{device_number}"
103106
self._name = device_name
104107
self._zone_type = zone_type
105108
self._state = 0

homeassistant/components/satel_integra/config_flow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@
9090
class SatelConfigFlow(ConfigFlow, domain=DOMAIN):
9191
"""Handle a Satel Integra config flow."""
9292

93-
VERSION = 1
94-
MINOR_VERSION = 2
93+
VERSION = 2
94+
MINOR_VERSION = 1
9595

9696
@staticmethod
9797
@callback

homeassistant/components/satel_integra/switch.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ async def async_setup_entry(
4646
switchable_output_num,
4747
switchable_output_name,
4848
config_entry.options.get(CONF_CODE),
49+
config_entry.entry_id,
4950
),
5051
],
5152
config_subentry_id=subentry.subentry_id,
@@ -57,10 +58,10 @@ class SatelIntegraSwitch(SwitchEntity):
5758

5859
_attr_should_poll = False
5960

60-
def __init__(self, controller, device_number, device_name, code):
61+
def __init__(self, controller, device_number, device_name, code, config_entry_id):
6162
"""Initialize the binary_sensor."""
6263
self._device_number = device_number
63-
self._attr_unique_id = f"satel_switch_{device_number}"
64+
self._attr_unique_id = f"{config_entry_id}_switch_{device_number}"
6465
self._name = device_name
6566
self._state = False
6667
self._code = code

tests/components/satel_integra/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
MOCK_CONFIG_DATA = {CONF_HOST: "192.168.0.2", CONF_PORT: DEFAULT_PORT}
2121
MOCK_CONFIG_OPTIONS = {CONF_CODE: "1234"}
2222

23+
MOCK_ENTRY_ID = "1234567890"
24+
2325
MOCK_PARTITION_SUBENTRY = ConfigSubentry(
2426
subentry_type=SUBENTRY_TYPE_PARTITION,
2527
subentry_id="ID_PARTITION",

tests/components/satel_integra/conftest.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from . import (
1212
MOCK_CONFIG_DATA,
1313
MOCK_CONFIG_OPTIONS,
14+
MOCK_ENTRY_ID,
1415
MOCK_OUTPUT_SUBENTRY,
1516
MOCK_PARTITION_SUBENTRY,
1617
MOCK_SWITCHABLE_OUTPUT_SUBENTRY,
@@ -58,9 +59,9 @@ def mock_config_entry() -> MockConfigEntry:
5859
title="192.168.0.2",
5960
data=MOCK_CONFIG_DATA,
6061
options=MOCK_CONFIG_OPTIONS,
61-
entry_id="SATEL_INTEGRA_CONFIG_ENTRY_1",
62-
version=1,
63-
minor_version=2,
62+
entry_id=MOCK_ENTRY_ID,
63+
version=2,
64+
minor_version=1,
6465
)
6566

6667

0 commit comments

Comments
 (0)