Use device class enums in WLED (#60684)

This commit is contained in:
Franck Nijhof 2021-12-01 10:16:44 +01:00 committed by GitHub
parent 73a4dba2ae
commit 74f7f28f1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 21 deletions

View File

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
DEVICE_CLASS_UPDATE, BinarySensorDeviceClass,
BinarySensorEntity, BinarySensorEntity,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
@ -33,7 +33,7 @@ class WLEDUpdateBinarySensor(WLEDEntity, BinarySensorEntity):
"""Defines a WLED firmware binary sensor.""" """Defines a WLED firmware binary sensor."""
_attr_entity_category = ENTITY_CATEGORY_DIAGNOSTIC _attr_entity_category = ENTITY_CATEGORY_DIAGNOSTIC
_attr_device_class = DEVICE_CLASS_UPDATE _attr_device_class = BinarySensorDeviceClass.UPDATE
def __init__(self, coordinator: WLEDDataUpdateCoordinator) -> None: def __init__(self, coordinator: WLEDDataUpdateCoordinator) -> None:
"""Initialize the button entity.""" """Initialize the button entity."""

View File

@ -8,16 +8,14 @@ from datetime import datetime, timedelta
from wled import Device as WLEDDevice from wled import Device as WLEDDevice
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
DEVICE_CLASS_CURRENT,
STATE_CLASS_MEASUREMENT, STATE_CLASS_MEASUREMENT,
SensorDeviceClass,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
DATA_BYTES, DATA_BYTES,
DEVICE_CLASS_SIGNAL_STRENGTH,
DEVICE_CLASS_TIMESTAMP,
ELECTRIC_CURRENT_MILLIAMPERE, ELECTRIC_CURRENT_MILLIAMPERE,
ENTITY_CATEGORY_DIAGNOSTIC, ENTITY_CATEGORY_DIAGNOSTIC,
PERCENTAGE, PERCENTAGE,
@ -52,7 +50,7 @@ SENSORS: tuple[WLEDSensorEntityDescription, ...] = (
key="estimated_current", key="estimated_current",
name="Estimated Current", name="Estimated Current",
native_unit_of_measurement=ELECTRIC_CURRENT_MILLIAMPERE, native_unit_of_measurement=ELECTRIC_CURRENT_MILLIAMPERE,
device_class=DEVICE_CLASS_CURRENT, device_class=SensorDeviceClass.CURRENT,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
value_fn=lambda device: device.info.leds.power, value_fn=lambda device: device.info.leds.power,
@ -68,13 +66,13 @@ SENSORS: tuple[WLEDSensorEntityDescription, ...] = (
name="Max Current", name="Max Current",
native_unit_of_measurement=ELECTRIC_CURRENT_MILLIAMPERE, native_unit_of_measurement=ELECTRIC_CURRENT_MILLIAMPERE,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
device_class=DEVICE_CLASS_CURRENT, device_class=SensorDeviceClass.CURRENT,
value_fn=lambda device: device.info.leds.max_power, value_fn=lambda device: device.info.leds.max_power,
), ),
WLEDSensorEntityDescription( WLEDSensorEntityDescription(
key="uptime", key="uptime",
name="Uptime", name="Uptime",
device_class=DEVICE_CLASS_TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
value_fn=lambda device: (utcnow() - timedelta(seconds=device.info.uptime)), value_fn=lambda device: (utcnow() - timedelta(seconds=device.info.uptime)),
@ -102,7 +100,7 @@ SENSORS: tuple[WLEDSensorEntityDescription, ...] = (
key="wifi_rssi", key="wifi_rssi",
name="Wi-Fi RSSI", name="Wi-Fi RSSI",
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT, native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
device_class=DEVICE_CLASS_SIGNAL_STRENGTH, device_class=SensorDeviceClass.SIGNAL_STRENGTH,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
value_fn=lambda device: device.info.wifi.rssi if device.info.wifi else None, value_fn=lambda device: device.info.wifi.rssi if device.info.wifi else None,

View File

@ -3,7 +3,7 @@ from unittest.mock import MagicMock
import pytest import pytest
from homeassistant.components.binary_sensor import DEVICE_CLASS_UPDATE from homeassistant.components.binary_sensor import BinarySensorDeviceClass
from homeassistant.const import ( from homeassistant.const import (
ATTR_DEVICE_CLASS, ATTR_DEVICE_CLASS,
ATTR_ICON, ATTR_ICON,
@ -25,7 +25,7 @@ async def test_update_available(
state = hass.states.get("binary_sensor.wled_rgb_light_firmware") state = hass.states.get("binary_sensor.wled_rgb_light_firmware")
assert state assert state
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_UPDATE assert state.attributes.get(ATTR_DEVICE_CLASS) == BinarySensorDeviceClass.UPDATE
assert state.state == STATE_ON assert state.state == STATE_ON
assert ATTR_ICON not in state.attributes assert ATTR_ICON not in state.attributes
@ -44,7 +44,7 @@ async def test_no_update_available(
state = hass.states.get("binary_sensor.wled_websocket_firmware") state = hass.states.get("binary_sensor.wled_websocket_firmware")
assert state assert state
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_UPDATE assert state.attributes.get(ATTR_DEVICE_CLASS) == BinarySensorDeviceClass.UPDATE
assert state.state == STATE_OFF assert state.state == STATE_OFF
assert ATTR_ICON not in state.attributes assert ATTR_ICON not in state.attributes

View File

@ -4,12 +4,7 @@ from unittest.mock import MagicMock, patch
import pytest import pytest
from homeassistant.components.sensor import ( from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN, SensorDeviceClass
DEVICE_CLASS_CURRENT,
DEVICE_CLASS_SIGNAL_STRENGTH,
DEVICE_CLASS_TIMESTAMP,
DOMAIN as SENSOR_DOMAIN,
)
from homeassistant.components.wled.const import DOMAIN from homeassistant.components.wled.const import DOMAIN
from homeassistant.const import ( from homeassistant.const import (
ATTR_DEVICE_CLASS, ATTR_DEVICE_CLASS,
@ -98,7 +93,7 @@ async def test_sensors(
assert ( assert (
state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ELECTRIC_CURRENT_MILLIAMPERE state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ELECTRIC_CURRENT_MILLIAMPERE
) )
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_CURRENT assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.CURRENT
assert state.state == "470" assert state.state == "470"
entry = registry.async_get("sensor.wled_rgb_light_estimated_current") entry = registry.async_get("sensor.wled_rgb_light_estimated_current")
@ -108,7 +103,7 @@ async def test_sensors(
state = hass.states.get("sensor.wled_rgb_light_uptime") state = hass.states.get("sensor.wled_rgb_light_uptime")
assert state assert state
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_TIMESTAMP assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TIMESTAMP
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
assert state.state == "2019-11-11T09:10:00+00:00" assert state.state == "2019-11-11T09:10:00+00:00"
@ -143,7 +138,7 @@ async def test_sensors(
state = hass.states.get("sensor.wled_rgb_light_wifi_rssi") state = hass.states.get("sensor.wled_rgb_light_wifi_rssi")
assert state assert state
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_SIGNAL_STRENGTH assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.SIGNAL_STRENGTH
assert ( assert (
state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
== SIGNAL_STRENGTH_DECIBELS_MILLIWATT == SIGNAL_STRENGTH_DECIBELS_MILLIWATT