From 07532c3153e3e614a70ade43477ab6fceec28894 Mon Sep 17 00:00:00 2001 From: Robert Hillis Date: Fri, 17 Dec 2021 05:07:18 -0500 Subject: [PATCH] Use enums in sensor tests (#62152) --- .../sensor/test_device_condition.py | 18 +++----- .../components/sensor/test_device_trigger.py | 18 +++----- tests/components/sensor/test_init.py | 41 ++++++++++--------- .../sensor/test_significant_change.py | 22 ++++------ 4 files changed, 40 insertions(+), 59 deletions(-) diff --git a/tests/components/sensor/test_device_condition.py b/tests/components/sensor/test_device_condition.py index 5742f7b47c4..5504bd997af 100644 --- a/tests/components/sensor/test_device_condition.py +++ b/tests/components/sensor/test_device_condition.py @@ -2,14 +2,9 @@ import pytest 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.const import ( - CONF_PLATFORM, - DEVICE_CLASS_BATTERY, - PERCENTAGE, - STATE_UNKNOWN, -) +from homeassistant.const import CONF_PLATFORM, PERCENTAGE, STATE_UNKNOWN from homeassistant.helpers import device_registry from homeassistant.setup import async_setup_component @@ -22,10 +17,7 @@ from tests.common import ( mock_registry, ) from tests.components.blueprint.conftest import stub_blueprint_populate # noqa: F401 -from tests.testing_config.custom_components.test.sensor import ( - DEVICE_CLASSES, - UNITS_OF_MEASUREMENT, -) +from tests.testing_config.custom_components.test.sensor import UNITS_OF_MEASUREMENT @pytest.fixture @@ -126,8 +118,8 @@ async def test_get_conditions_no_state(hass, device_reg, entity_reg): @pytest.mark.parametrize( "set_state,device_class_reg,device_class_state,unit_reg,unit_state", [ - (False, DEVICE_CLASS_BATTERY, None, PERCENTAGE, None), - (True, None, DEVICE_CLASS_BATTERY, None, PERCENTAGE), + (False, SensorDeviceClass.BATTERY, None, PERCENTAGE, None), + (True, None, SensorDeviceClass.BATTERY, None, PERCENTAGE), ], ) async def test_get_condition_capabilities( diff --git a/tests/components/sensor/test_device_trigger.py b/tests/components/sensor/test_device_trigger.py index b8b3ee46a43..41671a4faa8 100644 --- a/tests/components/sensor/test_device_trigger.py +++ b/tests/components/sensor/test_device_trigger.py @@ -4,14 +4,9 @@ from datetime import timedelta import pytest 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.const import ( - CONF_PLATFORM, - DEVICE_CLASS_BATTERY, - PERCENTAGE, - STATE_UNKNOWN, -) +from homeassistant.const import CONF_PLATFORM, PERCENTAGE, STATE_UNKNOWN from homeassistant.helpers import device_registry from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util @@ -26,10 +21,7 @@ from tests.common import ( mock_registry, ) from tests.components.blueprint.conftest import stub_blueprint_populate # noqa: F401 -from tests.testing_config.custom_components.test.sensor import ( - DEVICE_CLASSES, - UNITS_OF_MEASUREMENT, -) +from tests.testing_config.custom_components.test.sensor import UNITS_OF_MEASUREMENT @pytest.fixture @@ -93,8 +85,8 @@ async def test_get_triggers(hass, device_reg, entity_reg, enable_custom_integrat @pytest.mark.parametrize( "set_state,device_class_reg,device_class_state,unit_reg,unit_state", [ - (False, DEVICE_CLASS_BATTERY, None, PERCENTAGE, None), - (True, None, DEVICE_CLASS_BATTERY, None, PERCENTAGE), + (False, SensorDeviceClass.BATTERY, None, PERCENTAGE, None), + (True, None, SensorDeviceClass.BATTERY, None, PERCENTAGE), ], ) async def test_get_trigger_capabilities( diff --git a/tests/components/sensor/test_init.py b/tests/components/sensor/test_init.py index d5deee41679..919736fb59e 100644 --- a/tests/components/sensor/test_init.py +++ b/tests/components/sensor/test_init.py @@ -4,12 +4,9 @@ from datetime import date, datetime, timezone import pytest from pytest import approx -from homeassistant.components.sensor import SensorEntityDescription +from homeassistant.components.sensor import SensorDeviceClass, SensorEntityDescription from homeassistant.const import ( ATTR_UNIT_OF_MEASUREMENT, - DEVICE_CLASS_DATE, - DEVICE_CLASS_TEMPERATURE, - DEVICE_CLASS_TIMESTAMP, STATE_UNKNOWN, TEMP_CELSIUS, TEMP_FAHRENHEIT, @@ -45,7 +42,7 @@ async def test_temperature_conversion( name="Test", native_value=str(native_value), native_unit_of_measurement=native_unit, - device_class=DEVICE_CLASS_TEMPERATURE, + device_class=SensorDeviceClass.TEMPERATURE, ) 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.init(empty=True) 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( - 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( - name="Test", native_value=None, device_class=DEVICE_CLASS_TIMESTAMP + name="Test", native_value=None, device_class=SensorDeviceClass.TIMESTAMP ) 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( name="Test", native_value=test_local_timestamp, - device_class=DEVICE_CLASS_TIMESTAMP, + device_class=SensorDeviceClass.TIMESTAMP, ) 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( "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-09", ), ( - DEVICE_CLASS_DATE, + SensorDeviceClass.DATE, "2021-01-09T00:00:00+01:00", "2021-01-08", ), ( - DEVICE_CLASS_TIMESTAMP, + SensorDeviceClass.TIMESTAMP, "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-09T12:00:00+00:00", ), ( - DEVICE_CLASS_TIMESTAMP, + SensorDeviceClass.TIMESTAMP, "2021-01-09T12:00:00+04:00", "2021-01-09T08:00:00+00:00", ), ( - DEVICE_CLASS_TIMESTAMP, + SensorDeviceClass.TIMESTAMP, "2021-01-09 12:00:00+01:00", "2021-01-09T11:00:00+00:00", ), ( - DEVICE_CLASS_TIMESTAMP, + SensorDeviceClass.TIMESTAMP, "2021-01-09 12:00:00", "2021-01-09T12:00:00", ), ( - DEVICE_CLASS_TIMESTAMP, + SensorDeviceClass.TIMESTAMP, "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.init(empty=True) 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"}}) diff --git a/tests/components/sensor/test_significant_change.py b/tests/components/sensor/test_significant_change.py index 22a2c22ecc7..051a92f3b07 100644 --- a/tests/components/sensor/test_significant_change.py +++ b/tests/components/sensor/test_significant_change.py @@ -1,13 +1,7 @@ """Test the sensor significant change platform.""" import pytest -from homeassistant.components.sensor.significant_change import ( - DEVICE_CLASS_AQI, - DEVICE_CLASS_BATTERY, - DEVICE_CLASS_HUMIDITY, - DEVICE_CLASS_TEMPERATURE, - async_check_significant_change, -) +from homeassistant.components.sensor import SensorDeviceClass, significant_change from homeassistant.const import ( ATTR_DEVICE_CLASS, ATTR_UNIT_OF_MEASUREMENT, @@ -16,24 +10,24 @@ from homeassistant.const import ( ) AQI_ATTRS = { - ATTR_DEVICE_CLASS: DEVICE_CLASS_AQI, + ATTR_DEVICE_CLASS: SensorDeviceClass.AQI, } BATTERY_ATTRS = { - ATTR_DEVICE_CLASS: DEVICE_CLASS_BATTERY, + ATTR_DEVICE_CLASS: SensorDeviceClass.BATTERY, } HUMIDITY_ATTRS = { - ATTR_DEVICE_CLASS: DEVICE_CLASS_HUMIDITY, + ATTR_DEVICE_CLASS: SensorDeviceClass.HUMIDITY, } TEMP_CELSIUS_ATTRS = { - ATTR_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE, + ATTR_DEVICE_CLASS: SensorDeviceClass.TEMPERATURE, ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS, } TEMP_FREEDOM_ATTRS = { - ATTR_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE, + ATTR_DEVICE_CLASS: SensorDeviceClass.TEMPERATURE, 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): """Detect temperature significant changes.""" 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 )