Coverage for custom_components/appliance_autonotify/device.py: 96%

28 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-28 12:19 +0000

1"""The device of each appliance's entry, linked to the device Home Assistant already has for it""" 

2 

3from __future__ import annotations 

4 

5from typing import TYPE_CHECKING 

6 

7from homeassistant.const import CONF_DEVICE_ID, CONF_TYPE 

8from homeassistant.core import HomeAssistant, callback 

9from homeassistant.helpers import area_registry as ar 

10from homeassistant.helpers import device_registry as dr 

11from homeassistant.helpers import entity_registry as er 

12 

13from .compat import LINKED_DEVICES 

14from .const import CONF_POWER_ENTITY, TYPE_POWER 

15 

16if TYPE_CHECKING: 

17 from homeassistant.config_entries import ConfigEntry 

18 

19 

20def source_device(hass: HomeAssistant, entry: ConfigEntry) -> dr.DeviceEntry | None: 

21 """The device the appliance is watched through: its own, or for one known by its power, the plug's""" 

22 if entry.data.get(CONF_TYPE) == TYPE_POWER: 

23 power = er.async_get(hass).async_get(entry.options.get(CONF_POWER_ENTITY, "")) 

24 device_id = power.device_id if power is not None else None 

25 else: 

26 device_id = entry.data.get(CONF_DEVICE_ID) 

27 device = dr.async_get(hass).async_get(device_id) if device_id else None 

28 # not a child device of another, such as a part of a bigger appliance, which shares no hardware of its own to link to 

29 return device if isinstance(device, dr.DeviceEntry) else None 

30 

31 

32@callback 

33def async_register_device(hass: HomeAssistant, entry: ConfigEntry, name: str) -> dr.DeviceEntry | None: 

34 """A device for the entry, so it's listed under the entry on the integration's page, and shown alongside the 

35 appliance's own device: linked to it, as the same hardware, by the identifiers and connections they have in 

36 common, or for an appliance known by its power, connected through the plug, which is other hardware, and none at 

37 all before devices were linked""" 

38 if not LINKED_DEVICES: 

39 return None 

40 source = source_device(hass, entry) 

41 area = ar.async_get(hass).async_get_area(source.area_id) if source is not None and source.area_id else None 

42 suggested_area = area.name if area is not None else None 

43 if source is None: 

44 # a power sensor of no device, so with nothing to link to 

45 return dr.async_get(hass).async_get_or_create( 

46 config_entry_id=entry.entry_id, identifiers={(entry.domain, entry.entry_id)}, name=name 

47 ) 

48 if entry.data.get(CONF_TYPE) == TYPE_POWER: 

49 return dr.async_get(hass).async_get_or_create( 

50 config_entry_id=entry.entry_id, 

51 identifiers={(entry.domain, entry.entry_id)}, 

52 name=name, 

53 suggested_area=suggested_area, 

54 via_device_id=source.id, 

55 ) 

56 return dr.async_get(hass).async_get_or_create( 

57 config_entry_id=entry.entry_id, 

58 identifiers=set(source.identifiers), 

59 connections=set(source.connections), 

60 name=name, 

61 manufacturer=source.manufacturer, 

62 model=source.model, 

63 model_id=source.model_id, 

64 suggested_area=suggested_area, 

65 )