Sort unit lists sent to frontend (#88898)
parent
a2a23564a4
commit
390daf1723
|
@ -29,7 +29,10 @@ def ws_device_class_units(
|
|||
) -> None:
|
||||
"""Return supported units for a 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:
|
||||
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})
|
||||
|
|
|
@ -29,7 +29,10 @@ def ws_device_class_units(
|
|||
) -> None:
|
||||
"""Return supported units for a 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:
|
||||
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})
|
||||
|
|
|
@ -27,4 +27,7 @@ def ws_convertible_units(
|
|||
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
|
||||
) -> None:
|
||||
"""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})
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
"""Test the number websocket API."""
|
||||
from pytest_unordered import unordered
|
||||
|
||||
from homeassistant.components.number.const import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
@ -17,21 +15,19 @@ async def test_device_class_units(
|
|||
client = await hass_ws_client(hass)
|
||||
|
||||
# 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",
|
||||
"device_class": "temperature",
|
||||
}
|
||||
)
|
||||
msg = await client.receive_json()
|
||||
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
|
||||
await client.send_json(
|
||||
await client.send_json_auto_id(
|
||||
{
|
||||
"id": 2,
|
||||
"type": "number/device_class_convertible_units",
|
||||
"device_class": "energy",
|
||||
}
|
||||
|
@ -41,13 +37,12 @@ async def test_device_class_units(
|
|||
assert msg["result"] == {"units": []}
|
||||
|
||||
# Unknown device class
|
||||
await client.send_json(
|
||||
await client.send_json_auto_id(
|
||||
{
|
||||
"id": 3,
|
||||
"type": "number/device_class_convertible_units",
|
||||
"device_class": "kebabsås",
|
||||
}
|
||||
)
|
||||
msg = await client.receive_json()
|
||||
assert msg["success"]
|
||||
assert msg["result"] == {"units": unordered([])}
|
||||
assert msg["result"] == {"units": []}
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
"""Test the sensor websocket API."""
|
||||
from pytest_unordered import unordered
|
||||
|
||||
from homeassistant.components.sensor.const import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
@ -17,9 +15,8 @@ async def test_device_class_units(
|
|||
client = await hass_ws_client(hass)
|
||||
|
||||
# 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",
|
||||
"device_class": "speed",
|
||||
}
|
||||
|
@ -27,15 +24,23 @@ async def test_device_class_units(
|
|||
msg = await client.receive_json()
|
||||
assert msg["success"]
|
||||
assert msg["result"] == {
|
||||
"units": unordered(
|
||||
["km/h", "kn", "mph", "in/h", "in/d", "ft/s", "mm/d", "mm/h", "m/s"]
|
||||
)
|
||||
"units": ["ft/s", "in/d", "in/h", "km/h", "kn", "m/s", "mm/d", "mm/h", "mph"]
|
||||
}
|
||||
|
||||
# Device class with units which sensor doesn't allow customizing & converting
|
||||
await client.send_json(
|
||||
# Device class with units which include `None`
|
||||
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",
|
||||
"device_class": "pm1",
|
||||
}
|
||||
|
@ -45,13 +50,12 @@ async def test_device_class_units(
|
|||
assert msg["result"] == {"units": []}
|
||||
|
||||
# Unknown device class
|
||||
await client.send_json(
|
||||
await client.send_json_auto_id(
|
||||
{
|
||||
"id": 3,
|
||||
"type": "sensor/device_class_convertible_units",
|
||||
"device_class": "kebabsås",
|
||||
}
|
||||
)
|
||||
msg = await client.receive_json()
|
||||
assert msg["success"]
|
||||
assert msg["result"] == {"units": unordered([])}
|
||||
assert msg["result"] == {"units": []}
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
"""Test the weather websocket API."""
|
||||
from pytest_unordered import unordered
|
||||
|
||||
from homeassistant.components.weather.const import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
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["result"] == {
|
||||
"units": {
|
||||
"precipitation_unit": unordered(["mm", "in"]),
|
||||
"pressure_unit": unordered(["mbar", "mmHg", "inHg", "hPa"]),
|
||||
"temperature_unit": unordered(["°F", "°C"]),
|
||||
"visibility_unit": unordered(["km", "mi"]),
|
||||
"wind_speed_unit": unordered(["mph", "km/h", "kn", "m/s", "ft/s"]),
|
||||
"precipitation_unit": ["in", "mm"],
|
||||
"pressure_unit": ["hPa", "inHg", "mbar", "mmHg"],
|
||||
"temperature_unit": ["°C", "°F"],
|
||||
"visibility_unit": ["km", "mi"],
|
||||
"wind_speed_unit": ["ft/s", "km/h", "kn", "m/s", "mph"],
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue