Add rules for converting volumes (#80951)

* Add rules for converting volumes

* Use SensorDeviceClass in new tests

* Tweak tests

* Update flo tests

* Update sensor tests
pull/81000/head
Erik Montnemery 2022-10-26 10:29:33 +02:00 committed by GitHub
parent 8714fc5c2c
commit 352976fd1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 5 deletions

View File

@ -36,8 +36,12 @@ from homeassistant.const import (
TEMPERATURE,
UNIT_NOT_RECOGNIZED_TEMPLATE,
VOLUME,
VOLUME_CUBIC_FEET,
VOLUME_CUBIC_METERS,
VOLUME_FLUID_OUNCE,
VOLUME_GALLONS,
VOLUME_LITERS,
VOLUME_MILLILITERS,
WIND_SPEED,
)
from homeassistant.helpers.frame import report
@ -270,9 +274,18 @@ METRIC_SYSTEM = UnitSystem(
("distance", LENGTH_INCHES): LENGTH_MILLIMETERS,
("distance", LENGTH_MILES): LENGTH_KILOMETERS,
("distance", LENGTH_YARD): LENGTH_METERS,
# Convert non-metric volumes of gas meters
("gas", VOLUME_CUBIC_FEET): VOLUME_CUBIC_METERS,
# Convert non-metric speeds except knots to km/h
("speed", SPEED_FEET_PER_SECOND): SPEED_KILOMETERS_PER_HOUR,
("speed", SPEED_MILES_PER_HOUR): SPEED_KILOMETERS_PER_HOUR,
# Convert non-metric volumes
("volume", VOLUME_CUBIC_FEET): VOLUME_CUBIC_METERS,
("volume", VOLUME_FLUID_OUNCE): VOLUME_MILLILITERS,
("volume", VOLUME_GALLONS): VOLUME_LITERS,
# Convert non-metric volumes of water meters
("water", VOLUME_CUBIC_FEET): VOLUME_CUBIC_METERS,
("water", VOLUME_GALLONS): VOLUME_LITERS,
},
length=LENGTH_KILOMETERS,
mass=MASS_GRAMS,
@ -291,9 +304,18 @@ US_CUSTOMARY_SYSTEM = UnitSystem(
("distance", LENGTH_KILOMETERS): LENGTH_MILES,
("distance", LENGTH_METERS): LENGTH_FEET,
("distance", LENGTH_MILLIMETERS): LENGTH_INCHES,
# Convert non-USCS volumes of gas meters
("gas", VOLUME_CUBIC_METERS): VOLUME_CUBIC_FEET,
# Convert non-USCS speeds except knots to mph
("speed", SPEED_METERS_PER_SECOND): SPEED_MILES_PER_HOUR,
("speed", SPEED_KILOMETERS_PER_HOUR): SPEED_MILES_PER_HOUR,
# Convert non-USCS volumes
("volume", VOLUME_CUBIC_METERS): VOLUME_CUBIC_FEET,
("volume", VOLUME_LITERS): VOLUME_GALLONS,
("volume", VOLUME_MILLILITERS): VOLUME_FLUID_OUNCE,
# Convert non-USCS volumes of water meters
("water", VOLUME_CUBIC_METERS): VOLUME_CUBIC_FEET,
("water", VOLUME_LITERS): VOLUME_GALLONS,
},
length=LENGTH_MILES,
mass=MASS_POUNDS,

View File

@ -3,12 +3,14 @@ from homeassistant.components.flo.const import DOMAIN as FLO_DOMAIN
from homeassistant.components.sensor import ATTR_STATE_CLASS, SensorStateClass
from homeassistant.const import ATTR_ENTITY_ID, CONF_PASSWORD, CONF_USERNAME
from homeassistant.setup import async_setup_component
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
from .common import TEST_PASSWORD, TEST_USER_ID
async def test_sensors(hass, config_entry, aioclient_mock_fixture):
"""Test Flo by Moen sensors."""
hass.config.units = US_CUSTOMARY_SYSTEM
config_entry.add_to_hass(hass)
assert await async_setup_component(
hass, FLO_DOMAIN, {CONF_USERNAME: TEST_USER_ID, CONF_PASSWORD: TEST_PASSWORD}
@ -49,7 +51,7 @@ async def test_sensors(hass, config_entry, aioclient_mock_fixture):
== SensorStateClass.MEASUREMENT
)
assert hass.states.get("sensor.smart_water_shutoff_water_temperature").state == "21"
assert hass.states.get("sensor.smart_water_shutoff_water_temperature").state == "70"
assert (
hass.states.get("sensor.smart_water_shutoff_water_temperature").attributes[
ATTR_STATE_CLASS
@ -58,7 +60,7 @@ async def test_sensors(hass, config_entry, aioclient_mock_fixture):
)
# and 3 entities for the detector
assert hass.states.get("sensor.kitchen_sink_temperature").state == "16"
assert hass.states.get("sensor.kitchen_sink_temperature").state == "61"
assert (
hass.states.get("sensor.kitchen_sink_temperature").attributes[ATTR_STATE_CLASS]
== SensorStateClass.MEASUREMENT

View File

@ -567,11 +567,11 @@ async def test_custom_unit(
SensorDeviceClass.VOLUME,
),
(
VOLUME_LITERS,
VOLUME_FLUID_OUNCE,
VOLUME_FLUID_OUNCE,
VOLUME_LITERS,
VOLUME_LITERS,
78,
2.3,
77.8,
SensorDeviceClass.VOLUME,
),
(

View File

@ -27,7 +27,12 @@ from homeassistant.const import (
TEMP_CELSIUS,
TEMPERATURE,
VOLUME,
VOLUME_CUBIC_FEET,
VOLUME_CUBIC_METERS,
VOLUME_FLUID_OUNCE,
VOLUME_GALLONS,
VOLUME_LITERS,
VOLUME_MILLILITERS,
WIND_SPEED,
)
from homeassistant.exceptions import HomeAssistantError
@ -398,6 +403,10 @@ def test_get_unit_system_invalid(key: str) -> None:
(SensorDeviceClass.DISTANCE, LENGTH_YARD, LENGTH_METERS),
(SensorDeviceClass.DISTANCE, LENGTH_KILOMETERS, None),
(SensorDeviceClass.DISTANCE, "very_long", None),
# Test gas meter conversion
(SensorDeviceClass.GAS, VOLUME_CUBIC_FEET, VOLUME_CUBIC_METERS),
(SensorDeviceClass.GAS, VOLUME_CUBIC_METERS, None),
(SensorDeviceClass.GAS, "very_much", None),
# Test speed conversion
(SensorDeviceClass.SPEED, SPEED_FEET_PER_SECOND, SPEED_KILOMETERS_PER_HOUR),
(SensorDeviceClass.SPEED, SPEED_MILES_PER_HOUR, SPEED_KILOMETERS_PER_HOUR),
@ -405,6 +414,20 @@ def test_get_unit_system_invalid(key: str) -> None:
(SensorDeviceClass.SPEED, SPEED_KNOTS, None),
(SensorDeviceClass.SPEED, SPEED_METERS_PER_SECOND, None),
(SensorDeviceClass.SPEED, "very_fast", None),
# Test volume conversion
(SensorDeviceClass.VOLUME, VOLUME_CUBIC_FEET, VOLUME_CUBIC_METERS),
(SensorDeviceClass.VOLUME, VOLUME_FLUID_OUNCE, VOLUME_MILLILITERS),
(SensorDeviceClass.VOLUME, VOLUME_GALLONS, VOLUME_LITERS),
(SensorDeviceClass.VOLUME, VOLUME_CUBIC_METERS, None),
(SensorDeviceClass.VOLUME, VOLUME_LITERS, None),
(SensorDeviceClass.VOLUME, VOLUME_MILLILITERS, None),
(SensorDeviceClass.VOLUME, "very_much", None),
# Test water meter conversion
(SensorDeviceClass.WATER, VOLUME_CUBIC_FEET, VOLUME_CUBIC_METERS),
(SensorDeviceClass.WATER, VOLUME_GALLONS, VOLUME_LITERS),
(SensorDeviceClass.WATER, VOLUME_CUBIC_METERS, None),
(SensorDeviceClass.WATER, VOLUME_LITERS, None),
(SensorDeviceClass.WATER, "very_much", None),
),
)
def test_get_metric_converted_unit_(
@ -427,6 +450,10 @@ def test_get_metric_converted_unit_(
(SensorDeviceClass.DISTANCE, LENGTH_MILLIMETERS, LENGTH_INCHES),
(SensorDeviceClass.DISTANCE, LENGTH_MILES, None),
(SensorDeviceClass.DISTANCE, "very_long", None),
# Test gas meter conversion
(SensorDeviceClass.GAS, VOLUME_CUBIC_METERS, VOLUME_CUBIC_FEET),
(SensorDeviceClass.GAS, VOLUME_CUBIC_FEET, None),
(SensorDeviceClass.GAS, "very_much", None),
# Test speed conversion
(SensorDeviceClass.SPEED, SPEED_METERS_PER_SECOND, SPEED_MILES_PER_HOUR),
(SensorDeviceClass.SPEED, SPEED_KILOMETERS_PER_HOUR, SPEED_MILES_PER_HOUR),
@ -434,6 +461,20 @@ def test_get_metric_converted_unit_(
(SensorDeviceClass.SPEED, SPEED_KNOTS, None),
(SensorDeviceClass.SPEED, SPEED_MILES_PER_HOUR, None),
(SensorDeviceClass.SPEED, "very_fast", None),
# Test volume conversion
(SensorDeviceClass.VOLUME, VOLUME_CUBIC_METERS, VOLUME_CUBIC_FEET),
(SensorDeviceClass.VOLUME, VOLUME_LITERS, VOLUME_GALLONS),
(SensorDeviceClass.VOLUME, VOLUME_MILLILITERS, VOLUME_FLUID_OUNCE),
(SensorDeviceClass.VOLUME, VOLUME_CUBIC_FEET, None),
(SensorDeviceClass.VOLUME, VOLUME_FLUID_OUNCE, None),
(SensorDeviceClass.VOLUME, VOLUME_GALLONS, None),
(SensorDeviceClass.VOLUME, "very_much", None),
# Test water meter conversion
(SensorDeviceClass.WATER, VOLUME_CUBIC_METERS, VOLUME_CUBIC_FEET),
(SensorDeviceClass.WATER, VOLUME_LITERS, VOLUME_GALLONS),
(SensorDeviceClass.WATER, VOLUME_CUBIC_FEET, None),
(SensorDeviceClass.WATER, VOLUME_GALLONS, None),
(SensorDeviceClass.WATER, "very_much", None),
),
)
def test_get_us_converted_unit(