mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Rewrite season sensor tests to pytest style (#40918)
* Update test_sensor.py * Update test_sensor.py * Update test_sensor.py * Update test_sensor.py * Update test_sensor.py * Update test_sensor.py * Update test_sensor.py * Update test_sensor.py * fix tests. * Update sensor.py * Update test_sensor.py * fix tests. * Update sensor.py * Update test_sensor.py * Update test_sensor.py * Update test_sensor.py
This commit is contained in:
parent
a2c3c33b22
commit
3a266fa116
@ -10,7 +10,7 @@ from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|||||||
from homeassistant.const import CONF_NAME, CONF_TYPE
|
from homeassistant.const import CONF_NAME, CONF_TYPE
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
import homeassistant.util.dt as dt_util
|
from homeassistant.util.dt import utcnow
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -72,7 +72,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
hemisphere = EQUATOR
|
hemisphere = EQUATOR
|
||||||
|
|
||||||
_LOGGER.debug(_type)
|
_LOGGER.debug(_type)
|
||||||
add_entities([Season(hass, hemisphere, _type, name)])
|
add_entities([Season(hass, hemisphere, _type, name)], True)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -117,9 +117,9 @@ class Season(Entity):
|
|||||||
self.hass = hass
|
self.hass = hass
|
||||||
self._name = name
|
self._name = name
|
||||||
self.hemisphere = hemisphere
|
self.hemisphere = hemisphere
|
||||||
self.datetime = dt_util.utcnow().replace(tzinfo=None)
|
self.datetime = None
|
||||||
self.type = season_tracking_type
|
self.type = season_tracking_type
|
||||||
self.season = get_season(self.datetime, self.hemisphere, self.type)
|
self.season = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -143,5 +143,5 @@ class Season(Entity):
|
|||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update season."""
|
"""Update season."""
|
||||||
self.datetime = dt_util.utcnow().replace(tzinfo=None)
|
self.datetime = utcnow().replace(tzinfo=None)
|
||||||
self.season = get_season(self.datetime, self.hemisphere, self.type)
|
self.season = get_season(self.datetime, self.hemisphere, self.type)
|
||||||
|
@ -1,12 +1,20 @@
|
|||||||
"""The tests for the Season sensor platform."""
|
"""The tests for the Season sensor platform."""
|
||||||
# pylint: disable=protected-access
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import unittest
|
|
||||||
|
|
||||||
import homeassistant.components.season.sensor as season
|
import pytest
|
||||||
from homeassistant.setup import setup_component
|
|
||||||
|
|
||||||
from tests.common import get_test_home_assistant
|
from homeassistant.components.season.sensor import (
|
||||||
|
STATE_AUTUMN,
|
||||||
|
STATE_SPRING,
|
||||||
|
STATE_SUMMER,
|
||||||
|
STATE_WINTER,
|
||||||
|
TYPE_ASTRONOMICAL,
|
||||||
|
TYPE_METEOROLOGICAL,
|
||||||
|
)
|
||||||
|
from homeassistant.const import STATE_UNKNOWN
|
||||||
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
|
from tests.async_mock import patch
|
||||||
|
|
||||||
HEMISPHERE_NORTHERN = {
|
HEMISPHERE_NORTHERN = {
|
||||||
"homeassistant": {"latitude": "48.864716", "longitude": "2.349014"},
|
"homeassistant": {"latitude": "48.864716", "longitude": "2.349014"},
|
||||||
@ -28,217 +36,90 @@ HEMISPHERE_EMPTY = {
|
|||||||
"sensor": {"platform": "season", "type": "meteorological"},
|
"sensor": {"platform": "season", "type": "meteorological"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NORTHERN_PARAMETERS = [
|
||||||
|
(TYPE_ASTRONOMICAL, datetime(2017, 9, 3, 0, 0), STATE_SUMMER),
|
||||||
|
(TYPE_METEOROLOGICAL, datetime(2017, 8, 13, 0, 0), STATE_SUMMER),
|
||||||
|
(TYPE_ASTRONOMICAL, datetime(2017, 9, 23, 0, 0), STATE_AUTUMN),
|
||||||
|
(TYPE_METEOROLOGICAL, datetime(2017, 9, 3, 0, 0), STATE_AUTUMN),
|
||||||
|
(TYPE_ASTRONOMICAL, datetime(2017, 12, 25, 0, 0), STATE_WINTER),
|
||||||
|
(TYPE_METEOROLOGICAL, datetime(2017, 12, 3, 0, 0), STATE_WINTER),
|
||||||
|
(TYPE_ASTRONOMICAL, datetime(2017, 4, 1, 0, 0), STATE_SPRING),
|
||||||
|
(TYPE_METEOROLOGICAL, datetime(2017, 3, 3, 0, 0), STATE_SPRING),
|
||||||
|
]
|
||||||
|
|
||||||
# pylint: disable=invalid-name
|
SOUTHERN_PARAMETERS = [
|
||||||
class TestSeason(unittest.TestCase):
|
(TYPE_ASTRONOMICAL, datetime(2017, 12, 25, 0, 0), STATE_SUMMER),
|
||||||
"""Test the season platform."""
|
(TYPE_METEOROLOGICAL, datetime(2017, 12, 3, 0, 0), STATE_SUMMER),
|
||||||
|
(TYPE_ASTRONOMICAL, datetime(2017, 4, 1, 0, 0), STATE_AUTUMN),
|
||||||
|
(TYPE_METEOROLOGICAL, datetime(2017, 3, 3, 0, 0), STATE_AUTUMN),
|
||||||
|
(TYPE_ASTRONOMICAL, datetime(2017, 9, 3, 0, 0), STATE_WINTER),
|
||||||
|
(TYPE_METEOROLOGICAL, datetime(2017, 8, 13, 0, 0), STATE_WINTER),
|
||||||
|
(TYPE_ASTRONOMICAL, datetime(2017, 9, 23, 0, 0), STATE_SPRING),
|
||||||
|
(TYPE_METEOROLOGICAL, datetime(2017, 9, 3, 0, 0), STATE_SPRING),
|
||||||
|
]
|
||||||
|
|
||||||
DEVICE = None
|
|
||||||
CONFIG_ASTRONOMICAL = {"type": "astronomical"}
|
|
||||||
CONFIG_METEOROLOGICAL = {"type": "meteorological"}
|
|
||||||
|
|
||||||
def add_entities(self, devices):
|
def idfn(val):
|
||||||
"""Mock add devices."""
|
"""Provide IDs for pytest parametrize."""
|
||||||
for device in devices:
|
if isinstance(val, (datetime)):
|
||||||
self.DEVICE = device
|
return val.strftime("%Y%m%d")
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
"""Set up things to be run when tests are started."""
|
|
||||||
self.hass = get_test_home_assistant()
|
|
||||||
self.addCleanup(self.hass.stop)
|
|
||||||
|
|
||||||
def test_season_should_be_summer_northern_astronomical(self):
|
@pytest.mark.parametrize("type,day,expected", NORTHERN_PARAMETERS, ids=idfn)
|
||||||
"""Test that season should be summer."""
|
async def test_season_northern_hemisphere(hass, type, day, expected):
|
||||||
# A known day in summer
|
"""Test that season should be summer."""
|
||||||
summer_day = datetime(2017, 9, 3, 0, 0)
|
hass.config.latitude = HEMISPHERE_NORTHERN["homeassistant"]["latitude"]
|
||||||
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):
|
config = {
|
||||||
"""Test that season should be summer."""
|
**HEMISPHERE_NORTHERN,
|
||||||
# A known day in summer
|
"sensor": {"platform": "season", "type": type},
|
||||||
summer_day = datetime(2017, 8, 13, 0, 0)
|
}
|
||||||
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):
|
with patch("homeassistant.components.season.sensor.utcnow", return_value=day):
|
||||||
"""Test that season should be autumn."""
|
assert await async_setup_component(hass, "sensor", config)
|
||||||
# A known day in autumn
|
await hass.async_block_till_done()
|
||||||
autumn_day = datetime(2017, 9, 23, 0, 0)
|
|
||||||
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):
|
state = hass.states.get("sensor.season")
|
||||||
"""Test that season should be autumn."""
|
assert state
|
||||||
# A known day in autumn
|
assert state.state == expected
|
||||||
autumn_day = datetime(2017, 9, 3, 0, 0)
|
|
||||||
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)
|
|
||||||
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):
|
@pytest.mark.parametrize("type,day,expected", SOUTHERN_PARAMETERS, ids=idfn)
|
||||||
"""Test that season should be winter."""
|
async def test_season_southern_hemisphere(hass, type, day, expected):
|
||||||
# A known day in winter
|
"""Test that season should be summer."""
|
||||||
winter_day = datetime(2017, 12, 3, 0, 0)
|
hass.config.latitude = HEMISPHERE_SOUTHERN["homeassistant"]["latitude"]
|
||||||
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):
|
config = {
|
||||||
"""Test that season should be spring."""
|
**HEMISPHERE_SOUTHERN,
|
||||||
# A known day in spring
|
"sensor": {"platform": "season", "type": type},
|
||||||
spring_day = datetime(2017, 4, 1, 0, 0)
|
}
|
||||||
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):
|
with patch("homeassistant.components.season.sensor.utcnow", return_value=day):
|
||||||
"""Test that season should be spring."""
|
assert await async_setup_component(hass, "sensor", config)
|
||||||
# A known day in spring
|
await hass.async_block_till_done()
|
||||||
spring_day = datetime(2017, 3, 3, 0, 0)
|
|
||||||
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):
|
state = hass.states.get("sensor.season")
|
||||||
"""Test that season should be winter."""
|
assert state
|
||||||
# A known day in winter
|
assert state.state == expected
|
||||||
winter_day = datetime(2017, 9, 3, 0, 0)
|
|
||||||
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)
|
|
||||||
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):
|
async def test_season_equator(hass):
|
||||||
"""Test that season should be spring."""
|
"""Test that season should be unknown for equator."""
|
||||||
# A known day in spring
|
hass.config.latitude = HEMISPHERE_EQUATOR["homeassistant"]["latitude"]
|
||||||
spring_day = datetime(2017, 9, 23, 0, 0)
|
day = datetime(2017, 9, 3, 0, 0)
|
||||||
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):
|
with patch("homeassistant.components.season.sensor.utcnow", return_value=day):
|
||||||
"""Test that season should be spring."""
|
assert await async_setup_component(hass, "sensor", HEMISPHERE_EQUATOR)
|
||||||
# A known day in spring
|
await hass.async_block_till_done()
|
||||||
spring_day = datetime(2017, 9, 3, 0, 0)
|
|
||||||
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):
|
state = hass.states.get("sensor.season")
|
||||||
"""Test that season should be summer."""
|
assert state
|
||||||
# A known day in summer
|
assert state.state == STATE_UNKNOWN
|
||||||
summer_day = datetime(2017, 12, 25, 0, 0)
|
|
||||||
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)
|
|
||||||
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):
|
async def test_setup_hemisphere_empty(hass):
|
||||||
"""Test that season should be spring."""
|
"""Test platform setup of missing latlong."""
|
||||||
# A known day in spring
|
hass.config.latitude = None
|
||||||
autumn_day = datetime(2017, 4, 1, 0, 0)
|
assert await async_setup_component(hass, "sensor", HEMISPHERE_EMPTY)
|
||||||
current_season = season.get_season(
|
await hass.async_block_till_done()
|
||||||
autumn_day, season.SOUTHERN, season.TYPE_ASTRONOMICAL
|
assert hass.config.as_dict()["latitude"] is None
|
||||||
)
|
|
||||||
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)
|
|
||||||
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)
|
|
||||||
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."""
|
|
||||||
self.hass.config.latitude = HEMISPHERE_NORTHERN["homeassistant"]["latitude"]
|
|
||||||
assert setup_component(self.hass, "sensor", HEMISPHERE_NORTHERN)
|
|
||||||
self.hass.block_till_done()
|
|
||||||
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."""
|
|
||||||
self.hass.config.latitude = HEMISPHERE_SOUTHERN["homeassistant"]["latitude"]
|
|
||||||
assert setup_component(self.hass, "sensor", HEMISPHERE_SOUTHERN)
|
|
||||||
self.hass.block_till_done()
|
|
||||||
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."""
|
|
||||||
self.hass.config.latitude = HEMISPHERE_EQUATOR["homeassistant"]["latitude"]
|
|
||||||
assert setup_component(self.hass, "sensor", HEMISPHERE_EQUATOR)
|
|
||||||
self.hass.block_till_done()
|
|
||||||
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
|
|
||||||
assert setup_component(self.hass, "sensor", HEMISPHERE_EMPTY)
|
|
||||||
self.hass.block_till_done()
|
|
||||||
assert self.hass.config.as_dict()["latitude"] is None
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user