1 """Handles Hue resource of type `device` mapping to Home Assistant device."""
3 from __future__
import annotations
5 from typing
import TYPE_CHECKING
7 from aiohue.v2
import HueBridgeV2
8 from aiohue.v2.controllers.events
import EventType
9 from aiohue.v2.controllers.groups
import Room, Zone
10 from aiohue.v2.models.device
import Device, DeviceArchetypes
11 from aiohue.v2.models.resource
import ResourceTypes
26 from ..const
import DOMAIN
29 from ..bridge
import HueBridge
33 """Manage setup of devices from Hue devices."""
34 entry = bridge.config_entry
36 api: HueBridgeV2 = bridge.api
37 dev_reg = dr.async_get(hass)
38 dev_controller = api.devices
41 def add_device(hue_resource: Device | Room | Zone) -> dr.DeviceEntry:
42 """Register a Hue device in device registry."""
43 if isinstance(hue_resource, (Room, Zone)):
45 return dev_reg.async_get_or_create(
46 config_entry_id=entry.entry_id,
47 entry_type=dr.DeviceEntryType.SERVICE,
48 identifiers={(DOMAIN, hue_resource.id)},
49 name=hue_resource.metadata.name,
50 model=hue_resource.type.value.title(),
51 manufacturer=api.config.bridge_device.product_data.manufacturer_name,
52 via_device=(DOMAIN, api.config.bridge_device.id),
53 suggested_area=hue_resource.metadata.name
54 if hue_resource.type == ResourceTypes.ROOM
58 model = f
"{hue_resource.product_data.product_name} ({hue_resource.product_data.model_id})"
60 ATTR_IDENTIFIERS: {(DOMAIN, hue_resource.id)},
61 ATTR_SW_VERSION: hue_resource.product_data.software_version,
62 ATTR_NAME: hue_resource.metadata.name,
64 ATTR_MANUFACTURER: hue_resource.product_data.manufacturer_name,
66 if room := dev_controller.get_room(hue_resource.id):
67 params[ATTR_SUGGESTED_AREA] = room.metadata.name
68 if hue_resource.metadata.archetype == DeviceArchetypes.BRIDGE_V2:
69 params[ATTR_IDENTIFIERS].
add((DOMAIN, api.config.bridge_id))
71 params[ATTR_VIA_DEVICE] = (DOMAIN, api.config.bridge_device.id)
72 zigbee = dev_controller.get_zigbee_connectivity(hue_resource.id)
73 if zigbee
and zigbee.mac_address:
74 params[ATTR_CONNECTIONS] = {(dr.CONNECTION_NETWORK_MAC, zigbee.mac_address)}
76 return dev_reg.async_get_or_create(config_entry_id=entry.entry_id, **params)
79 def remove_device(hue_device_id: str) ->
None:
80 """Remove device from registry."""
81 if device := dev_reg.async_get_device(identifiers={(DOMAIN, hue_device_id)}):
83 dev_reg.async_remove_device(device.id)
86 def handle_device_event(
87 evt_type: EventType, hue_resource: Device | Room | Zone
89 """Handle event from Hue controller."""
90 if evt_type == EventType.RESOURCE_DELETED:
91 remove_device(hue_resource.id)
94 add_device(hue_resource)
97 known_devices = [add_device(hue_device)
for hue_device
in dev_controller]
98 known_devices += [add_device(hue_room)
for hue_room
in api.groups.room]
99 known_devices += [add_device(hue_zone)
for hue_zone
in api.groups.zone]
102 for device
in dr.async_entries_for_config_entry(dev_reg, entry.entry_id):
103 if device
not in known_devices:
104 dev_reg.async_remove_device(device.id)
107 entry.async_on_unload(dev_controller.subscribe(handle_device_event))
108 entry.async_on_unload(api.groups.room.subscribe(handle_device_event))
109 entry.async_on_unload(api.groups.zone.subscribe(handle_device_event))
bool add(self, _T matcher)
def async_setup_devices(HueBridge bridge)