Refactor exception handling in Vallox (#55461)

pull/55839/head
Andre Richter 2021-09-06 12:03:45 +02:00 committed by GitHub
parent 4475cf24c8
commit d50b700dc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 50 deletions

View File

@ -167,12 +167,13 @@ class ValloxStateProxy:
try:
self._metric_cache = await self._client.fetch_metrics()
self._profile = await self._client.get_profile()
self._valid = True
except (OSError, ValloxApiException) as err:
_LOGGER.error("Error during state cache update: %s", err)
self._valid = False
_LOGGER.error("Error during state cache update: %s", err)
return
self._valid = True
async_dispatcher_send(self._hass, SIGNAL_VALLOX_STATE_UPDATE)

View File

@ -110,11 +110,7 @@ class ValloxFan(FanEntity):
"""Fetch state from the device."""
try:
# Fetch if the whole device is in regular operation state.
mode = self._state_proxy.fetch_metric(METRIC_KEY_MODE)
if mode == MODE_ON:
self._state = True
else:
self._state = False
self._state = self._state_proxy.fetch_metric(METRIC_KEY_MODE) == MODE_ON
# Fetch the profile fan speeds.
self._fan_speed_home = int(
@ -133,11 +129,12 @@ class ValloxFan(FanEntity):
)
)
self._available = True
except (OSError, KeyError) as err:
self._available = False
_LOGGER.error("Error updating fan: %s", err)
return
self._available = True
#
# The fan entity model has changed to use percentages and preset_modes
@ -161,32 +158,35 @@ class ValloxFan(FanEntity):
if speed is not None:
return
if self._state is False:
try:
await self._client.set_values({METRIC_KEY_MODE: MODE_ON})
# This state change affects other entities like sensors. Force
# an immediate update that can be observed by all parties
# involved.
await self._state_proxy.async_update(None)
except OSError as err:
self._available = False
_LOGGER.error("Error turning on: %s", err)
else:
if self._state is True:
_LOGGER.error("Already on")
return
try:
await self._client.set_values({METRIC_KEY_MODE: MODE_ON})
except OSError as err:
self._available = False
_LOGGER.error("Error turning on: %s", err)
return
# This state change affects other entities like sensors. Force an immediate update that can
# be observed by all parties involved.
await self._state_proxy.async_update(None)
async def async_turn_off(self, **kwargs) -> None:
"""Turn the device off."""
if self._state is True:
try:
await self._client.set_values({METRIC_KEY_MODE: MODE_OFF})
# Same as for turn_on method.
await self._state_proxy.async_update(None)
except OSError as err:
self._available = False
_LOGGER.error("Error turning off: %s", err)
else:
if self._state is False:
_LOGGER.error("Already off")
return
try:
await self._client.set_values({METRIC_KEY_MODE: MODE_OFF})
except OSError as err:
self._available = False
_LOGGER.error("Error turning off: %s", err)
return
# Same as for turn_on method.
await self._state_proxy.async_update(None)

View File

@ -67,11 +67,13 @@ class ValloxSensor(SensorEntity):
self._attr_native_value = self._state_proxy.fetch_metric(
self.entity_description.metric_key
)
self._attr_available = True
except (OSError, KeyError) as err:
self._attr_available = False
_LOGGER.error("Error updating sensor: %s", err)
return
self._attr_available = True
class ValloxProfileSensor(ValloxSensor):
@ -81,11 +83,13 @@ class ValloxProfileSensor(ValloxSensor):
"""Fetch state from the ventilation unit."""
try:
self._attr_native_value = self._state_proxy.get_profile()
self._attr_available = True
except OSError as err:
self._attr_available = False
_LOGGER.error("Error updating sensor: %s", err)
return
self._attr_available = True
# There seems to be a quirk with respect to the fan speed reporting. The device
@ -101,17 +105,19 @@ class ValloxFanSpeedSensor(ValloxSensor):
async def async_update(self):
"""Fetch state from the ventilation unit."""
try:
# If device is in regular operation, continue.
if self._state_proxy.fetch_metric(METRIC_KEY_MODE) == MODE_ON:
await super().async_update()
else:
# Report zero percent otherwise.
self._attr_native_value = 0
self._attr_available = True
fan_on = self._state_proxy.fetch_metric(METRIC_KEY_MODE) == MODE_ON
except (OSError, KeyError) as err:
self._attr_available = False
_LOGGER.error("Error updating sensor: %s", err)
return
if fan_on:
await super().async_update()
else:
# Report zero percent otherwise.
self._attr_native_value = 0
self._attr_available = True
class ValloxFilterRemainingSensor(ValloxSensor):
@ -123,18 +129,20 @@ class ValloxFilterRemainingSensor(ValloxSensor):
days_remaining = int(
self._state_proxy.fetch_metric(self.entity_description.metric_key)
)
days_remaining_delta = timedelta(days=days_remaining)
# Since only a delta of days is received from the device, fix the
# time so the timestamp does not change with every update.
now = datetime.utcnow().replace(hour=13, minute=0, second=0, microsecond=0)
self._attr_native_value = (now + days_remaining_delta).isoformat()
self._attr_available = True
except (OSError, KeyError) as err:
self._attr_available = False
_LOGGER.error("Error updating sensor: %s", err)
return
days_remaining_delta = timedelta(days=days_remaining)
# Since only a delta of days is received from the device, fix the
# time so the timestamp does not change with every update.
now = datetime.utcnow().replace(hour=13, minute=0, second=0, microsecond=0)
self._attr_native_value = (now + days_remaining_delta).isoformat()
self._attr_available = True
@dataclass