Sort unit lists sent to frontend (#88898)

pull/88922/head
Erik Montnemery 2023-02-28 16:12:49 +01:00 committed by GitHub
parent a2a23564a4
commit 390daf1723
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 35 deletions

View File

@ -29,7 +29,10 @@ def ws_device_class_units(
) -> None: ) -> None:
"""Return supported units for a device class.""" """Return supported units for a device class."""
device_class = msg["device_class"] device_class = msg["device_class"]
convertible_units = set() convertible_units = []
if device_class in UNIT_CONVERTERS and device_class in DEVICE_CLASS_UNITS: if device_class in UNIT_CONVERTERS and device_class in DEVICE_CLASS_UNITS:
convertible_units = DEVICE_CLASS_UNITS[device_class] convertible_units = sorted(
DEVICE_CLASS_UNITS[device_class],
key=lambda s: str.casefold(str(s)),
)
connection.send_result(msg["id"], {"units": convertible_units}) connection.send_result(msg["id"], {"units": convertible_units})

View File

@ -29,7 +29,10 @@ def ws_device_class_units(
) -> None: ) -> None:
"""Return supported units for a device class.""" """Return supported units for a device class."""
device_class = msg["device_class"] device_class = msg["device_class"]
convertible_units = set() convertible_units = []
if device_class in UNIT_CONVERTERS and device_class in DEVICE_CLASS_UNITS: if device_class in UNIT_CONVERTERS and device_class in DEVICE_CLASS_UNITS:
convertible_units = DEVICE_CLASS_UNITS[device_class] convertible_units = sorted(
DEVICE_CLASS_UNITS[device_class],
key=lambda s: str.casefold(str(s)),
)
connection.send_result(msg["id"], {"units": convertible_units}) connection.send_result(msg["id"], {"units": convertible_units})

View File

@ -27,4 +27,7 @@ def ws_convertible_units(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any] hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
) -> None: ) -> None:
"""Return supported units for a device class.""" """Return supported units for a device class."""
connection.send_result(msg["id"], {"units": VALID_UNITS}) sorted_units = {
key: sorted(units, key=str.casefold) for key, units in VALID_UNITS.items()
}
connection.send_result(msg["id"], {"units": sorted_units})

View File

@ -1,6 +1,4 @@
"""Test the number websocket API.""" """Test the number websocket API."""
from pytest_unordered import unordered
from homeassistant.components.number.const import DOMAIN from homeassistant.components.number.const import DOMAIN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
@ -17,21 +15,19 @@ async def test_device_class_units(
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
# Device class with units which number allows customizing & converting # Device class with units which number allows customizing & converting
await client.send_json( await client.send_json_auto_id(
{ {
"id": 1,
"type": "number/device_class_convertible_units", "type": "number/device_class_convertible_units",
"device_class": "temperature", "device_class": "temperature",
} }
) )
msg = await client.receive_json() msg = await client.receive_json()
assert msg["success"] assert msg["success"]
assert msg["result"] == {"units": unordered(["°F", "°C", "K"])} assert msg["result"] == {"units": ["K", "°C", "°F"]}
# Device class with units which number doesn't allow customizing & converting # Device class with units which number doesn't allow customizing & converting
await client.send_json( await client.send_json_auto_id(
{ {
"id": 2,
"type": "number/device_class_convertible_units", "type": "number/device_class_convertible_units",
"device_class": "energy", "device_class": "energy",
} }
@ -41,13 +37,12 @@ async def test_device_class_units(
assert msg["result"] == {"units": []} assert msg["result"] == {"units": []}
# Unknown device class # Unknown device class
await client.send_json( await client.send_json_auto_id(
{ {
"id": 3,
"type": "number/device_class_convertible_units", "type": "number/device_class_convertible_units",
"device_class": "kebabsås", "device_class": "kebabsås",
} }
) )
msg = await client.receive_json() msg = await client.receive_json()
assert msg["success"] assert msg["success"]
assert msg["result"] == {"units": unordered([])} assert msg["result"] == {"units": []}

View File

@ -1,6 +1,4 @@
"""Test the sensor websocket API.""" """Test the sensor websocket API."""
from pytest_unordered import unordered
from homeassistant.components.sensor.const import DOMAIN from homeassistant.components.sensor.const import DOMAIN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
@ -17,9 +15,8 @@ async def test_device_class_units(
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
# Device class with units which sensor allows customizing & converting # Device class with units which sensor allows customizing & converting
await client.send_json( await client.send_json_auto_id(
{ {
"id": 1,
"type": "sensor/device_class_convertible_units", "type": "sensor/device_class_convertible_units",
"device_class": "speed", "device_class": "speed",
} }
@ -27,15 +24,23 @@ async def test_device_class_units(
msg = await client.receive_json() msg = await client.receive_json()
assert msg["success"] assert msg["success"]
assert msg["result"] == { assert msg["result"] == {
"units": unordered( "units": ["ft/s", "in/d", "in/h", "km/h", "kn", "m/s", "mm/d", "mm/h", "mph"]
["km/h", "kn", "mph", "in/h", "in/d", "ft/s", "mm/d", "mm/h", "m/s"]
)
} }
# Device class with units which sensor doesn't allow customizing & converting # Device class with units which include `None`
await client.send_json( await client.send_json_auto_id(
{
"type": "sensor/device_class_convertible_units",
"device_class": "power_factor",
}
)
msg = await client.receive_json()
assert msg["success"]
assert msg["result"] == {"units": ["%", None]}
# Device class with units which sensor doesn't allow customizing & converting
await client.send_json_auto_id(
{ {
"id": 2,
"type": "sensor/device_class_convertible_units", "type": "sensor/device_class_convertible_units",
"device_class": "pm1", "device_class": "pm1",
} }
@ -45,13 +50,12 @@ async def test_device_class_units(
assert msg["result"] == {"units": []} assert msg["result"] == {"units": []}
# Unknown device class # Unknown device class
await client.send_json( await client.send_json_auto_id(
{ {
"id": 3,
"type": "sensor/device_class_convertible_units", "type": "sensor/device_class_convertible_units",
"device_class": "kebabsås", "device_class": "kebabsås",
} }
) )
msg = await client.receive_json() msg = await client.receive_json()
assert msg["success"] assert msg["success"]
assert msg["result"] == {"units": unordered([])} assert msg["result"] == {"units": []}

View File

@ -1,6 +1,4 @@
"""Test the weather websocket API.""" """Test the weather websocket API."""
from pytest_unordered import unordered
from homeassistant.components.weather.const import DOMAIN from homeassistant.components.weather.const import DOMAIN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
@ -22,10 +20,10 @@ async def test_device_class_units(hass: HomeAssistant, hass_ws_client) -> None:
assert msg["success"] assert msg["success"]
assert msg["result"] == { assert msg["result"] == {
"units": { "units": {
"precipitation_unit": unordered(["mm", "in"]), "precipitation_unit": ["in", "mm"],
"pressure_unit": unordered(["mbar", "mmHg", "inHg", "hPa"]), "pressure_unit": ["hPa", "inHg", "mbar", "mmHg"],
"temperature_unit": unordered(["°F", "°C"]), "temperature_unit": ["°C", "°F"],
"visibility_unit": unordered(["km", "mi"]), "visibility_unit": ["km", "mi"],
"wind_speed_unit": unordered(["mph", "km/h", "kn", "m/s", "ft/s"]), "wind_speed_unit": ["ft/s", "km/h", "kn", "m/s", "mph"],
} }
} }