2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the Yr sensor platform."""
|
2016-01-08 20:30:16 +00:00
|
|
|
from datetime import datetime
|
2015-12-28 01:37:32 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
import pytest
|
2015-12-01 12:24:03 +00:00
|
|
|
|
|
|
|
import homeassistant.components.sensor as sensor
|
2016-01-08 20:30:16 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2015-12-01 12:24:03 +00:00
|
|
|
|
2016-02-14 23:08:23 +00:00
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
2015-12-01 12:24:03 +00:00
|
|
|
|
2015-12-28 01:37:32 +00:00
|
|
|
@pytest.mark.usefixtures('betamax_session')
|
|
|
|
class TestSensorYr:
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the Yr sensor."""
|
2015-12-01 12:24:03 +00:00
|
|
|
|
2015-12-28 01:37:32 +00:00
|
|
|
def setup_method(self, method):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup things to be run when tests are started."""
|
2016-02-14 23:08:23 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2015-12-27 19:07:25 +00:00
|
|
|
self.hass.config.latitude = 32.87336
|
|
|
|
self.hass.config.longitude = 117.22743
|
2015-12-01 12:24:03 +00:00
|
|
|
|
2015-12-28 01:37:32 +00:00
|
|
|
def teardown_method(self, method):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop everything that was started."""
|
2015-12-01 12:24:03 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
2015-12-28 01:37:32 +00:00
|
|
|
def test_default_setup(self, betamax_session):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the default setup."""
|
2016-01-08 20:30:16 +00:00
|
|
|
now = datetime(2016, 1, 5, 1, tzinfo=dt_util.UTC)
|
|
|
|
|
2015-12-28 01:37:32 +00:00
|
|
|
with patch('homeassistant.components.sensor.yr.requests.Session',
|
|
|
|
return_value=betamax_session):
|
2016-01-08 20:30:16 +00: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-28 01:37:32 +00:00
|
|
|
|
2015-12-01 12:24:03 +00:00
|
|
|
state = self.hass.states.get('sensor.yr_symbol')
|
|
|
|
|
2016-02-01 14:50:17 +00:00
|
|
|
assert '46' == state.state
|
2015-12-28 01:37:32 +00:00
|
|
|
assert state.state.isnumeric()
|
|
|
|
assert state.attributes.get('unit_of_measurement') is None
|
|
|
|
|
|
|
|
def test_custom_setup(self, betamax_session):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test a custom setup."""
|
2016-02-01 14:50:17 +00:00
|
|
|
now = datetime(2016, 1, 5, 1, tzinfo=dt_util.UTC)
|
|
|
|
|
2015-12-28 01:37:32 +00:00
|
|
|
with patch('homeassistant.components.sensor.yr.requests.Session',
|
|
|
|
return_value=betamax_session):
|
2016-02-01 14:50:17 +00: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-28 01:37:32 +00:00
|
|
|
}
|
2016-02-01 14:50:17 +00:00
|
|
|
})
|
2015-12-01 12:24:03 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get('sensor.yr_pressure')
|
2016-02-01 14:50:17 +00:00
|
|
|
assert 'hPa' == state.attributes.get('unit_of_measurement')
|
|
|
|
assert '1025.1' == state.state
|
2015-12-01 12:24:03 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get('sensor.yr_wind_direction')
|
2016-02-13 13:19:11 +00:00
|
|
|
assert '°' == state.attributes.get('unit_of_measurement')
|
2016-02-01 14:50:17 +00:00
|
|
|
assert '81.8' == state.state
|
2015-12-01 12:24:03 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get('sensor.yr_humidity')
|
2016-02-01 14:50:17 +00:00
|
|
|
assert '%' == state.attributes.get('unit_of_measurement')
|
|
|
|
assert '79.6' == state.state
|
2015-12-01 12:24:03 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get('sensor.yr_fog')
|
2016-02-01 14:50:17 +00:00
|
|
|
assert '%' == state.attributes.get('unit_of_measurement')
|
|
|
|
assert '0.0' == state.state
|
2015-12-01 12:24:03 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get('sensor.yr_wind_speed')
|
2015-12-28 01:37:32 +00:00
|
|
|
assert 'm/s', state.attributes.get('unit_of_measurement')
|
2016-02-01 14:50:17 +00:00
|
|
|
assert '4.3' == state.state
|