2015-12-01 13:24:03 +01:00
|
|
|
"""
|
|
|
|
tests.components.sensor.test_yr
|
2016-02-13 14:19:11 +01:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2015-12-01 13:24:03 +01:00
|
|
|
|
|
|
|
Tests Yr sensor.
|
|
|
|
"""
|
2016-01-08 13:30:16 -07:00
|
|
|
from datetime import datetime
|
2015-12-27 17:37:32 -08:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
import pytest
|
2015-12-01 13:24:03 +01:00
|
|
|
|
|
|
|
import homeassistant.core as ha
|
|
|
|
import homeassistant.components.sensor as sensor
|
2016-01-08 13:30:16 -07:00
|
|
|
import homeassistant.util.dt as dt_util
|
2015-12-01 13:24:03 +01:00
|
|
|
|
|
|
|
|
2015-12-27 17:37:32 -08:00
|
|
|
@pytest.mark.usefixtures('betamax_session')
|
|
|
|
class TestSensorYr:
|
2015-12-01 13:24:03 +01:00
|
|
|
""" Test the Yr sensor. """
|
|
|
|
|
2015-12-27 17:37:32 -08:00
|
|
|
def setup_method(self, method):
|
2015-12-01 13:24:03 +01:00
|
|
|
self.hass = ha.HomeAssistant()
|
2015-12-27 11:07:25 -08:00
|
|
|
self.hass.config.latitude = 32.87336
|
|
|
|
self.hass.config.longitude = 117.22743
|
2015-12-01 13:24:03 +01:00
|
|
|
|
2015-12-27 17:37:32 -08:00
|
|
|
def teardown_method(self, method):
|
2015-12-01 13:24:03 +01:00
|
|
|
""" Stop down stuff we started. """
|
|
|
|
self.hass.stop()
|
|
|
|
|
2015-12-27 17:37:32 -08:00
|
|
|
def test_default_setup(self, betamax_session):
|
2016-01-08 13:30:16 -07:00
|
|
|
now = datetime(2016, 1, 5, 1, tzinfo=dt_util.UTC)
|
|
|
|
|
2015-12-27 17:37:32 -08:00
|
|
|
with patch('homeassistant.components.sensor.yr.requests.Session',
|
|
|
|
return_value=betamax_session):
|
2016-01-08 13:30:16 -07:00
|
|
|
with patch('homeassistant.components.sensor.yr.dt_util.utcnow',
|
|
|
|
return_value=now):
|
|
|
|
assert sensor.setup(self.hass, {
|
|
|
|
'sensor': {
|
|
|
|
'platform': 'yr',
|
|
|
|
'elevation': 0,
|
|
|
|
}
|
|
|
|
})
|
2015-12-27 17:37:32 -08:00
|
|
|
|
2015-12-01 13:24:03 +01:00
|
|
|
state = self.hass.states.get('sensor.yr_symbol')
|
|
|
|
|
2016-02-01 15:50:17 +01:00
|
|
|
assert '46' == state.state
|
2015-12-27 17:37:32 -08:00
|
|
|
assert state.state.isnumeric()
|
|
|
|
assert state.attributes.get('unit_of_measurement') is None
|
|
|
|
|
|
|
|
def test_custom_setup(self, betamax_session):
|
2016-02-01 15:50:17 +01:00
|
|
|
now = datetime(2016, 1, 5, 1, tzinfo=dt_util.UTC)
|
|
|
|
|
2015-12-27 17:37:32 -08:00
|
|
|
with patch('homeassistant.components.sensor.yr.requests.Session',
|
|
|
|
return_value=betamax_session):
|
2016-02-01 15:50:17 +01:00
|
|
|
with patch('homeassistant.components.sensor.yr.dt_util.utcnow',
|
|
|
|
return_value=now):
|
|
|
|
assert sensor.setup(self.hass, {
|
|
|
|
'sensor': {
|
|
|
|
'platform': 'yr',
|
|
|
|
'elevation': 0,
|
|
|
|
'monitored_conditions': {
|
|
|
|
'pressure',
|
|
|
|
'windDirection',
|
|
|
|
'humidity',
|
|
|
|
'fog',
|
|
|
|
'windSpeed'
|
|
|
|
}
|
2015-12-27 17:37:32 -08:00
|
|
|
}
|
2016-02-01 15:50:17 +01:00
|
|
|
})
|
2015-12-01 13:24:03 +01:00
|
|
|
|
|
|
|
state = self.hass.states.get('sensor.yr_pressure')
|
2016-02-01 15:50:17 +01:00
|
|
|
assert 'hPa' == state.attributes.get('unit_of_measurement')
|
|
|
|
assert '1025.1' == state.state
|
2015-12-01 13:24:03 +01:00
|
|
|
|
|
|
|
state = self.hass.states.get('sensor.yr_wind_direction')
|
2016-02-13 14:19:11 +01:00
|
|
|
assert '°' == state.attributes.get('unit_of_measurement')
|
2016-02-01 15:50:17 +01:00
|
|
|
assert '81.8' == state.state
|
2015-12-01 13:24:03 +01:00
|
|
|
|
|
|
|
state = self.hass.states.get('sensor.yr_humidity')
|
2016-02-01 15:50:17 +01:00
|
|
|
assert '%' == state.attributes.get('unit_of_measurement')
|
|
|
|
assert '79.6' == state.state
|
2015-12-01 13:24:03 +01:00
|
|
|
|
|
|
|
state = self.hass.states.get('sensor.yr_fog')
|
2016-02-01 15:50:17 +01:00
|
|
|
assert '%' == state.attributes.get('unit_of_measurement')
|
|
|
|
assert '0.0' == state.state
|
2015-12-01 13:24:03 +01:00
|
|
|
|
|
|
|
state = self.hass.states.get('sensor.yr_wind_speed')
|
2015-12-27 17:37:32 -08:00
|
|
|
assert 'm/s', state.attributes.get('unit_of_measurement')
|
2016-02-01 15:50:17 +01:00
|
|
|
assert '4.3' == state.state
|