Add and use more unit constants (#32122)
* Add and use speed constants * Add and use meter based volume and area constants * Add and use more mass unit constants * Add and use concentration unit constants * Add and use watts per square meter constant * Use more time constants * Use more data constants
This commit is contained in:
1 parent
c98f50115d
commit
496bd3dddf
44 files changed
+422
-177
No files matched your search
@@ -16,6 +16,9 @@ from homeassistant.components.awair.sensor import (
|
||||
)
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.const import (
|
||||
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
||||
CONCENTRATION_PARTS_PER_BILLION,
|
||||
CONCENTRATION_PARTS_PER_MILLION,
|
||||
DEVICE_CLASS_HUMIDITY,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
STATE_UNAVAILABLE,
|
||||
@@ -183,7 +186,7 @@ async def test_awair_co2(hass):
|
||||
sensor = hass.states.get("sensor.awair_co2")
|
||||
assert sensor.state == "612"
|
||||
assert sensor.attributes["device_class"] == DEVICE_CLASS_CARBON_DIOXIDE
|
||||
assert sensor.attributes["unit_of_measurement"] == "ppm"
|
||||
assert sensor.attributes["unit_of_measurement"] == CONCENTRATION_PARTS_PER_MILLION
|
||||
|
||||
|
||||
async def test_awair_voc(hass):
|
||||
@@ -193,7 +196,7 @@ async def test_awair_voc(hass):
|
||||
sensor = hass.states.get("sensor.awair_voc")
|
||||
assert sensor.state == "1012"
|
||||
assert sensor.attributes["device_class"] == DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS
|
||||
assert sensor.attributes["unit_of_measurement"] == "ppb"
|
||||
assert sensor.attributes["unit_of_measurement"] == CONCENTRATION_PARTS_PER_BILLION
|
||||
|
||||
|
||||
async def test_awair_dust(hass):
|
||||
@@ -205,7 +208,10 @@ async def test_awair_dust(hass):
|
||||
sensor = hass.states.get("sensor.awair_pm2_5")
|
||||
assert sensor.state == "6.2"
|
||||
assert sensor.attributes["device_class"] == DEVICE_CLASS_PM2_5
|
||||
assert sensor.attributes["unit_of_measurement"] == "µg/m3"
|
||||
assert (
|
||||
sensor.attributes["unit_of_measurement"]
|
||||
== CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
|
||||
)
|
||||
|
||||
|
||||
async def test_awair_unsupported_sensors(hass):
|
||||
|
||||
@@ -15,7 +15,7 @@ import pytest
|
||||
|
||||
from homeassistant.bootstrap import async_setup_component
|
||||
from homeassistant.components.dsmr.sensor import DerivativeDSMREntity
|
||||
from homeassistant.const import TIME_HOURS
|
||||
from homeassistant.const import TIME_HOURS, VOLUME_CUBIC_METERS
|
||||
|
||||
from tests.common import assert_setup_component
|
||||
|
||||
@@ -67,7 +67,7 @@ async def test_default_setup(hass, mock_connection_factory):
|
||||
GAS_METER_READING: MBusObject(
|
||||
[
|
||||
{"value": datetime.datetime.fromtimestamp(1551642213)},
|
||||
{"value": Decimal(745.695), "unit": "m3"},
|
||||
{"value": Decimal(745.695), "unit": VOLUME_CUBIC_METERS},
|
||||
]
|
||||
),
|
||||
}
|
||||
@@ -101,7 +101,7 @@ async def test_default_setup(hass, mock_connection_factory):
|
||||
# check if gas consumption is parsed correctly
|
||||
gas_consumption = hass.states.get("sensor.gas_consumption")
|
||||
assert gas_consumption.state == "745.695"
|
||||
assert gas_consumption.attributes.get("unit_of_measurement") == "m3"
|
||||
assert gas_consumption.attributes.get("unit_of_measurement") == VOLUME_CUBIC_METERS
|
||||
|
||||
|
||||
async def test_derivative():
|
||||
@@ -119,7 +119,7 @@ async def test_derivative():
|
||||
"1.0.0": MBusObject(
|
||||
[
|
||||
{"value": datetime.datetime.fromtimestamp(1551642213)},
|
||||
{"value": Decimal(745.695), "unit": "m3"},
|
||||
{"value": Decimal(745.695), "unit": VOLUME_CUBIC_METERS},
|
||||
]
|
||||
)
|
||||
}
|
||||
@@ -131,7 +131,7 @@ async def test_derivative():
|
||||
"1.0.0": MBusObject(
|
||||
[
|
||||
{"value": datetime.datetime.fromtimestamp(1551642543)},
|
||||
{"value": Decimal(745.698), "unit": "m3"},
|
||||
{"value": Decimal(745.698), "unit": VOLUME_CUBIC_METERS},
|
||||
]
|
||||
)
|
||||
}
|
||||
@@ -141,7 +141,7 @@ async def test_derivative():
|
||||
abs(entity.state - 0.033) < 0.00001
|
||||
), "state should be hourly usage calculated from first and second update"
|
||||
|
||||
assert entity.unit_of_measurement == f"m3/{TIME_HOURS}"
|
||||
assert entity.unit_of_measurement == f"{VOLUME_CUBIC_METERS}/{TIME_HOURS}"
|
||||
|
||||
|
||||
async def test_v4_meter(hass, mock_connection_factory):
|
||||
@@ -160,7 +160,7 @@ async def test_v4_meter(hass, mock_connection_factory):
|
||||
HOURLY_GAS_METER_READING: MBusObject(
|
||||
[
|
||||
{"value": datetime.datetime.fromtimestamp(1551642213)},
|
||||
{"value": Decimal(745.695), "unit": "m3"},
|
||||
{"value": Decimal(745.695), "unit": VOLUME_CUBIC_METERS},
|
||||
]
|
||||
),
|
||||
ELECTRICITY_ACTIVE_TARIFF: CosemObject([{"value": "0001", "unit": ""}]),
|
||||
@@ -185,7 +185,7 @@ async def test_v4_meter(hass, mock_connection_factory):
|
||||
# check if gas consumption is parsed correctly
|
||||
gas_consumption = hass.states.get("sensor.gas_consumption")
|
||||
assert gas_consumption.state == "745.695"
|
||||
assert gas_consumption.attributes.get("unit_of_measurement") == "m3"
|
||||
assert gas_consumption.attributes.get("unit_of_measurement") == VOLUME_CUBIC_METERS
|
||||
|
||||
|
||||
async def test_belgian_meter(hass, mock_connection_factory):
|
||||
@@ -204,7 +204,7 @@ async def test_belgian_meter(hass, mock_connection_factory):
|
||||
BELGIUM_HOURLY_GAS_METER_READING: MBusObject(
|
||||
[
|
||||
{"value": datetime.datetime.fromtimestamp(1551642213)},
|
||||
{"value": Decimal(745.695), "unit": "m3"},
|
||||
{"value": Decimal(745.695), "unit": VOLUME_CUBIC_METERS},
|
||||
]
|
||||
),
|
||||
ELECTRICITY_ACTIVE_TARIFF: CosemObject([{"value": "0001", "unit": ""}]),
|
||||
@@ -229,7 +229,7 @@ async def test_belgian_meter(hass, mock_connection_factory):
|
||||
# check if gas consumption is parsed correctly
|
||||
gas_consumption = hass.states.get("sensor.gas_consumption")
|
||||
assert gas_consumption.state == "745.695"
|
||||
assert gas_consumption.attributes.get("unit_of_measurement") == "m3"
|
||||
assert gas_consumption.attributes.get("unit_of_measurement") == VOLUME_CUBIC_METERS
|
||||
|
||||
|
||||
async def test_belgian_meter_low(hass, mock_connection_factory):
|
||||
|
||||
@@ -8,7 +8,12 @@ import pytest
|
||||
|
||||
from homeassistant.components.foobot import sensor as foobot
|
||||
import homeassistant.components.sensor as sensor
|
||||
from homeassistant.const import TEMP_CELSIUS
|
||||
from homeassistant.const import (
|
||||
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
||||
CONCENTRATION_PARTS_PER_BILLION,
|
||||
CONCENTRATION_PARTS_PER_MILLION,
|
||||
TEMP_CELSIUS,
|
||||
)
|
||||
from homeassistant.exceptions import PlatformNotReady
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
@@ -33,11 +38,11 @@ async def test_default_setup(hass, aioclient_mock):
|
||||
assert await async_setup_component(hass, sensor.DOMAIN, {"sensor": VALID_CONFIG})
|
||||
|
||||
metrics = {
|
||||
"co2": ["1232.0", "ppm"],
|
||||
"co2": ["1232.0", CONCENTRATION_PARTS_PER_MILLION],
|
||||
"temperature": ["21.1", TEMP_CELSIUS],
|
||||
"humidity": ["49.5", "%"],
|
||||
"pm2_5": ["144.8", "µg/m3"],
|
||||
"voc": ["340.7", "ppb"],
|
||||
"pm2_5": ["144.8", CONCENTRATION_MICROGRAMS_PER_CUBIC_METER],
|
||||
"voc": ["340.7", CONCENTRATION_PARTS_PER_BILLION],
|
||||
"index": ["138.9", "%"],
|
||||
}
|
||||
|
||||
|
||||
@@ -20,10 +20,14 @@ from homeassistant.components.homematicip_cloud.sensor import (
|
||||
ATTR_TEMPERATURE_OFFSET,
|
||||
ATTR_WIND_DIRECTION,
|
||||
ATTR_WIND_DIRECTION_VARIATION,
|
||||
TIME_HOURS,
|
||||
)
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, POWER_WATT, TEMP_CELSIUS
|
||||
from homeassistant.const import (
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
POWER_WATT,
|
||||
SPEED_KILOMETERS_PER_HOUR,
|
||||
TEMP_CELSIUS,
|
||||
)
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from .helper import async_manipulate_test_data, get_and_check_entity_basics
|
||||
@@ -285,7 +289,7 @@ 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] == f"km/{TIME_HOURS}"
|
||||
assert ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == SPEED_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"
|
||||
|
||||
@@ -4,7 +4,7 @@ from unittest.mock import DEFAULT, Mock, patch
|
||||
|
||||
import homeassistant.components.mhz19.sensor as mhz19
|
||||
from homeassistant.components.sensor import DOMAIN
|
||||
from homeassistant.const import TEMP_FAHRENHEIT
|
||||
from homeassistant.const import CONCENTRATION_PARTS_PER_MILLION, TEMP_FAHRENHEIT
|
||||
from homeassistant.setup import setup_component
|
||||
|
||||
from tests.common import assert_setup_component, get_test_home_assistant
|
||||
@@ -100,7 +100,7 @@ class TestMHZ19Sensor(unittest.TestCase):
|
||||
|
||||
assert "name: CO2" == sensor.name
|
||||
assert 1000 == sensor.state
|
||||
assert "ppm" == sensor.unit_of_measurement
|
||||
assert CONCENTRATION_PARTS_PER_MILLION == sensor.unit_of_measurement
|
||||
assert sensor.should_poll
|
||||
assert {"temperature": 24} == sensor.device_state_attributes
|
||||
|
||||
|
||||
@@ -5,7 +5,11 @@ from homeassistant import setup
|
||||
from homeassistant.components import climate, sensor
|
||||
from homeassistant.components.demo.sensor import DemoSensor
|
||||
import homeassistant.components.prometheus as prometheus
|
||||
from homeassistant.const import DEVICE_CLASS_POWER, ENERGY_KILO_WATT_HOUR
|
||||
from homeassistant.const import (
|
||||
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
||||
DEVICE_CLASS_POWER,
|
||||
ENERGY_KILO_WATT_HOUR,
|
||||
)
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
|
||||
@@ -47,7 +51,12 @@ async def prometheus_client(loop, hass, hass_client):
|
||||
await sensor4.async_update_ha_state()
|
||||
|
||||
sensor5 = DemoSensor(
|
||||
None, "SPS30 PM <1µm Weight concentration", 3.7069, None, "µg/m³", None
|
||||
None,
|
||||
"SPS30 PM <1µm Weight concentration",
|
||||
3.7069,
|
||||
None,
|
||||
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
||||
None,
|
||||
)
|
||||
sensor5.hass = hass
|
||||
sensor5.entity_id = "sensor.sps30_pm_1um_weight_concentration"
|
||||
|
||||
@@ -3,7 +3,7 @@ from datetime import datetime
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.bootstrap import async_setup_component
|
||||
from homeassistant.const import TIME_SECONDS
|
||||
from homeassistant.const import SPEED_METERS_PER_SECOND
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import assert_setup_component, load_fixture
|
||||
@@ -71,7 +71,7 @@ async def test_custom_setup(hass, aioclient_mock):
|
||||
assert state.state == "0.0"
|
||||
|
||||
state = hass.states.get("sensor.yr_wind_speed")
|
||||
assert state.attributes.get("unit_of_measurement") == f"m/{TIME_SECONDS}"
|
||||
assert state.attributes.get("unit_of_measurement") == SPEED_METERS_PER_SECOND
|
||||
assert state.state == "3.5"
|
||||
|
||||
|
||||
@@ -117,5 +117,5 @@ async def test_forecast_setup(hass, aioclient_mock):
|
||||
assert state.state == "0.0"
|
||||
|
||||
state = hass.states.get("sensor.yr_wind_speed")
|
||||
assert state.attributes.get("unit_of_measurement") == f"m/{TIME_SECONDS}"
|
||||
assert state.attributes.get("unit_of_measurement") == SPEED_METERS_PER_SECOND
|
||||
assert state.state == "3.6"
|
||||
Reference in new issue
Block a user