Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Tellstick."""
2 
3 import logging
4 
5 from tellcore.constants import TELLSTICK_DIM, TELLSTICK_UP
6 from tellcore.telldus import AsyncioCallbackDispatcher, TelldusCore
7 from tellcorenet import TellCoreClient
8 import voluptuous as vol
9 
10 from homeassistant.const import CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_STOP
11 from homeassistant.core import HomeAssistant, callback
12 from homeassistant.helpers import discovery
14 from homeassistant.helpers.dispatcher import async_dispatcher_send
15 from homeassistant.helpers.typing import ConfigType
16 
17 from .const import (
18  ATTR_DISCOVER_CONFIG,
19  ATTR_DISCOVER_DEVICES,
20  DATA_TELLSTICK,
21  DEFAULT_SIGNAL_REPETITIONS,
22  SIGNAL_TELLCORE_CALLBACK,
23 )
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 CONF_SIGNAL_REPETITIONS = "signal_repetitions"
28 
29 DOMAIN = "tellstick"
30 
31 CONFIG_SCHEMA = vol.Schema(
32  {
33  DOMAIN: vol.Schema(
34  {
35  vol.Inclusive(CONF_HOST, "tellcore-net"): cv.string,
36  vol.Inclusive(CONF_PORT, "tellcore-net"): vol.All(
37  cv.ensure_list, [cv.port], vol.Length(min=2, max=2)
38  ),
39  vol.Optional(
40  CONF_SIGNAL_REPETITIONS, default=DEFAULT_SIGNAL_REPETITIONS
41  ): vol.Coerce(int),
42  }
43  )
44  },
45  extra=vol.ALLOW_EXTRA,
46 )
47 
48 
49 def _discover(hass, config, component_name, found_tellcore_devices):
50  """Set up and send the discovery event."""
51  if not found_tellcore_devices:
52  return
53 
54  _LOGGER.debug(
55  "Discovered %d new %s devices", len(found_tellcore_devices), component_name
56  )
57 
58  signal_repetitions = config[DOMAIN].get(CONF_SIGNAL_REPETITIONS)
59 
60  discovery.load_platform(
61  hass,
62  component_name,
63  DOMAIN,
64  {
65  ATTR_DISCOVER_DEVICES: found_tellcore_devices,
66  ATTR_DISCOVER_CONFIG: signal_repetitions,
67  },
68  config,
69  )
70 
71 
72 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
73  """Set up the Tellstick component."""
74 
75  conf = config.get(DOMAIN, {})
76  net_host = conf.get(CONF_HOST)
77  net_ports = conf.get(CONF_PORT)
78 
79  # Initialize remote tellcore client
80  if net_host:
81  net_client = TellCoreClient(
82  host=net_host, port_client=net_ports[0], port_events=net_ports[1]
83  )
84  net_client.start()
85 
86  def stop_tellcore_net(event):
87  """Event handler to stop the client."""
88  net_client.stop()
89 
90  hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_tellcore_net)
91 
92  try:
93  tellcore_lib = TelldusCore(
94  callback_dispatcher=AsyncioCallbackDispatcher(hass.loop)
95  )
96  except OSError:
97  _LOGGER.exception("Could not initialize Tellstick")
98  return False
99 
100  # Get all devices, switches and lights alike
101  tellcore_devices = tellcore_lib.devices()
102 
103  # Register devices
104  hass.data[DATA_TELLSTICK] = {device.id: device for device in tellcore_devices}
105 
106  # Discover the lights
107  _discover(
108  hass,
109  config,
110  "light",
111  [device.id for device in tellcore_devices if device.methods(TELLSTICK_DIM)],
112  )
113 
114  # Discover the cover
115  _discover(
116  hass,
117  config,
118  "cover",
119  [device.id for device in tellcore_devices if device.methods(TELLSTICK_UP)],
120  )
121 
122  # Discover the switches
123  _discover(
124  hass,
125  config,
126  "switch",
127  [
128  device.id
129  for device in tellcore_devices
130  if (not device.methods(TELLSTICK_UP) and not device.methods(TELLSTICK_DIM))
131  ],
132  )
133 
134  @callback
135  def async_handle_callback(tellcore_id, tellcore_command, tellcore_data, cid):
136  """Handle the actual callback from Tellcore."""
138  hass, SIGNAL_TELLCORE_CALLBACK, tellcore_id, tellcore_command, tellcore_data
139  )
140 
141  # Register callback
142  callback_id = tellcore_lib.register_device_event(async_handle_callback)
143 
144  def clean_up_callback(event):
145  """Unregister the callback bindings."""
146  if callback_id is not None:
147  tellcore_lib.unregister_callback(callback_id)
148 
149  hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, clean_up_callback)
150 
151  return True
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
def _discover(hass, config, component_name, found_tellcore_devices)
Definition: __init__.py:49
bool setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:72
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:193