mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +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
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import Entity
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -72,7 +72,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
hemisphere = EQUATOR
|
||||
|
||||
_LOGGER.debug(_type)
|
||||
add_entities([Season(hass, hemisphere, _type, name)])
|
||||
add_entities([Season(hass, hemisphere, _type, name)], True)
|
||||
|
||||
return True
|
||||
|
||||
@ -117,9 +117,9 @@ class Season(Entity):
|
||||
self.hass = hass
|
||||
self._name = name
|
||||
self.hemisphere = hemisphere
|
||||
self.datetime = dt_util.utcnow().replace(tzinfo=None)
|
||||
self.datetime = None
|
||||
self.type = season_tracking_type
|
||||
self.season = get_season(self.datetime, self.hemisphere, self.type)
|
||||
self.season = None
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
@ -143,5 +143,5 @@ class Season(Entity):
|
||||
|
||||
def update(self):
|
||||
"""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)
|
||||
|
@ -1,12 +1,20 @@
|
||||
"""The tests for the Season sensor platform."""
|
||||
# pylint: disable=protected-access
|
||||
from datetime import datetime
|
||||
import unittest
|
||||
|
||||
import homeassistant.components.season.sensor as season
|
||||
from homeassistant.setup import setup_component
|
||||
import pytest
|
||||
|
||||
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 = {
|
||||
"homeassistant": {"latitude": "48.864716", "longitude": "2.349014"},
|
||||
@ -28,217 +36,90 @@ HEMISPHERE_EMPTY = {
|
||||
"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
|
||||
class TestSeason(unittest.TestCase):
|
||||
"""Test the season platform."""
|
||||
SOUTHERN_PARAMETERS = [
|
||||
(TYPE_ASTRONOMICAL, datetime(2017, 12, 25, 0, 0), STATE_SUMMER),
|
||||
(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):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
self.DEVICE = device
|
||||
def idfn(val):
|
||||
"""Provide IDs for pytest parametrize."""
|
||||
if isinstance(val, (datetime)):
|
||||
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):
|
||||
"""Test that season should be summer."""
|
||||
# A known day in summer
|
||||
summer_day = datetime(2017, 9, 3, 0, 0)
|
||||
current_season = season.get_season(
|
||||
summer_day, season.NORTHERN, season.TYPE_ASTRONOMICAL
|
||||
)
|
||||
assert season.STATE_SUMMER == current_season
|
||||
@pytest.mark.parametrize("type,day,expected", NORTHERN_PARAMETERS, ids=idfn)
|
||||
async def test_season_northern_hemisphere(hass, type, day, expected):
|
||||
"""Test that season should be summer."""
|
||||
hass.config.latitude = HEMISPHERE_NORTHERN["homeassistant"]["latitude"]
|
||||
|
||||
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)
|
||||
current_season = season.get_season(
|
||||
summer_day, season.NORTHERN, season.TYPE_METEOROLOGICAL
|
||||
)
|
||||
assert season.STATE_SUMMER == current_season
|
||||
config = {
|
||||
**HEMISPHERE_NORTHERN,
|
||||
"sensor": {"platform": "season", "type": type},
|
||||
}
|
||||
|
||||
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)
|
||||
current_season = season.get_season(
|
||||
autumn_day, season.NORTHERN, season.TYPE_ASTRONOMICAL
|
||||
)
|
||||
assert season.STATE_AUTUMN == current_season
|
||||
with patch("homeassistant.components.season.sensor.utcnow", return_value=day):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
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)
|
||||
current_season = season.get_season(
|
||||
autumn_day, season.NORTHERN, season.TYPE_METEOROLOGICAL
|
||||
)
|
||||
assert season.STATE_AUTUMN == current_season
|
||||
state = hass.states.get("sensor.season")
|
||||
assert state
|
||||
assert state.state == expected
|
||||
|
||||
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):
|
||||
"""Test that season should be winter."""
|
||||
# A known day in winter
|
||||
winter_day = datetime(2017, 12, 3, 0, 0)
|
||||
current_season = season.get_season(
|
||||
winter_day, season.NORTHERN, season.TYPE_METEOROLOGICAL
|
||||
)
|
||||
assert season.STATE_WINTER == current_season
|
||||
@pytest.mark.parametrize("type,day,expected", SOUTHERN_PARAMETERS, ids=idfn)
|
||||
async def test_season_southern_hemisphere(hass, type, day, expected):
|
||||
"""Test that season should be summer."""
|
||||
hass.config.latitude = HEMISPHERE_SOUTHERN["homeassistant"]["latitude"]
|
||||
|
||||
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)
|
||||
current_season = season.get_season(
|
||||
spring_day, season.NORTHERN, season.TYPE_ASTRONOMICAL
|
||||
)
|
||||
assert season.STATE_SPRING == current_season
|
||||
config = {
|
||||
**HEMISPHERE_SOUTHERN,
|
||||
"sensor": {"platform": "season", "type": type},
|
||||
}
|
||||
|
||||
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)
|
||||
current_season = season.get_season(
|
||||
spring_day, season.NORTHERN, season.TYPE_METEOROLOGICAL
|
||||
)
|
||||
assert season.STATE_SPRING == current_season
|
||||
with patch("homeassistant.components.season.sensor.utcnow", return_value=day):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
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)
|
||||
current_season = season.get_season(
|
||||
winter_day, season.SOUTHERN, season.TYPE_ASTRONOMICAL
|
||||
)
|
||||
assert season.STATE_WINTER == current_season
|
||||
state = hass.states.get("sensor.season")
|
||||
assert state
|
||||
assert state.state == expected
|
||||
|
||||
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):
|
||||
"""Test that season should be spring."""
|
||||
# A known day in spring
|
||||
spring_day = datetime(2017, 9, 23, 0, 0)
|
||||
current_season = season.get_season(
|
||||
spring_day, season.SOUTHERN, season.TYPE_ASTRONOMICAL
|
||||
)
|
||||
assert season.STATE_SPRING == current_season
|
||||
async def test_season_equator(hass):
|
||||
"""Test that season should be unknown for equator."""
|
||||
hass.config.latitude = HEMISPHERE_EQUATOR["homeassistant"]["latitude"]
|
||||
day = datetime(2017, 9, 3, 0, 0)
|
||||
|
||||
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)
|
||||
current_season = season.get_season(
|
||||
spring_day, season.SOUTHERN, season.TYPE_METEOROLOGICAL
|
||||
)
|
||||
assert season.STATE_SPRING == current_season
|
||||
with patch("homeassistant.components.season.sensor.utcnow", return_value=day):
|
||||
assert await async_setup_component(hass, "sensor", HEMISPHERE_EQUATOR)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
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)
|
||||
current_season = season.get_season(
|
||||
summer_day, season.SOUTHERN, season.TYPE_ASTRONOMICAL
|
||||
)
|
||||
assert season.STATE_SUMMER == current_season
|
||||
state = hass.states.get("sensor.season")
|
||||
assert state
|
||||
assert state.state == STATE_UNKNOWN
|
||||
|
||||
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):
|
||||
"""Test that season should be spring."""
|
||||
# A known day in spring
|
||||
autumn_day = datetime(2017, 4, 1, 0, 0)
|
||||
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)
|
||||
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
|
||||
async def test_setup_hemisphere_empty(hass):
|
||||
"""Test platform setup of missing latlong."""
|
||||
hass.config.latitude = None
|
||||
assert await async_setup_component(hass, "sensor", HEMISPHERE_EMPTY)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.config.as_dict()["latitude"] is None
|
||||
|
Loading…
x
Reference in New Issue
Block a user