Limit fronius error messages on failed connection (#45824)

* Do not print several error messages on failed connection

* Change wrapping of exception, implement available

* Simplify exception flow

* Remove unnecessary init

* Add available property to actual entities

* Rebase and formatting
This commit is contained in:
Niels Mündler 2021-02-15 18:28:28 +01:00 committed by GitHub
parent 886067a327
commit f2ca4acff0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,6 +2,7 @@
import copy import copy
from datetime import timedelta from datetime import timedelta
import logging import logging
from typing import Dict
from pyfronius import Fronius from pyfronius import Fronius
import voluptuous as vol import voluptuous as vol
@ -130,6 +131,7 @@ class FroniusAdapter:
self._name = name self._name = name
self._device = device self._device = device
self._fetched = {} self._fetched = {}
self._available = True
self.sensors = set() self.sensors = set()
self._registered_sensors = set() self._registered_sensors = set()
@ -145,21 +147,32 @@ class FroniusAdapter:
"""Return the state attributes.""" """Return the state attributes."""
return self._fetched return self._fetched
@property
def available(self):
"""Whether the fronius device is active."""
return self._available
async def async_update(self): async def async_update(self):
"""Retrieve and update latest state.""" """Retrieve and update latest state."""
values = {}
try: try:
values = await self._update() values = await self._update()
except ConnectionError: except ConnectionError:
_LOGGER.error("Failed to update: connection error") # fronius devices are often powered by self-produced solar energy
# and henced turned off at night.
# Therefore we will not print multiple errors when connection fails
if self._available:
self._available = False
_LOGGER.error("Failed to update: connection error")
return
except ValueError: except ValueError:
_LOGGER.error( _LOGGER.error(
"Failed to update: invalid response returned." "Failed to update: invalid response returned."
"Maybe the configured device is not supported" "Maybe the configured device is not supported"
) )
if not values:
return return
self._available = True # reset connection failure
attributes = self._fetched attributes = self._fetched
# Copy data of current fronius device # Copy data of current fronius device
for key, entry in values.items(): for key, entry in values.items():
@ -182,7 +195,7 @@ class FroniusAdapter:
for sensor in self._registered_sensors: for sensor in self._registered_sensors:
sensor.async_schedule_update_ha_state(True) sensor.async_schedule_update_ha_state(True)
async def _update(self): async def _update(self) -> Dict:
"""Return values of interest.""" """Return values of interest."""
async def register(self, sensor): async def register(self, sensor):
@ -268,6 +281,11 @@ class FroniusTemplateSensor(Entity):
"""Device should not be polled, returns False.""" """Device should not be polled, returns False."""
return False return False
@property
def available(self):
"""Whether the fronius device is active."""
return self.parent.available
async def async_update(self): async def async_update(self):
"""Update the internal state.""" """Update the internal state."""
state = self.parent.data.get(self._name) state = self.parent.data.get(self._name)