From e35ab75c0b8378278310bfba35411dea5b7a9835 Mon Sep 17 00:00:00 2001 From: Michael <35783820+mib1185@users.noreply.github.com> Date: Sun, 15 Jan 2023 14:52:55 +0100 Subject: [PATCH] Replace the usage of unit constants by enumerations in Tests [h-l] (#85934) --- tests/components/hddtemp/test_sensor.py | 10 +-- .../here_travel_time/test_sensor.py | 16 ++--- .../homekit/test_get_accessories.py | 7 +- tests/components/homekit/test_type_sensors.py | 15 ++-- .../homekit/test_type_thermostats.py | 17 +++-- tests/components/homekit/test_util.py | 11 ++- .../homematicip_cloud/test_sensor.py | 32 +++++---- tests/components/homewizard/test_sensor.py | 26 +++---- tests/components/huisbaasje/test_sensor.py | 53 ++++++++------ .../ign_sismologia/test_geo_location.py | 8 +-- tests/components/integration/test_sensor.py | 69 ++++++++++--------- tests/components/iotawatt/test_sensor.py | 20 +++--- .../landisgyr_heat_meter/test_sensor.py | 20 +++--- tests/components/lcn/test_sensor.py | 6 +- tests/components/litterrobot/test_sensor.py | 4 +- tests/components/luftdaten/test_sensor.py | 10 +-- 16 files changed, 174 insertions(+), 150 deletions(-) diff --git a/tests/components/hddtemp/test_sensor.py b/tests/components/hddtemp/test_sensor.py index 7c7ac5ef1e8..c95c6af50f4 100644 --- a/tests/components/hddtemp/test_sensor.py +++ b/tests/components/hddtemp/test_sensor.py @@ -4,7 +4,7 @@ from unittest.mock import patch import pytest -from homeassistant.const import TEMP_CELSIUS +from homeassistant.const import UnitOfTemperature from homeassistant.setup import async_setup_component VALID_CONFIG_MINIMAL = {"sensor": {"platform": "hddtemp"}} @@ -31,25 +31,25 @@ REFERENCE = { "/dev/sda1": { "device": "/dev/sda1", "temperature": "29", - "unit_of_measurement": TEMP_CELSIUS, + "unit_of_measurement": UnitOfTemperature.CELSIUS, "model": "WDC WD30EZRX-12DC0B0", }, "/dev/sdb1": { "device": "/dev/sdb1", "temperature": "32", - "unit_of_measurement": TEMP_CELSIUS, + "unit_of_measurement": UnitOfTemperature.CELSIUS, "model": "WDC WD15EADS-11P7B2", }, "/dev/sdc1": { "device": "/dev/sdc1", "temperature": "29", - "unit_of_measurement": TEMP_CELSIUS, + "unit_of_measurement": UnitOfTemperature.CELSIUS, "model": "WDC WD20EARX-22MMMB0", }, "/dev/sdd1": { "device": "/dev/sdd1", "temperature": "32", - "unit_of_measurement": TEMP_CELSIUS, + "unit_of_measurement": UnitOfTemperature.CELSIUS, "model": "WDC WD15EARS-00Z5B1", }, } diff --git a/tests/components/here_travel_time/test_sensor.py b/tests/components/here_travel_time/test_sensor.py index f9f12504891..157020d90d3 100644 --- a/tests/components/here_travel_time/test_sensor.py +++ b/tests/components/here_travel_time/test_sensor.py @@ -59,8 +59,8 @@ from homeassistant.const import ( CONF_MODE, CONF_NAME, EVENT_HOMEASSISTANT_START, - TIME_MINUTES, UnitOfLength, + UnitOfTime, ) from homeassistant.core import CoreState, HomeAssistant, State from homeassistant.setup import async_setup_component @@ -146,7 +146,7 @@ async def test_sensor( await hass.async_block_till_done() duration = hass.states.get("sensor.test_duration") - assert duration.attributes.get("unit_of_measurement") == TIME_MINUTES + assert duration.attributes.get("unit_of_measurement") == UnitOfTime.MINUTES assert duration.attributes.get(ATTR_ICON) == icon assert duration.state == "26" @@ -485,13 +485,13 @@ async def test_restore_state(hass): "1234", attributes={ ATTR_LAST_RESET: last_reset, - ATTR_UNIT_OF_MEASUREMENT: TIME_MINUTES, + ATTR_UNIT_OF_MEASUREMENT: UnitOfTime.MINUTES, ATTR_STATE_CLASS: SensorStateClass.MEASUREMENT, }, ), { "native_value": 1234, - "native_unit_of_measurement": TIME_MINUTES, + "native_unit_of_measurement": UnitOfTime.MINUTES, "icon": "mdi:car", "last_reset": last_reset, }, @@ -502,13 +502,13 @@ async def test_restore_state(hass): "5678", attributes={ ATTR_LAST_RESET: last_reset, - ATTR_UNIT_OF_MEASUREMENT: TIME_MINUTES, + ATTR_UNIT_OF_MEASUREMENT: UnitOfTime.MINUTES, ATTR_STATE_CLASS: SensorStateClass.MEASUREMENT, }, ), { "native_value": 5678, - "native_unit_of_measurement": TIME_MINUTES, + "native_unit_of_measurement": UnitOfTime.MINUTES, "icon": "mdi:car", "last_reset": last_reset, }, @@ -581,12 +581,12 @@ async def test_restore_state(hass): # restore from cache state = hass.states.get("sensor.test_duration") assert state.state == "1234" - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TIME_MINUTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfTime.MINUTES assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT state = hass.states.get("sensor.test_duration_in_traffic") assert state.state == "5678" - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TIME_MINUTES + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfTime.MINUTES assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT state = hass.states.get("sensor.test_distance") diff --git a/tests/components/homekit/test_get_accessories.py b/tests/components/homekit/test_get_accessories.py index 320407f480a..6ef7202fb4e 100644 --- a/tests/components/homekit/test_get_accessories.py +++ b/tests/components/homekit/test_get_accessories.py @@ -30,8 +30,7 @@ from homeassistant.const import ( LIGHT_LUX, PERCENTAGE, STATE_UNKNOWN, - TEMP_CELSIUS, - TEMP_FAHRENHEIT, + UnitOfTemperature, ) from homeassistant.core import State @@ -269,13 +268,13 @@ def test_type_media_player(type_name, entity_id, state, attrs, config): "TemperatureSensor", "sensor.temperature", "23", - {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}, + {ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.CELSIUS}, ), ( "TemperatureSensor", "sensor.temperature", "74", - {ATTR_UNIT_OF_MEASUREMENT: TEMP_FAHRENHEIT}, + {ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT}, ), ], ) diff --git a/tests/components/homekit/test_type_sensors.py b/tests/components/homekit/test_type_sensors.py index 28dfe04932f..8baf8ee9df9 100644 --- a/tests/components/homekit/test_type_sensors.py +++ b/tests/components/homekit/test_type_sensors.py @@ -31,8 +31,7 @@ from homeassistant.const import ( STATE_OFF, STATE_ON, STATE_UNKNOWN, - TEMP_CELSIUS, - TEMP_FAHRENHEIT, + UnitOfTemperature, ) from homeassistant.core import CoreState from homeassistant.helpers import entity_registry as er @@ -56,21 +55,25 @@ async def test_temperature(hass, hk_driver): assert acc.char_temp.properties[key] == value hass.states.async_set( - entity_id, STATE_UNKNOWN, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS} + entity_id, STATE_UNKNOWN, {ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.CELSIUS} ) await hass.async_block_till_done() assert acc.char_temp.value == 0.0 - hass.states.async_set(entity_id, "20", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}) + hass.states.async_set( + entity_id, "20", {ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.CELSIUS} + ) await hass.async_block_till_done() assert acc.char_temp.value == 20 - hass.states.async_set(entity_id, "0", {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}) + hass.states.async_set( + entity_id, "0", {ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.CELSIUS} + ) await hass.async_block_till_done() assert acc.char_temp.value == 0 hass.states.async_set( - entity_id, "75.2", {ATTR_UNIT_OF_MEASUREMENT: TEMP_FAHRENHEIT} + entity_id, "75.2", {ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.FAHRENHEIT} ) await hass.async_block_till_done() assert acc.char_temp.value == 24 diff --git a/tests/components/homekit/test_type_thermostats.py b/tests/components/homekit/test_type_thermostats.py index 902c70aba5d..1dd191f5303 100644 --- a/tests/components/homekit/test_type_thermostats.py +++ b/tests/components/homekit/test_type_thermostats.py @@ -68,8 +68,7 @@ from homeassistant.const import ( ATTR_TEMPERATURE, CONF_TEMPERATURE_UNIT, EVENT_HOMEASSISTANT_START, - TEMP_CELSIUS, - TEMP_FAHRENHEIT, + UnitOfTemperature, ) from homeassistant.core import CoreState from homeassistant.helpers import entity_registry as er @@ -877,7 +876,9 @@ async def test_thermostat_fahrenheit(hass, hk_driver, events): }, ) await hass.async_block_till_done() - with patch.object(hass.config.units, CONF_TEMPERATURE_UNIT, new=TEMP_FAHRENHEIT): + with patch.object( + hass.config.units, CONF_TEMPERATURE_UNIT, new=UnitOfTemperature.FAHRENHEIT + ): acc = Thermostat(hass, hk_driver, "Climate", entity_id, 1, None) hk_driver.add_accessory(acc) await acc.run() @@ -986,7 +987,7 @@ async def test_thermostat_get_temperature_range(hass, hk_driver): await hass.async_block_till_done() assert acc.get_temperature_range() == (20, 25) - acc._unit = TEMP_FAHRENHEIT + acc._unit = UnitOfTemperature.FAHRENHEIT hass.states.async_set( entity_id, HVACMode.OFF, {ATTR_MIN_TEMP: 60, ATTR_MAX_TEMP: 70} ) @@ -1761,7 +1762,7 @@ async def test_water_heater(hass, hk_driver, events): assert call_set_temperature[0].data[ATTR_TEMPERATURE] == 52.0 assert acc.char_target_temp.value == 52.0 assert len(events) == 1 - assert events[-1].data[ATTR_VALUE] == f"52.0{TEMP_CELSIUS}" + assert events[-1].data[ATTR_VALUE] == f"52.0{UnitOfTemperature.CELSIUS}" acc.char_target_heat_cool.client_update_value(1) await hass.async_block_till_done() @@ -1779,7 +1780,9 @@ async def test_water_heater_fahrenheit(hass, hk_driver, events): hass.states.async_set(entity_id, HVACMode.HEAT) await hass.async_block_till_done() - with patch.object(hass.config.units, CONF_TEMPERATURE_UNIT, new=TEMP_FAHRENHEIT): + with patch.object( + hass.config.units, CONF_TEMPERATURE_UNIT, new=UnitOfTemperature.FAHRENHEIT + ): acc = WaterHeater(hass, hk_driver, "WaterHeater", entity_id, 2, None) await acc.run() await hass.async_block_till_done() @@ -1819,7 +1822,7 @@ async def test_water_heater_get_temperature_range(hass, hk_driver): await hass.async_block_till_done() assert acc.get_temperature_range() == (20, 25) - acc._unit = TEMP_FAHRENHEIT + acc._unit = UnitOfTemperature.FAHRENHEIT hass.states.async_set( entity_id, HVACMode.OFF, {ATTR_MIN_TEMP: 60, ATTR_MAX_TEMP: 70} ) diff --git a/tests/components/homekit/test_util.py b/tests/components/homekit/test_util.py index f3811ce34c5..b7031cedb37 100644 --- a/tests/components/homekit/test_util.py +++ b/tests/components/homekit/test_util.py @@ -48,8 +48,7 @@ from homeassistant.const import ( CONF_PORT, CONF_TYPE, STATE_UNKNOWN, - TEMP_CELSIUS, - TEMP_FAHRENHEIT, + UnitOfTemperature, ) from homeassistant.core import State @@ -211,14 +210,14 @@ def test_cleanup_name_for_homekit(): def test_temperature_to_homekit(): """Test temperature conversion from HA to HomeKit.""" - assert temperature_to_homekit(20.46, TEMP_CELSIUS) == 20.5 - assert temperature_to_homekit(92.1, TEMP_FAHRENHEIT) == 33.4 + assert temperature_to_homekit(20.46, UnitOfTemperature.CELSIUS) == 20.5 + assert temperature_to_homekit(92.1, UnitOfTemperature.FAHRENHEIT) == 33.4 def test_temperature_to_states(): """Test temperature conversion from HomeKit to HA.""" - assert temperature_to_states(20, TEMP_CELSIUS) == 20.0 - assert temperature_to_states(20.2, TEMP_FAHRENHEIT) == 68.5 + assert temperature_to_states(20, UnitOfTemperature.CELSIUS) == 20.0 + assert temperature_to_states(20.2, UnitOfTemperature.FAHRENHEIT) == 68.5 def test_density_to_air_quality(): diff --git a/tests/components/homematicip_cloud/test_sensor.py b/tests/components/homematicip_cloud/test_sensor.py index b4da2a83c38..9b8630e96d4 100644 --- a/tests/components/homematicip_cloud/test_sensor.py +++ b/tests/components/homematicip_cloud/test_sensor.py @@ -24,13 +24,13 @@ from homeassistant.components.homematicip_cloud.sensor import ( from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.const import ( ATTR_UNIT_OF_MEASUREMENT, - LENGTH_MILLIMETERS, LIGHT_LUX, PERCENTAGE, - POWER_WATT, - SPEED_KILOMETERS_PER_HOUR, STATE_UNKNOWN, - TEMP_CELSIUS, + UnitOfLength, + UnitOfPower, + UnitOfSpeed, + UnitOfTemperature, ) from homeassistant.setup import async_setup_component @@ -133,7 +133,7 @@ async def test_hmip_temperature_sensor1(hass, default_mock_hap_factory): ) assert ha_state.state == "21.0" - assert ha_state.attributes["unit_of_measurement"] == TEMP_CELSIUS + assert ha_state.attributes["unit_of_measurement"] == UnitOfTemperature.CELSIUS await async_manipulate_test_data(hass, hmip_device, "actualTemperature", 23.5) ha_state = hass.states.get(entity_id) assert ha_state.state == "23.5" @@ -158,7 +158,7 @@ async def test_hmip_temperature_sensor2(hass, default_mock_hap_factory): ) assert ha_state.state == "20.0" - assert ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == TEMP_CELSIUS + assert ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfTemperature.CELSIUS await async_manipulate_test_data(hass, hmip_device, "valveActualTemperature", 23.5) ha_state = hass.states.get(entity_id) assert ha_state.state == "23.5" @@ -183,7 +183,7 @@ async def test_hmip_temperature_sensor3(hass, default_mock_hap_factory): ) assert ha_state.state == "23.3" - assert ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == TEMP_CELSIUS + assert ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfTemperature.CELSIUS await async_manipulate_test_data(hass, hmip_device, "actualTemperature", 23.5) ha_state = hass.states.get(entity_id) assert ha_state.state == "23.5" @@ -228,7 +228,7 @@ async def test_hmip_thermostat_evo_temperature(hass, default_mock_hap_factory): ) assert ha_state.state == "18.7" - assert ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == TEMP_CELSIUS + assert ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfTemperature.CELSIUS await async_manipulate_test_data(hass, hmip_device, "valveActualTemperature", 23.5) ha_state = hass.states.get(entity_id) assert ha_state.state == "23.5" @@ -252,7 +252,7 @@ async def test_hmip_power_sensor(hass, default_mock_hap_factory): ) assert ha_state.state == "0.0" - assert ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == POWER_WATT + assert ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfPower.WATT await async_manipulate_test_data(hass, hmip_device, "currentPowerConsumption", 23.5) ha_state = hass.states.get(entity_id) assert ha_state.state == "23.5" @@ -332,7 +332,9 @@ async def test_hmip_windspeed_sensor(hass, default_mock_hap_factory): ) assert ha_state.state == "2.6" - assert ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == SPEED_KILOMETERS_PER_HOUR + assert ( + ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfSpeed.KILOMETERS_PER_HOUR + ) await async_manipulate_test_data(hass, hmip_device, "windSpeed", 9.4) ha_state = hass.states.get(entity_id) assert ha_state.state == "9.4" @@ -352,7 +354,7 @@ async def test_hmip_windspeed_sensor(hass, default_mock_hap_factory): 205: "SSW", 227.5: "SW", 250: "WSW", - 272.5: POWER_WATT, + 272.5: UnitOfPower.WATT, 295: "WNW", 317.5: "NW", 340: "NNW", @@ -379,7 +381,7 @@ async def test_hmip_today_rain_sensor(hass, default_mock_hap_factory): ) assert ha_state.state == "3.9" - assert ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == LENGTH_MILLIMETERS + assert ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfLength.MILLIMETERS await async_manipulate_test_data(hass, hmip_device, "todayRainCounter", 14.2) ha_state = hass.states.get(entity_id) assert ha_state.state == "14.2" @@ -404,7 +406,7 @@ async def test_hmip_temperature_external_sensor_channel_1( ha_state = hass.states.get(entity_id) assert ha_state.state == "25.4" - assert ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == TEMP_CELSIUS + assert ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfTemperature.CELSIUS await async_manipulate_test_data(hass, hmip_device, "temperatureExternalOne", 23.5) ha_state = hass.states.get(entity_id) assert ha_state.state == "23.5" @@ -429,7 +431,7 @@ async def test_hmip_temperature_external_sensor_channel_2( ha_state = hass.states.get(entity_id) assert ha_state.state == "22.4" - assert ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == TEMP_CELSIUS + assert ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfTemperature.CELSIUS await async_manipulate_test_data(hass, hmip_device, "temperatureExternalTwo", 23.4) ha_state = hass.states.get(entity_id) assert ha_state.state == "23.4" @@ -452,7 +454,7 @@ async def test_hmip_temperature_external_sensor_delta(hass, default_mock_hap_fac ha_state = hass.states.get(entity_id) assert ha_state.state == "0.4" - assert ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == TEMP_CELSIUS + assert ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfTemperature.CELSIUS await async_manipulate_test_data( hass, hmip_device, "temperatureExternalDelta", -0.5 ) diff --git a/tests/components/homewizard/test_sensor.py b/tests/components/homewizard/test_sensor.py index 8ed6f9365c8..2e85dfff459 100644 --- a/tests/components/homewizard/test_sensor.py +++ b/tests/components/homewizard/test_sensor.py @@ -16,9 +16,9 @@ from homeassistant.const import ( ATTR_FRIENDLY_NAME, ATTR_ICON, ATTR_UNIT_OF_MEASUREMENT, - ENERGY_KILO_WATT_HOUR, - POWER_WATT, - VOLUME_CUBIC_METERS, + UnitOfEnergy, + UnitOfPower, + UnitOfVolume, ) from homeassistant.helpers import entity_registry as er import homeassistant.util.dt as dt_util @@ -206,7 +206,7 @@ async def test_sensor_entity_total_power_import_t1_kwh( == "Product Name (aabbccddeeff) Total power import T1" ) assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY assert ATTR_ICON not in state.attributes @@ -248,7 +248,7 @@ async def test_sensor_entity_total_power_import_t2_kwh( == "Product Name (aabbccddeeff) Total power import T2" ) assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY assert ATTR_ICON not in state.attributes @@ -290,7 +290,7 @@ async def test_sensor_entity_total_power_export_t1_kwh( == "Product Name (aabbccddeeff) Total power export T1" ) assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY assert ATTR_ICON not in state.attributes @@ -332,7 +332,7 @@ async def test_sensor_entity_total_power_export_t2_kwh( == "Product Name (aabbccddeeff) Total power export T2" ) assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY assert ATTR_ICON not in state.attributes @@ -370,7 +370,7 @@ async def test_sensor_entity_active_power( == "Product Name (aabbccddeeff) Active power" ) assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER assert ATTR_ICON not in state.attributes @@ -410,7 +410,7 @@ async def test_sensor_entity_active_power_l1( == "Product Name (aabbccddeeff) Active power L1" ) assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER assert ATTR_ICON not in state.attributes @@ -450,7 +450,7 @@ async def test_sensor_entity_active_power_l2( == "Product Name (aabbccddeeff) Active power L2" ) assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER assert ATTR_ICON not in state.attributes @@ -490,7 +490,7 @@ async def test_sensor_entity_active_power_l3( == "Product Name (aabbccddeeff) Active power L3" ) assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER assert ATTR_ICON not in state.attributes @@ -526,7 +526,7 @@ async def test_sensor_entity_total_gas(hass, mock_config_entry_data, mock_config == "Product Name (aabbccddeeff) Total gas" ) assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == VOLUME_CUBIC_METERS + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfVolume.CUBIC_METERS assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.GAS assert ATTR_ICON not in state.attributes @@ -608,7 +608,7 @@ async def test_sensor_entity_total_liters( ) assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == VOLUME_CUBIC_METERS + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfVolume.CUBIC_METERS assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.WATER assert state.attributes.get(ATTR_ICON) == "mdi:gauge" diff --git a/tests/components/huisbaasje/test_sensor.py b/tests/components/huisbaasje/test_sensor.py index 43789988003..bd792f3230f 100644 --- a/tests/components/huisbaasje/test_sensor.py +++ b/tests/components/huisbaasje/test_sensor.py @@ -15,9 +15,9 @@ from homeassistant.const import ( CONF_ID, CONF_PASSWORD, CONF_USERNAME, - ENERGY_KILO_WATT_HOUR, - POWER_WATT, - VOLUME_CUBIC_METERS, + UnitOfEnergy, + UnitOfPower, + UnitOfVolume, ) from homeassistant.core import HomeAssistant @@ -65,7 +65,9 @@ async def test_setup_entry(hass: HomeAssistant): current_power.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT ) - assert current_power.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT + assert ( + current_power.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT + ) current_power_in = hass.states.get("sensor.huisbaasje_current_power_in_peak") assert current_power_in.state == "1012.0" @@ -78,7 +80,10 @@ async def test_setup_entry(hass: HomeAssistant): current_power_in.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT ) - assert current_power_in.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT + assert ( + current_power_in.attributes.get(ATTR_UNIT_OF_MEASUREMENT) + == UnitOfPower.WATT + ) current_power_in_low = hass.states.get( "sensor.huisbaasje_current_power_in_off_peak" @@ -94,7 +99,8 @@ async def test_setup_entry(hass: HomeAssistant): is SensorStateClass.MEASUREMENT ) assert ( - current_power_in_low.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT + current_power_in_low.attributes.get(ATTR_UNIT_OF_MEASUREMENT) + == UnitOfPower.WATT ) current_power_out = hass.states.get("sensor.huisbaasje_current_power_out_peak") @@ -108,7 +114,10 @@ async def test_setup_entry(hass: HomeAssistant): current_power_out.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT ) - assert current_power_out.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT + assert ( + current_power_out.attributes.get(ATTR_UNIT_OF_MEASUREMENT) + == UnitOfPower.WATT + ) current_power_out_low = hass.states.get( "sensor.huisbaasje_current_power_out_off_peak" @@ -124,7 +133,8 @@ async def test_setup_entry(hass: HomeAssistant): is SensorStateClass.MEASUREMENT ) assert ( - current_power_out_low.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT + current_power_out_low.attributes.get(ATTR_UNIT_OF_MEASUREMENT) + == UnitOfPower.WATT ) energy_consumption_peak_today = hass.states.get( @@ -145,7 +155,7 @@ async def test_setup_entry(hass: HomeAssistant): ) assert ( energy_consumption_peak_today.attributes.get(ATTR_UNIT_OF_MEASUREMENT) - == ENERGY_KILO_WATT_HOUR + == UnitOfEnergy.KILO_WATT_HOUR ) energy_consumption_off_peak_today = hass.states.get( @@ -166,7 +176,7 @@ async def test_setup_entry(hass: HomeAssistant): ) assert ( energy_consumption_off_peak_today.attributes.get(ATTR_UNIT_OF_MEASUREMENT) - == ENERGY_KILO_WATT_HOUR + == UnitOfEnergy.KILO_WATT_HOUR ) energy_production_peak_today = hass.states.get( @@ -187,7 +197,7 @@ async def test_setup_entry(hass: HomeAssistant): ) assert ( energy_production_peak_today.attributes.get(ATTR_UNIT_OF_MEASUREMENT) - == ENERGY_KILO_WATT_HOUR + == UnitOfEnergy.KILO_WATT_HOUR ) energy_production_off_peak_today = hass.states.get( @@ -208,7 +218,7 @@ async def test_setup_entry(hass: HomeAssistant): ) assert ( energy_production_off_peak_today.attributes.get(ATTR_UNIT_OF_MEASUREMENT) - == ENERGY_KILO_WATT_HOUR + == UnitOfEnergy.KILO_WATT_HOUR ) energy_today = hass.states.get("sensor.huisbaasje_energy_today") @@ -223,7 +233,7 @@ async def test_setup_entry(hass: HomeAssistant): ) assert ( energy_today.attributes.get(ATTR_UNIT_OF_MEASUREMENT) - == ENERGY_KILO_WATT_HOUR + == UnitOfEnergy.KILO_WATT_HOUR ) energy_this_week = hass.states.get("sensor.huisbaasje_energy_this_week") @@ -239,7 +249,7 @@ async def test_setup_entry(hass: HomeAssistant): ) assert ( energy_this_week.attributes.get(ATTR_UNIT_OF_MEASUREMENT) - == ENERGY_KILO_WATT_HOUR + == UnitOfEnergy.KILO_WATT_HOUR ) energy_this_month = hass.states.get("sensor.huisbaasje_energy_this_month") @@ -255,7 +265,7 @@ async def test_setup_entry(hass: HomeAssistant): ) assert ( energy_this_month.attributes.get(ATTR_UNIT_OF_MEASUREMENT) - == ENERGY_KILO_WATT_HOUR + == UnitOfEnergy.KILO_WATT_HOUR ) energy_this_year = hass.states.get("sensor.huisbaasje_energy_this_year") @@ -271,7 +281,7 @@ async def test_setup_entry(hass: HomeAssistant): ) assert ( energy_this_year.attributes.get(ATTR_UNIT_OF_MEASUREMENT) - == ENERGY_KILO_WATT_HOUR + == UnitOfEnergy.KILO_WATT_HOUR ) current_gas = hass.states.get("sensor.huisbaasje_current_gas") @@ -294,7 +304,10 @@ async def test_setup_entry(hass: HomeAssistant): gas_today.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.TOTAL_INCREASING ) - assert gas_today.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == VOLUME_CUBIC_METERS + assert ( + gas_today.attributes.get(ATTR_UNIT_OF_MEASUREMENT) + == UnitOfVolume.CUBIC_METERS + ) gas_this_week = hass.states.get("sensor.huisbaasje_gas_this_week") assert gas_this_week.state == "5.6" @@ -306,7 +319,7 @@ async def test_setup_entry(hass: HomeAssistant): ) assert ( gas_this_week.attributes.get(ATTR_UNIT_OF_MEASUREMENT) - == VOLUME_CUBIC_METERS + == UnitOfVolume.CUBIC_METERS ) gas_this_month = hass.states.get("sensor.huisbaasje_gas_this_month") @@ -319,7 +332,7 @@ async def test_setup_entry(hass: HomeAssistant): ) assert ( gas_this_month.attributes.get(ATTR_UNIT_OF_MEASUREMENT) - == VOLUME_CUBIC_METERS + == UnitOfVolume.CUBIC_METERS ) gas_this_year = hass.states.get("sensor.huisbaasje_gas_this_year") @@ -332,7 +345,7 @@ async def test_setup_entry(hass: HomeAssistant): ) assert ( gas_this_year.attributes.get(ATTR_UNIT_OF_MEASUREMENT) - == VOLUME_CUBIC_METERS + == UnitOfVolume.CUBIC_METERS ) # Assert mocks are called diff --git a/tests/components/ign_sismologia/test_geo_location.py b/tests/components/ign_sismologia/test_geo_location.py index 1ad5ae2a2b2..f04ccbaaf19 100644 --- a/tests/components/ign_sismologia/test_geo_location.py +++ b/tests/components/ign_sismologia/test_geo_location.py @@ -24,7 +24,7 @@ from homeassistant.const import ( CONF_LONGITUDE, CONF_RADIUS, EVENT_HOMEASSISTANT_START, - LENGTH_KILOMETERS, + UnitOfLength, ) from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util @@ -128,7 +128,7 @@ async def test_setup(hass): ), ATTR_IMAGE_URL: "http://image.url/map.jpg", ATTR_MAGNITUDE: 5.7, - ATTR_UNIT_OF_MEASUREMENT: LENGTH_KILOMETERS, + ATTR_UNIT_OF_MEASUREMENT: UnitOfLength.KILOMETERS, ATTR_SOURCE: "ign_sismologia", ATTR_ICON: "mdi:pulse", } @@ -144,7 +144,7 @@ async def test_setup(hass): ATTR_FRIENDLY_NAME: "M 4.6", ATTR_TITLE: "Title 2", ATTR_MAGNITUDE: 4.6, - ATTR_UNIT_OF_MEASUREMENT: LENGTH_KILOMETERS, + ATTR_UNIT_OF_MEASUREMENT: UnitOfLength.KILOMETERS, ATTR_SOURCE: "ign_sismologia", ATTR_ICON: "mdi:pulse", } @@ -160,7 +160,7 @@ async def test_setup(hass): ATTR_FRIENDLY_NAME: "Region 3", ATTR_TITLE: "Title 3", ATTR_REGION: "Region 3", - ATTR_UNIT_OF_MEASUREMENT: LENGTH_KILOMETERS, + ATTR_UNIT_OF_MEASUREMENT: UnitOfLength.KILOMETERS, ATTR_SOURCE: "ign_sismologia", ATTR_ICON: "mdi:pulse", } diff --git a/tests/components/integration/test_sensor.py b/tests/components/integration/test_sensor.py index 9cc1bbb2692..12f9781a81a 100644 --- a/tests/components/integration/test_sensor.py +++ b/tests/components/integration/test_sensor.py @@ -5,17 +5,13 @@ from unittest.mock import patch from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass from homeassistant.const import ( ATTR_UNIT_OF_MEASUREMENT, - DATA_KILOBYTES, - DATA_RATE_BYTES_PER_SECOND, - ENERGY_KILO_WATT_HOUR, - ENERGY_WATT_HOUR, - POWER_KILO_WATT, - POWER_WATT, STATE_UNAVAILABLE, STATE_UNKNOWN, - TIME_HOURS, - TIME_SECONDS, + UnitOfDataRate, + UnitOfEnergy, + UnitOfInformation, UnitOfPower, + UnitOfTime, ) from homeassistant.core import HomeAssistant, State from homeassistant.setup import async_setup_component @@ -40,7 +36,9 @@ async def test_state(hass) -> None: assert await async_setup_component(hass, "sensor", config) entity_id = config["sensor"]["source"] - hass.states.async_set(entity_id, 1, {ATTR_UNIT_OF_MEASUREMENT: POWER_KILO_WATT}) + hass.states.async_set( + entity_id, 1, {ATTR_UNIT_OF_MEASUREMENT: UnitOfPower.KILO_WATT} + ) await hass.async_block_till_done() state = hass.states.get("sensor.integration") @@ -55,7 +53,7 @@ async def test_state(hass) -> None: 1, { "device_class": SensorDeviceClass.POWER, - ATTR_UNIT_OF_MEASUREMENT: POWER_KILO_WATT, + ATTR_UNIT_OF_MEASUREMENT: UnitOfPower.KILO_WATT, }, force_update=True, ) @@ -67,7 +65,7 @@ async def test_state(hass) -> None: # Testing a power sensor at 1 KiloWatts for 1hour = 1kWh assert round(float(state.state), config["sensor"]["round"]) == 1.0 - assert state.attributes.get("unit_of_measurement") == ENERGY_KILO_WATT_HOUR + assert state.attributes.get("unit_of_measurement") == UnitOfEnergy.KILO_WATT_HOUR assert state.attributes.get("device_class") == SensorDeviceClass.ENERGY assert state.attributes.get("state_class") is SensorStateClass.TOTAL @@ -82,7 +80,7 @@ async def test_restore_state(hass: HomeAssistant) -> None: "100.0", { "device_class": SensorDeviceClass.ENERGY, - "unit_of_measurement": ENERGY_KILO_WATT_HOUR, + "unit_of_measurement": UnitOfEnergy.KILO_WATT_HOUR, }, ), ), @@ -103,7 +101,7 @@ async def test_restore_state(hass: HomeAssistant) -> None: state = hass.states.get("sensor.integration") assert state assert state.state == "100.00" - assert state.attributes.get("unit_of_measurement") == ENERGY_KILO_WATT_HOUR + assert state.attributes.get("unit_of_measurement") == UnitOfEnergy.KILO_WATT_HOUR assert state.attributes.get("device_class") == SensorDeviceClass.ENERGY @@ -166,7 +164,7 @@ async def test_trapezoidal(hass): hass.states.async_set( entity_id, value, - {ATTR_UNIT_OF_MEASUREMENT: POWER_KILO_WATT}, + {ATTR_UNIT_OF_MEASUREMENT: UnitOfPower.KILO_WATT}, force_update=True, ) await hass.async_block_till_done() @@ -176,7 +174,7 @@ async def test_trapezoidal(hass): assert round(float(state.state), config["sensor"]["round"]) == 8.33 - assert state.attributes.get("unit_of_measurement") == ENERGY_KILO_WATT_HOUR + assert state.attributes.get("unit_of_measurement") == UnitOfEnergy.KILO_WATT_HOUR async def test_left(hass): @@ -194,7 +192,9 @@ async def test_left(hass): assert await async_setup_component(hass, "sensor", config) entity_id = config["sensor"]["source"] - hass.states.async_set(entity_id, 0, {ATTR_UNIT_OF_MEASUREMENT: POWER_KILO_WATT}) + hass.states.async_set( + entity_id, 0, {ATTR_UNIT_OF_MEASUREMENT: UnitOfPower.KILO_WATT} + ) await hass.async_block_till_done() # Testing a power sensor with non-monotonic intervals and values @@ -204,7 +204,7 @@ async def test_left(hass): hass.states.async_set( entity_id, value, - {ATTR_UNIT_OF_MEASUREMENT: POWER_KILO_WATT}, + {ATTR_UNIT_OF_MEASUREMENT: UnitOfPower.KILO_WATT}, force_update=True, ) await hass.async_block_till_done() @@ -214,7 +214,7 @@ async def test_left(hass): assert round(float(state.state), config["sensor"]["round"]) == 7.5 - assert state.attributes.get("unit_of_measurement") == ENERGY_KILO_WATT_HOUR + assert state.attributes.get("unit_of_measurement") == UnitOfEnergy.KILO_WATT_HOUR async def test_right(hass): @@ -232,7 +232,9 @@ async def test_right(hass): assert await async_setup_component(hass, "sensor", config) entity_id = config["sensor"]["source"] - hass.states.async_set(entity_id, 0, {ATTR_UNIT_OF_MEASUREMENT: POWER_KILO_WATT}) + hass.states.async_set( + entity_id, 0, {ATTR_UNIT_OF_MEASUREMENT: UnitOfPower.KILO_WATT} + ) await hass.async_block_till_done() # Testing a power sensor with non-monotonic intervals and values @@ -242,7 +244,7 @@ async def test_right(hass): hass.states.async_set( entity_id, value, - {ATTR_UNIT_OF_MEASUREMENT: POWER_KILO_WATT}, + {ATTR_UNIT_OF_MEASUREMENT: UnitOfPower.KILO_WATT}, force_update=True, ) await hass.async_block_till_done() @@ -252,7 +254,7 @@ async def test_right(hass): assert round(float(state.state), config["sensor"]["round"]) == 9.17 - assert state.attributes.get("unit_of_measurement") == ENERGY_KILO_WATT_HOUR + assert state.attributes.get("unit_of_measurement") == UnitOfEnergy.KILO_WATT_HOUR async def test_prefix(hass): @@ -270,13 +272,16 @@ async def test_prefix(hass): assert await async_setup_component(hass, "sensor", config) entity_id = config["sensor"]["source"] - hass.states.async_set(entity_id, 1000, {"unit_of_measurement": POWER_WATT}) + hass.states.async_set(entity_id, 1000, {"unit_of_measurement": UnitOfPower.WATT}) await hass.async_block_till_done() now = dt_util.utcnow() + timedelta(seconds=3600) with patch("homeassistant.util.dt.utcnow", return_value=now): hass.states.async_set( - entity_id, 1000, {"unit_of_measurement": POWER_WATT}, force_update=True + entity_id, + 1000, + {"unit_of_measurement": UnitOfPower.WATT}, + force_update=True, ) await hass.async_block_till_done() @@ -285,7 +290,7 @@ async def test_prefix(hass): # Testing a power sensor at 1000 Watts for 1hour = 1kWh assert round(float(state.state), config["sensor"]["round"]) == 1.0 - assert state.attributes.get("unit_of_measurement") == ENERGY_KILO_WATT_HOUR + assert state.attributes.get("unit_of_measurement") == UnitOfEnergy.KILO_WATT_HOUR async def test_suffix(hass): @@ -297,7 +302,7 @@ async def test_suffix(hass): "source": "sensor.bytes_per_second", "round": 2, "unit_prefix": "k", - "unit_time": TIME_SECONDS, + "unit_time": UnitOfTime.SECONDS, } } @@ -305,7 +310,7 @@ async def test_suffix(hass): entity_id = config["sensor"]["source"] hass.states.async_set( - entity_id, 1000, {ATTR_UNIT_OF_MEASUREMENT: DATA_RATE_BYTES_PER_SECOND} + entity_id, 1000, {ATTR_UNIT_OF_MEASUREMENT: UnitOfDataRate.BYTES_PER_SECOND} ) await hass.async_block_till_done() @@ -314,7 +319,7 @@ async def test_suffix(hass): hass.states.async_set( entity_id, 1000, - {ATTR_UNIT_OF_MEASUREMENT: DATA_RATE_BYTES_PER_SECOND}, + {ATTR_UNIT_OF_MEASUREMENT: UnitOfDataRate.BYTES_PER_SECOND}, force_update=True, ) await hass.async_block_till_done() @@ -324,7 +329,7 @@ async def test_suffix(hass): # Testing a network speed sensor at 1000 bytes/s over 10s = 10kbytes assert round(float(state.state)) == 10 - assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == DATA_KILOBYTES + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfInformation.KILOBYTES async def test_suffix_2(hass): @@ -335,7 +340,7 @@ async def test_suffix_2(hass): "name": "integration", "source": "sensor.cubic_meters_per_hour", "round": 2, - "unit_time": TIME_HOURS, + "unit_time": UnitOfTime.HOURS, } } @@ -384,7 +389,7 @@ async def test_units(hass): await hass.async_block_till_done() hass.states.async_set(entity_id, 200, {"unit_of_measurement": None}) await hass.async_block_till_done() - hass.states.async_set(entity_id, 300, {"unit_of_measurement": POWER_WATT}) + hass.states.async_set(entity_id, 300, {"unit_of_measurement": UnitOfPower.WATT}) await hass.async_block_till_done() state = hass.states.get("sensor.integration") @@ -392,7 +397,7 @@ async def test_units(hass): # Testing the sensor ignored the source sensor's units until # they became valid - assert state.attributes.get("unit_of_measurement") == ENERGY_WATT_HOUR + assert state.attributes.get("unit_of_measurement") == UnitOfEnergy.WATT_HOUR # When source state goes to None / Unknown, expect an early exit without # changes to the state or unit_of_measurement @@ -401,7 +406,7 @@ async def test_units(hass): new_state = hass.states.get("sensor.integration") assert state == new_state - assert state.attributes.get("unit_of_measurement") == ENERGY_WATT_HOUR + assert state.attributes.get("unit_of_measurement") == UnitOfEnergy.WATT_HOUR async def test_device_class(hass): diff --git a/tests/components/iotawatt/test_sensor.py b/tests/components/iotawatt/test_sensor.py index b025ed13d73..68523d5b0fc 100644 --- a/tests/components/iotawatt/test_sensor.py +++ b/tests/components/iotawatt/test_sensor.py @@ -11,8 +11,8 @@ from homeassistant.const import ( ATTR_DEVICE_CLASS, ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, - ENERGY_WATT_HOUR, - POWER_WATT, + UnitOfEnergy, + UnitOfPower, ) from homeassistant.core import State from homeassistant.setup import async_setup_component @@ -47,7 +47,7 @@ async def test_sensor_type_input(hass, mock_iotawatt): assert state.state == "23" assert state.attributes[ATTR_STATE_CLASS] is SensorStateClass.MEASUREMENT assert state.attributes[ATTR_FRIENDLY_NAME] == "My Sensor" - assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == POWER_WATT + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfPower.WATT assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.POWER assert state.attributes["channel"] == "1" assert state.attributes["type"] == "Input" @@ -74,7 +74,7 @@ async def test_sensor_type_output(hass, mock_iotawatt): assert state.state == "243" assert state.attributes[ATTR_STATE_CLASS] is SensorStateClass.TOTAL assert state.attributes[ATTR_FRIENDLY_NAME] == "My WattHour Sensor" - assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == ENERGY_WATT_HOUR + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfEnergy.WATT_HOUR assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.ENERGY assert state.attributes["type"] == "Output" @@ -101,7 +101,7 @@ async def test_sensor_type_accumulated_output(hass, mock_iotawatt): "100.0", { "device_class": SensorDeviceClass.ENERGY, - "unit_of_measurement": ENERGY_WATT_HOUR, + "unit_of_measurement": UnitOfEnergy.WATT_HOUR, "last_update": DUMMY_DATE, }, ), @@ -124,7 +124,7 @@ async def test_sensor_type_accumulated_output(hass, mock_iotawatt): == "My WattHour Accumulated Output Sensor.wh Accumulated" ) assert state.attributes[ATTR_STATE_CLASS] is SensorStateClass.TOTAL - assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == ENERGY_WATT_HOUR + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfEnergy.WATT_HOUR assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.ENERGY assert state.attributes["type"] == "Output" assert state.attributes[ATTR_LAST_UPDATE] is not None @@ -165,7 +165,7 @@ async def test_sensor_type_accumulated_output_error_restore(hass, mock_iotawatt) == "My WattHour Accumulated Output Sensor.wh Accumulated" ) assert state.attributes[ATTR_STATE_CLASS] is SensorStateClass.TOTAL - assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == ENERGY_WATT_HOUR + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfEnergy.WATT_HOUR assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.ENERGY assert state.attributes["type"] == "Output" assert state.attributes[ATTR_LAST_UPDATE] is not None @@ -191,7 +191,7 @@ async def test_sensor_type_multiple_accumulated_output(hass, mock_iotawatt): "100.0", { "device_class": SensorDeviceClass.ENERGY, - "unit_of_measurement": ENERGY_WATT_HOUR, + "unit_of_measurement": UnitOfEnergy.WATT_HOUR, "last_update": DUMMY_DATE, }, ), @@ -200,7 +200,7 @@ async def test_sensor_type_multiple_accumulated_output(hass, mock_iotawatt): "50.0", { "device_class": SensorDeviceClass.ENERGY, - "unit_of_measurement": ENERGY_WATT_HOUR, + "unit_of_measurement": UnitOfEnergy.WATT_HOUR, "last_update": DUMMY_DATE, }, ), @@ -223,7 +223,7 @@ async def test_sensor_type_multiple_accumulated_output(hass, mock_iotawatt): == "My WattHour Accumulated Output Sensor.wh Accumulated" ) assert state.attributes[ATTR_STATE_CLASS] is SensorStateClass.TOTAL - assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == ENERGY_WATT_HOUR + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfEnergy.WATT_HOUR assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.ENERGY assert state.attributes["type"] == "Output" assert state.attributes[ATTR_LAST_UPDATE] is not None diff --git a/tests/components/landisgyr_heat_meter/test_sensor.py b/tests/components/landisgyr_heat_meter/test_sensor.py index cbaca71e52f..6721ae00a23 100644 --- a/tests/components/landisgyr_heat_meter/test_sensor.py +++ b/tests/components/landisgyr_heat_meter/test_sensor.py @@ -19,8 +19,8 @@ from homeassistant.const import ( ATTR_ENTITY_ID, ATTR_ICON, ATTR_UNIT_OF_MEASUREMENT, - ENERGY_MEGA_WATT_HOUR, - VOLUME_CUBIC_METERS, + UnitOfEnergy, + UnitOfVolume, ) from homeassistant.core import CoreState, State from homeassistant.helpers import entity_registry @@ -82,14 +82,14 @@ async def test_create_sensors(mock_heat_meter, hass): state = hass.states.get("sensor.heat_meter_heat_usage") assert state assert state.state == "34.16669" - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_MEGA_WATT_HOUR + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.MEGA_WATT_HOUR assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY state = hass.states.get("sensor.heat_meter_volume_usage") assert state assert state.state == "456" - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == VOLUME_CUBIC_METERS + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfVolume.CUBIC_METERS assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL state = hass.states.get("sensor.heat_meter_device_number") @@ -122,13 +122,13 @@ async def test_restore_state(mock_heat_meter, hass): "34167", attributes={ ATTR_LAST_RESET: last_reset, - ATTR_UNIT_OF_MEASUREMENT: ENERGY_MEGA_WATT_HOUR, + ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.MEGA_WATT_HOUR, ATTR_STATE_CLASS: SensorStateClass.TOTAL, }, ), { "native_value": 34167, - "native_unit_of_measurement": ENERGY_MEGA_WATT_HOUR, + "native_unit_of_measurement": UnitOfEnergy.MEGA_WATT_HOUR, "icon": "mdi:fire", "last_reset": last_reset, }, @@ -139,13 +139,13 @@ async def test_restore_state(mock_heat_meter, hass): "456", attributes={ ATTR_LAST_RESET: last_reset, - ATTR_UNIT_OF_MEASUREMENT: VOLUME_CUBIC_METERS, + ATTR_UNIT_OF_MEASUREMENT: UnitOfVolume.CUBIC_METERS, ATTR_STATE_CLASS: SensorStateClass.TOTAL, }, ), { "native_value": 456, - "native_unit_of_measurement": VOLUME_CUBIC_METERS, + "native_unit_of_measurement": UnitOfVolume.CUBIC_METERS, "icon": "mdi:fire", "last_reset": last_reset, }, @@ -183,13 +183,13 @@ async def test_restore_state(mock_heat_meter, hass): state = hass.states.get("sensor.heat_meter_heat_usage") assert state assert state.state == "34167" - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_MEGA_WATT_HOUR + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.MEGA_WATT_HOUR assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL state = hass.states.get("sensor.heat_meter_volume_usage") assert state assert state.state == "456" - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == VOLUME_CUBIC_METERS + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfVolume.CUBIC_METERS assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL state = hass.states.get("sensor.heat_meter_device_number") diff --git a/tests/components/lcn/test_sensor.py b/tests/components/lcn/test_sensor.py index 4b6c0beb7e2..b9bb91cbb9a 100644 --- a/tests/components/lcn/test_sensor.py +++ b/tests/components/lcn/test_sensor.py @@ -8,7 +8,7 @@ from homeassistant.const import ( ATTR_UNIT_OF_MEASUREMENT, STATE_UNAVAILABLE, STATE_UNKNOWN, - TEMP_CELSIUS, + UnitOfTemperature, ) from homeassistant.helpers import entity_registry as er @@ -35,11 +35,11 @@ async def test_entity_state(hass, lcn_connection): """Test state of entity.""" state = hass.states.get(SENSOR_VAR1) assert state - assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == TEMP_CELSIUS + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfTemperature.CELSIUS state = hass.states.get(SENSOR_SETPOINT1) assert state - assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == TEMP_CELSIUS + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfTemperature.CELSIUS state = hass.states.get(SENSOR_LED6) assert state diff --git a/tests/components/litterrobot/test_sensor.py b/tests/components/litterrobot/test_sensor.py index beed13c75dd..9586e7cdbfc 100644 --- a/tests/components/litterrobot/test_sensor.py +++ b/tests/components/litterrobot/test_sensor.py @@ -4,7 +4,7 @@ from unittest.mock import MagicMock import pytest from homeassistant.components.sensor import DOMAIN as PLATFORM_DOMAIN, SensorDeviceClass -from homeassistant.const import MASS_POUNDS, PERCENTAGE, STATE_UNKNOWN +from homeassistant.const import PERCENTAGE, STATE_UNKNOWN, UnitOfMass from homeassistant.core import HomeAssistant from .conftest import setup_integration @@ -92,7 +92,7 @@ async def test_litter_robot_sensor( assert sensor.attributes["unit_of_measurement"] == PERCENTAGE sensor = hass.states.get("sensor.test_pet_weight") assert sensor.state == "12.0" - assert sensor.attributes["unit_of_measurement"] == MASS_POUNDS + assert sensor.attributes["unit_of_measurement"] == UnitOfMass.POUNDS async def test_feeder_robot_sensor( diff --git a/tests/components/luftdaten/test_sensor.py b/tests/components/luftdaten/test_sensor.py index f3d0f1c0b1f..e9e86fd9f1b 100644 --- a/tests/components/luftdaten/test_sensor.py +++ b/tests/components/luftdaten/test_sensor.py @@ -12,8 +12,8 @@ from homeassistant.const import ( ATTR_UNIT_OF_MEASUREMENT, CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, PERCENTAGE, - PRESSURE_PA, - TEMP_CELSIUS, + UnitOfPressure, + UnitOfTemperature, ) from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er @@ -40,7 +40,7 @@ async def test_luftdaten_sensors( assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Sensor 12345 Temperature" assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TEMPERATURE assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_CELSIUS + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfTemperature.CELSIUS assert ATTR_ICON not in state.attributes entry = entity_registry.async_get("sensor.sensor_12345_humidity") @@ -68,7 +68,7 @@ async def test_luftdaten_sensors( assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Sensor 12345 Pressure" assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PRESSURE assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PRESSURE_PA + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPressure.PA assert ATTR_ICON not in state.attributes entry = entity_registry.async_get("sensor.sensor_12345_pressure_at_sealevel") @@ -84,7 +84,7 @@ async def test_luftdaten_sensors( ) assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.PRESSURE assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT - assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PRESSURE_PA + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPressure.PA assert ATTR_ICON not in state.attributes entry = entity_registry.async_get("sensor.sensor_12345_pm10")