core/tests/components/season/test_sensor.py

245 lines
9.4 KiB
Python
Raw Normal View History

"""The tests for the Season sensor platform."""
# pylint: disable=protected-access
import unittest
from datetime import datetime
from homeassistant.setup import setup_component
Consolidate all platforms that have tests (#22109) * Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
2019-03-19 06:07:39 +00:00
import homeassistant.components.season.sensor as season
from tests.common import get_test_home_assistant
HEMISPHERE_NORTHERN = {
2019-07-31 19:25:30 +00:00
"homeassistant": {"latitude": "48.864716", "longitude": "2.349014"},
"sensor": {"platform": "season", "type": "astronomical"},
}
HEMISPHERE_SOUTHERN = {
2019-07-31 19:25:30 +00:00
"homeassistant": {"latitude": "-33.918861", "longitude": "18.423300"},
"sensor": {"platform": "season", "type": "astronomical"},
}
HEMISPHERE_EQUATOR = {
2019-07-31 19:25:30 +00:00
"homeassistant": {"latitude": "0", "longitude": "-51.065100"},
"sensor": {"platform": "season", "type": "astronomical"},
}
HEMISPHERE_EMPTY = {
2019-07-31 19:25:30 +00:00
"homeassistant": {},
"sensor": {"platform": "season", "type": "meteorological"},
}
# pylint: disable=invalid-name
class TestSeason(unittest.TestCase):
"""Test the season platform."""
DEVICE = None
2019-07-31 19:25:30 +00:00
CONFIG_ASTRONOMICAL = {"type": "astronomical"}
CONFIG_METEOROLOGICAL = {"type": "meteorological"}
def add_entities(self, devices):
"""Mock add devices."""
for device in devices:
self.DEVICE = device
def setUp(self):
2018-08-19 20:29:08 +00:00
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
def tearDown(self):
"""Stop everything that was started."""
self.hass.stop()
def test_season_should_be_summer_northern_astronomical(self):
"""Test that season should be summer."""
# A known day in summer
summer_day = datetime(2017, 9, 3, 0, 0)
2019-07-31 19:25:30 +00:00
current_season = season.get_season(
summer_day, season.NORTHERN, season.TYPE_ASTRONOMICAL
)
assert season.STATE_SUMMER == current_season
def test_season_should_be_summer_northern_meteorological(self):
"""Test that season should be summer."""
# A known day in summer
summer_day = datetime(2017, 8, 13, 0, 0)
2019-07-31 19:25:30 +00:00
current_season = season.get_season(
summer_day, season.NORTHERN, season.TYPE_METEOROLOGICAL
)
assert season.STATE_SUMMER == current_season
def test_season_should_be_autumn_northern_astronomical(self):
"""Test that season should be autumn."""
# A known day in autumn
autumn_day = datetime(2017, 9, 23, 0, 0)
2019-07-31 19:25:30 +00:00
current_season = season.get_season(
autumn_day, season.NORTHERN, season.TYPE_ASTRONOMICAL
)
assert season.STATE_AUTUMN == current_season
def test_season_should_be_autumn_northern_meteorological(self):
"""Test that season should be autumn."""
# A known day in autumn
autumn_day = datetime(2017, 9, 3, 0, 0)
2019-07-31 19:25:30 +00:00
current_season = season.get_season(
autumn_day, season.NORTHERN, season.TYPE_METEOROLOGICAL
)
assert season.STATE_AUTUMN == current_season
def test_season_should_be_winter_northern_astronomical(self):
"""Test that season should be winter."""
# A known day in winter
winter_day = datetime(2017, 12, 25, 0, 0)
2019-07-31 19:25:30 +00:00
current_season = season.get_season(
winter_day, season.NORTHERN, season.TYPE_ASTRONOMICAL
)
assert season.STATE_WINTER == current_season
def test_season_should_be_winter_northern_meteorological(self):
"""Test that season should be winter."""
# A known day in winter
winter_day = datetime(2017, 12, 3, 0, 0)
2019-07-31 19:25:30 +00:00
current_season = season.get_season(
winter_day, season.NORTHERN, season.TYPE_METEOROLOGICAL
)
assert season.STATE_WINTER == current_season
def test_season_should_be_spring_northern_astronomical(self):
"""Test that season should be spring."""
# A known day in spring
spring_day = datetime(2017, 4, 1, 0, 0)
2019-07-31 19:25:30 +00:00
current_season = season.get_season(
spring_day, season.NORTHERN, season.TYPE_ASTRONOMICAL
)
assert season.STATE_SPRING == current_season
def test_season_should_be_spring_northern_meteorological(self):
"""Test that season should be spring."""
# A known day in spring
spring_day = datetime(2017, 3, 3, 0, 0)
2019-07-31 19:25:30 +00:00
current_season = season.get_season(
spring_day, season.NORTHERN, season.TYPE_METEOROLOGICAL
)
assert season.STATE_SPRING == current_season
def test_season_should_be_winter_southern_astronomical(self):
"""Test that season should be winter."""
# A known day in winter
winter_day = datetime(2017, 9, 3, 0, 0)
2019-07-31 19:25:30 +00:00
current_season = season.get_season(
winter_day, season.SOUTHERN, season.TYPE_ASTRONOMICAL
)
assert season.STATE_WINTER == current_season
def test_season_should_be_winter_southern_meteorological(self):
"""Test that season should be winter."""
# A known day in winter
winter_day = datetime(2017, 8, 13, 0, 0)
2019-07-31 19:25:30 +00:00
current_season = season.get_season(
winter_day, season.SOUTHERN, season.TYPE_METEOROLOGICAL
)
assert season.STATE_WINTER == current_season
def test_season_should_be_spring_southern_astronomical(self):
"""Test that season should be spring."""
# A known day in spring
spring_day = datetime(2017, 9, 23, 0, 0)
2019-07-31 19:25:30 +00:00
current_season = season.get_season(
spring_day, season.SOUTHERN, season.TYPE_ASTRONOMICAL
)
assert season.STATE_SPRING == current_season
def test_season_should_be_spring_southern_meteorological(self):
"""Test that season should be spring."""
# A known day in spring
spring_day = datetime(2017, 9, 3, 0, 0)
2019-07-31 19:25:30 +00:00
current_season = season.get_season(
spring_day, season.SOUTHERN, season.TYPE_METEOROLOGICAL
)
assert season.STATE_SPRING == current_season
def test_season_should_be_summer_southern_astronomical(self):
"""Test that season should be summer."""
# A known day in summer
summer_day = datetime(2017, 12, 25, 0, 0)
2019-07-31 19:25:30 +00:00
current_season = season.get_season(
summer_day, season.SOUTHERN, season.TYPE_ASTRONOMICAL
)
assert season.STATE_SUMMER == current_season
def test_season_should_be_summer_southern_meteorological(self):
"""Test that season should be summer."""
# A known day in summer
summer_day = datetime(2017, 12, 3, 0, 0)
2019-07-31 19:25:30 +00:00
current_season = season.get_season(
summer_day, season.SOUTHERN, season.TYPE_METEOROLOGICAL
)
assert season.STATE_SUMMER == current_season
def test_season_should_be_autumn_southern_astronomical(self):
"""Test that season should be spring."""
# A known day in spring
autumn_day = datetime(2017, 4, 1, 0, 0)
2019-07-31 19:25:30 +00:00
current_season = season.get_season(
autumn_day, season.SOUTHERN, season.TYPE_ASTRONOMICAL
)
assert season.STATE_AUTUMN == current_season
def test_season_should_be_autumn_southern_meteorological(self):
"""Test that season should be autumn."""
# A known day in autumn
autumn_day = datetime(2017, 3, 3, 0, 0)
2019-07-31 19:25:30 +00:00
current_season = season.get_season(
autumn_day, season.SOUTHERN, season.TYPE_METEOROLOGICAL
)
assert season.STATE_AUTUMN == current_season
def test_on_equator_results_in_none(self):
"""Test that season should be unknown."""
# A known day in summer if astronomical and northern
summer_day = datetime(2017, 9, 3, 0, 0)
2019-07-31 19:25:30 +00:00
current_season = season.get_season(
summer_day, season.EQUATOR, season.TYPE_ASTRONOMICAL
)
assert current_season is None
def test_setup_hemisphere_northern(self):
"""Test platform setup of northern hemisphere."""
2019-07-31 19:25:30 +00:00
self.hass.config.latitude = HEMISPHERE_NORTHERN["homeassistant"]["latitude"]
assert setup_component(self.hass, "sensor", HEMISPHERE_NORTHERN)
assert (
self.hass.config.as_dict()["latitude"]
== HEMISPHERE_NORTHERN["homeassistant"]["latitude"]
)
state = self.hass.states.get("sensor.season")
assert state.attributes.get("friendly_name") == "Season"
def test_setup_hemisphere_southern(self):
"""Test platform setup of southern hemisphere."""
2019-07-31 19:25:30 +00:00
self.hass.config.latitude = HEMISPHERE_SOUTHERN["homeassistant"]["latitude"]
assert setup_component(self.hass, "sensor", HEMISPHERE_SOUTHERN)
assert (
self.hass.config.as_dict()["latitude"]
== HEMISPHERE_SOUTHERN["homeassistant"]["latitude"]
)
state = self.hass.states.get("sensor.season")
assert state.attributes.get("friendly_name") == "Season"
def test_setup_hemisphere_equator(self):
"""Test platform setup of equator."""
2019-07-31 19:25:30 +00:00
self.hass.config.latitude = HEMISPHERE_EQUATOR["homeassistant"]["latitude"]
assert setup_component(self.hass, "sensor", HEMISPHERE_EQUATOR)
assert (
self.hass.config.as_dict()["latitude"]
== HEMISPHERE_EQUATOR["homeassistant"]["latitude"]
)
state = self.hass.states.get("sensor.season")
assert state.attributes.get("friendly_name") == "Season"
def test_setup_hemisphere_empty(self):
"""Test platform setup of missing latlong."""
self.hass.config.latitude = None
2019-07-31 19:25:30 +00:00
assert setup_component(self.hass, "sensor", HEMISPHERE_EMPTY)
assert self.hass.config.as_dict()["latitude"] is None