Clear miflora sensor state on exception (#29276)

* Clear state on exception

Clear state if querying the device fails. The state is then set to unknown, so it can be tracked if a miflora device isn't responding any more.

* Add available()

Signal valid data via available()
pull/29319/head
Christian Ferbar 2019-12-02 20:24:16 +01:00 committed by Paulus Schoutsen
parent 9771826ed6
commit 1804c6edc5
1 changed files with 10 additions and 5 deletions

View File

@ -106,6 +106,7 @@ class MiFloraSensor(Entity):
self._icon = icon
self._name = name
self._state = None
self._available = False
self.data = []
self._force_update = force_update
# Median is used to filter out outliers. median of 3 will filter
@ -132,6 +133,11 @@ class MiFloraSensor(Entity):
"""Return the state of the sensor."""
return self._state
@property
def available(self):
"""Return True if entity is available."""
return self._available
@property
def unit_of_measurement(self):
"""Return the units of measurement."""
@ -156,15 +162,14 @@ class MiFloraSensor(Entity):
try:
_LOGGER.debug("Polling data for %s", self.name)
data = self.poller.parameter_value(self.parameter)
except OSError as ioerr:
_LOGGER.info("Polling error %s", ioerr)
return
except BluetoothBackendException as bterror:
_LOGGER.info("Polling error %s", bterror)
except (OSError, BluetoothBackendException) as err:
_LOGGER.info("Polling error %s: %s", type(err).__name__, err)
self._available = False
return
if data is not None:
_LOGGER.debug("%s = %s", self.name, data)
self._available = True
self.data.append(data)
else:
_LOGGER.info("Did not receive any data from Mi Flora sensor %s", self.name)