From b150b981b201e0a19dc6fd835def2b14a48f81aa Mon Sep 17 00:00:00 2001 From: Alexey Khit Date: Thu, 17 Dec 2020 11:36:15 +0300 Subject: [PATCH] Add exception check to read climate data --- custom_components/xiaomi_gateway3/climate.py | 30 ++++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/custom_components/xiaomi_gateway3/climate.py b/custom_components/xiaomi_gateway3/climate.py index 7a6ebfdd..c0e8e3d1 100644 --- a/custom_components/xiaomi_gateway3/climate.py +++ b/custom_components/xiaomi_gateway3/climate.py @@ -4,6 +4,8 @@ from . import DOMAIN, Gateway3Device from .core.gateway3 import Gateway3 +_LOGGER = logging.getLogger(__name__) + HVAC_MODES = [HVAC_MODE_HEAT, HVAC_MODE_COOL, HVAC_MODE_OFF] FAN_MODES = [FAN_LOW, FAN_MEDIUM, FAN_HIGH, FAN_AUTO] @@ -76,18 +78,22 @@ def supported_features(self): return SUPPORT_TARGET_TEMPERATURE | SUPPORT_FAN_MODE def update(self, data: dict = None): - if 'power' in data: # 0 - off, 1 - on - self._is_on = data['power'] - if 'mode' in data: # 0 - heat, 1 - cool - self._hvac_mode = HVAC_MODES[data['mode']] - if 'fan_mode' in data: # 0 - low, 3 - auto - self._fan_mode = FAN_MODES[data['fan_mode']] - if 'current_temperature' in data: - self._current_temp = data['current_temperature'] - if 'target_temperature' in data: - self._target_temp = data['target_temperature'] - if self._attr in data: - self._state = bytearray(data[self._attr].to_bytes(4, 'big')) + try: + if 'power' in data: # 0 - off, 1 - on + self._is_on = data['power'] + if 'mode' in data: # 0 - heat, 1 - cool + self._hvac_mode = HVAC_MODES[data['mode']] + if 'fan_mode' in data: # 0 - low, 3 - auto + self._fan_mode = FAN_MODES[data['fan_mode']] + if 'current_temperature' in data: + self._current_temp = data['current_temperature'] + if 'target_temperature' in data: + self._target_temp = data['target_temperature'] + if self._attr in data: + self._state = bytearray(data[self._attr].to_bytes(4, 'big')) + except: + _LOGGER.exception(f"Can't read climate data: {data}") + self.async_write_ha_state() def set_temperature(self, **kwargs) -> None: