Home Assistant Unofficial Reference 2024.12.1
device.py
Go to the documentation of this file.
1 """Handle KNX Devices."""
2 
3 from __future__ import annotations
4 
5 from xknx import XKNX
6 from xknx.core import XknxConnectionState
7 from xknx.io.gateway_scanner import GatewayDescriptor
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers import device_registry as dr
12 from homeassistant.helpers.device_registry import DeviceInfo
13 
14 from .const import DOMAIN
15 
16 
18  """Class for KNX Interface Device handling."""
19 
20  def __init__(self, hass: HomeAssistant, entry: ConfigEntry, xknx: XKNX) -> None:
21  """Initialize interface device class."""
22  self.hasshass = hass
23  self.device_registrydevice_registry = dr.async_get(hass)
24  self.gateway_descriptorgateway_descriptor: GatewayDescriptor | None = None
25  self.xknxxknx = xknx
26 
27  _device_id = (DOMAIN, f"_{entry.entry_id}_interface")
28  self.devicedevice = self.device_registrydevice_registry.async_get_or_create(
29  config_entry_id=entry.entry_id,
30  name="KNX Interface",
31  identifiers={_device_id},
32  )
33  self.device_infodevice_info = DeviceInfo(identifiers={_device_id})
34 
35  self.xknxxknx.connection_manager.register_connection_state_changed_cb(
36  self.connection_state_changed_cbconnection_state_changed_cb
37  )
38 
39  async def update(self) -> None:
40  """Update interface properties on new connection."""
41  self.gateway_descriptorgateway_descriptor = await self.xknxxknx.knxip_interface.gateway_info()
42 
43  self.device_registrydevice_registry.async_update_device(
44  device_id=self.devicedevice.id,
45  model=str(self.gateway_descriptorgateway_descriptor.name)
46  if self.gateway_descriptorgateway_descriptor
47  else None,
48  )
49 
50  def connection_state_changed_cb(self, state: XknxConnectionState) -> None:
51  """Call invoked after a KNX connection state change was received."""
52  if state is XknxConnectionState.CONNECTED:
53  self.hasshass.async_create_task(self.updateupdate())
None connection_state_changed_cb(self, XknxConnectionState state)
Definition: device.py:50
None __init__(self, HomeAssistant hass, ConfigEntry entry, XKNX xknx)
Definition: device.py:20
None async_update_device(HomeAssistant hass, ConfigEntry entry, str adapter, AdapterDetails details)
Definition: __init__.py:294