Use enums in sensor tests (#62152)

This commit is contained in:
Robert Hillis 2021-12-17 05:07:18 -05:00 committed by GitHub
parent ad171944da
commit 07532c3153
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 59 deletions

View File

@ -2,14 +2,9 @@
import pytest import pytest
import homeassistant.components.automation as automation import homeassistant.components.automation as automation
from homeassistant.components.sensor import DOMAIN from homeassistant.components.sensor import DEVICE_CLASSES, DOMAIN, SensorDeviceClass
from homeassistant.components.sensor.device_condition import ENTITY_CONDITIONS from homeassistant.components.sensor.device_condition import ENTITY_CONDITIONS
from homeassistant.const import ( from homeassistant.const import CONF_PLATFORM, PERCENTAGE, STATE_UNKNOWN
CONF_PLATFORM,
DEVICE_CLASS_BATTERY,
PERCENTAGE,
STATE_UNKNOWN,
)
from homeassistant.helpers import device_registry from homeassistant.helpers import device_registry
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
@ -22,10 +17,7 @@ from tests.common import (
mock_registry, mock_registry,
) )
from tests.components.blueprint.conftest import stub_blueprint_populate # noqa: F401 from tests.components.blueprint.conftest import stub_blueprint_populate # noqa: F401
from tests.testing_config.custom_components.test.sensor import ( from tests.testing_config.custom_components.test.sensor import UNITS_OF_MEASUREMENT
DEVICE_CLASSES,
UNITS_OF_MEASUREMENT,
)
@pytest.fixture @pytest.fixture
@ -126,8 +118,8 @@ async def test_get_conditions_no_state(hass, device_reg, entity_reg):
@pytest.mark.parametrize( @pytest.mark.parametrize(
"set_state,device_class_reg,device_class_state,unit_reg,unit_state", "set_state,device_class_reg,device_class_state,unit_reg,unit_state",
[ [
(False, DEVICE_CLASS_BATTERY, None, PERCENTAGE, None), (False, SensorDeviceClass.BATTERY, None, PERCENTAGE, None),
(True, None, DEVICE_CLASS_BATTERY, None, PERCENTAGE), (True, None, SensorDeviceClass.BATTERY, None, PERCENTAGE),
], ],
) )
async def test_get_condition_capabilities( async def test_get_condition_capabilities(

View File

@ -4,14 +4,9 @@ from datetime import timedelta
import pytest import pytest
import homeassistant.components.automation as automation import homeassistant.components.automation as automation
from homeassistant.components.sensor import DOMAIN from homeassistant.components.sensor import DEVICE_CLASSES, DOMAIN, SensorDeviceClass
from homeassistant.components.sensor.device_trigger import ENTITY_TRIGGERS from homeassistant.components.sensor.device_trigger import ENTITY_TRIGGERS
from homeassistant.const import ( from homeassistant.const import CONF_PLATFORM, PERCENTAGE, STATE_UNKNOWN
CONF_PLATFORM,
DEVICE_CLASS_BATTERY,
PERCENTAGE,
STATE_UNKNOWN,
)
from homeassistant.helpers import device_registry from homeassistant.helpers import device_registry
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
@ -26,10 +21,7 @@ from tests.common import (
mock_registry, mock_registry,
) )
from tests.components.blueprint.conftest import stub_blueprint_populate # noqa: F401 from tests.components.blueprint.conftest import stub_blueprint_populate # noqa: F401
from tests.testing_config.custom_components.test.sensor import ( from tests.testing_config.custom_components.test.sensor import UNITS_OF_MEASUREMENT
DEVICE_CLASSES,
UNITS_OF_MEASUREMENT,
)
@pytest.fixture @pytest.fixture
@ -93,8 +85,8 @@ async def test_get_triggers(hass, device_reg, entity_reg, enable_custom_integrat
@pytest.mark.parametrize( @pytest.mark.parametrize(
"set_state,device_class_reg,device_class_state,unit_reg,unit_state", "set_state,device_class_reg,device_class_state,unit_reg,unit_state",
[ [
(False, DEVICE_CLASS_BATTERY, None, PERCENTAGE, None), (False, SensorDeviceClass.BATTERY, None, PERCENTAGE, None),
(True, None, DEVICE_CLASS_BATTERY, None, PERCENTAGE), (True, None, SensorDeviceClass.BATTERY, None, PERCENTAGE),
], ],
) )
async def test_get_trigger_capabilities( async def test_get_trigger_capabilities(

View File

@ -4,12 +4,9 @@ from datetime import date, datetime, timezone
import pytest import pytest
from pytest import approx from pytest import approx
from homeassistant.components.sensor import SensorEntityDescription from homeassistant.components.sensor import SensorDeviceClass, SensorEntityDescription
from homeassistant.const import ( from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT, ATTR_UNIT_OF_MEASUREMENT,
DEVICE_CLASS_DATE,
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_TIMESTAMP,
STATE_UNKNOWN, STATE_UNKNOWN,
TEMP_CELSIUS, TEMP_CELSIUS,
TEMP_FAHRENHEIT, TEMP_FAHRENHEIT,
@ -45,7 +42,7 @@ async def test_temperature_conversion(
name="Test", name="Test",
native_value=str(native_value), native_value=str(native_value),
native_unit_of_measurement=native_unit, native_unit_of_measurement=native_unit,
device_class=DEVICE_CLASS_TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
) )
entity0 = platform.ENTITIES["0"] entity0 = platform.ENTITIES["0"]
@ -124,21 +121,23 @@ async def test_datetime_conversion(hass, caplog, enable_custom_integrations):
platform = getattr(hass.components, "test.sensor") platform = getattr(hass.components, "test.sensor")
platform.init(empty=True) platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor( platform.ENTITIES["0"] = platform.MockSensor(
name="Test", native_value=test_timestamp, device_class=DEVICE_CLASS_TIMESTAMP name="Test",
native_value=test_timestamp,
device_class=SensorDeviceClass.TIMESTAMP,
) )
platform.ENTITIES["1"] = platform.MockSensor( platform.ENTITIES["1"] = platform.MockSensor(
name="Test", native_value=test_date, device_class=DEVICE_CLASS_DATE name="Test", native_value=test_date, device_class=SensorDeviceClass.DATE
) )
platform.ENTITIES["2"] = platform.MockSensor( platform.ENTITIES["2"] = platform.MockSensor(
name="Test", native_value=None, device_class=DEVICE_CLASS_TIMESTAMP name="Test", native_value=None, device_class=SensorDeviceClass.TIMESTAMP
) )
platform.ENTITIES["3"] = platform.MockSensor( platform.ENTITIES["3"] = platform.MockSensor(
name="Test", native_value=None, device_class=DEVICE_CLASS_DATE name="Test", native_value=None, device_class=SensorDeviceClass.DATE
) )
platform.ENTITIES["4"] = platform.MockSensor( platform.ENTITIES["4"] = platform.MockSensor(
name="Test", name="Test",
native_value=test_local_timestamp, native_value=test_local_timestamp,
device_class=DEVICE_CLASS_TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
) )
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
@ -163,44 +162,44 @@ async def test_datetime_conversion(hass, caplog, enable_custom_integrations):
@pytest.mark.parametrize( @pytest.mark.parametrize(
"device_class,native_value,state_value", "device_class,native_value,state_value",
[ [
(DEVICE_CLASS_DATE, "2021-11-09", "2021-11-09"), (SensorDeviceClass.DATE, "2021-11-09", "2021-11-09"),
( (
DEVICE_CLASS_DATE, SensorDeviceClass.DATE,
"2021-01-09T12:00:00+00:00", "2021-01-09T12:00:00+00:00",
"2021-01-09", "2021-01-09",
), ),
( (
DEVICE_CLASS_DATE, SensorDeviceClass.DATE,
"2021-01-09T00:00:00+01:00", "2021-01-09T00:00:00+01:00",
"2021-01-08", "2021-01-08",
), ),
( (
DEVICE_CLASS_TIMESTAMP, SensorDeviceClass.TIMESTAMP,
"2021-01-09T12:00:00+00:00", "2021-01-09T12:00:00+00:00",
"2021-01-09T12:00:00+00:00", "2021-01-09T12:00:00+00:00",
), ),
( (
DEVICE_CLASS_TIMESTAMP, SensorDeviceClass.TIMESTAMP,
"2021-01-09 12:00:00+00:00", "2021-01-09 12:00:00+00:00",
"2021-01-09T12:00:00+00:00", "2021-01-09T12:00:00+00:00",
), ),
( (
DEVICE_CLASS_TIMESTAMP, SensorDeviceClass.TIMESTAMP,
"2021-01-09T12:00:00+04:00", "2021-01-09T12:00:00+04:00",
"2021-01-09T08:00:00+00:00", "2021-01-09T08:00:00+00:00",
), ),
( (
DEVICE_CLASS_TIMESTAMP, SensorDeviceClass.TIMESTAMP,
"2021-01-09 12:00:00+01:00", "2021-01-09 12:00:00+01:00",
"2021-01-09T11:00:00+00:00", "2021-01-09T11:00:00+00:00",
), ),
( (
DEVICE_CLASS_TIMESTAMP, SensorDeviceClass.TIMESTAMP,
"2021-01-09 12:00:00", "2021-01-09 12:00:00",
"2021-01-09T12:00:00", "2021-01-09T12:00:00",
), ),
( (
DEVICE_CLASS_TIMESTAMP, SensorDeviceClass.TIMESTAMP,
"2021-01-09T12:00:00", "2021-01-09T12:00:00",
"2021-01-09T12:00:00", "2021-01-09T12:00:00",
), ),
@ -237,7 +236,9 @@ async def test_reject_timezoneless_datetime_str(
platform = getattr(hass.components, "test.sensor") platform = getattr(hass.components, "test.sensor")
platform.init(empty=True) platform.init(empty=True)
platform.ENTITIES["0"] = platform.MockSensor( platform.ENTITIES["0"] = platform.MockSensor(
name="Test", native_value=test_timestamp, device_class=DEVICE_CLASS_TIMESTAMP name="Test",
native_value=test_timestamp,
device_class=SensorDeviceClass.TIMESTAMP,
) )
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})

View File

@ -1,13 +1,7 @@
"""Test the sensor significant change platform.""" """Test the sensor significant change platform."""
import pytest import pytest
from homeassistant.components.sensor.significant_change import ( from homeassistant.components.sensor import SensorDeviceClass, significant_change
DEVICE_CLASS_AQI,
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_TEMPERATURE,
async_check_significant_change,
)
from homeassistant.const import ( from homeassistant.const import (
ATTR_DEVICE_CLASS, ATTR_DEVICE_CLASS,
ATTR_UNIT_OF_MEASUREMENT, ATTR_UNIT_OF_MEASUREMENT,
@ -16,24 +10,24 @@ from homeassistant.const import (
) )
AQI_ATTRS = { AQI_ATTRS = {
ATTR_DEVICE_CLASS: DEVICE_CLASS_AQI, ATTR_DEVICE_CLASS: SensorDeviceClass.AQI,
} }
BATTERY_ATTRS = { BATTERY_ATTRS = {
ATTR_DEVICE_CLASS: DEVICE_CLASS_BATTERY, ATTR_DEVICE_CLASS: SensorDeviceClass.BATTERY,
} }
HUMIDITY_ATTRS = { HUMIDITY_ATTRS = {
ATTR_DEVICE_CLASS: DEVICE_CLASS_HUMIDITY, ATTR_DEVICE_CLASS: SensorDeviceClass.HUMIDITY,
} }
TEMP_CELSIUS_ATTRS = { TEMP_CELSIUS_ATTRS = {
ATTR_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE, ATTR_DEVICE_CLASS: SensorDeviceClass.TEMPERATURE,
ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS, ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS,
} }
TEMP_FREEDOM_ATTRS = { TEMP_FREEDOM_ATTRS = {
ATTR_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE, ATTR_DEVICE_CLASS: SensorDeviceClass.TEMPERATURE,
ATTR_UNIT_OF_MEASUREMENT: TEMP_FAHRENHEIT, ATTR_UNIT_OF_MEASUREMENT: TEMP_FAHRENHEIT,
} }
@ -63,6 +57,8 @@ TEMP_FREEDOM_ATTRS = {
async def test_significant_change_temperature(old_state, new_state, attrs, result): async def test_significant_change_temperature(old_state, new_state, attrs, result):
"""Detect temperature significant changes.""" """Detect temperature significant changes."""
assert ( assert (
async_check_significant_change(None, old_state, attrs, new_state, attrs) significant_change.async_check_significant_change(
None, old_state, attrs, new_state, attrs
)
is result is result
) )