Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for AquaLogic devices."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 import threading
8 import time
9 
10 from aqualogic.core import AquaLogic
11 import voluptuous as vol
12 
13 from homeassistant.const import (
14  CONF_HOST,
15  CONF_PORT,
16  EVENT_HOMEASSISTANT_START,
17  EVENT_HOMEASSISTANT_STOP,
18 )
19 from homeassistant.core import Event, HomeAssistant
20 from homeassistant.helpers import config_validation as cv
21 from homeassistant.helpers.dispatcher import dispatcher_send
22 from homeassistant.helpers.typing import ConfigType
23 
24 _LOGGER = logging.getLogger(__name__)
25 
26 DOMAIN = "aqualogic"
27 UPDATE_TOPIC = f"{DOMAIN}_update"
28 CONF_UNIT = "unit"
29 RECONNECT_INTERVAL = timedelta(seconds=10)
30 
31 CONFIG_SCHEMA = vol.Schema(
32  {
33  DOMAIN: vol.Schema(
34  {vol.Required(CONF_HOST): cv.string, vol.Required(CONF_PORT): cv.port}
35  )
36  },
37  extra=vol.ALLOW_EXTRA,
38 )
39 
40 
41 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
42  """Set up AquaLogic platform."""
43  host = config[DOMAIN][CONF_HOST]
44  port = config[DOMAIN][CONF_PORT]
45  processor = AquaLogicProcessor(hass, host, port)
46  hass.data[DOMAIN] = processor
47  hass.bus.listen_once(EVENT_HOMEASSISTANT_START, processor.start_listen)
48  hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, processor.shutdown)
49  _LOGGER.debug("AquaLogicProcessor %s:%i initialized", host, port)
50  return True
51 
52 
53 class AquaLogicProcessor(threading.Thread):
54  """AquaLogic event processor thread."""
55 
56  def __init__(self, hass: HomeAssistant, host: str, port: int) -> None:
57  """Initialize the data object."""
58  super().__init__(daemon=True)
59  self._hass_hass = hass
60  self._host_host = host
61  self._port_port = port
62  self._shutdown_shutdown = False
63  self._panel_panel = None
64 
65  def start_listen(self, event: Event) -> None:
66  """Start event-processing thread."""
67  _LOGGER.debug("Event processing thread started")
68  self.start()
69 
70  def shutdown(self, event: Event) -> None:
71  """Signal shutdown of processing event."""
72  _LOGGER.debug("Event processing signaled exit")
73  self._shutdown_shutdown = True
74 
75  def data_changed(self, panel: AquaLogic) -> None:
76  """Aqualogic data changed callback."""
77  dispatcher_send(self._hass_hass, UPDATE_TOPIC)
78 
79  def run(self) -> None:
80  """Event thread."""
81 
82  while True:
83  panel = AquaLogic()
84  self._panel_panel = panel
85  panel.connect(self._host_host, self._port_port)
86  panel.process(self.data_changeddata_changed)
87 
88  if self._shutdown_shutdown:
89  return
90 
91  _LOGGER.error("Connection to %s:%d lost", self._host_host, self._port_port)
92  time.sleep(RECONNECT_INTERVAL.total_seconds())
93 
94  @property
95  def panel(self) -> AquaLogic | None:
96  """Retrieve the AquaLogic object."""
97  return self._panel_panel
None __init__(self, HomeAssistant hass, str host, int port)
Definition: __init__.py:56
bool setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:41
None dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:137