2019-02-13 20:21:14 +00:00
|
|
|
"""Support for VELUX KLF 200 devices."""
|
2017-07-08 14:12:19 +00:00
|
|
|
import logging
|
2019-11-25 23:36:37 +00:00
|
|
|
|
|
|
|
from pyvlx import PyVLX, PyVLXException
|
2017-07-08 14:12:19 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-11-25 23:36:37 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, EVENT_HOMEASSISTANT_STOP
|
2017-07-08 14:12:19 +00:00
|
|
|
from homeassistant.helpers import discovery
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
|
|
|
DOMAIN = "velux"
|
|
|
|
DATA_VELUX = "data_velux"
|
2021-03-02 20:43:59 +00:00
|
|
|
PLATFORMS = ["cover", "scene"]
|
2017-07-08 14:12:19 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{vol.Required(CONF_HOST): cv.string, vol.Required(CONF_PASSWORD): cv.string}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2017-07-08 14:12:19 +00:00
|
|
|
|
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async def async_setup(hass, config):
|
2017-07-08 14:12:19 +00:00
|
|
|
"""Set up the velux component."""
|
|
|
|
try:
|
2019-09-17 18:22:39 +00:00
|
|
|
hass.data[DATA_VELUX] = VeluxModule(hass, config[DOMAIN])
|
|
|
|
hass.data[DATA_VELUX].setup()
|
2018-02-24 18:24:33 +00:00
|
|
|
await hass.data[DATA_VELUX].async_start()
|
2017-07-08 14:12:19 +00:00
|
|
|
|
|
|
|
except PyVLXException as ex:
|
|
|
|
_LOGGER.exception("Can't connect to velux interface: %s", ex)
|
|
|
|
return False
|
|
|
|
|
2021-03-02 20:43:59 +00:00
|
|
|
for platform in PLATFORMS:
|
2018-07-23 12:05:38 +00:00
|
|
|
hass.async_create_task(
|
2021-03-02 20:43:59 +00:00
|
|
|
discovery.async_load_platform(hass, platform, DOMAIN, {}, config)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-07-08 14:12:19 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class VeluxModule:
|
|
|
|
"""Abstraction for velux component."""
|
|
|
|
|
2019-09-17 18:22:39 +00:00
|
|
|
def __init__(self, hass, domain_config):
|
2017-07-08 14:12:19 +00:00
|
|
|
"""Initialize for velux component."""
|
2019-09-17 18:22:39 +00:00
|
|
|
self.pyvlx = None
|
|
|
|
self._hass = hass
|
|
|
|
self._domain_config = domain_config
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
"""Velux component setup."""
|
|
|
|
|
|
|
|
async def on_hass_stop(event):
|
|
|
|
"""Close connection when hass stops."""
|
|
|
|
_LOGGER.debug("Velux interface terminated")
|
|
|
|
await self.pyvlx.disconnect()
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2021-01-29 19:07:58 +00:00
|
|
|
async def async_reboot_gateway(service_call):
|
|
|
|
await self.pyvlx.reboot_gateway()
|
|
|
|
|
2019-09-17 18:22:39 +00:00
|
|
|
self._hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, on_hass_stop)
|
|
|
|
host = self._domain_config.get(CONF_HOST)
|
|
|
|
password = self._domain_config.get(CONF_PASSWORD)
|
2019-07-31 19:25:30 +00:00
|
|
|
self.pyvlx = PyVLX(host=host, password=password)
|
2017-07-08 14:12:19 +00:00
|
|
|
|
2021-01-29 19:07:58 +00:00
|
|
|
self._hass.services.async_register(
|
|
|
|
DOMAIN, "reboot_gateway", async_reboot_gateway
|
|
|
|
)
|
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async def async_start(self):
|
2017-07-08 14:12:19 +00:00
|
|
|
"""Start velux component."""
|
2019-09-17 18:22:39 +00:00
|
|
|
_LOGGER.debug("Velux interface started")
|
2018-02-24 18:24:33 +00:00
|
|
|
await self.pyvlx.load_scenes()
|
2019-02-01 01:13:47 +00:00
|
|
|
await self.pyvlx.load_nodes()
|