Skip to content

Commit 750bce2

Browse files
authored
Also migrate dsmr entries for devices with correct serial (#123407)
dsmr: also migrate entries for devices with correct serial When the dsmr code could not find the serial_nr for the gas meter, it creates the gas meter device with the entry_id as identifier. But when there is a correct serial_nr, it will use that as identifier for the dsmr gas device. Now the migration code did not take this into account, so migration to the new name failed since it didn't look for the device with correct serial_nr. This commit fixes this and adds a test for this.
1 parent 94af95c commit 750bce2

File tree

2 files changed

+131
-35
lines changed

2 files changed

+131
-35
lines changed

Diff for: homeassistant/components/dsmr/sensor.py

+36-35
Original file line numberDiff line numberDiff line change
@@ -431,41 +431,42 @@ def rename_old_gas_to_mbus(
431431
) -> None:
432432
"""Rename old gas sensor to mbus variant."""
433433
dev_reg = dr.async_get(hass)
434-
device_entry_v1 = dev_reg.async_get_device(identifiers={(DOMAIN, entry.entry_id)})
435-
if device_entry_v1 is not None:
436-
device_id = device_entry_v1.id
437-
438-
ent_reg = er.async_get(hass)
439-
entries = er.async_entries_for_device(ent_reg, device_id)
440-
441-
for entity in entries:
442-
if entity.unique_id.endswith(
443-
"belgium_5min_gas_meter_reading"
444-
) or entity.unique_id.endswith("hourly_gas_meter_reading"):
445-
try:
446-
ent_reg.async_update_entity(
447-
entity.entity_id,
448-
new_unique_id=mbus_device_id,
449-
device_id=mbus_device_id,
450-
)
451-
except ValueError:
452-
LOGGER.debug(
453-
"Skip migration of %s because it already exists",
454-
entity.entity_id,
455-
)
456-
else:
457-
LOGGER.debug(
458-
"Migrated entity %s from unique id %s to %s",
459-
entity.entity_id,
460-
entity.unique_id,
461-
mbus_device_id,
462-
)
463-
# Cleanup old device
464-
dev_entities = er.async_entries_for_device(
465-
ent_reg, device_id, include_disabled_entities=True
466-
)
467-
if not dev_entities:
468-
dev_reg.async_remove_device(device_id)
434+
for dev_id in (mbus_device_id, entry.entry_id):
435+
device_entry_v1 = dev_reg.async_get_device(identifiers={(DOMAIN, dev_id)})
436+
if device_entry_v1 is not None:
437+
device_id = device_entry_v1.id
438+
439+
ent_reg = er.async_get(hass)
440+
entries = er.async_entries_for_device(ent_reg, device_id)
441+
442+
for entity in entries:
443+
if entity.unique_id.endswith(
444+
"belgium_5min_gas_meter_reading"
445+
) or entity.unique_id.endswith("hourly_gas_meter_reading"):
446+
try:
447+
ent_reg.async_update_entity(
448+
entity.entity_id,
449+
new_unique_id=mbus_device_id,
450+
device_id=mbus_device_id,
451+
)
452+
except ValueError:
453+
LOGGER.debug(
454+
"Skip migration of %s because it already exists",
455+
entity.entity_id,
456+
)
457+
else:
458+
LOGGER.debug(
459+
"Migrated entity %s from unique id %s to %s",
460+
entity.entity_id,
461+
entity.unique_id,
462+
mbus_device_id,
463+
)
464+
# Cleanup old device
465+
dev_entities = er.async_entries_for_device(
466+
ent_reg, device_id, include_disabled_entities=True
467+
)
468+
if not dev_entities:
469+
dev_reg.async_remove_device(device_id)
469470

470471

471472
def is_supported_description(

Diff for: tests/components/dsmr/test_mbus_migration.py

+95
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,101 @@ async def test_migrate_hourly_gas_to_mbus(
219219
)
220220

221221

222+
async def test_migrate_gas_with_devid_to_mbus(
223+
hass: HomeAssistant,
224+
entity_registry: er.EntityRegistry,
225+
device_registry: dr.DeviceRegistry,
226+
dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock],
227+
) -> None:
228+
"""Test migration of unique_id."""
229+
(connection_factory, transport, protocol) = dsmr_connection_fixture
230+
231+
mock_entry = MockConfigEntry(
232+
domain=DOMAIN,
233+
unique_id="/dev/ttyUSB0",
234+
data={
235+
"port": "/dev/ttyUSB0",
236+
"dsmr_version": "5B",
237+
"serial_id": "1234",
238+
"serial_id_gas": "37464C4F32313139303333373331",
239+
},
240+
options={
241+
"time_between_update": 0,
242+
},
243+
)
244+
245+
mock_entry.add_to_hass(hass)
246+
247+
old_unique_id = "37464C4F32313139303333373331_belgium_5min_gas_meter_reading"
248+
249+
device = device_registry.async_get_or_create(
250+
config_entry_id=mock_entry.entry_id,
251+
identifiers={(DOMAIN, "37464C4F32313139303333373331")},
252+
name="Gas Meter",
253+
)
254+
await hass.async_block_till_done()
255+
256+
entity: er.RegistryEntry = entity_registry.async_get_or_create(
257+
suggested_object_id="gas_meter_reading",
258+
disabled_by=None,
259+
domain=SENSOR_DOMAIN,
260+
platform=DOMAIN,
261+
device_id=device.id,
262+
unique_id=old_unique_id,
263+
config_entry=mock_entry,
264+
)
265+
assert entity.unique_id == old_unique_id
266+
await hass.async_block_till_done()
267+
268+
telegram = Telegram()
269+
telegram.add(
270+
MBUS_DEVICE_TYPE,
271+
CosemObject((0, 1), [{"value": "003", "unit": ""}]),
272+
"MBUS_DEVICE_TYPE",
273+
)
274+
telegram.add(
275+
MBUS_EQUIPMENT_IDENTIFIER,
276+
CosemObject(
277+
(0, 1),
278+
[{"value": "37464C4F32313139303333373331", "unit": ""}],
279+
),
280+
"MBUS_EQUIPMENT_IDENTIFIER",
281+
)
282+
telegram.add(
283+
MBUS_METER_READING,
284+
MBusObject(
285+
(0, 1),
286+
[
287+
{"value": datetime.datetime.fromtimestamp(1551642213)},
288+
{"value": Decimal(745.695), "unit": "m3"},
289+
],
290+
),
291+
"MBUS_METER_READING",
292+
)
293+
294+
assert await hass.config_entries.async_setup(mock_entry.entry_id)
295+
await hass.async_block_till_done()
296+
297+
telegram_callback = connection_factory.call_args_list[0][0][2]
298+
299+
# simulate a telegram pushed from the smartmeter and parsed by dsmr_parser
300+
telegram_callback(telegram)
301+
302+
# after receiving telegram entities need to have the chance to be created
303+
await hass.async_block_till_done()
304+
305+
assert (
306+
entity_registry.async_get_entity_id(SENSOR_DOMAIN, DOMAIN, old_unique_id)
307+
is None
308+
)
309+
assert (
310+
entity_registry.async_get_entity_id(
311+
SENSOR_DOMAIN, DOMAIN, "37464C4F32313139303333373331"
312+
)
313+
== "sensor.gas_meter_reading"
314+
)
315+
316+
222317
async def test_migrate_gas_to_mbus_exists(
223318
hass: HomeAssistant,
224319
entity_registry: er.EntityRegistry,

0 commit comments

Comments
 (0)