Disconnect velux on hass stop (#26266)

* velux KLF200 device did not disconnect properly when rebooting the hass device.
disconnect is now being called on the 'EVENT_HOMEASSISTANT_STOP' event

* removed comment

* removed comment

* trigger bot

* trigger bot

* trigger bot

* logging casing fixed. code moved from init.

* logger level debug

logger level moved from info to debug
only config[DOMAIN] exposed to module
imports moved to top

* DOMAIN part of config passed to module.

* removed trailing whitespaces etc.

* black --fast changes

* added missing docstring

* D400 First line should end with a period

* black formatting
This commit is contained in:
gibman 2019-09-17 20:22:39 +02:00 committed by Martin Hjelmare
parent 12f68af107
commit ed13cab8d6

View File

@ -1,11 +1,12 @@
"""Support for VELUX KLF 200 devices.""" """Support for VELUX KLF 200 devices."""
import logging import logging
import voluptuous as vol import voluptuous as vol
from pyvlx import PyVLX
from pyvlx import PyVLXException
from homeassistant.helpers import discovery from homeassistant.helpers import discovery
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.const import CONF_HOST, CONF_PASSWORD from homeassistant.const import CONF_HOST, CONF_PASSWORD, EVENT_HOMEASSISTANT_STOP
DOMAIN = "velux" DOMAIN = "velux"
DATA_VELUX = "data_velux" DATA_VELUX = "data_velux"
@ -24,10 +25,9 @@ CONFIG_SCHEMA = vol.Schema(
async def async_setup(hass, config): async def async_setup(hass, config):
"""Set up the velux component.""" """Set up the velux component."""
from pyvlx import PyVLXException
try: try:
hass.data[DATA_VELUX] = VeluxModule(hass, config) hass.data[DATA_VELUX] = VeluxModule(hass, config[DOMAIN])
hass.data[DATA_VELUX].setup()
await hass.data[DATA_VELUX].async_start() await hass.data[DATA_VELUX].async_start()
except PyVLXException as ex: except PyVLXException as ex:
@ -44,15 +44,27 @@ async def async_setup(hass, config):
class VeluxModule: class VeluxModule:
"""Abstraction for velux component.""" """Abstraction for velux component."""
def __init__(self, hass, config): def __init__(self, hass, domain_config):
"""Initialize for velux component.""" """Initialize for velux component."""
from pyvlx import PyVLX self.pyvlx = None
self._hass = hass
self._domain_config = domain_config
host = config[DOMAIN].get(CONF_HOST) def setup(self):
password = config[DOMAIN].get(CONF_PASSWORD) """Velux component setup."""
async def on_hass_stop(event):
"""Close connection when hass stops."""
_LOGGER.debug("Velux interface terminated")
await self.pyvlx.disconnect()
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)
self.pyvlx = PyVLX(host=host, password=password) self.pyvlx = PyVLX(host=host, password=password)
async def async_start(self): async def async_start(self):
"""Start velux component.""" """Start velux component."""
_LOGGER.debug("Velux interface started")
await self.pyvlx.load_scenes() await self.pyvlx.load_scenes()
await self.pyvlx.load_nodes() await self.pyvlx.load_nodes()