* CREATE Somfy component * CREATE cover Somfy platform * USE somfy id as unique id * UPDATE all the devices in one call to limit the number of call * FIX Don't load devices if not yet configured * IMP Replace configurator by a simple notification * ADD log in case state does not match * IMP wording * REMOVE debug stuf * ADD support for tilt position * UPDATE requirements * FIX Use code instead of authorization response - Will allow to setup Somfy without https * HANDLE stateless devices (Somfy RTS) * FIX import locally 3rd party library * UPDATE pymfy to 0.4.3 * ADD missing docstring * FIX For Somfy 100 means closed and 0 opened * FIX position can be None * ENHANCE error management when error 500 occurs at setup * FIX indent * ROLLBACK tilt modification - See https://community.home-assistant.io/t/somfy-tahoma-official-api/61448/90?u=tetienne * FIX Look for capability instead of state * DON'T use exception to test if a feature is available * UPDATE dependency * ADD device_info property * AVOID object creation in each method * REMOVE unused constants * ADD missing doc * IMP Only make one call to add_entities * USE dict[key] instead of get method * IMP Don't pass hass object to the entities * FIX Don't end logging messages with period * USE config entries instead of a cache file * IMPLEMENT async_unload_entry * CONSOLIDATE package - see home-assistant/architecture#124 * UPDATE to pymfy 0.5.1 * SIMPLIFY config flow * ADD French translation * FIX 80 vs 79 max length * ABORT flow asap * FIX A tupple was returned * MIGRATE to manifest.json * ADD a placeholder async_setup_platform coroutine - It's currently required and expected by the platform helper. * FIX codeowner * ADD missing translations file * USE new external step * UPGRADE pymfy version * Close Somfy tab automatically * ADD manufacturer - Somfy only for the moment. * HANDLE missing code or state in Somfy request * REMOVE unused strings * DECLARE somfy component to use config_flow * APPLY static check remarks * FIX async method cannot be called from sync context * FIX only unload what has been loaded during entry setup * DON't catch them all * DON'T log full stacktrace * ABORT conflig flow if configuration missing * OMIT Somfy files for coverage * ADD tests about Somfy config flow * ADD pymfy to the test dependencies
161 lines
4.4 KiB
Python
161 lines
4.4 KiB
Python
"""
|
|
Support for Somfy hubs.
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
https://home-assistant.io/components/somfy/
|
|
"""
|
|
import logging
|
|
from datetime import timedelta
|
|
from functools import partial
|
|
|
|
import voluptuous as vol
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
from homeassistant import config_entries
|
|
from homeassistant.components.somfy import config_flow
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import CONF_TOKEN
|
|
from homeassistant.helpers.entity import Entity
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
|
from homeassistant.util import Throttle
|
|
|
|
API = 'api'
|
|
|
|
DEVICES = 'devices'
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=10)
|
|
|
|
DOMAIN = 'somfy'
|
|
|
|
CONF_CLIENT_ID = 'client_id'
|
|
CONF_CLIENT_SECRET = 'client_secret'
|
|
|
|
SOMFY_AUTH_CALLBACK_PATH = '/auth/somfy/callback'
|
|
SOMFY_AUTH_START = '/auth/somfy'
|
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
DOMAIN: vol.Schema({
|
|
vol.Required(CONF_CLIENT_ID): cv.string,
|
|
vol.Required(CONF_CLIENT_SECRET): cv.string
|
|
})
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
SOMFY_COMPONENTS = ['cover']
|
|
|
|
|
|
async def async_setup(hass, config):
|
|
"""Set up the Somfy component."""
|
|
if DOMAIN not in config:
|
|
return True
|
|
|
|
hass.data[DOMAIN] = {}
|
|
|
|
config_flow.register_flow_implementation(
|
|
hass, config[DOMAIN][CONF_CLIENT_ID],
|
|
config[DOMAIN][CONF_CLIENT_SECRET])
|
|
|
|
hass.async_create_task(
|
|
hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
context={'source': config_entries.SOURCE_IMPORT},
|
|
))
|
|
|
|
return True
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
|
|
"""Set up Somfy from a config entry."""
|
|
def token_saver(token):
|
|
_LOGGER.debug('Saving updated token')
|
|
entry.data[CONF_TOKEN] = token
|
|
update_entry = partial(
|
|
hass.config_entries.async_update_entry,
|
|
data={**entry.data}
|
|
)
|
|
hass.add_job(update_entry, entry)
|
|
|
|
# Force token update.
|
|
from pymfy.api.somfy_api import SomfyApi
|
|
hass.data[DOMAIN][API] = SomfyApi(
|
|
entry.data['refresh_args']['client_id'],
|
|
entry.data['refresh_args']['client_secret'],
|
|
token=entry.data[CONF_TOKEN],
|
|
token_updater=token_saver
|
|
)
|
|
|
|
await update_all_devices(hass)
|
|
|
|
for component in SOMFY_COMPONENTS:
|
|
hass.async_create_task(
|
|
hass.config_entries.async_forward_entry_setup(entry, component))
|
|
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry):
|
|
"""Unload a config entry."""
|
|
hass.data[DOMAIN].pop(API, None)
|
|
return True
|
|
|
|
|
|
class SomfyEntity(Entity):
|
|
"""Representation of a generic Somfy device."""
|
|
|
|
def __init__(self, device, api):
|
|
"""Initialize the Somfy device."""
|
|
self.device = device
|
|
self.api = api
|
|
|
|
@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 {
|
|
'identifiers': {(DOMAIN, self.unique_id)},
|
|
'name': self.name,
|
|
'model': self.device.type,
|
|
'via_hub': (DOMAIN, self.device.site_id),
|
|
# For the moment, Somfy only returns their own device.
|
|
'manufacturer': 'Somfy'
|
|
}
|
|
|
|
async def async_update(self):
|
|
"""Update the device with the latest data."""
|
|
await update_all_devices(self.hass)
|
|
devices = self.hass.data[DOMAIN][DEVICES]
|
|
self.device = next((d for d in devices if d.id == self.device.id),
|
|
self.device)
|
|
|
|
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])
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
async def update_all_devices(hass):
|
|
"""Update all the devices."""
|
|
from requests import HTTPError
|
|
try:
|
|
data = hass.data[DOMAIN]
|
|
data[DEVICES] = await hass.async_add_executor_job(
|
|
data[API].get_devices)
|
|
except HTTPError:
|
|
_LOGGER.warning("Cannot update devices")
|
|
return False
|
|
return True
|