Remove old system ID format from SimpliSafe base stations (#82993)

pull/83012/head^2
Aaron Bach 2022-11-30 14:10:56 -07:00 committed by GitHub
parent 4239923ea2
commit 671e6b3832
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 379 additions and 325 deletions

View File

@ -296,7 +296,8 @@ def _async_register_base_station(
) -> None:
"""Register a new bridge."""
device_registry = dr.async_get(hass)
device_registry.async_get_or_create(
base_station = device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
identifiers={(DOMAIN, str(system.system_id))},
manufacturer="SimpliSafe",
@ -304,6 +305,20 @@ def _async_register_base_station(
name=system.address,
)
# Check for an old system ID format and remove it:
if old_base_station := device_registry.async_get_device(
{(DOMAIN, system.system_id)} # type: ignore[arg-type]
):
# Update the new base station with any properties the user might have configured
# on the old base station:
device_registry.async_update_device(
base_station.id,
area_id=old_base_station.area_id,
disabled_by=old_base_station.disabled_by,
name_by_user=old_base_station.name_by_user,
)
device_registry.async_remove_device(old_base_station.id)
@callback
def _async_standardize_config_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:

View File

@ -15,7 +15,7 @@ from tests.common import MockConfigEntry, load_fixture
CODE = "12345"
PASSWORD = "password"
SYSTEM_ID = "system_123"
SYSTEM_ID = 12345
@pytest.fixture(name="api")
@ -79,7 +79,8 @@ def data_settings_fixture():
@pytest.fixture(name="data_subscription", scope="package")
def data_subscription_fixture():
"""Define subscription data."""
return json.loads(load_fixture("subscription_data.json", "simplisafe"))
data = json.loads(load_fixture("subscription_data.json", "simplisafe"))
return {SYSTEM_ID: data}
@pytest.fixture(name="reauth_config")

View File

@ -1,7 +1,6 @@
{
"system_123": {
"uid": 12345,
"sid": "system_123",
"sid": 12345,
"sStatus": 20,
"activated": 1445034752,
"planSku": "SSEDSM2",
@ -331,5 +330,4 @@
"billInterval": 2628000,
"pinUnlockedBy": "pin",
"autoActivation": null
}
}

View File

@ -21,7 +21,7 @@ async def test_entry_diagnostics(hass, config_entry, hass_client, setup_simplisa
"disabled_by": None,
},
"subscription_data": {
"system_123": {
"12345": {
"uid": REDACTED,
"sid": REDACTED,
"sStatus": 20,

View File

@ -0,0 +1,40 @@
"""Define tests for SimpliSafe setup."""
from unittest.mock import patch
from homeassistant.components.simplisafe import DOMAIN
from homeassistant.helpers import device_registry as dr
from homeassistant.setup import async_setup_component
async def test_base_station_migration(hass, api, config, config_entry):
"""Test that errors are shown when duplicates are added."""
old_identifers = (DOMAIN, 12345)
new_identifiers = (DOMAIN, "12345")
device_registry = dr.async_get(hass)
device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
identifiers={old_identifers},
manufacturer="SimpliSafe",
name="old",
)
with patch(
"homeassistant.components.simplisafe.config_flow.API.async_from_auth",
return_value=api,
), patch(
"homeassistant.components.simplisafe.API.async_from_auth",
return_value=api,
), patch(
"homeassistant.components.simplisafe.API.async_from_refresh_token",
return_value=api,
), patch(
"homeassistant.components.simplisafe.SimpliSafe._async_start_websocket_loop"
), patch(
"homeassistant.components.simplisafe.PLATFORMS", []
):
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()
assert device_registry.async_get_device(identifiers={old_identifers}) is None
assert device_registry.async_get_device(identifiers={new_identifiers}) is not None