Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for w800rf32 devices."""
2 
3 import logging
4 
5 import voluptuous as vol
6 import W800rf32 as w800
7 
8 from homeassistant.const import (
9  CONF_DEVICE,
10  EVENT_HOMEASSISTANT_START,
11  EVENT_HOMEASSISTANT_STOP,
12 )
13 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.dispatcher import dispatcher_send
16 from homeassistant.helpers.typing import ConfigType
17 
18 DATA_W800RF32 = "data_w800rf32"
19 DOMAIN = "w800rf32"
20 
21 W800RF32_DEVICE = "w800rf32_{}"
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 CONFIG_SCHEMA = vol.Schema(
26  {DOMAIN: vol.Schema({vol.Required(CONF_DEVICE): cv.string})}, extra=vol.ALLOW_EXTRA
27 )
28 
29 
30 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
31  """Set up the w800rf32 component."""
32 
33  # Declare the Handle event
34  def handle_receive(event):
35  """Handle received messages from w800rf32 gateway."""
36  # Log event
37  if not event.device:
38  return
39  _LOGGER.debug("Receive W800rf32 event in handle_receive")
40 
41  # Get device_type from device_id in hass.data
42  device_id = event.device.lower()
43  signal = W800RF32_DEVICE.format(device_id)
44  dispatcher_send(hass, signal, event)
45 
46  # device --> /dev/ttyUSB0
47  device = config[DOMAIN][CONF_DEVICE]
48  w800_object = w800.Connect(device, None)
49 
50  def _start_w800rf32(event):
51  w800_object.event_callback = handle_receive
52 
53  hass.bus.listen_once(EVENT_HOMEASSISTANT_START, _start_w800rf32)
54 
55  def _shutdown_w800rf32(event):
56  """Close connection with w800rf32."""
57  w800_object.close_connection()
58 
59  hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, _shutdown_w800rf32)
60 
61  hass.data[DATA_W800RF32] = w800_object
62 
63  return True
bool setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:30
None dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:137