Config validation of yr sensor (#1767)
This commit is contained in:
parent
24257fe4a3
commit
d3493c7e5a
2 changed files with 26 additions and 28 deletions
|
@ -5,9 +5,10 @@ For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/sensor.yr/
|
https://home-assistant.io/components/sensor.yr/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
|
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
@ -18,6 +19,8 @@ _LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
REQUIREMENTS = ['xmltodict']
|
REQUIREMENTS = ['xmltodict']
|
||||||
|
|
||||||
|
CONF_MONITORED_CONDITIONS = 'monitored_conditions'
|
||||||
|
|
||||||
# Sensor types are defined like so:
|
# Sensor types are defined like so:
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES = {
|
||||||
'symbol': ['Symbol', None],
|
'symbol': ['Symbol', None],
|
||||||
|
@ -36,6 +39,13 @@ SENSOR_TYPES = {
|
||||||
'dewpointTemperature': ['Dewpoint temperature', '°C'],
|
'dewpointTemperature': ['Dewpoint temperature', '°C'],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PLATFORM_SCHEMA = vol.Schema({
|
||||||
|
vol.Required('platform'): 'yr',
|
||||||
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=[]):
|
||||||
|
[vol.In(SENSOR_TYPES.keys())],
|
||||||
|
vol.Optional('elevation'): cv.positive_int,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Setup the Yr.no sensor."""
|
"""Setup the Yr.no sensor."""
|
||||||
|
@ -58,12 +68,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
weather = YrData(coordinates)
|
weather = YrData(coordinates)
|
||||||
|
|
||||||
dev = []
|
dev = []
|
||||||
if 'monitored_conditions' in config:
|
for sensor_type in config[CONF_MONITORED_CONDITIONS]:
|
||||||
for variable in config['monitored_conditions']:
|
dev.append(YrSensor(sensor_type, weather))
|
||||||
if variable not in SENSOR_TYPES:
|
|
||||||
_LOGGER.error('Sensor type: "%s" does not exist', variable)
|
|
||||||
else:
|
|
||||||
dev.append(YrSensor(variable, weather))
|
|
||||||
|
|
||||||
# add symbol as default sensor
|
# add symbol as default sensor
|
||||||
if len(dev) == 0:
|
if len(dev) == 0:
|
||||||
|
|
|
@ -4,9 +4,8 @@ from unittest.mock import patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import homeassistant.components.sensor as sensor
|
from homeassistant.bootstrap import _setup_component
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from tests.common import get_test_home_assistant
|
from tests.common import get_test_home_assistant
|
||||||
|
|
||||||
|
|
||||||
|
@ -32,12 +31,9 @@ class TestSensorYr:
|
||||||
return_value=betamax_session):
|
return_value=betamax_session):
|
||||||
with patch('homeassistant.components.sensor.yr.dt_util.utcnow',
|
with patch('homeassistant.components.sensor.yr.dt_util.utcnow',
|
||||||
return_value=now):
|
return_value=now):
|
||||||
assert sensor.setup(self.hass, {
|
assert _setup_component(self.hass, 'sensor', {
|
||||||
'sensor': {
|
'sensor': {'platform': 'yr',
|
||||||
'platform': 'yr',
|
'elevation': 0}})
|
||||||
'elevation': 0,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
state = self.hass.states.get('sensor.yr_symbol')
|
state = self.hass.states.get('sensor.yr_symbol')
|
||||||
|
|
||||||
|
@ -53,19 +49,15 @@ class TestSensorYr:
|
||||||
return_value=betamax_session):
|
return_value=betamax_session):
|
||||||
with patch('homeassistant.components.sensor.yr.dt_util.utcnow',
|
with patch('homeassistant.components.sensor.yr.dt_util.utcnow',
|
||||||
return_value=now):
|
return_value=now):
|
||||||
assert sensor.setup(self.hass, {
|
assert _setup_component(self.hass, 'sensor', {
|
||||||
'sensor': {
|
'sensor': {'platform': 'yr',
|
||||||
'platform': 'yr',
|
'elevation': 0,
|
||||||
'elevation': 0,
|
'monitored_conditions': [
|
||||||
'monitored_conditions': {
|
'pressure',
|
||||||
'pressure',
|
'windDirection',
|
||||||
'windDirection',
|
'humidity',
|
||||||
'humidity',
|
'fog',
|
||||||
'fog',
|
'windSpeed']}})
|
||||||
'windSpeed'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
state = self.hass.states.get('sensor.yr_pressure')
|
state = self.hass.states.get('sensor.yr_pressure')
|
||||||
assert 'hPa' == state.attributes.get('unit_of_measurement')
|
assert 'hPa' == state.attributes.get('unit_of_measurement')
|
||||||
|
|
Loading…
Add table
Reference in a new issue