1 """Support for the MAX! Cube LAN Gateway."""
4 from threading
import Lock
7 from maxcube.cube
import MaxCube
8 import voluptuous
as vol
18 _LOGGER = logging.getLogger(__name__)
25 NOTIFICATION_ID =
"maxcube_notification"
26 NOTIFICATION_TITLE =
"Max!Cube gateway setup"
28 CONF_GATEWAYS =
"gateways"
30 CONFIG_GATEWAY = vol.Schema(
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,
38 CONFIG_SCHEMA = vol.Schema(
42 vol.Required(CONF_GATEWAYS, default={}): vol.All(
43 cv.ensure_list, [CONFIG_GATEWAY]
48 extra=vol.ALLOW_EXTRA,
52 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
53 """Establish connection to MAX! Cube."""
55 if DATA_KEY
not in hass.data:
56 hass.data[DATA_KEY] = {}
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()
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(
73 f
"Error: {ex}<br />You will need to restart Home Assistant after"
76 title=NOTIFICATION_TITLE,
77 notification_id=NOTIFICATION_ID,
79 connection_failed += 1
81 if connection_failed >= len(gateways):
85 load_platform(hass, Platform.BINARY_SENSOR, DOMAIN, {}, config)
91 """Keep the cube instance in one place and centralize the update."""
94 """Initialize the Cube Handle."""
96 self.
cubecube.use_persistent_connection = scan_interval <= 300
102 """Pull the latest data from the MAX! Cube."""
104 with self.
mutexmutex:
107 _LOGGER.debug(
"Updating")
112 _LOGGER.error(
"Max!Cube connection failed")
115 self.
_updatets_updatets = time.monotonic()
117 _LOGGER.debug(
"Skipping update")
def __init__(self, cube, scan_interval)
bool setup(HomeAssistant hass, ConfigType config)
None load_platform(core.HomeAssistant hass, Platform|str component, str platform, DiscoveryInfoType|None discovered, ConfigType hass_config)