2020-02-20 23:29:46 +00:00
|
|
|
"""Support for Somfy hubs."""
|
2019-10-18 20:06:33 +00:00
|
|
|
import asyncio
|
2019-06-11 15:45:34 +00:00
|
|
|
from datetime import timedelta
|
2019-12-09 11:07:10 +00:00
|
|
|
import logging
|
2019-06-11 15:45:34 +00:00
|
|
|
|
2019-11-06 18:55:56 +00:00
|
|
|
from requests import HTTPError
|
2019-12-09 11:07:10 +00:00
|
|
|
import voluptuous as vol
|
2019-06-11 15:45:34 +00:00
|
|
|
|
|
|
|
from homeassistant.components.somfy import config_flow
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2019-12-09 11:07:10 +00:00
|
|
|
from homeassistant.helpers import config_entry_oauth2_flow, config_validation as cv
|
2019-06-11 15:45:34 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
|
|
|
from homeassistant.util import Throttle
|
|
|
|
|
2019-10-18 20:06:33 +00:00
|
|
|
from . import api
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
API = "api"
|
2019-06-11 15:45:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEVICES = "devices"
|
2019-06-11 15:45:34 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-12-05 20:33:56 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=30)
|
2019-06-11 15:45:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN = "somfy"
|
2019-06-11 15:45:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_CLIENT_ID = "client_id"
|
|
|
|
CONF_CLIENT_SECRET = "client_secret"
|
2020-03-19 23:09:14 +00:00
|
|
|
CONF_OPTIMISTIC = "optimistic"
|
2019-06-11 15:45:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SOMFY_AUTH_CALLBACK_PATH = "/auth/somfy/callback"
|
|
|
|
SOMFY_AUTH_START = "/auth/somfy"
|
2019-06-11 15:45:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
2020-03-19 23:09:14 +00:00
|
|
|
vol.Inclusive(CONF_CLIENT_ID, "oauth"): cv.string,
|
|
|
|
vol.Inclusive(CONF_CLIENT_SECRET, "oauth"): cv.string,
|
2020-03-01 15:42:26 +00:00
|
|
|
vol.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2019-06-11 15:45:34 +00:00
|
|
|
|
2019-11-28 09:42:17 +00:00
|
|
|
SOMFY_COMPONENTS = ["cover", "switch"]
|
2019-06-11 15:45:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup(hass, config):
|
|
|
|
"""Set up the Somfy component."""
|
|
|
|
hass.data[DOMAIN] = {}
|
2020-03-19 23:09:14 +00:00
|
|
|
domain_config = config.get(DOMAIN, {})
|
|
|
|
hass.data[DOMAIN][CONF_OPTIMISTIC] = domain_config.get(CONF_OPTIMISTIC, False)
|
2019-06-11 15:45:34 +00:00
|
|
|
|
2020-03-19 23:09:14 +00:00
|
|
|
if CONF_CLIENT_ID in domain_config:
|
|
|
|
config_flow.SomfyFlowHandler.async_register_implementation(
|
2019-10-18 20:06:33 +00:00
|
|
|
hass,
|
2020-03-19 23:09:14 +00:00
|
|
|
config_entry_oauth2_flow.LocalOAuth2Implementation(
|
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
config[DOMAIN][CONF_CLIENT_ID],
|
|
|
|
config[DOMAIN][CONF_CLIENT_SECRET],
|
|
|
|
"https://accounts.somfy.com/oauth/oauth/v2/auth",
|
|
|
|
"https://accounts.somfy.com/oauth/oauth/v2/token",
|
|
|
|
),
|
|
|
|
)
|
2019-06-11 15:45:34 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
|
|
|
|
"""Set up Somfy from a config entry."""
|
2019-10-18 20:06:33 +00:00
|
|
|
# Backwards compat
|
|
|
|
if "auth_implementation" not in entry.data:
|
|
|
|
hass.config_entries.async_update_entry(
|
|
|
|
entry, data={**entry.data, "auth_implementation": DOMAIN}
|
2019-06-11 15:45:34 +00:00
|
|
|
)
|
|
|
|
|
2019-10-18 20:06:33 +00:00
|
|
|
implementation = await config_entry_oauth2_flow.async_get_config_entry_implementation(
|
|
|
|
hass, entry
|
2019-06-11 15:45:34 +00:00
|
|
|
)
|
|
|
|
|
2019-10-18 20:06:33 +00:00
|
|
|
hass.data[DOMAIN][API] = api.ConfigEntrySomfyApi(hass, entry, implementation)
|
|
|
|
|
2019-06-11 15:45:34 +00:00
|
|
|
await update_all_devices(hass)
|
|
|
|
|
|
|
|
for component in SOMFY_COMPONENTS:
|
|
|
|
hass.async_create_task(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.config_entries.async_forward_entry_setup(entry, component)
|
|
|
|
)
|
2019-06-11 15:45:34 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry):
|
|
|
|
"""Unload a config entry."""
|
|
|
|
hass.data[DOMAIN].pop(API, None)
|
2019-10-18 20:06:33 +00:00
|
|
|
await asyncio.gather(
|
|
|
|
*[
|
|
|
|
hass.config_entries.async_forward_entry_unload(entry, component)
|
|
|
|
for component in SOMFY_COMPONENTS
|
|
|
|
]
|
|
|
|
)
|
2019-06-11 15:45:34 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class SomfyEntity(Entity):
|
|
|
|
"""Representation of a generic Somfy device."""
|
|
|
|
|
2019-10-18 20:06:33 +00:00
|
|
|
def __init__(self, device, somfy_api):
|
2019-06-11 15:45:34 +00:00
|
|
|
"""Initialize the Somfy device."""
|
|
|
|
self.device = device
|
2019-10-18 20:06:33 +00:00
|
|
|
self.api = somfy_api
|
2019-06-11 15:45:34 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the unique id base on the id returned by Somfy."""
|
|
|
|
return self.device.id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device."""
|
|
|
|
return self.device.name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return device specific attributes.
|
|
|
|
|
|
|
|
Implemented by platform classes.
|
|
|
|
"""
|
|
|
|
return {
|
2019-07-31 19:25:30 +00:00
|
|
|
"identifiers": {(DOMAIN, self.unique_id)},
|
|
|
|
"name": self.name,
|
|
|
|
"model": self.device.type,
|
|
|
|
"via_hub": (DOMAIN, self.device.site_id),
|
2019-06-11 15:45:34 +00:00
|
|
|
# For the moment, Somfy only returns their own device.
|
2019-07-31 19:25:30 +00:00
|
|
|
"manufacturer": "Somfy",
|
2019-06-11 15:45:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Update the device with the latest data."""
|
|
|
|
await update_all_devices(self.hass)
|
|
|
|
devices = self.hass.data[DOMAIN][DEVICES]
|
2019-07-31 19:25:30 +00:00
|
|
|
self.device = next((d for d in devices if d.id == self.device.id), self.device)
|
2019-06-11 15:45:34 +00:00
|
|
|
|
|
|
|
def has_capability(self, capability):
|
|
|
|
"""Test if device has a capability."""
|
|
|
|
capabilities = self.device.capabilities
|
|
|
|
return bool([c for c in capabilities if c.name == capability])
|
|
|
|
|
|
|
|
|
2019-07-17 15:09:46 +00:00
|
|
|
@Throttle(SCAN_INTERVAL)
|
2019-06-11 15:45:34 +00:00
|
|
|
async def update_all_devices(hass):
|
|
|
|
"""Update all the devices."""
|
|
|
|
try:
|
|
|
|
data = hass.data[DOMAIN]
|
2019-07-31 19:25:30 +00:00
|
|
|
data[DEVICES] = await hass.async_add_executor_job(data[API].get_devices)
|
2019-11-06 18:55:56 +00:00
|
|
|
except HTTPError as err:
|
|
|
|
_LOGGER.warning("Cannot update devices: %s", err.response.status_code)
|