Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions homeassistant/components/adguard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,5 @@ def device_info(self) -> Dict[str, Any]:
"name": "AdGuard Home",
"manufacturer": "AdGuard Team",
"sw_version": self.hass.data[DOMAIN].get(DATA_ADGUARD_VERION),
"type": "service",
}
1 change: 1 addition & 0 deletions homeassistant/components/config/device_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def _entry_dict(entry):
"model": entry.model,
"name": entry.name,
"sw_version": entry.sw_version,
"entry_type": entry.entry_type,
"id": entry.id,
"via_device_id": entry.via_device_id,
"area_id": entry.area_id,
Expand Down
38 changes: 25 additions & 13 deletions homeassistant/helpers/device_registry.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Provide a way to connect entities belonging to one device."""
from collections import OrderedDict
import logging
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List, Optional, Set, Tuple
import uuid

import attr
Expand Down Expand Up @@ -32,19 +32,24 @@
class DeviceEntry:
"""Device Registry Entry."""

config_entries = attr.ib(type=set, converter=set, default=attr.Factory(set))
connections = attr.ib(type=set, converter=set, default=attr.Factory(set))
identifiers = attr.ib(type=set, converter=set, default=attr.Factory(set))
manufacturer = attr.ib(type=str, default=None)
model = attr.ib(type=str, default=None)
name = attr.ib(type=str, default=None)
sw_version = attr.ib(type=str, default=None)
via_device_id = attr.ib(type=str, default=None)
area_id = attr.ib(type=str, default=None)
name_by_user = attr.ib(type=str, default=None)
id = attr.ib(type=str, default=attr.Factory(lambda: uuid.uuid4().hex))
config_entries: Set[str] = attr.ib(converter=set, default=attr.Factory(set))
connections: Set[Tuple[str, str]] = attr.ib(
converter=set, default=attr.Factory(set)
)
identifiers: Set[Tuple[str, str]] = attr.ib(
converter=set, default=attr.Factory(set)
)
manufacturer: str = attr.ib(default=None)
model: str = attr.ib(default=None)
name: str = attr.ib(default=None)
sw_version: str = attr.ib(default=None)
via_device_id: str = attr.ib(default=None)
area_id: str = attr.ib(default=None)
name_by_user: str = attr.ib(default=None)
entry_type: str = attr.ib(default=None)
id: str = attr.ib(default=attr.Factory(lambda: uuid.uuid4().hex))
# This value is not stored, just used to keep track of events to fire.
is_new = attr.ib(type=bool, default=False)
is_new: bool = attr.ib(default=False)


def format_mac(mac: str) -> str:
Expand Down Expand Up @@ -105,6 +110,7 @@ def async_get_or_create(
model=_UNDEF,
name=_UNDEF,
sw_version=_UNDEF,
entry_type=_UNDEF,
via_device=None,
):
"""Get device. Create if it doesn't exist."""
Expand Down Expand Up @@ -144,6 +150,7 @@ def async_get_or_create(
model=model,
name=name,
sw_version=sw_version,
entry_type=entry_type,
)

@callback
Expand Down Expand Up @@ -189,6 +196,7 @@ def _async_update_device(
model=_UNDEF,
name=_UNDEF,
sw_version=_UNDEF,
entry_type=_UNDEF,
via_device_id=_UNDEF,
area_id=_UNDEF,
name_by_user=_UNDEF,
Expand Down Expand Up @@ -236,6 +244,7 @@ def _async_update_device(
("model", model),
("name", name),
("sw_version", sw_version),
("entry_type", entry_type),
("via_device_id", via_device_id),
):
if value is not _UNDEF and value != getattr(old, attr_name):
Expand Down Expand Up @@ -291,6 +300,8 @@ async def async_load(self):
model=device["model"],
name=device["name"],
sw_version=device["sw_version"],
# Introduced in 0.110
entry_type=device.get("entry_type"),
id=device["id"],
# Introduced in 0.79
# renamed in 0.95
Expand Down Expand Up @@ -323,6 +334,7 @@ def _data_to_save(self) -> Dict[str, List[Dict[str, Any]]]:
"model": entry.model,
"name": entry.name,
"sw_version": entry.sw_version,
"entry_type": entry.entry_type,
"id": entry.id,
"via_device_id": entry.via_device_id,
"area_id": entry.area_id,
Expand Down
1 change: 1 addition & 0 deletions homeassistant/helpers/entity_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ async def _async_add_entity(
"model",
"name",
"sw_version",
"entry_type",
"via_device",
):
if key in device_info:
Expand Down
3 changes: 3 additions & 0 deletions tests/components/config/test_device_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ async def test_list_devices(hass, client, registry):
manufacturer="manufacturer",
model="model",
via_device=("bridgeid", "0123"),
entry_type="service",
)

await client.send_json({"id": 5, "type": "config/device_registry/list"})
Expand All @@ -49,6 +50,7 @@ async def test_list_devices(hass, client, registry):
"model": "model",
"name": None,
"sw_version": None,
"entry_type": None,
"via_device_id": None,
"area_id": None,
"name_by_user": None,
Expand All @@ -60,6 +62,7 @@ async def test_list_devices(hass, client, registry):
"model": "model",
"name": None,
"sw_version": None,
"entry_type": "service",
"via_device_id": dev1,
"area_id": None,
"name_by_user": None,
Expand Down
9 changes: 9 additions & 0 deletions tests/helpers/test_device_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ async def test_loading_from_storage(hass, hass_storage):
"model": "model",
"name": "name",
"sw_version": "version",
"entry_type": "service",
"area_id": "12345A",
"name_by_user": "Test Friendly Name",
}
Expand All @@ -168,6 +169,7 @@ async def test_loading_from_storage(hass, hass_storage):
assert entry.id == "abcdefghijklm"
assert entry.area_id == "12345A"
assert entry.name_by_user == "Test Friendly Name"
assert entry.entry_type == "service"
assert isinstance(entry.config_entries, set)


Expand Down Expand Up @@ -304,6 +306,9 @@ async def test_loading_saving_data(hass, registry):
identifiers={("hue", "0123")},
manufacturer="manufacturer",
model="via",
name="Original Name",
sw_version="Orig SW 1",
entry_type="device",
)

orig_light = registry.async_get_or_create(
Expand All @@ -317,6 +322,10 @@ async def test_loading_saving_data(hass, registry):

assert len(registry.devices) == 2

orig_via = registry.async_update_device(
orig_via.id, area_id="mock-area-id", name_by_user="mock-name-by-user"
)

# Now load written data in new registry
registry2 = device_registry.DeviceRegistry(hass)
await flush_store(registry._store)
Expand Down
2 changes: 2 additions & 0 deletions tests/helpers/test_entity_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
"model": "test-model",
"name": "test-name",
"sw_version": "test-sw",
"entry_type": "service",
"via_device": ("hue", "via-id"),
},
),
Expand All @@ -730,6 +731,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
assert device.model == "test-model"
assert device.name == "test-name"
assert device.sw_version == "test-sw"
assert device.entry_type == "service"
assert device.via_device_id == via.id


Expand Down