2021-03-16 23:32:02 +00:00
|
|
|
"""The Screenlogic integration."""
|
|
|
|
import asyncio
|
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from screenlogicpy import ScreenLogicError, ScreenLogicGateway
|
|
|
|
from screenlogicpy.const import (
|
2021-03-21 10:56:46 +00:00
|
|
|
EQUIPMENT,
|
2021-03-16 23:32:02 +00:00
|
|
|
SL_GATEWAY_IP,
|
|
|
|
SL_GATEWAY_NAME,
|
|
|
|
SL_GATEWAY_PORT,
|
|
|
|
)
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import CONF_IP_ADDRESS, CONF_PORT, CONF_SCAN_INTERVAL
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
|
|
|
from homeassistant.helpers import device_registry as dr
|
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
UpdateFailed,
|
|
|
|
)
|
|
|
|
|
|
|
|
from .config_flow import async_discover_gateways_by_unique_id, name_for_mac
|
|
|
|
from .const import DEFAULT_SCAN_INTERVAL, DISCOVERED_GATEWAYS, DOMAIN
|
2021-04-21 18:45:50 +00:00
|
|
|
from .services import async_load_screenlogic_services, async_unload_screenlogic_services
|
2021-03-16 23:32:02 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-03-21 10:56:46 +00:00
|
|
|
PLATFORMS = ["switch", "sensor", "binary_sensor", "climate"]
|
2021-03-16 23:32:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup(hass: HomeAssistant, config: dict):
|
|
|
|
"""Set up the Screenlogic component."""
|
|
|
|
domain_data = hass.data[DOMAIN] = {}
|
|
|
|
domain_data[DISCOVERED_GATEWAYS] = await async_discover_gateways_by_unique_id(hass)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""Set up Screenlogic from a config entry."""
|
|
|
|
mac = entry.unique_id
|
|
|
|
# Attempt to re-discover named gateway to follow IP changes
|
|
|
|
discovered_gateways = hass.data[DOMAIN][DISCOVERED_GATEWAYS]
|
|
|
|
if mac in discovered_gateways:
|
|
|
|
connect_info = discovered_gateways[mac]
|
|
|
|
else:
|
2021-03-19 14:26:36 +00:00
|
|
|
_LOGGER.warning("Gateway rediscovery failed")
|
2021-03-16 23:32:02 +00:00
|
|
|
# Static connection defined or fallback from discovery
|
|
|
|
connect_info = {
|
|
|
|
SL_GATEWAY_NAME: name_for_mac(mac),
|
|
|
|
SL_GATEWAY_IP: entry.data[CONF_IP_ADDRESS],
|
|
|
|
SL_GATEWAY_PORT: entry.data[CONF_PORT],
|
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
|
|
|
gateway = ScreenLogicGateway(**connect_info)
|
|
|
|
except ScreenLogicError as ex:
|
|
|
|
_LOGGER.error("Error while connecting to the gateway %s: %s", connect_info, ex)
|
|
|
|
raise ConfigEntryNotReady from ex
|
|
|
|
|
2021-03-29 23:27:17 +00:00
|
|
|
# The api library uses a shared socket connection and does not handle concurrent
|
|
|
|
# requests very well.
|
|
|
|
api_lock = asyncio.Lock()
|
|
|
|
|
2021-03-16 23:32:02 +00:00
|
|
|
coordinator = ScreenlogicDataUpdateCoordinator(
|
2021-03-29 23:27:17 +00:00
|
|
|
hass, config_entry=entry, gateway=gateway, api_lock=api_lock
|
2021-03-16 23:32:02 +00:00
|
|
|
)
|
|
|
|
|
2021-04-21 18:45:50 +00:00
|
|
|
async_load_screenlogic_services(hass)
|
2021-03-16 23:32:02 +00:00
|
|
|
|
2021-03-29 22:51:39 +00:00
|
|
|
await coordinator.async_config_entry_first_refresh()
|
2021-03-16 23:32:02 +00:00
|
|
|
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = {
|
|
|
|
"coordinator": coordinator,
|
|
|
|
"listener": entry.add_update_listener(async_update_listener),
|
|
|
|
}
|
|
|
|
|
2021-04-27 20:10:04 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
2021-03-16 23:32:02 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""Unload a config entry."""
|
2021-04-27 20:10:04 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2021-03-16 23:32:02 +00:00
|
|
|
hass.data[DOMAIN][entry.entry_id]["listener"]()
|
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
|
2021-04-21 18:45:50 +00:00
|
|
|
async_unload_screenlogic_services(hass)
|
|
|
|
|
2021-03-16 23:32:02 +00:00
|
|
|
return unload_ok
|
|
|
|
|
|
|
|
|
|
|
|
async def async_update_listener(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""Handle options update."""
|
|
|
|
await hass.config_entries.async_reload(entry.entry_id)
|
|
|
|
|
|
|
|
|
|
|
|
class ScreenlogicDataUpdateCoordinator(DataUpdateCoordinator):
|
|
|
|
"""Class to manage the data update for the Screenlogic component."""
|
|
|
|
|
2021-03-29 23:27:17 +00:00
|
|
|
def __init__(self, hass, *, config_entry, gateway, api_lock):
|
2021-03-16 23:32:02 +00:00
|
|
|
"""Initialize the Screenlogic Data Update Coordinator."""
|
|
|
|
self.config_entry = config_entry
|
|
|
|
self.gateway = gateway
|
2021-03-29 23:27:17 +00:00
|
|
|
self.api_lock = api_lock
|
2021-03-16 23:32:02 +00:00
|
|
|
self.screenlogic_data = {}
|
2021-04-21 18:45:50 +00:00
|
|
|
|
2021-03-16 23:32:02 +00:00
|
|
|
interval = timedelta(
|
|
|
|
seconds=config_entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
|
|
|
|
)
|
|
|
|
super().__init__(
|
|
|
|
hass,
|
|
|
|
_LOGGER,
|
|
|
|
name=DOMAIN,
|
|
|
|
update_interval=interval,
|
|
|
|
)
|
|
|
|
|
|
|
|
async def _async_update_data(self):
|
|
|
|
"""Fetch data from the Screenlogic gateway."""
|
|
|
|
try:
|
2021-03-29 23:27:17 +00:00
|
|
|
async with self.api_lock:
|
|
|
|
await self.hass.async_add_executor_job(self.gateway.update)
|
2021-03-16 23:32:02 +00:00
|
|
|
except ScreenLogicError as error:
|
|
|
|
raise UpdateFailed(error) from error
|
2021-03-21 10:56:46 +00:00
|
|
|
return self.gateway.get_data()
|
2021-03-16 23:32:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ScreenlogicEntity(CoordinatorEntity):
|
|
|
|
"""Base class for all ScreenLogic entities."""
|
|
|
|
|
2021-03-21 10:56:46 +00:00
|
|
|
def __init__(self, coordinator, data_key):
|
2021-03-16 23:32:02 +00:00
|
|
|
"""Initialize of the entity."""
|
|
|
|
super().__init__(coordinator)
|
2021-03-21 10:56:46 +00:00
|
|
|
self._data_key = data_key
|
2021-03-16 23:32:02 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def mac(self):
|
|
|
|
"""Mac address."""
|
|
|
|
return self.coordinator.config_entry.unique_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Entity Unique ID."""
|
|
|
|
return f"{self.mac}_{self._data_key}"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def config_data(self):
|
|
|
|
"""Shortcut for config data."""
|
|
|
|
return self.coordinator.data["config"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def gateway(self):
|
|
|
|
"""Return the gateway."""
|
|
|
|
return self.coordinator.gateway
|
|
|
|
|
|
|
|
@property
|
|
|
|
def gateway_name(self):
|
|
|
|
"""Return the configured name of the gateway."""
|
|
|
|
return self.gateway.name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return device information for the controller."""
|
2021-03-21 10:56:46 +00:00
|
|
|
controller_type = self.config_data["controller_type"]
|
2021-03-16 23:32:02 +00:00
|
|
|
hardware_type = self.config_data["hardware_type"]
|
2021-04-11 22:12:59 +00:00
|
|
|
try:
|
|
|
|
equipment_model = EQUIPMENT.CONTROLLER_HARDWARE[controller_type][
|
|
|
|
hardware_type
|
|
|
|
]
|
|
|
|
except KeyError:
|
|
|
|
equipment_model = f"Unknown Model C:{controller_type} H:{hardware_type}"
|
2021-03-16 23:32:02 +00:00
|
|
|
return {
|
|
|
|
"connections": {(dr.CONNECTION_NETWORK_MAC, self.mac)},
|
|
|
|
"name": self.gateway_name,
|
|
|
|
"manufacturer": "Pentair",
|
2021-04-11 22:12:59 +00:00
|
|
|
"model": equipment_model,
|
2021-03-16 23:32:02 +00:00
|
|
|
}
|