mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Add WS command sensor/numeric_device_classes (#101257)
This commit is contained in:
parent
b97ec2cfce
commit
775751ece5
@ -8,13 +8,19 @@ import voluptuous as vol
|
|||||||
from homeassistant.components import websocket_api
|
from homeassistant.components import websocket_api
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
|
||||||
from .const import DEVICE_CLASS_UNITS, UNIT_CONVERTERS
|
from .const import (
|
||||||
|
DEVICE_CLASS_UNITS,
|
||||||
|
NON_NUMERIC_DEVICE_CLASSES,
|
||||||
|
UNIT_CONVERTERS,
|
||||||
|
SensorDeviceClass,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_setup(hass: HomeAssistant) -> None:
|
def async_setup(hass: HomeAssistant) -> None:
|
||||||
"""Set up the sensor websocket API."""
|
"""Set up the sensor websocket API."""
|
||||||
websocket_api.async_register_command(hass, ws_device_class_units)
|
websocket_api.async_register_command(hass, ws_device_class_units)
|
||||||
|
websocket_api.async_register_command(hass, ws_numeric_device_classes)
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@ -36,3 +42,19 @@ def ws_device_class_units(
|
|||||||
key=lambda s: str.casefold(str(s)),
|
key=lambda s: str.casefold(str(s)),
|
||||||
)
|
)
|
||||||
connection.send_result(msg["id"], {"units": convertible_units})
|
connection.send_result(msg["id"], {"units": convertible_units})
|
||||||
|
|
||||||
|
|
||||||
|
@callback
|
||||||
|
@websocket_api.websocket_command(
|
||||||
|
{
|
||||||
|
vol.Required("type"): "sensor/numeric_device_classes",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
def ws_numeric_device_classes(
|
||||||
|
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
|
||||||
|
) -> None:
|
||||||
|
"""Return numeric sensor device classes."""
|
||||||
|
numeric_device_classes = set(SensorDeviceClass) - NON_NUMERIC_DEVICE_CLASSES
|
||||||
|
connection.send_result(
|
||||||
|
msg["id"], {"numeric_device_classes": list(numeric_device_classes)}
|
||||||
|
)
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
"""Test the sensor websocket API."""
|
"""Test the sensor websocket API."""
|
||||||
from homeassistant.components.sensor.const import DOMAIN
|
from pytest_unordered import unordered
|
||||||
|
|
||||||
|
from homeassistant.components.sensor.const import (
|
||||||
|
DOMAIN,
|
||||||
|
NON_NUMERIC_DEVICE_CLASSES,
|
||||||
|
SensorDeviceClass,
|
||||||
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
@ -59,3 +65,22 @@ 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"] == {"units": []}
|
assert msg["result"] == {"units": []}
|
||||||
|
|
||||||
|
|
||||||
|
async def test_numeric_device_classes(
|
||||||
|
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
|
||||||
|
) -> None:
|
||||||
|
"""Test we can get numeric device classes."""
|
||||||
|
numeric_device_classes = set(SensorDeviceClass) - NON_NUMERIC_DEVICE_CLASSES
|
||||||
|
|
||||||
|
assert await async_setup_component(hass, DOMAIN, {})
|
||||||
|
|
||||||
|
client = await hass_ws_client(hass)
|
||||||
|
|
||||||
|
# Device class with units which sensor allows customizing & converting
|
||||||
|
await client.send_json_auto_id({"type": "sensor/numeric_device_classes"})
|
||||||
|
msg = await client.receive_json()
|
||||||
|
assert msg["success"]
|
||||||
|
assert msg["result"] == {
|
||||||
|
"numeric_device_classes": unordered(list(numeric_device_classes))
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user