Migrate powerwall unique ids to use the gateway din (#107509)
This commit is contained in:
parent
e4a15354f4
commit
e7c25d1c36
4 changed files with 93 additions and 7 deletions
|
@ -16,10 +16,10 @@ from homeassistant.const import (
|
|||
STATE_UNKNOWN,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from .mocks import _mock_powerwall_with_fixtures
|
||||
from .mocks import MOCK_GATEWAY_DIN, _mock_powerwall_with_fixtures
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||
|
||||
|
@ -44,7 +44,7 @@ async def test_sensors(
|
|||
|
||||
device_registry = dr.async_get(hass)
|
||||
reg_device = device_registry.async_get_device(
|
||||
identifiers={("powerwall", "TG0123456789AB_TG9876543210BA")},
|
||||
identifiers={("powerwall", MOCK_GATEWAY_DIN)},
|
||||
)
|
||||
assert reg_device.model == "PowerWall 2 (GW1)"
|
||||
assert reg_device.sw_version == "1.50.1 c58c2df3"
|
||||
|
@ -173,3 +173,64 @@ async def test_sensors_with_empty_meters(hass: HomeAssistant) -> None:
|
|||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("sensor.mysite_solar_power") is None
|
||||
|
||||
|
||||
async def test_unique_id_migrate(
|
||||
hass: HomeAssistant, entity_registry_enabled_by_default: None
|
||||
) -> None:
|
||||
"""Test we can migrate unique ids of the sensors."""
|
||||
device_registry = dr.async_get(hass)
|
||||
ent_reg = er.async_get(hass)
|
||||
config_entry = MockConfigEntry(domain=DOMAIN, data={CONF_IP_ADDRESS: "1.2.3.4"})
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
mock_powerwall = await _mock_powerwall_with_fixtures(hass)
|
||||
old_unique_id = "_".join(sorted(["TG0123456789AB", "TG9876543210BA"]))
|
||||
new_unique_id = MOCK_GATEWAY_DIN
|
||||
device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
identifiers={("powerwall", old_unique_id)},
|
||||
manufacturer="Tesla",
|
||||
)
|
||||
old_mysite_load_power_entity = ent_reg.async_get_or_create(
|
||||
"sensor",
|
||||
DOMAIN,
|
||||
unique_id=f"{old_unique_id}_load_instant_power",
|
||||
suggested_object_id="mysite_load_power",
|
||||
config_entry=config_entry,
|
||||
)
|
||||
assert old_mysite_load_power_entity.entity_id == "sensor.mysite_load_power"
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.powerwall.config_flow.Powerwall",
|
||||
return_value=mock_powerwall,
|
||||
), patch(
|
||||
"homeassistant.components.powerwall.Powerwall", return_value=mock_powerwall
|
||||
):
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
reg_device = device_registry.async_get_device(
|
||||
identifiers={("powerwall", MOCK_GATEWAY_DIN)},
|
||||
)
|
||||
old_reg_device = device_registry.async_get_device(
|
||||
identifiers={("powerwall", old_unique_id)},
|
||||
)
|
||||
assert old_reg_device is None
|
||||
assert reg_device is not None
|
||||
|
||||
assert (
|
||||
ent_reg.async_get_entity_id(
|
||||
"sensor", DOMAIN, f"{old_unique_id}_load_instant_power"
|
||||
)
|
||||
is None
|
||||
)
|
||||
assert (
|
||||
ent_reg.async_get_entity_id(
|
||||
"sensor", DOMAIN, f"{new_unique_id}_load_instant_power"
|
||||
)
|
||||
is not None
|
||||
)
|
||||
|
||||
state = hass.states.get("sensor.mysite_load_power")
|
||||
assert state.state == "1.971"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue