Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WS command sensor/numeric_device_classes #101257

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion homeassistant/components/sensor/websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
from homeassistant.components import websocket_api
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
def async_setup(hass: HomeAssistant) -> None:
"""Set up the sensor websocket API."""
websocket_api.async_register_command(hass, ws_device_class_units)
websocket_api.async_register_command(hass, ws_numeric_device_classes)


@callback
Expand All @@ -36,3 +42,19 @@ def ws_device_class_units(
key=lambda s: str.casefold(str(s)),
)
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)}
)
27 changes: 26 additions & 1 deletion tests/components/sensor/test_websocket_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
"""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.setup import async_setup_component

Expand Down Expand Up @@ -59,3 +65,22 @@ async def test_device_class_units(
msg = await client.receive_json()
assert msg["success"]
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))
}