Fix mold_indicator errors at startup (#17346)

* Initial changes to resolve issue 16733

Added logic to ensure that if the state is unknown during startup that the error about being unable to parse the value is not logged.
Further,  also ensured that if an attribute is set to None it does not try to convert the None value to Fahrenheit as that will cause an error.

* Cleaned up and added few comments

Cleaned up some lines based on flake8 and pylint.
Added some comment lines on the items added.

* Changed to async and using async_added_to_hass

Changed sensor to use async.
Registering state tracking for sensors and initial setup is now done upon the home assistant start event.

* Updated test and small fix

Updated test to handle unavailable state of sensor and return of None for attributes when data is unavailable.
Ensured that atributes are set to None when state is unavailable due to incorrect data.

* Fixed some flake8 issues in test

Fixed some flake8 issues in test_moldindicator.py.

* Updates based on review

Updates based on review from MartinHjelmare

* Added sensor entity_id to logger errors

Added sensor entity_id to logger error messages
Update test to use constant STATE_UNKNOWN instead of fixed string.
This commit is contained in:
ehendrix23 authored and Martin Hjelmare committed 2018-10-17 18:42:39 +02:00
1 parent 33860bf23c
commit daf9d28565
2 files changed
+249 -59

No files matched your search

+117 -6
View File
@@ -5,8 +5,8 @@ from homeassistant.setup import setup_component
import homeassistant.components.sensor as sensor
from homeassistant.components.sensor.mold_indicator import (ATTR_DEWPOINT,
ATTR_CRITICAL_TEMP)
from homeassistant.const import (ATTR_UNIT_OF_MEASUREMENT,
TEMP_CELSIUS)
from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT, STATE_UNKNOWN, TEMP_CELSIUS)
from tests.common import get_test_home_assistant
@@ -44,7 +44,7 @@ class TestSensorMoldIndicator(unittest.TestCase):
assert moldind
assert '%' == moldind.attributes.get('unit_of_measurement')
def test_invalidhum(self):
def test_invalidcalib(self):
"""Test invalid sensor values."""
self.hass.states.set('test.indoortemp', '10',
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
@@ -53,6 +53,32 @@ class TestSensorMoldIndicator(unittest.TestCase):
self.hass.states.set('test.indoorhumidity', '0',
{ATTR_UNIT_OF_MEASUREMENT: '%'})
self.assertTrue(setup_component(self.hass, sensor.DOMAIN, {
'sensor': {
'platform': 'mold_indicator',
'indoor_temp_sensor': 'test.indoortemp',
'outdoor_temp_sensor': 'test.outdoortemp',
'indoor_humidity_sensor': 'test.indoorhumidity',
'calibration_factor': 0
}
}))
self.hass.start()
self.hass.block_till_done()
moldind = self.hass.states.get('sensor.mold_indicator')
assert moldind
assert moldind.state == 'unavailable'
assert moldind.attributes.get(ATTR_DEWPOINT) is None
assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None
def test_invalidhum(self):
"""Test invalid sensor values."""
self.hass.states.set('test.indoortemp', '10',
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
self.hass.states.set('test.outdoortemp', '10',
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
self.hass.states.set('test.indoorhumidity', '-1',
{ATTR_UNIT_OF_MEASUREMENT: '%'})
self.assertTrue(setup_component(self.hass, sensor.DOMAIN, {
'sensor': {
'platform': 'mold_indicator',
@@ -62,9 +88,32 @@ class TestSensorMoldIndicator(unittest.TestCase):
'calibration_factor': 2.0
}
}))
self.hass.start()
self.hass.block_till_done()
moldind = self.hass.states.get('sensor.mold_indicator')
assert moldind
assert moldind.state == '0'
assert moldind.state == 'unavailable'
assert moldind.attributes.get(ATTR_DEWPOINT) is None
assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None
self.hass.states.set('test.indoorhumidity', 'A',
{ATTR_UNIT_OF_MEASUREMENT: '%'})
self.hass.block_till_done()
moldind = self.hass.states.get('sensor.mold_indicator')
assert moldind
assert moldind.state == 'unavailable'
assert moldind.attributes.get(ATTR_DEWPOINT) is None
assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None
self.hass.states.set('test.indoorhumidity', '10',
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
self.hass.block_till_done()
moldind = self.hass.states.get('sensor.mold_indicator')
assert moldind
assert moldind.state == 'unavailable'
assert moldind.attributes.get(ATTR_DEWPOINT) is None
assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None
def test_calculation(self):
"""Test the mold indicator internal calculations."""
@@ -77,7 +126,8 @@ class TestSensorMoldIndicator(unittest.TestCase):
'calibration_factor': 2.0
}
}))
self.hass.start()
self.hass.block_till_done()
moldind = self.hass.states.get('sensor.mold_indicator')
assert moldind
@@ -98,6 +148,66 @@ class TestSensorMoldIndicator(unittest.TestCase):
assert state
assert state == '68'
def test_unknown_sensor(self):
"""Test the sensor_changed function."""
self.assertTrue(setup_component(self.hass, sensor.DOMAIN, {
'sensor': {
'platform': 'mold_indicator',
'indoor_temp_sensor': 'test.indoortemp',
'outdoor_temp_sensor': 'test.outdoortemp',
'indoor_humidity_sensor': 'test.indoorhumidity',
'calibration_factor': 2.0
}
}))
self.hass.start()
self.hass.states.set('test.indoortemp', STATE_UNKNOWN,
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
self.hass.block_till_done()
moldind = self.hass.states.get('sensor.mold_indicator')
assert moldind
assert moldind.state == 'unavailable'
assert moldind.attributes.get(ATTR_DEWPOINT) is None
assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None
self.hass.states.set('test.indoortemp', '30',
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
self.hass.states.set('test.outdoortemp', STATE_UNKNOWN,
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
self.hass.block_till_done()
moldind = self.hass.states.get('sensor.mold_indicator')
assert moldind
assert moldind.state == 'unavailable'
assert moldind.attributes.get(ATTR_DEWPOINT) is None
assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None
self.hass.states.set('test.outdoortemp', '25',
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
self.hass.states.set('test.indoorhumidity', STATE_UNKNOWN,
{ATTR_UNIT_OF_MEASUREMENT: '%'})
self.hass.block_till_done()
moldind = self.hass.states.get('sensor.mold_indicator')
assert moldind
assert moldind.state == 'unavailable'
assert moldind.attributes.get(ATTR_DEWPOINT) is None
assert moldind.attributes.get(ATTR_CRITICAL_TEMP) is None
self.hass.states.set('test.indoorhumidity', '20',
{ATTR_UNIT_OF_MEASUREMENT: '%'})
self.hass.block_till_done()
moldind = self.hass.states.get('sensor.mold_indicator')
assert moldind
assert moldind.state == '23'
dewpoint = moldind.attributes.get(ATTR_DEWPOINT)
assert dewpoint
assert dewpoint > 4.58
assert dewpoint < 4.59
esttemp = moldind.attributes.get(ATTR_CRITICAL_TEMP)
assert esttemp
assert esttemp == 27.5
def test_sensor_changed(self):
"""Test the sensor_changed function."""
self.assertTrue(setup_component(self.hass, sensor.DOMAIN, {
@@ -109,6 +219,7 @@ class TestSensorMoldIndicator(unittest.TestCase):
'calibration_factor': 2.0
}
}))
self.hass.start()
self.hass.states.set('test.indoortemp', '30',
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
@@ -121,6 +232,6 @@ class TestSensorMoldIndicator(unittest.TestCase):
assert self.hass.states.get('sensor.mold_indicator').state == '57'
self.hass.states.set('test.indoorhumidity', '20',
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
{ATTR_UNIT_OF_MEASUREMENT: '%'})
self.hass.block_till_done()
assert self.hass.states.get('sensor.mold_indicator').state == '23'