Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """LCN parent entity class."""
2 
3 from collections.abc import Callable
4 
5 from homeassistant.config_entries import ConfigEntry
6 from homeassistant.const import CONF_ADDRESS, CONF_DOMAIN, CONF_NAME, CONF_RESOURCE
7 from homeassistant.helpers.device_registry import DeviceInfo
8 from homeassistant.helpers.entity import Entity
9 from homeassistant.helpers.typing import ConfigType
10 
11 from .const import CONF_DOMAIN_DATA, DOMAIN
12 from .helpers import (
13  AddressType,
14  DeviceConnectionType,
15  InputType,
16  generate_unique_id,
17  get_device_connection,
18  get_device_model,
19 )
20 
21 
23  """Parent class for all entities associated with the LCN component."""
24 
25  _attr_should_poll = False
26  device_connection: DeviceConnectionType
27 
28  def __init__(
29  self,
30  config: ConfigType,
31  config_entry: ConfigEntry,
32  ) -> None:
33  """Initialize the LCN device."""
34  self.configconfig = config
35  self.config_entryconfig_entry = config_entry
36  self.address: AddressType = config[CONF_ADDRESS]
37  self._unregister_for_inputs_unregister_for_inputs: Callable | None = None
38  self._name: str = config[CONF_NAME]
39 
40  @property
41  def unique_id(self) -> str:
42  """Return a unique ID."""
43  return generate_unique_id(
44  self.config_entryconfig_entry.entry_id, self.address, self.configconfig[CONF_RESOURCE]
45  )
46 
47  @property
48  def device_info(self) -> DeviceInfo | None:
49  """Return device specific attributes."""
50  address = f"{'g' if self.address[2] else 'm'}{self.address[0]:03d}{self.address[1]:03d}"
51  model = (
52  "LCN resource"
53  f" ({get_device_model(self.config[CONF_DOMAIN], self.config[CONF_DOMAIN_DATA])})"
54  )
55 
56  return DeviceInfo(
57  identifiers={(DOMAIN, self.unique_idunique_idunique_id)},
58  name=f"{address}.{self.config[CONF_RESOURCE]}",
59  model=model,
60  manufacturer="Issendorff",
61  via_device=(
62  DOMAIN,
64  self.config_entryconfig_entry.entry_id, self.configconfig[CONF_ADDRESS]
65  ),
66  ),
67  )
68 
69  async def async_added_to_hass(self) -> None:
70  """Run when entity about to be added to hass."""
71  self.device_connectiondevice_connection = get_device_connection(
72  self.hasshass, self.configconfig[CONF_ADDRESS], self.config_entryconfig_entry
73  )
74  if not self.device_connectiondevice_connection.is_group:
75  self._unregister_for_inputs_unregister_for_inputs = self.device_connectiondevice_connection.register_for_inputs(
76  self.input_receivedinput_received
77  )
78 
79  async def async_will_remove_from_hass(self) -> None:
80  """Run when entity will be removed from hass."""
81  if self._unregister_for_inputs_unregister_for_inputs is not None:
82  self._unregister_for_inputs_unregister_for_inputs()
83 
84  @property
85  def name(self) -> str:
86  """Return the name of the device."""
87  return self._name
88 
89  def input_received(self, input_obj: InputType) -> None:
90  """Set state/value when LCN input object (command) is received."""
None input_received(self, InputType input_obj)
Definition: entity.py:89
None __init__(self, ConfigType config, ConfigEntry config_entry)
Definition: entity.py:32
DeviceConnectionType get_device_connection(HomeAssistant hass, AddressType address, ConfigEntry config_entry)
Definition: helpers.py:76
def generate_unique_id(device, metric)
Definition: entity.py:16