Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Support for Tellstick."""
2 
3 import logging
4 import threading
5 
6 from tellcore.constants import TELLSTICK_DIM, TELLSTICK_TURNOFF, TELLSTICK_TURNON
7 from tellcore.library import TelldusError
8 
9 from homeassistant.helpers.dispatcher import async_dispatcher_connect
10 from homeassistant.helpers.entity import Entity
11 
12 from .const import SIGNAL_TELLCORE_CALLBACK
13 
14 _LOGGER = logging.getLogger(__name__)
15 
16 # Use a global tellstick domain lock to avoid getting Tellcore errors when
17 # calling concurrently.
18 TELLSTICK_LOCK = threading.RLock()
19 
20 
22  """Representation of a Tellstick device.
23 
24  Contains the common logic for all Tellstick devices.
25  """
26 
27  _attr_assumed_state = True
28  _attr_should_poll = False
29 
30  def __init__(self, tellcore_device, signal_repetitions):
31  """Init the Tellstick device."""
32  self._signal_repetitions_signal_repetitions = signal_repetitions
33  self._state_state = None
34  self._requested_state_requested_state = None
35  self._requested_data_requested_data = None
36  self._repeats_left_repeats_left = 0
37 
38  # Look up our corresponding tellcore device
39  self._tellcore_device_tellcore_device = tellcore_device
40  self._attr_name_attr_name = tellcore_device.name
41  self._attr_unique_id_attr_unique_id = tellcore_device.id
42 
43  async def async_added_to_hass(self):
44  """Register callbacks."""
45  self.async_on_removeasync_on_remove(
47  self.hasshass, SIGNAL_TELLCORE_CALLBACK, self.update_from_callbackupdate_from_callback
48  )
49  )
50 
51  @property
52  def is_on(self):
53  """Return true if the device is on."""
54  return self._state_state
55 
56  def _parse_ha_data(self, kwargs):
57  """Turn the value from HA into something useful."""
58  raise NotImplementedError
59 
60  def _parse_tellcore_data(self, tellcore_data):
61  """Turn the value received from tellcore into something useful."""
62  raise NotImplementedError
63 
64  def _update_model(self, new_state, data):
65  """Update the device entity state to match the arguments."""
66  raise NotImplementedError
67 
68  def _send_device_command(self, requested_state, requested_data):
69  """Let tellcore update the actual device to the requested state."""
70  raise NotImplementedError
71 
73  """Send a tellstick command once and decrease the repeat count."""
74 
75  with TELLSTICK_LOCK:
76  if self._repeats_left_repeats_left > 0:
77  self._repeats_left_repeats_left -= 1
78  try:
79  self._send_device_command_send_device_command(
80  self._requested_state_requested_state, self._requested_data_requested_data
81  )
82  except TelldusError as err:
83  _LOGGER.error(err)
84 
85  def _change_device_state(self, new_state, data):
86  """Turn on or off the device."""
87  with TELLSTICK_LOCK:
88  # Set the requested state and number of repeats before calling
89  # _send_repeated_command the first time. Subsequent calls will be
90  # made from the callback. (We don't want to queue a lot of commands
91  # in case the user toggles the switch the other way before the
92  # queue is fully processed.)
93  self._requested_state_requested_state = new_state
94  self._requested_data_requested_data = data
95  self._repeats_left_repeats_left = self._signal_repetitions_signal_repetitions
96  self._send_repeated_command_send_repeated_command()
97 
98  # Sooner or later this will propagate to the model from the
99  # callback, but for a fluid UI experience update it directly.
100  self._update_model_update_model(new_state, data)
101  self.schedule_update_ha_stateschedule_update_ha_state()
102 
103  def turn_on(self, **kwargs):
104  """Turn the switch on."""
105  self._change_device_state_change_device_state(True, self._parse_ha_data_parse_ha_data(kwargs))
106 
107  def turn_off(self, **kwargs):
108  """Turn the switch off."""
109  self._change_device_state_change_device_state(False, None)
110 
111  def _update_model_from_command(self, tellcore_command, tellcore_data):
112  """Update the model, from a sent tellcore command and data."""
113 
114  if tellcore_command not in [TELLSTICK_TURNON, TELLSTICK_TURNOFF, TELLSTICK_DIM]:
115  _LOGGER.debug("Unhandled tellstick command: %d", tellcore_command)
116  return
117 
118  self._update_model_update_model(
119  tellcore_command != TELLSTICK_TURNOFF,
120  self._parse_tellcore_data_parse_tellcore_data(tellcore_data),
121  )
122 
123  def update_from_callback(self, tellcore_id, tellcore_command, tellcore_data):
124  """Handle updates from the tellcore callback."""
125  if tellcore_id != self._tellcore_device_tellcore_device.id:
126  return
127 
128  self._update_model_from_command_update_model_from_command(tellcore_command, tellcore_data)
129  self.schedule_update_ha_stateschedule_update_ha_state()
130 
131  # This is a benign race on _repeats_left -- it's checked with the lock
132  # in _send_repeated_command.
133  if self._repeats_left_repeats_left > 0:
134  self._send_repeated_command_send_repeated_command()
135 
137  """Read the current state of the device from the tellcore library."""
138 
139  with TELLSTICK_LOCK:
140  try:
141  last_command = self._tellcore_device_tellcore_device.last_sent_command(
142  TELLSTICK_TURNON | TELLSTICK_TURNOFF | TELLSTICK_DIM
143  )
144  last_data = self._tellcore_device_tellcore_device.last_sent_value()
145  self._update_model_from_command_update_model_from_command(last_command, last_data)
146  except TelldusError as err:
147  _LOGGER.error(err)
148 
149  def update(self):
150  """Poll the current state of the device."""
151  self._update_from_tellcore_update_from_tellcore()
def __init__(self, tellcore_device, signal_repetitions)
Definition: entity.py:30
def _send_device_command(self, requested_state, requested_data)
Definition: entity.py:68
def _update_model_from_command(self, tellcore_command, tellcore_data)
Definition: entity.py:111
def update_from_callback(self, tellcore_id, tellcore_command, tellcore_data)
Definition: entity.py:123
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103