2019-04-09 08:31:34 +02:00
|
|
|
"""Component for the Somfy MyLink device supporting the Synergy API."""
|
2021-01-10 20:24:22 +01:00
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
|
2019-12-05 06:17:45 +01:00
|
|
|
from somfy_mylink_synergy import SomfyMyLinkSynergy
|
2019-04-09 08:31:34 +02:00
|
|
|
|
2021-05-10 11:53:37 +02:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2019-04-09 08:31:34 +02:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_PORT
|
2021-05-10 11:53:37 +02:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-01-10 20:24:22 +01:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2019-04-09 08:31:34 +02:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
|
2021-05-10 11:53:37 +02:00
|
|
|
from .const import CONF_SYSTEM_ID, DATA_SOMFY_MYLINK, DOMAIN, MYLINK_STATUS, PLATFORMS
|
2019-04-09 08:31:34 +02:00
|
|
|
|
2021-05-10 11:53:37 +02:00
|
|
|
UNDO_UPDATE_LISTENER = "undo_update_listener"
|
2021-01-10 20:24:22 +01:00
|
|
|
|
2021-05-10 11:53:37 +02:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2021-01-10 20:24:22 +01:00
|
|
|
|
2021-12-21 12:46:10 +01:00
|
|
|
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
2021-01-10 20:24:22 +01:00
|
|
|
|
|
|
|
|
2021-05-27 17:39:06 +02:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-01-10 20:24:22 +01:00
|
|
|
"""Set up Somfy MyLink from a config entry."""
|
2021-05-10 11:53:37 +02:00
|
|
|
hass.data.setdefault(DOMAIN, {})
|
2021-01-10 20:24:22 +01:00
|
|
|
|
|
|
|
config = entry.data
|
|
|
|
somfy_mylink = SomfyMyLinkSynergy(
|
|
|
|
config[CONF_SYSTEM_ID], config[CONF_HOST], config[CONF_PORT]
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
mylink_status = await somfy_mylink.status_info()
|
|
|
|
except asyncio.TimeoutError as ex:
|
|
|
|
raise ConfigEntryNotReady(
|
|
|
|
"Unable to connect to the Somfy MyLink device, please check your settings"
|
|
|
|
) from ex
|
|
|
|
|
2021-01-11 21:10:02 +01:00
|
|
|
if not mylink_status or "error" in mylink_status:
|
2021-01-10 20:24:22 +01:00
|
|
|
_LOGGER.error(
|
2021-03-19 15:26:36 +01:00
|
|
|
"Somfy Mylink failed to setup because of an error: %s",
|
2021-01-11 21:10:02 +01:00
|
|
|
mylink_status.get("error", {}).get(
|
|
|
|
"message", "Empty response from mylink device"
|
|
|
|
),
|
2021-01-10 20:24:22 +01:00
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
2021-02-08 22:51:46 +01:00
|
|
|
if "result" not in mylink_status:
|
|
|
|
raise ConfigEntryNotReady("The Somfy MyLink device returned an empty result")
|
|
|
|
|
2021-01-10 20:24:22 +01:00
|
|
|
undo_listener = entry.add_update_listener(_async_update_listener)
|
|
|
|
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = {
|
|
|
|
DATA_SOMFY_MYLINK: somfy_mylink,
|
|
|
|
MYLINK_STATUS: mylink_status,
|
|
|
|
UNDO_UPDATE_LISTENER: undo_listener,
|
|
|
|
}
|
|
|
|
|
2022-07-09 17:27:42 +02:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2021-01-10 20:24:22 +01:00
|
|
|
|
2019-04-09 08:31:34 +02:00
|
|
|
return True
|
2021-01-10 20:24:22 +01:00
|
|
|
|
|
|
|
|
2022-02-02 10:36:04 +01:00
|
|
|
async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
2021-01-10 20:24:22 +01:00
|
|
|
"""Handle options update."""
|
|
|
|
await hass.config_entries.async_reload(entry.entry_id)
|
|
|
|
|
|
|
|
|
2021-10-06 10:48:11 +02:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-01-10 20:24:22 +01:00
|
|
|
"""Unload a config entry."""
|
2021-04-27 22:10:04 +02:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2021-01-10 20:24:22 +01:00
|
|
|
|
|
|
|
hass.data[DOMAIN][entry.entry_id][UNDO_UPDATE_LISTENER]()
|
|
|
|
|
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
|
|
|
|
return unload_ok
|