core/tests/components/sensor/test_yr.py

81 lines
3.2 KiB
Python
Raw Normal View History

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
2016-04-10 17:43:05 +00:00
from homeassistant.bootstrap import _setup_component
2016-01-08 20:30:16 +00:00
import homeassistant.util.dt as dt_util
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-06-09 04:05:57 +00:00
now = datetime(2016, 6, 9, 1, tzinfo=dt_util.UTC)
2016-01-08 20:30:16 +00:00
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):
2016-04-10 17:43:05 +00:00
assert _setup_component(self.hass, 'sensor', {
'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-06-09 04:05:57 +00:00
assert '3' == 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-06-09 04:05:57 +00:00
now = datetime(2016, 6, 9, 1, tzinfo=dt_util.UTC)
2016-02-01 14:50:17 +00:00
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):
2016-04-10 17:43:05 +00:00
assert _setup_component(self.hass, 'sensor', {
'sensor': {'platform': 'yr',
'elevation': 0,
'monitored_conditions': [
'pressure',
'windDirection',
'humidity',
'fog',
'windSpeed']}})
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')
2016-06-09 04:05:57 +00:00
assert '1009.3' == state.state
2015-12-01 12:24:03 +00:00
state = self.hass.states.get('sensor.yr_wind_direction')
assert '°' == state.attributes.get('unit_of_measurement')
2016-06-09 04:05:57 +00:00
assert '103.6' == 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')
2016-06-09 04:05:57 +00:00
assert '55.5' == 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-06-09 04:05:57 +00:00
assert '3.5' == state.state