Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for the MAX! Cube LAN Gateway."""
2 
3 import logging
4 from threading import Lock
5 import time
6 
7 from maxcube.cube import MaxCube
8 import voluptuous as vol
9 
10 from homeassistant.components import persistent_notification
11 from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SCAN_INTERVAL, Platform
12 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.discovery import load_platform
15 from homeassistant.helpers.typing import ConfigType
16 from homeassistant.util.dt import now
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 DEFAULT_PORT = 62910
21 DOMAIN = "maxcube"
22 
23 DATA_KEY = "maxcube"
24 
25 NOTIFICATION_ID = "maxcube_notification"
26 NOTIFICATION_TITLE = "Max!Cube gateway setup"
27 
28 CONF_GATEWAYS = "gateways"
29 
30 CONFIG_GATEWAY = vol.Schema(
31  {
32  vol.Required(CONF_HOST): cv.string,
33  vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
34  vol.Optional(CONF_SCAN_INTERVAL, default=300): cv.time_period,
35  }
36 )
37 
38 CONFIG_SCHEMA = vol.Schema(
39  {
40  DOMAIN: vol.Schema(
41  {
42  vol.Required(CONF_GATEWAYS, default={}): vol.All(
43  cv.ensure_list, [CONFIG_GATEWAY]
44  )
45  }
46  )
47  },
48  extra=vol.ALLOW_EXTRA,
49 )
50 
51 
52 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
53  """Establish connection to MAX! Cube."""
54 
55  if DATA_KEY not in hass.data:
56  hass.data[DATA_KEY] = {}
57 
58  connection_failed = 0
59  gateways = config[DOMAIN][CONF_GATEWAYS]
60  for gateway in gateways:
61  host = gateway[CONF_HOST]
62  port = gateway[CONF_PORT]
63  scan_interval = gateway[CONF_SCAN_INTERVAL].total_seconds()
64 
65  try:
66  cube = MaxCube(host, port, now=now)
67  hass.data[DATA_KEY][host] = MaxCubeHandle(cube, scan_interval)
68  except TimeoutError as ex:
69  _LOGGER.error("Unable to connect to Max!Cube gateway: %s", str(ex))
70  persistent_notification.create(
71  hass,
72  (
73  f"Error: {ex}<br />You will need to restart Home Assistant after"
74  " fixing."
75  ),
76  title=NOTIFICATION_TITLE,
77  notification_id=NOTIFICATION_ID,
78  )
79  connection_failed += 1
80 
81  if connection_failed >= len(gateways):
82  return False
83 
84  load_platform(hass, Platform.CLIMATE, DOMAIN, {}, config)
85  load_platform(hass, Platform.BINARY_SENSOR, DOMAIN, {}, config)
86 
87  return True
88 
89 
91  """Keep the cube instance in one place and centralize the update."""
92 
93  def __init__(self, cube, scan_interval):
94  """Initialize the Cube Handle."""
95  self.cubecube = cube
96  self.cubecube.use_persistent_connection = scan_interval <= 300 # seconds
97  self.scan_intervalscan_interval = scan_interval
98  self.mutexmutex = Lock()
99  self._updatets_updatets = time.monotonic()
100 
101  def update(self) -> None:
102  """Pull the latest data from the MAX! Cube."""
103  # Acquire mutex to prevent simultaneous update from multiple threads
104  with self.mutexmutex:
105  # Only update every update_interval
106  if (time.monotonic() - self._updatets_updatets) >= self.scan_intervalscan_interval:
107  _LOGGER.debug("Updating")
108 
109  try:
110  self.cubecube.update()
111  except TimeoutError:
112  _LOGGER.error("Max!Cube connection failed")
113  return
114 
115  self._updatets_updatets = time.monotonic()
116  else:
117  _LOGGER.debug("Skipping update")
def __init__(self, cube, scan_interval)
Definition: __init__.py:93
bool setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:52
None load_platform(core.HomeAssistant hass, Platform|str component, str platform, DiscoveryInfoType|None discovered, ConfigType hass_config)
Definition: discovery.py:137