mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 13:57:10 +00:00
Fix DSMR migration (#135068)
This commit is contained in:
parent
43ec63eabc
commit
e052ab27f2
@ -20,6 +20,7 @@ from dsmr_parser.objects import DSMRObject, MbusDevice, Telegram
|
|||||||
import serial
|
import serial
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
|
DOMAIN as SENSOR_DOMAIN,
|
||||||
SensorDeviceClass,
|
SensorDeviceClass,
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
@ -456,23 +457,29 @@ def rename_old_gas_to_mbus(
|
|||||||
if entity.unique_id.endswith(
|
if entity.unique_id.endswith(
|
||||||
"belgium_5min_gas_meter_reading"
|
"belgium_5min_gas_meter_reading"
|
||||||
) or entity.unique_id.endswith("hourly_gas_meter_reading"):
|
) or entity.unique_id.endswith("hourly_gas_meter_reading"):
|
||||||
try:
|
if ent_reg.async_get_entity_id(
|
||||||
ent_reg.async_update_entity(
|
SENSOR_DOMAIN, DOMAIN, mbus_device_id
|
||||||
entity.entity_id,
|
):
|
||||||
new_unique_id=mbus_device_id,
|
|
||||||
)
|
|
||||||
except ValueError:
|
|
||||||
LOGGER.debug(
|
LOGGER.debug(
|
||||||
"Skip migration of %s because it already exists",
|
"Skip migration of %s because it already exists",
|
||||||
entity.entity_id,
|
entity.entity_id,
|
||||||
)
|
)
|
||||||
else:
|
continue
|
||||||
LOGGER.debug(
|
new_device = dev_reg.async_get_or_create(
|
||||||
"Migrated entity %s from unique id %s to %s",
|
config_entry_id=entry.entry_id,
|
||||||
entity.entity_id,
|
identifiers={(DOMAIN, mbus_device_id)},
|
||||||
entity.unique_id,
|
)
|
||||||
mbus_device_id,
|
ent_reg.async_update_entity(
|
||||||
)
|
entity.entity_id,
|
||||||
|
new_unique_id=mbus_device_id,
|
||||||
|
device_id=new_device.id,
|
||||||
|
)
|
||||||
|
LOGGER.debug(
|
||||||
|
"Migrated entity %s from unique id %s to %s",
|
||||||
|
entity.entity_id,
|
||||||
|
entity.unique_id,
|
||||||
|
mbus_device_id,
|
||||||
|
)
|
||||||
# Cleanup old device
|
# Cleanup old device
|
||||||
dev_entities = er.async_entries_for_device(
|
dev_entities = er.async_entries_for_device(
|
||||||
ent_reg, device_id, include_disabled_entities=True
|
ent_reg, device_id, include_disabled_entities=True
|
||||||
|
@ -10,6 +10,7 @@ from dsmr_parser.obis_references import (
|
|||||||
MBUS_METER_READING,
|
MBUS_METER_READING,
|
||||||
)
|
)
|
||||||
from dsmr_parser.objects import CosemObject, MBusObject, Telegram
|
from dsmr_parser.objects import CosemObject, MBusObject, Telegram
|
||||||
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.dsmr.const import DOMAIN
|
from homeassistant.components.dsmr.const import DOMAIN
|
||||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||||
@ -102,6 +103,17 @@ async def test_migrate_gas_to_mbus(
|
|||||||
# after receiving telegram entities need to have the chance to be created
|
# after receiving telegram entities need to have the chance to be created
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
# Check a new device is created and the old device has been removed
|
||||||
|
assert len(device_registry.devices) == 1
|
||||||
|
assert not device_registry.async_get(device.id)
|
||||||
|
new_entity = entity_registry.async_get("sensor.gas_meter_reading")
|
||||||
|
new_device = device_registry.async_get(new_entity.device_id)
|
||||||
|
new_dev_entities = er.async_entries_for_device(
|
||||||
|
entity_registry, new_device.id, include_disabled_entities=True
|
||||||
|
)
|
||||||
|
assert new_dev_entities == [new_entity]
|
||||||
|
|
||||||
|
# Check no entities are connected to the old device
|
||||||
dev_entities = er.async_entries_for_device(
|
dev_entities = er.async_entries_for_device(
|
||||||
entity_registry, device.id, include_disabled_entities=True
|
entity_registry, device.id, include_disabled_entities=True
|
||||||
)
|
)
|
||||||
@ -202,6 +214,17 @@ async def test_migrate_hourly_gas_to_mbus(
|
|||||||
# after receiving telegram entities need to have the chance to be created
|
# after receiving telegram entities need to have the chance to be created
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
# Check a new device is created and the old device has been removed
|
||||||
|
assert len(device_registry.devices) == 1
|
||||||
|
assert not device_registry.async_get(device.id)
|
||||||
|
new_entity = entity_registry.async_get("sensor.gas_meter_reading")
|
||||||
|
new_device = device_registry.async_get(new_entity.device_id)
|
||||||
|
new_dev_entities = er.async_entries_for_device(
|
||||||
|
entity_registry, new_device.id, include_disabled_entities=True
|
||||||
|
)
|
||||||
|
assert new_dev_entities == [new_entity]
|
||||||
|
|
||||||
|
# Check no entities are connected to the old device
|
||||||
dev_entities = er.async_entries_for_device(
|
dev_entities = er.async_entries_for_device(
|
||||||
entity_registry, device.id, include_disabled_entities=True
|
entity_registry, device.id, include_disabled_entities=True
|
||||||
)
|
)
|
||||||
@ -302,6 +325,18 @@ async def test_migrate_gas_with_devid_to_mbus(
|
|||||||
# after receiving telegram entities need to have the chance to be created
|
# after receiving telegram entities need to have the chance to be created
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
# Check a new device is not created and the old device has not been removed
|
||||||
|
assert len(device_registry.devices) == 1
|
||||||
|
assert device_registry.async_get(device.id)
|
||||||
|
new_entity = entity_registry.async_get("sensor.gas_meter_reading")
|
||||||
|
new_device = device_registry.async_get(new_entity.device_id)
|
||||||
|
assert new_device.id == device.id
|
||||||
|
# Check entities are still connected to the old device
|
||||||
|
dev_entities = er.async_entries_for_device(
|
||||||
|
entity_registry, device.id, include_disabled_entities=True
|
||||||
|
)
|
||||||
|
assert dev_entities == [new_entity]
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
entity_registry.async_get_entity_id(SENSOR_DOMAIN, DOMAIN, old_unique_id)
|
entity_registry.async_get_entity_id(SENSOR_DOMAIN, DOMAIN, old_unique_id)
|
||||||
is None
|
is None
|
||||||
@ -319,6 +354,7 @@ async def test_migrate_gas_to_mbus_exists(
|
|||||||
entity_registry: er.EntityRegistry,
|
entity_registry: er.EntityRegistry,
|
||||||
device_registry: dr.DeviceRegistry,
|
device_registry: dr.DeviceRegistry,
|
||||||
dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock],
|
dsmr_connection_fixture: tuple[MagicMock, MagicMock, MagicMock],
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test migration of unique_id."""
|
"""Test migration of unique_id."""
|
||||||
(connection_factory, transport, protocol) = dsmr_connection_fixture
|
(connection_factory, transport, protocol) = dsmr_connection_fixture
|
||||||
@ -380,7 +416,7 @@ async def test_migrate_gas_to_mbus_exists(
|
|||||||
telegram = Telegram()
|
telegram = Telegram()
|
||||||
telegram.add(
|
telegram.add(
|
||||||
MBUS_DEVICE_TYPE,
|
MBUS_DEVICE_TYPE,
|
||||||
CosemObject((0, 0), [{"value": "003", "unit": ""}]),
|
CosemObject((0, 1), [{"value": "003", "unit": ""}]),
|
||||||
"MBUS_DEVICE_TYPE",
|
"MBUS_DEVICE_TYPE",
|
||||||
)
|
)
|
||||||
telegram.add(
|
telegram.add(
|
||||||
@ -414,7 +450,32 @@ async def test_migrate_gas_to_mbus_exists(
|
|||||||
# after receiving telegram entities need to have the chance to be created
|
# after receiving telegram entities need to have the chance to be created
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
# Check a new device is not created and the old device has not been removed
|
||||||
|
assert len(device_registry.devices) == 2
|
||||||
|
assert device_registry.async_get(device.id)
|
||||||
|
assert device_registry.async_get(device2.id)
|
||||||
|
entity = entity_registry.async_get("sensor.gas_meter_reading")
|
||||||
|
dev_entities = er.async_entries_for_device(
|
||||||
|
entity_registry, device.id, include_disabled_entities=True
|
||||||
|
)
|
||||||
|
assert dev_entities == [entity]
|
||||||
|
entity2 = entity_registry.async_get("sensor.gas_meter_reading_alt")
|
||||||
|
dev2_entities = er.async_entries_for_device(
|
||||||
|
entity_registry, device2.id, include_disabled_entities=True
|
||||||
|
)
|
||||||
|
assert dev2_entities == [entity2]
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
entity_registry.async_get_entity_id(SENSOR_DOMAIN, DOMAIN, old_unique_id)
|
entity_registry.async_get_entity_id(SENSOR_DOMAIN, DOMAIN, old_unique_id)
|
||||||
== "sensor.gas_meter_reading"
|
== "sensor.gas_meter_reading"
|
||||||
)
|
)
|
||||||
|
assert (
|
||||||
|
entity_registry.async_get_entity_id(
|
||||||
|
SENSOR_DOMAIN, DOMAIN, "37464C4F32313139303333373331"
|
||||||
|
)
|
||||||
|
== "sensor.gas_meter_reading_alt"
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
"Skip migration of sensor.gas_meter_reading because it already exists"
|
||||||
|
in caplog.text
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user