Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Entity representing a Blue Current charge point."""
2 
3 from abc import abstractmethod
4 
5 from homeassistant.const import ATTR_NAME
6 from homeassistant.core import callback
7 from homeassistant.helpers.device_registry import DeviceInfo
8 from homeassistant.helpers.dispatcher import async_dispatcher_connect
9 from homeassistant.helpers.entity import Entity
10 
11 from . import Connector
12 from .const import DOMAIN, MODEL_TYPE
13 
14 
16  """Define a base Blue Current entity."""
17 
18  _attr_has_entity_name = True
19  _attr_should_poll = False
20 
21  def __init__(self, connector: Connector, signal: str) -> None:
22  """Initialize the entity."""
23  self.connectorconnector = connector
24  self.signalsignal = signal
25  self.has_valuehas_value = False
26 
27  async def async_added_to_hass(self) -> None:
28  """Register callbacks."""
29 
30  @callback
31  def update() -> None:
32  """Update the state."""
33  self.update_from_latest_dataupdate_from_latest_data()
34  self.async_write_ha_stateasync_write_ha_state()
35 
36  self.async_on_removeasync_on_remove(async_dispatcher_connect(self.hasshass, self.signalsignal, update))
37 
38  self.update_from_latest_dataupdate_from_latest_data()
39 
40  @property
41  def available(self) -> bool:
42  """Return entity availability."""
43  return self.connectorconnector.connected and self.has_valuehas_value
44 
45  @callback
46  @abstractmethod
47  def update_from_latest_data(self) -> None:
48  """Update the entity from the latest data."""
49 
50 
51 class ChargepointEntity(BlueCurrentEntity):
52  """Define a base charge point entity."""
53 
54  def __init__(self, connector: Connector, evse_id: str) -> None:
55  """Initialize the entity."""
56  super().__init__(connector, f"{DOMAIN}_charge_point_update_{evse_id}")
57 
58  chargepoint_name = connector.charge_points[evse_id][ATTR_NAME]
59 
60  self.evse_idevse_id = evse_id
61  self._attr_device_info_attr_device_info = DeviceInfo(
62  identifiers={(DOMAIN, evse_id)},
63  name=chargepoint_name if chargepoint_name != "" else evse_id,
64  manufacturer="Blue Current",
65  model=connector.charge_points[evse_id][MODEL_TYPE],
66  )
None __init__(self, Connector connector, str signal)
Definition: entity.py:21
None __init__(self, Connector connector, str evse_id)
Definition: entity.py:54
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103