Refactor exception handling in Vallox (#55461)
This commit is contained in:
parent
4475cf24c8
commit
d50b700dc7
3 changed files with 59 additions and 50 deletions
|
@ -167,12 +167,13 @@ class ValloxStateProxy:
|
||||||
try:
|
try:
|
||||||
self._metric_cache = await self._client.fetch_metrics()
|
self._metric_cache = await self._client.fetch_metrics()
|
||||||
self._profile = await self._client.get_profile()
|
self._profile = await self._client.get_profile()
|
||||||
self._valid = True
|
|
||||||
|
|
||||||
except (OSError, ValloxApiException) as err:
|
except (OSError, ValloxApiException) as err:
|
||||||
_LOGGER.error("Error during state cache update: %s", err)
|
|
||||||
self._valid = False
|
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)
|
async_dispatcher_send(self._hass, SIGNAL_VALLOX_STATE_UPDATE)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -110,11 +110,7 @@ class ValloxFan(FanEntity):
|
||||||
"""Fetch state from the device."""
|
"""Fetch state from the device."""
|
||||||
try:
|
try:
|
||||||
# Fetch if the whole device is in regular operation state.
|
# Fetch if the whole device is in regular operation state.
|
||||||
mode = self._state_proxy.fetch_metric(METRIC_KEY_MODE)
|
self._state = self._state_proxy.fetch_metric(METRIC_KEY_MODE) == MODE_ON
|
||||||
if mode == MODE_ON:
|
|
||||||
self._state = True
|
|
||||||
else:
|
|
||||||
self._state = False
|
|
||||||
|
|
||||||
# Fetch the profile fan speeds.
|
# Fetch the profile fan speeds.
|
||||||
self._fan_speed_home = int(
|
self._fan_speed_home = int(
|
||||||
|
@ -133,11 +129,12 @@ class ValloxFan(FanEntity):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
self._available = True
|
|
||||||
|
|
||||||
except (OSError, KeyError) as err:
|
except (OSError, KeyError) as err:
|
||||||
self._available = False
|
self._available = False
|
||||||
_LOGGER.error("Error updating fan: %s", err)
|
_LOGGER.error("Error updating fan: %s", err)
|
||||||
|
return
|
||||||
|
|
||||||
|
self._available = True
|
||||||
|
|
||||||
#
|
#
|
||||||
# The fan entity model has changed to use percentages and preset_modes
|
# The fan entity model has changed to use percentages and preset_modes
|
||||||
|
@ -161,32 +158,35 @@ class ValloxFan(FanEntity):
|
||||||
if speed is not None:
|
if speed is not None:
|
||||||
return
|
return
|
||||||
|
|
||||||
if self._state is False:
|
if self._state is True:
|
||||||
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:
|
|
||||||
_LOGGER.error("Already on")
|
_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:
|
async def async_turn_off(self, **kwargs) -> None:
|
||||||
"""Turn the device off."""
|
"""Turn the device off."""
|
||||||
if self._state is True:
|
if self._state is False:
|
||||||
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:
|
|
||||||
_LOGGER.error("Already off")
|
_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)
|
||||||
|
|
|
@ -67,11 +67,13 @@ class ValloxSensor(SensorEntity):
|
||||||
self._attr_native_value = self._state_proxy.fetch_metric(
|
self._attr_native_value = self._state_proxy.fetch_metric(
|
||||||
self.entity_description.metric_key
|
self.entity_description.metric_key
|
||||||
)
|
)
|
||||||
self._attr_available = True
|
|
||||||
|
|
||||||
except (OSError, KeyError) as err:
|
except (OSError, KeyError) as err:
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
_LOGGER.error("Error updating sensor: %s", err)
|
_LOGGER.error("Error updating sensor: %s", err)
|
||||||
|
return
|
||||||
|
|
||||||
|
self._attr_available = True
|
||||||
|
|
||||||
|
|
||||||
class ValloxProfileSensor(ValloxSensor):
|
class ValloxProfileSensor(ValloxSensor):
|
||||||
|
@ -81,11 +83,13 @@ class ValloxProfileSensor(ValloxSensor):
|
||||||
"""Fetch state from the ventilation unit."""
|
"""Fetch state from the ventilation unit."""
|
||||||
try:
|
try:
|
||||||
self._attr_native_value = self._state_proxy.get_profile()
|
self._attr_native_value = self._state_proxy.get_profile()
|
||||||
self._attr_available = True
|
|
||||||
|
|
||||||
except OSError as err:
|
except OSError as err:
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
_LOGGER.error("Error updating sensor: %s", err)
|
_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
|
# 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):
|
async def async_update(self):
|
||||||
"""Fetch state from the ventilation unit."""
|
"""Fetch state from the ventilation unit."""
|
||||||
try:
|
try:
|
||||||
# If device is in regular operation, continue.
|
fan_on = self._state_proxy.fetch_metric(METRIC_KEY_MODE) == MODE_ON
|
||||||
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
|
|
||||||
|
|
||||||
except (OSError, KeyError) as err:
|
except (OSError, KeyError) as err:
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
_LOGGER.error("Error updating sensor: %s", err)
|
_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):
|
class ValloxFilterRemainingSensor(ValloxSensor):
|
||||||
|
@ -123,18 +129,20 @@ class ValloxFilterRemainingSensor(ValloxSensor):
|
||||||
days_remaining = int(
|
days_remaining = int(
|
||||||
self._state_proxy.fetch_metric(self.entity_description.metric_key)
|
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:
|
except (OSError, KeyError) as err:
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
_LOGGER.error("Error updating sensor: %s", err)
|
_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
|
@dataclass
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue