1 """Support for Tellstick."""
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
19 ATTR_DISCOVER_DEVICES,
21 DEFAULT_SIGNAL_REPETITIONS,
22 SIGNAL_TELLCORE_CALLBACK,
25 _LOGGER = logging.getLogger(__name__)
27 CONF_SIGNAL_REPETITIONS =
"signal_repetitions"
31 CONFIG_SCHEMA = vol.Schema(
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)
40 CONF_SIGNAL_REPETITIONS, default=DEFAULT_SIGNAL_REPETITIONS
45 extra=vol.ALLOW_EXTRA,
49 def _discover(hass, config, component_name, found_tellcore_devices):
50 """Set up and send the discovery event."""
51 if not found_tellcore_devices:
55 "Discovered %d new %s devices", len(found_tellcore_devices), component_name
58 signal_repetitions = config[DOMAIN].
get(CONF_SIGNAL_REPETITIONS)
60 discovery.load_platform(
65 ATTR_DISCOVER_DEVICES: found_tellcore_devices,
66 ATTR_DISCOVER_CONFIG: signal_repetitions,
72 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
73 """Set up the Tellstick component."""
75 conf = config.get(DOMAIN, {})
76 net_host = conf.get(CONF_HOST)
77 net_ports = conf.get(CONF_PORT)
81 net_client = TellCoreClient(
82 host=net_host, port_client=net_ports[0], port_events=net_ports[1]
86 def stop_tellcore_net(event):
87 """Event handler to stop the client."""
90 hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_tellcore_net)
93 tellcore_lib = TelldusCore(
94 callback_dispatcher=AsyncioCallbackDispatcher(hass.loop)
97 _LOGGER.exception(
"Could not initialize Tellstick")
101 tellcore_devices = tellcore_lib.devices()
104 hass.data[DATA_TELLSTICK] = {device.id: device
for device
in tellcore_devices}
111 [device.id
for device
in tellcore_devices
if device.methods(TELLSTICK_DIM)],
119 [device.id
for device
in tellcore_devices
if device.methods(TELLSTICK_UP)],
129 for device
in tellcore_devices
130 if (
not device.methods(TELLSTICK_UP)
and not device.methods(TELLSTICK_DIM))
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
142 callback_id = tellcore_lib.register_device_event(async_handle_callback)
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)
149 hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, clean_up_callback)
web.Response get(self, web.Request request, str config_key)
def _discover(hass, config, component_name, found_tellcore_devices)
bool setup(HomeAssistant hass, ConfigType config)
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)