Add support for sensor state STATE_UNAVAILABLE (#24641)

* Fixed integration with ESPhome, which caused an error if ESPhome did not update fast enough on startup

* Set state to problem if sensor is unavailable

* Fix line length.
This commit is contained in:
Conrad Juhl Andersen authored and Paulus Schoutsen committed 2019-06-24 11:30:44 -07:00
1 parent d9420c1f73
commit 0792e72f71
2 files changed
+69 -13

No files matched your search

+25 -11
View File
@@ -9,7 +9,7 @@ from homeassistant.components import group
from homeassistant.components.recorder.util import execute, session_scope
from homeassistant.const import (
ATTR_TEMPERATURE, ATTR_UNIT_OF_MEASUREMENT, CONF_SENSORS, STATE_OK,
STATE_PROBLEM, STATE_UNKNOWN, TEMP_CELSIUS)
STATE_PROBLEM, STATE_UNAVAILABLE, STATE_UNKNOWN, TEMP_CELSIUS)
from homeassistant.core import callback
from homeassistant.exceptions import HomeAssistantError
import homeassistant.helpers.config_validation as cv
@@ -190,15 +190,25 @@ class Plant(Entity):
reading = self._sensormap[entity_id]
if reading == READING_MOISTURE:
self._moisture = int(float(value))
if value != STATE_UNAVAILABLE:
value = int(float(value))
self._moisture = value
elif reading == READING_BATTERY:
self._battery = int(float(value))
if value != STATE_UNAVAILABLE:
value = int(float(value))
self._battery = value
elif reading == READING_TEMPERATURE:
self._temperature = float(value)
if value != STATE_UNAVAILABLE:
value = float(value)
self._temperature = value
elif reading == READING_CONDUCTIVITY:
self._conductivity = int(float(value))
if value != STATE_UNAVAILABLE:
value = int(float(value))
self._conductivity = value
elif reading == READING_BRIGHTNESS:
self._brightness = int(float(value))
if value != STATE_UNAVAILABLE:
value = int(float(value))
self._brightness = value
self._brightness_history.add_measurement(
self._brightness, new_state.last_updated)
else:
@@ -216,12 +226,16 @@ class Plant(Entity):
params = self.READINGS[sensor_name]
value = getattr(self, '_{}'.format(sensor_name))
if value is not None:
if sensor_name == READING_BRIGHTNESS:
result.append(self._check_min(
sensor_name, self._brightness_history.max, params))
if value == STATE_UNAVAILABLE:
result.append('{} unavailable'.format(sensor_name))
else:
result.append(self._check_min(sensor_name, value, params))
result.append(self._check_max(sensor_name, value, params))
if sensor_name == READING_BRIGHTNESS:
result.append(self._check_min(
sensor_name, self._brightness_history.max, params))
else:
result.append(self._check_min(sensor_name, value,
params))
result.append(self._check_max(sensor_name, value, params))
result = [r for r in result if r is not None]