From 15ae970941b2b68c407ed59adedc7d4881a2fa2d Mon Sep 17 00:00:00 2001 From: Maikel Punie Date: Tue, 30 Jul 2019 12:56:40 +0200 Subject: [PATCH] Make the velbus component more robust in handling errors (#25567) * Add some try excepts for velbus as the python-velbus lib is not good in handling these * Only catch velbusExceptions * only wrap the lines that can cause the exception * Fix indentation mixup --- homeassistant/components/velbus/__init__.py | 15 +++++++++++---- homeassistant/components/velbus/climate.py | 8 +++++++- homeassistant/components/velbus/cover.py | 17 ++++++++++++++--- homeassistant/components/velbus/switch.py | 12 ++++++++++-- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/velbus/__init__.py b/homeassistant/components/velbus/__init__.py index 38d0d8850b3..58e036bffdf 100644 --- a/homeassistant/components/velbus/__init__.py +++ b/homeassistant/components/velbus/__init__.py @@ -7,6 +7,7 @@ import voluptuous as vol import homeassistant.helpers.config_validation as cv from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.const import CONF_PORT, CONF_NAME +from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers.entity import Entity from homeassistant.helpers.typing import HomeAssistantType @@ -54,8 +55,6 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry): """Establish connection with velbus.""" hass.data.setdefault(DOMAIN, {}) - controller = velbus.Controller(entry.data[CONF_PORT]) - def callback(): modules = controller.get_modules() discovery_info = { @@ -80,10 +79,18 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry): hass.config_entries.async_forward_entry_setup( entry, category)) - controller.scan(callback) + try: + controller = velbus.Controller(entry.data[CONF_PORT]) + controller.scan(callback) + except velbus.util.VelbusException as err: + _LOGGER.error('An error occurred: %s', err) + raise ConfigEntryNotReady def syn_clock(self, service=None): - controller.sync_clock() + try: + controller.sync_clock() + except velbus.util.VelbusException as err: + _LOGGER.error('An error occurred: %s', err) hass.services.async_register( DOMAIN, 'sync_clock', syn_clock, diff --git a/homeassistant/components/velbus/climate.py b/homeassistant/components/velbus/climate.py index 77f489c6c9d..3d20eabdc04 100644 --- a/homeassistant/components/velbus/climate.py +++ b/homeassistant/components/velbus/climate.py @@ -1,6 +1,8 @@ """Support for Velbus thermostat.""" import logging +from velbus.util import VelbusException + from homeassistant.components.climate import ClimateDevice from homeassistant.components.climate.const import ( HVAC_MODE_HEAT, SUPPORT_TARGET_TEMPERATURE) @@ -76,7 +78,11 @@ class VelbusClimate(VelbusEntity, ClimateDevice): temp = kwargs.get(ATTR_TEMPERATURE) if temp is None: return - self._module.set_temp(temp) + try: + self._module.set_temp(temp) + except VelbusException as err: + _LOGGER.error('A Velbus error occurred: %s', err) + return self.schedule_update_ha_state() def set_hvac_mode(self, hvac_mode): diff --git a/homeassistant/components/velbus/cover.py b/homeassistant/components/velbus/cover.py index ce4cf229775..1b5c57c92e5 100644 --- a/homeassistant/components/velbus/cover.py +++ b/homeassistant/components/velbus/cover.py @@ -1,6 +1,8 @@ """Support for Velbus covers.""" import logging +from velbus.util import VelbusException + from homeassistant.components.cover import ( CoverDevice, SUPPORT_CLOSE, SUPPORT_OPEN, SUPPORT_STOP) @@ -55,12 +57,21 @@ class VelbusCover(VelbusEntity, CoverDevice): def open_cover(self, **kwargs): """Open the cover.""" - self._module.open(self._channel) + try: + self._module.open(self._channel) + except VelbusException as err: + _LOGGER.error('A Velbus error occurred: %s', err) def close_cover(self, **kwargs): """Close the cover.""" - self._module.close(self._channel) + try: + self._module.close(self._channel) + except VelbusException as err: + _LOGGER.error('A Velbus error occurred: %s', err) def stop_cover(self, **kwargs): """Stop the cover.""" - self._module.stop(self._channel) + try: + self._module.stop(self._channel) + except VelbusException as err: + _LOGGER.error('A Velbus error occurred: %s', err) diff --git a/homeassistant/components/velbus/switch.py b/homeassistant/components/velbus/switch.py index 84d71019ea5..689e009e2b2 100644 --- a/homeassistant/components/velbus/switch.py +++ b/homeassistant/components/velbus/switch.py @@ -1,6 +1,8 @@ """Support for Velbus switches.""" import logging +from velbus.util import VelbusException + from homeassistant.components.switch import SwitchDevice from . import VelbusEntity @@ -37,8 +39,14 @@ class VelbusSwitch(VelbusEntity, SwitchDevice): def turn_on(self, **kwargs): """Instruct the switch to turn on.""" - self._module.turn_on(self._channel) + try: + self._module.turn_on(self._channel) + except VelbusException as err: + _LOGGER.error('A Velbus error occurred: %s', err) def turn_off(self, **kwargs): """Instruct the switch to turn off.""" - self._module.turn_off(self._channel) + try: + self._module.turn_off(self._channel) + except VelbusException as err: + _LOGGER.error('A Velbus error occurred: %s', err)