Add WS command sensor/device_class_convertible_units (#85213)

* Add WS command sensor/device_class_units

* Rename new command to device_class_convertible_units
This commit is contained in:
Erik Montnemery 2023-01-12 09:31:06 +01:00 committed by GitHub
parent 679e971131
commit 305fb86d50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 0 deletions

View file

@ -0,0 +1,35 @@
"""The sensor websocket API."""
from __future__ import annotations
from typing import Any
import voluptuous as vol
from homeassistant.components import websocket_api
from homeassistant.core import HomeAssistant, callback
from .const import DEVICE_CLASS_UNITS, UNIT_CONVERTERS
@callback
def async_setup(hass: HomeAssistant) -> None:
"""Set up the sensor websocket API."""
websocket_api.async_register_command(hass, ws_device_class_units)
@callback
@websocket_api.websocket_command(
{
vol.Required("type"): "sensor/device_class_convertible_units",
vol.Required("device_class"): str,
}
)
def ws_device_class_units(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
) -> None:
"""Return supported units for a device class."""
device_class = msg["device_class"]
convertible_units = set()
if device_class in UNIT_CONVERTERS and device_class in DEVICE_CLASS_UNITS:
convertible_units = DEVICE_CLASS_UNITS[device_class]
connection.send_result(msg["id"], {"units": convertible_units})