Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Base class for SUPLA channels."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from homeassistant.helpers.update_coordinator import CoordinatorEntity
8 
9 _LOGGER = logging.getLogger(__name__)
10 
11 
13  """Base class of a SUPLA Channel (an equivalent of HA's Entity)."""
14 
15  def __init__(self, config, server, coordinator):
16  """Init from config, hookup[ server and coordinator."""
17  super().__init__(coordinator)
18  self.server_nameserver_name = config["server_name"]
19  self.channel_idchannel_id = config["channel_id"]
20  self.serverserver = server
21 
22  @property
23  def channel_data(self):
24  """Return channel data taken from coordinator."""
25  return self.coordinator.data.get(self.channel_idchannel_id)
26 
27  @property
28  def unique_id(self) -> str:
29  """Return a unique ID."""
30  uid = self.channel_datachannel_data["iodevice"]["gUIDString"].lower()
31  channel_number = self.channel_datachannel_data["channelNumber"]
32  return f"supla-{uid}-{channel_number}"
33 
34  @property
35  def name(self) -> str | None:
36  """Return the name of the device."""
37  return self.channel_datachannel_data["caption"]
38 
39  @property
40  def available(self) -> bool:
41  """Return True if entity is available."""
42  if self.channel_datachannel_data is None:
43  return False
44  if (state := self.channel_datachannel_data.get("state")) is None:
45  return False
46  return state.get("connected")
47 
48  async def async_action(self, action, **add_pars):
49  """Run server action.
50 
51  Actions are currently hardcoded in components.
52  SUPLA's API enables autodiscovery
53  """
54  _LOGGER.debug(
55  "Executing action %s on channel %d, params: %s",
56  action,
57  self.channel_datachannel_data["id"],
58  add_pars,
59  )
60  await self.serverserver.execute_action(self.channel_datachannel_data["id"], action, **add_pars)
61 
62  # Update state
63  await self.coordinator.async_request_refresh()
def async_action(self, action, **add_pars)
Definition: entity.py:48
def __init__(self, config, server, coordinator)
Definition: entity.py:15
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88