33import logging
44from typing import Any , TypedDict
55
6- from homeassistant .config_entries import ConfigEntry
76from homeassistant .core import HomeAssistant , callback
87from homeassistant .exceptions import ConfigEntryNotReady
9- from homeassistant .helpers import entity_registry as er
8+ from homeassistant .helpers import device_registry as dr , entity_registry as er
109from homeassistant .helpers .storage import STORAGE_DIR
1110from plugwise_usb import Stick
1211from plugwise_usb .api import NodeEvent
1312from plugwise_usb .exceptions import StickError
1413
1514from .const import (
1615 CONF_USB_PATH ,
16+ DOMAIN ,
1717 NODES ,
1818 PLUGWISE_USB_PLATFORMS ,
1919 STICK ,
2020)
21- from .coordinator import PlugwiseUSBDataUpdateCoordinator
21+ from .coordinator import PlugwiseUSBConfigEntry , PlugwiseUSBDataUpdateCoordinator
2222
2323_LOGGER = logging .getLogger (__name__ )
2424UNSUBSCRIBE_DISCOVERY = "unsubscribe_discovery"
@@ -31,7 +31,7 @@ class NodeConfigEntry(TypedDict):
3131 coordinator : PlugwiseUSBDataUpdateCoordinator
3232
3333
34- async def async_setup_entry (hass : HomeAssistant , config_entry : ConfigEntry ):
34+ async def async_setup_entry (hass : HomeAssistant , config_entry : PlugwiseUSBConfigEntry ):
3535 """Establish connection with plugwise USB-stick."""
3636
3737 @callback
@@ -60,6 +60,19 @@ def _async_migrate_entity_entry(
6060 f"Failed to open connection to Plugwise USB stick at { config_entry .data [CONF_USB_PATH ]} "
6161 ) from StickError
6262
63+ device_registry = dr .async_get (hass )
64+ device_registry .async_get_or_create (
65+ config_entry_id = config_entry .entry_id ,
66+ connections = {(dr .CONNECTION_ZIGBEE , str (api_stick .mac_stick ))},
67+ hw_version = str (api_stick .hardware ),
68+ identifiers = {(DOMAIN , str (api_stick .mac_stick ))},
69+ manufacturer = "Plugwise" ,
70+ model = "Stick" ,
71+ name = str (api_stick .name ),
72+ serial_number = str (api_stick .mac_stick ),
73+ sw_version = str (api_stick .firmware ),
74+ )
75+
6376 config_entry .runtime_data [NODES ]: NodeConfigEntry = {}
6477
6578 async def async_node_discovered (node_event : NodeEvent , mac : str ) -> None :
@@ -69,7 +82,7 @@ async def async_node_discovered(node_event: NodeEvent, mac: str) -> None:
6982 _LOGGER .debug ("async_node_discovered | mac=%s" , mac )
7083 node = api_stick .nodes [mac ]
7184 _LOGGER .debug ("async_node_discovered | node_info=%s" , node .node_info )
72- coordinator = PlugwiseUSBDataUpdateCoordinator (hass , node )
85+ coordinator = PlugwiseUSBDataUpdateCoordinator (hass , config_entry , node )
7386 config_entry .runtime_data [NODES ][mac ] = coordinator
7487 await node .load ()
7588
@@ -94,10 +107,11 @@ async def async_node_discovered(node_event: NodeEvent, mac: str) -> None:
94107
95108 # Initiate background discovery task
96109 hass .async_create_task (api_stick .discover_nodes (load = True ))
110+
97111 return True
98112
99113
100- async def async_unload_entry (hass : HomeAssistant , config_entry : ConfigEntry ):
114+ async def async_unload_entry (hass : HomeAssistant , config_entry : PlugwiseUSBConfigEntry ):
101115 """Unload the Plugwise USB stick connection."""
102116 config_entry .runtime_data [UNSUBSCRIBE_DISCOVERY ]()
103117 unload = await hass .config_entries .async_unload_platforms (
@@ -109,7 +123,7 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry):
109123
110124@callback
111125def async_migrate_entity_entry (
112- config_entry : ConfigEntry , entity_entry : er .RegistryEntry
126+ config_entry : PlugwiseUSBConfigEntry , entity_entry : er .RegistryEntry
113127) -> dict [str , Any ] | None :
114128 """Migrate Plugwise USB entity entries.
115129
0 commit comments