Skip to content

Commit 6c84d25

Browse files
authored
Fix device identifiers in ping and add migration (#155343)
1 parent c4dc413 commit 6c84d25

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

homeassistant/components/ping/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from icmplib import SocketPermissionError, async_ping
88

99
from homeassistant.const import CONF_HOST, Platform
10-
from homeassistant.core import HomeAssistant
11-
from homeassistant.helpers import config_validation as cv
10+
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
11+
from homeassistant.helpers import config_validation as cv, device_registry as dr
1212
from homeassistant.helpers.typing import ConfigType
1313
from homeassistant.util.hass_dict import HassKey
1414

@@ -32,6 +32,19 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
3232

3333
async def async_setup_entry(hass: HomeAssistant, entry: PingConfigEntry) -> bool:
3434
"""Set up Ping (ICMP) from a config entry."""
35+
36+
# Migrate device registry identifiers from homeassistant domain to ping domain
37+
registry = dr.async_get(hass)
38+
if (
39+
device := registry.async_get_device(
40+
identifiers={(HOMEASSISTANT_DOMAIN, entry.entry_id)}
41+
)
42+
) is not None and entry.entry_id in device.config_entries:
43+
registry.async_update_device(
44+
device_id=device.id,
45+
new_identifiers={(DOMAIN, entry.entry_id)},
46+
)
47+
3548
privileged = hass.data[DATA_PRIVILEGED_KEY]
3649

3750
host: str = entry.options[CONF_HOST]

homeassistant/components/ping/entity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""Base entity for the Ping component."""
22

3-
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN
43
from homeassistant.helpers.device_registry import DeviceInfo
54
from homeassistant.helpers.update_coordinator import CoordinatorEntity
65

6+
from .const import DOMAIN
77
from .coordinator import PingConfigEntry, PingUpdateCoordinator
88

99

@@ -23,6 +23,6 @@ def __init__(
2323

2424
self._attr_unique_id = unique_id
2525
self._attr_device_info = DeviceInfo(
26-
identifiers={(HOMEASSISTANT_DOMAIN, config_entry.entry_id)},
26+
identifiers={(DOMAIN, config_entry.entry_id)},
2727
manufacturer="Ping",
2828
)

tests/components/ping/test_init.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Test init of ping component."""
2+
3+
import pytest
4+
5+
from homeassistant.config_entries import ConfigEntryState
6+
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
7+
from homeassistant.helpers import device_registry as dr
8+
9+
from tests.common import MockConfigEntry
10+
11+
12+
@pytest.mark.usefixtures("patch_setup")
13+
async def test_config_setup(
14+
hass: HomeAssistant,
15+
config_entry: MockConfigEntry,
16+
) -> None:
17+
"""Test for setup success."""
18+
config_entry.add_to_hass(hass)
19+
await hass.config_entries.async_setup(config_entry.entry_id)
20+
assert config_entry.state is ConfigEntryState.LOADED
21+
22+
23+
async def test_device_migration(
24+
hass: HomeAssistant,
25+
device_registry: dr.DeviceRegistry,
26+
config_entry: MockConfigEntry,
27+
) -> None:
28+
"""Test for device migration."""
29+
config_entry.add_to_hass(hass)
30+
31+
old_device = device_registry.async_get_or_create(
32+
config_entry_id=config_entry.entry_id,
33+
identifiers={(HOMEASSISTANT_DOMAIN, config_entry.entry_id)},
34+
)
35+
36+
await hass.config_entries.async_setup(config_entry.entry_id)
37+
await hass.async_block_till_done()
38+
39+
device = device_registry.async_get_or_create(
40+
config_entry_id=config_entry.entry_id,
41+
identifiers={(config_entry.domain, config_entry.entry_id)},
42+
)
43+
44+
assert device is not None
45+
assert device.id == old_device.id
46+
assert device.config_entries == {config_entry.entry_id}

0 commit comments

Comments
 (0)