Migrate to voluptuous (#3171)
parent
6f45906eda
commit
8467d07a3d
|
@ -7,48 +7,51 @@ https://home-assistant.io/components/sensor.mold_indicator/
|
||||||
import logging
|
import logging
|
||||||
import math
|
import math
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
import homeassistant.util as util
|
import homeassistant.util as util
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.helpers.event import track_state_change
|
from homeassistant.helpers.event import track_state_change
|
||||||
from homeassistant.const import (ATTR_UNIT_OF_MEASUREMENT,
|
from homeassistant.const import (
|
||||||
TEMP_CELSIUS, TEMP_FAHRENHEIT)
|
ATTR_UNIT_OF_MEASUREMENT, TEMP_CELSIUS, TEMP_FAHRENHEIT, CONF_NAME)
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DEFAULT_NAME = "Mold Indicator"
|
DEFAULT_NAME = 'Mold Indicator'
|
||||||
CONF_INDOOR_TEMP = "indoor_temp_sensor"
|
CONF_INDOOR_TEMP = 'indoor_temp_sensor'
|
||||||
CONF_OUTDOOR_TEMP = "outdoor_temp_sensor"
|
CONF_OUTDOOR_TEMP = 'outdoor_temp_sensor'
|
||||||
CONF_INDOOR_HUMIDITY = "indoor_humidity_sensor"
|
CONF_INDOOR_HUMIDITY = 'indoor_humidity_sensor'
|
||||||
CONF_CALIBRATION_FACTOR = "calibration_factor"
|
CONF_CALIBRATION_FACTOR = 'calibration_factor'
|
||||||
|
|
||||||
MAGNUS_K2 = 17.62
|
MAGNUS_K2 = 17.62
|
||||||
MAGNUS_K3 = 243.12
|
MAGNUS_K3 = 243.12
|
||||||
|
|
||||||
ATTR_DEWPOINT = "Dewpoint"
|
ATTR_DEWPOINT = 'Dewpoint'
|
||||||
ATTR_CRITICAL_TEMP = "Est. Crit. Temp"
|
ATTR_CRITICAL_TEMP = 'Est. Crit. Temp'
|
||||||
|
|
||||||
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
|
vol.Required(CONF_INDOOR_TEMP): cv.entity_id,
|
||||||
|
vol.Required(CONF_OUTDOOR_TEMP): cv.entity_id,
|
||||||
|
vol.Required(CONF_INDOOR_HUMIDITY): cv.entity_id,
|
||||||
|
vol.Optional(CONF_CALIBRATION_FACTOR): vol.Coerce(float),
|
||||||
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Setup MoldIndicator sensor."""
|
"""Setup MoldIndicator sensor."""
|
||||||
name = config.get('name', DEFAULT_NAME)
|
name = config.get(CONF_NAME, DEFAULT_NAME)
|
||||||
indoor_temp_sensor = config.get(CONF_INDOOR_TEMP)
|
indoor_temp_sensor = config.get(CONF_INDOOR_TEMP)
|
||||||
outdoor_temp_sensor = config.get(CONF_OUTDOOR_TEMP)
|
outdoor_temp_sensor = config.get(CONF_OUTDOOR_TEMP)
|
||||||
indoor_humidity_sensor = config.get(CONF_INDOOR_HUMIDITY)
|
indoor_humidity_sensor = config.get(CONF_INDOOR_HUMIDITY)
|
||||||
calib_factor = util.convert(config.get(CONF_CALIBRATION_FACTOR),
|
calib_factor = config.get(CONF_CALIBRATION_FACTOR)
|
||||||
float, None)
|
|
||||||
|
|
||||||
if None in (indoor_temp_sensor,
|
add_devices([MoldIndicator(
|
||||||
outdoor_temp_sensor, indoor_humidity_sensor):
|
hass, name, indoor_temp_sensor, outdoor_temp_sensor,
|
||||||
_LOGGER.error('Missing required key %s, %s or %s',
|
indoor_humidity_sensor, calib_factor)])
|
||||||
CONF_INDOOR_TEMP, CONF_OUTDOOR_TEMP,
|
|
||||||
CONF_INDOOR_HUMIDITY)
|
|
||||||
return False
|
|
||||||
|
|
||||||
add_devices_callback([MoldIndicator(
|
|
||||||
hass, name, indoor_temp_sensor,
|
|
||||||
outdoor_temp_sensor, indoor_humidity_sensor,
|
|
||||||
calib_factor)])
|
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=too-many-instance-attributes
|
# pylint: disable=too-many-instance-attributes
|
||||||
|
@ -83,16 +86,14 @@ class MoldIndicator(Entity):
|
||||||
indoor_hum = hass.states.get(indoor_humidity_sensor)
|
indoor_hum = hass.states.get(indoor_humidity_sensor)
|
||||||
|
|
||||||
if indoor_temp:
|
if indoor_temp:
|
||||||
self._indoor_temp = \
|
self._indoor_temp = MoldIndicator._update_temp_sensor(indoor_temp)
|
||||||
MoldIndicator._update_temp_sensor(indoor_temp)
|
|
||||||
|
|
||||||
if outdoor_temp:
|
if outdoor_temp:
|
||||||
self._outdoor_temp = \
|
self._outdoor_temp = MoldIndicator._update_temp_sensor(
|
||||||
MoldIndicator._update_temp_sensor(outdoor_temp)
|
outdoor_temp)
|
||||||
|
|
||||||
if indoor_hum:
|
if indoor_hum:
|
||||||
self._indoor_hum = \
|
self._indoor_hum = MoldIndicator._update_hum_sensor(indoor_hum)
|
||||||
MoldIndicator._update_hum_sensor(indoor_hum)
|
|
||||||
|
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
|
@ -130,18 +131,12 @@ class MoldIndicator(Entity):
|
||||||
state.state)
|
state.state)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# check unit
|
if unit != '%':
|
||||||
if unit != "%":
|
_LOGGER.error("Humidity sensor has unsupported unit: %s %s",
|
||||||
_LOGGER.error(
|
unit, " (allowed: %)")
|
||||||
"Humidity sensor has unsupported unit: %s %s",
|
|
||||||
unit,
|
|
||||||
" (allowed: %)")
|
|
||||||
|
|
||||||
# check range
|
|
||||||
if hum > 100 or hum < 0:
|
if hum > 100 or hum < 0:
|
||||||
_LOGGER.error(
|
_LOGGER.error("Humidity sensor out of range: %s %s", hum,
|
||||||
"Humidity sensor out of range: %s %s",
|
|
||||||
hum,
|
|
||||||
" (allowed: 0-100%)")
|
" (allowed: 0-100%)")
|
||||||
|
|
||||||
return hum
|
return hum
|
||||||
|
@ -162,15 +157,10 @@ class MoldIndicator(Entity):
|
||||||
return
|
return
|
||||||
|
|
||||||
if entity_id == self._indoor_temp_sensor:
|
if entity_id == self._indoor_temp_sensor:
|
||||||
# update the indoor temp sensor
|
|
||||||
self._indoor_temp = MoldIndicator._update_temp_sensor(new_state)
|
self._indoor_temp = MoldIndicator._update_temp_sensor(new_state)
|
||||||
|
|
||||||
elif entity_id == self._outdoor_temp_sensor:
|
elif entity_id == self._outdoor_temp_sensor:
|
||||||
# update outdoor temp sensor
|
|
||||||
self._outdoor_temp = MoldIndicator._update_temp_sensor(new_state)
|
self._outdoor_temp = MoldIndicator._update_temp_sensor(new_state)
|
||||||
|
|
||||||
elif entity_id == self._indoor_humidity_sensor:
|
elif entity_id == self._indoor_humidity_sensor:
|
||||||
# update humidity
|
|
||||||
self._indoor_hum = MoldIndicator._update_hum_sensor(new_state)
|
self._indoor_hum = MoldIndicator._update_hum_sensor(new_state)
|
||||||
|
|
||||||
self.update()
|
self.update()
|
||||||
|
@ -206,8 +196,7 @@ class MoldIndicator(Entity):
|
||||||
self._outdoor_temp + (self._indoor_temp - self._outdoor_temp) / \
|
self._outdoor_temp + (self._indoor_temp - self._outdoor_temp) / \
|
||||||
self._calib_factor
|
self._calib_factor
|
||||||
|
|
||||||
_LOGGER.debug(
|
_LOGGER.debug("Estimated Critical Temperature: %f " +
|
||||||
"Estimated Critical Temperature: %f " +
|
|
||||||
TEMP_CELSIUS, self._crit_temp)
|
TEMP_CELSIUS, self._crit_temp)
|
||||||
|
|
||||||
# Then calculate the humidity at this point
|
# Then calculate the humidity at this point
|
||||||
|
@ -242,7 +231,7 @@ class MoldIndicator(Entity):
|
||||||
@property
|
@property
|
||||||
def unit_of_measurement(self):
|
def unit_of_measurement(self):
|
||||||
"""Return the unit of measurement."""
|
"""Return the unit of measurement."""
|
||||||
return "%"
|
return '%'
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
|
@ -260,9 +249,7 @@ class MoldIndicator(Entity):
|
||||||
else:
|
else:
|
||||||
return {
|
return {
|
||||||
ATTR_DEWPOINT:
|
ATTR_DEWPOINT:
|
||||||
util.temperature.celsius_to_fahrenheit(
|
util.temperature.celsius_to_fahrenheit(self._dewpoint),
|
||||||
self._dewpoint),
|
|
||||||
ATTR_CRITICAL_TEMP:
|
ATTR_CRITICAL_TEMP:
|
||||||
util.temperature.celsius_to_fahrenheit(
|
util.temperature.celsius_to_fahrenheit(self._crit_temp),
|
||||||
self._crit_temp),
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ class TestSensorMoldIndicator(unittest.TestCase):
|
||||||
'indoor_temp_sensor': 'test.indoortemp',
|
'indoor_temp_sensor': 'test.indoortemp',
|
||||||
'outdoor_temp_sensor': 'test.outdoortemp',
|
'outdoor_temp_sensor': 'test.outdoortemp',
|
||||||
'indoor_humidity_sensor': 'test.indoorhumidity',
|
'indoor_humidity_sensor': 'test.indoorhumidity',
|
||||||
'calibration_factor': '2.0'
|
'calibration_factor': 2.0
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
@ -59,13 +59,11 @@ class TestSensorMoldIndicator(unittest.TestCase):
|
||||||
'indoor_temp_sensor': 'test.indoortemp',
|
'indoor_temp_sensor': 'test.indoortemp',
|
||||||
'outdoor_temp_sensor': 'test.outdoortemp',
|
'outdoor_temp_sensor': 'test.outdoortemp',
|
||||||
'indoor_humidity_sensor': 'test.indoorhumidity',
|
'indoor_humidity_sensor': 'test.indoorhumidity',
|
||||||
'calibration_factor': '2.0'
|
'calibration_factor': 2.0
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
moldind = self.hass.states.get('sensor.mold_indicator')
|
moldind = self.hass.states.get('sensor.mold_indicator')
|
||||||
assert moldind
|
assert moldind
|
||||||
|
|
||||||
# assert state
|
|
||||||
assert moldind.state == '0'
|
assert moldind.state == '0'
|
||||||
|
|
||||||
def test_calculation(self):
|
def test_calculation(self):
|
||||||
|
@ -76,7 +74,7 @@ class TestSensorMoldIndicator(unittest.TestCase):
|
||||||
'indoor_temp_sensor': 'test.indoortemp',
|
'indoor_temp_sensor': 'test.indoortemp',
|
||||||
'outdoor_temp_sensor': 'test.outdoortemp',
|
'outdoor_temp_sensor': 'test.outdoortemp',
|
||||||
'indoor_humidity_sensor': 'test.indoorhumidity',
|
'indoor_humidity_sensor': 'test.indoorhumidity',
|
||||||
'calibration_factor': '2.0'
|
'calibration_factor': 2.0
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
@ -108,23 +106,20 @@ class TestSensorMoldIndicator(unittest.TestCase):
|
||||||
'indoor_temp_sensor': 'test.indoortemp',
|
'indoor_temp_sensor': 'test.indoortemp',
|
||||||
'outdoor_temp_sensor': 'test.outdoortemp',
|
'outdoor_temp_sensor': 'test.outdoortemp',
|
||||||
'indoor_humidity_sensor': 'test.indoorhumidity',
|
'indoor_humidity_sensor': 'test.indoorhumidity',
|
||||||
'calibration_factor': '2.0'
|
'calibration_factor': 2.0
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
# Change indoor temp
|
|
||||||
self.hass.states.set('test.indoortemp', '30',
|
self.hass.states.set('test.indoortemp', '30',
|
||||||
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
|
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
|
||||||
self.hass.pool.block_till_done()
|
self.hass.pool.block_till_done()
|
||||||
assert self.hass.states.get('sensor.mold_indicator').state == '90'
|
assert self.hass.states.get('sensor.mold_indicator').state == '90'
|
||||||
|
|
||||||
# Change outdoor temp
|
|
||||||
self.hass.states.set('test.outdoortemp', '25',
|
self.hass.states.set('test.outdoortemp', '25',
|
||||||
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
|
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
|
||||||
self.hass.pool.block_till_done()
|
self.hass.pool.block_till_done()
|
||||||
assert self.hass.states.get('sensor.mold_indicator').state == '57'
|
assert self.hass.states.get('sensor.mold_indicator').state == '57'
|
||||||
|
|
||||||
# Change humidity
|
|
||||||
self.hass.states.set('test.indoorhumidity', '20',
|
self.hass.states.set('test.indoorhumidity', '20',
|
||||||
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
|
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
|
||||||
self.hass.pool.block_till_done()
|
self.hass.pool.block_till_done()
|
||||||
|
|
Loading…
Reference in New Issue