Add state class to HKC sensors (#75662)

This commit is contained in:
J. Nick Koston 2022-07-23 11:14:06 -05:00 committed by GitHub
parent 8d6247446b
commit 759add5184
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 34 additions and 12 deletions

View File

@ -34,8 +34,6 @@ from . import KNOWN_DEVICES, CharacteristicEntity, HomeKitEntity
from .connection import HKDevice from .connection import HKDevice
from .utils import folded_name from .utils import folded_name
CO2_ICON = "mdi:molecule-co2"
@dataclass @dataclass
class HomeKitSensorEntityDescription(SensorEntityDescription): class HomeKitSensorEntityDescription(SensorEntityDescription):
@ -200,9 +198,11 @@ SIMPLE_SENSOR: dict[str, HomeKitSensorEntityDescription] = {
} }
class HomeKitSensor(HomeKitEntity): class HomeKitSensor(HomeKitEntity, SensorEntity):
"""Representation of a HomeKit sensor.""" """Representation of a HomeKit sensor."""
_attr_state_class = SensorStateClass.MEASUREMENT
@property @property
def name(self) -> str | None: def name(self) -> str | None:
"""Return the name of the device.""" """Return the name of the device."""
@ -217,7 +217,7 @@ class HomeKitSensor(HomeKitEntity):
return full_name return full_name
class HomeKitHumiditySensor(HomeKitSensor, SensorEntity): class HomeKitHumiditySensor(HomeKitSensor):
"""Representation of a Homekit humidity sensor.""" """Representation of a Homekit humidity sensor."""
_attr_device_class = SensorDeviceClass.HUMIDITY _attr_device_class = SensorDeviceClass.HUMIDITY
@ -238,7 +238,7 @@ class HomeKitHumiditySensor(HomeKitSensor, SensorEntity):
return self.service.value(CharacteristicsTypes.RELATIVE_HUMIDITY_CURRENT) return self.service.value(CharacteristicsTypes.RELATIVE_HUMIDITY_CURRENT)
class HomeKitTemperatureSensor(HomeKitSensor, SensorEntity): class HomeKitTemperatureSensor(HomeKitSensor):
"""Representation of a Homekit temperature sensor.""" """Representation of a Homekit temperature sensor."""
_attr_device_class = SensorDeviceClass.TEMPERATURE _attr_device_class = SensorDeviceClass.TEMPERATURE
@ -259,7 +259,7 @@ class HomeKitTemperatureSensor(HomeKitSensor, SensorEntity):
return self.service.value(CharacteristicsTypes.TEMPERATURE_CURRENT) return self.service.value(CharacteristicsTypes.TEMPERATURE_CURRENT)
class HomeKitLightSensor(HomeKitSensor, SensorEntity): class HomeKitLightSensor(HomeKitSensor):
"""Representation of a Homekit light level sensor.""" """Representation of a Homekit light level sensor."""
_attr_device_class = SensorDeviceClass.ILLUMINANCE _attr_device_class = SensorDeviceClass.ILLUMINANCE
@ -280,10 +280,10 @@ class HomeKitLightSensor(HomeKitSensor, SensorEntity):
return self.service.value(CharacteristicsTypes.LIGHT_LEVEL_CURRENT) return self.service.value(CharacteristicsTypes.LIGHT_LEVEL_CURRENT)
class HomeKitCarbonDioxideSensor(HomeKitEntity, SensorEntity): class HomeKitCarbonDioxideSensor(HomeKitSensor):
"""Representation of a Homekit Carbon Dioxide sensor.""" """Representation of a Homekit Carbon Dioxide sensor."""
_attr_icon = CO2_ICON _attr_device_class = SensorDeviceClass.CO2
_attr_native_unit_of_measurement = CONCENTRATION_PARTS_PER_MILLION _attr_native_unit_of_measurement = CONCENTRATION_PARTS_PER_MILLION
def get_characteristic_types(self) -> list[str]: def get_characteristic_types(self) -> list[str]:
@ -293,7 +293,7 @@ class HomeKitCarbonDioxideSensor(HomeKitEntity, SensorEntity):
@property @property
def default_name(self) -> str: def default_name(self) -> str:
"""Return the default name of the device.""" """Return the default name of the device."""
return "CO2" return "Carbon Dioxide"
@property @property
def native_value(self) -> int: def native_value(self) -> int:
@ -301,7 +301,7 @@ class HomeKitCarbonDioxideSensor(HomeKitEntity, SensorEntity):
return self.service.value(CharacteristicsTypes.CARBON_DIOXIDE_LEVEL) return self.service.value(CharacteristicsTypes.CARBON_DIOXIDE_LEVEL)
class HomeKitBatterySensor(HomeKitSensor, SensorEntity): class HomeKitBatterySensor(HomeKitSensor):
"""Representation of a Homekit battery sensor.""" """Representation of a Homekit battery sensor."""
_attr_device_class = SensorDeviceClass.BATTERY _attr_device_class = SensorDeviceClass.BATTERY

View File

@ -7,6 +7,7 @@ service-label-index despite not being linked to a service-label.
https://github.com/home-assistant/core/pull/39090 https://github.com/home-assistant/core/pull/39090
""" """
from homeassistant.components.sensor import SensorStateClass
from homeassistant.const import PERCENTAGE from homeassistant.const import PERCENTAGE
from tests.components.homekit_controller.common import ( from tests.components.homekit_controller.common import (
@ -41,6 +42,7 @@ async def test_aqara_switch_setup(hass):
entity_id="sensor.programmable_switch_battery_sensor", entity_id="sensor.programmable_switch_battery_sensor",
friendly_name="Programmable Switch Battery Sensor", friendly_name="Programmable Switch Battery Sensor",
unique_id="homekit-111a1111a1a111-5", unique_id="homekit-111a1111a1a111-5",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unit_of_measurement=PERCENTAGE, unit_of_measurement=PERCENTAGE,
state="100", state="100",
), ),

View File

@ -46,6 +46,7 @@ async def test_arlo_baby_setup(hass):
entity_id="sensor.arlobabya0_battery", entity_id="sensor.arlobabya0_battery",
unique_id="homekit-00A0000000000-700", unique_id="homekit-00A0000000000-700",
friendly_name="ArloBabyA0 Battery", friendly_name="ArloBabyA0 Battery",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unit_of_measurement=PERCENTAGE, unit_of_measurement=PERCENTAGE,
state="82", state="82",
), ),
@ -53,6 +54,7 @@ async def test_arlo_baby_setup(hass):
entity_id="sensor.arlobabya0_humidity", entity_id="sensor.arlobabya0_humidity",
unique_id="homekit-00A0000000000-900", unique_id="homekit-00A0000000000-900",
friendly_name="ArloBabyA0 Humidity", friendly_name="ArloBabyA0 Humidity",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unit_of_measurement=PERCENTAGE, unit_of_measurement=PERCENTAGE,
state="60.099998", state="60.099998",
), ),
@ -60,6 +62,7 @@ async def test_arlo_baby_setup(hass):
entity_id="sensor.arlobabya0_temperature", entity_id="sensor.arlobabya0_temperature",
unique_id="homekit-00A0000000000-1000", unique_id="homekit-00A0000000000-1000",
friendly_name="ArloBabyA0 Temperature", friendly_name="ArloBabyA0 Temperature",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unit_of_measurement=TEMP_CELSIUS, unit_of_measurement=TEMP_CELSIUS,
state="24.0", state="24.0",
), ),

View File

@ -36,6 +36,7 @@ async def test_eve_degree_setup(hass):
entity_id="sensor.eve_degree_aa11_temperature", entity_id="sensor.eve_degree_aa11_temperature",
unique_id="homekit-AA00A0A00000-22", unique_id="homekit-AA00A0A00000-22",
friendly_name="Eve Degree AA11 Temperature", friendly_name="Eve Degree AA11 Temperature",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unit_of_measurement=TEMP_CELSIUS, unit_of_measurement=TEMP_CELSIUS,
state="22.7719116210938", state="22.7719116210938",
), ),
@ -43,6 +44,7 @@ async def test_eve_degree_setup(hass):
entity_id="sensor.eve_degree_aa11_humidity", entity_id="sensor.eve_degree_aa11_humidity",
unique_id="homekit-AA00A0A00000-27", unique_id="homekit-AA00A0A00000-27",
friendly_name="Eve Degree AA11 Humidity", friendly_name="Eve Degree AA11 Humidity",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unit_of_measurement=PERCENTAGE, unit_of_measurement=PERCENTAGE,
state="59.4818115234375", state="59.4818115234375",
), ),
@ -58,6 +60,7 @@ async def test_eve_degree_setup(hass):
entity_id="sensor.eve_degree_aa11_battery", entity_id="sensor.eve_degree_aa11_battery",
unique_id="homekit-AA00A0A00000-17", unique_id="homekit-AA00A0A00000-17",
friendly_name="Eve Degree AA11 Battery", friendly_name="Eve Degree AA11 Battery",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unit_of_measurement=PERCENTAGE, unit_of_measurement=PERCENTAGE,
state="65", state="65",
), ),

View File

@ -1,5 +1,6 @@
"""Tests for handling accessories on a Hue bridge via HomeKit.""" """Tests for handling accessories on a Hue bridge via HomeKit."""
from homeassistant.components.sensor import SensorStateClass
from homeassistant.const import PERCENTAGE from homeassistant.const import PERCENTAGE
from tests.components.homekit_controller.common import ( from tests.components.homekit_controller.common import (
@ -41,6 +42,7 @@ async def test_hue_bridge_setup(hass):
entities=[ entities=[
EntityTestInfo( EntityTestInfo(
entity_id="sensor.hue_dimmer_switch_battery", entity_id="sensor.hue_dimmer_switch_battery",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
friendly_name="Hue dimmer switch battery", friendly_name="Hue dimmer switch battery",
unique_id="homekit-6623462389072572-644245094400", unique_id="homekit-6623462389072572-644245094400",
unit_of_measurement=PERCENTAGE, unit_of_measurement=PERCENTAGE,

View File

@ -5,6 +5,7 @@ from homeassistant.components.cover import (
SUPPORT_OPEN, SUPPORT_OPEN,
SUPPORT_SET_POSITION, SUPPORT_SET_POSITION,
) )
from homeassistant.components.sensor import SensorStateClass
from homeassistant.const import PERCENTAGE from homeassistant.const import PERCENTAGE
from tests.components.homekit_controller.common import ( from tests.components.homekit_controller.common import (
@ -54,6 +55,7 @@ async def test_ryse_smart_bridge_setup(hass):
EntityTestInfo( EntityTestInfo(
entity_id="sensor.master_bath_south_ryse_shade_battery", entity_id="sensor.master_bath_south_ryse_shade_battery",
friendly_name="Master Bath South RYSE Shade Battery", friendly_name="Master Bath South RYSE Shade Battery",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unique_id="homekit-00:00:00:00:00:00-2-64", unique_id="homekit-00:00:00:00:00:00-2-64",
unit_of_measurement=PERCENTAGE, unit_of_measurement=PERCENTAGE,
state="100", state="100",
@ -80,6 +82,7 @@ async def test_ryse_smart_bridge_setup(hass):
EntityTestInfo( EntityTestInfo(
entity_id="sensor.ryse_smartshade_ryse_shade_battery", entity_id="sensor.ryse_smartshade_ryse_shade_battery",
friendly_name="RYSE SmartShade RYSE Shade Battery", friendly_name="RYSE SmartShade RYSE Shade Battery",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unique_id="homekit-00:00:00:00:00:00-3-64", unique_id="homekit-00:00:00:00:00:00-3-64",
unit_of_measurement=PERCENTAGE, unit_of_measurement=PERCENTAGE,
state="100", state="100",
@ -129,6 +132,7 @@ async def test_ryse_smart_bridge_four_shades_setup(hass):
EntityTestInfo( EntityTestInfo(
entity_id="sensor.lr_left_ryse_shade_battery", entity_id="sensor.lr_left_ryse_shade_battery",
friendly_name="LR Left RYSE Shade Battery", friendly_name="LR Left RYSE Shade Battery",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unique_id="homekit-00:00:00:00:00:00-2-64", unique_id="homekit-00:00:00:00:00:00-2-64",
unit_of_measurement=PERCENTAGE, unit_of_measurement=PERCENTAGE,
state="89", state="89",
@ -155,6 +159,7 @@ async def test_ryse_smart_bridge_four_shades_setup(hass):
EntityTestInfo( EntityTestInfo(
entity_id="sensor.lr_right_ryse_shade_battery", entity_id="sensor.lr_right_ryse_shade_battery",
friendly_name="LR Right RYSE Shade Battery", friendly_name="LR Right RYSE Shade Battery",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unique_id="homekit-00:00:00:00:00:00-3-64", unique_id="homekit-00:00:00:00:00:00-3-64",
unit_of_measurement=PERCENTAGE, unit_of_measurement=PERCENTAGE,
state="100", state="100",
@ -181,6 +186,7 @@ async def test_ryse_smart_bridge_four_shades_setup(hass):
EntityTestInfo( EntityTestInfo(
entity_id="sensor.br_left_ryse_shade_battery", entity_id="sensor.br_left_ryse_shade_battery",
friendly_name="BR Left RYSE Shade Battery", friendly_name="BR Left RYSE Shade Battery",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unique_id="homekit-00:00:00:00:00:00-4-64", unique_id="homekit-00:00:00:00:00:00-4-64",
unit_of_measurement=PERCENTAGE, unit_of_measurement=PERCENTAGE,
state="100", state="100",
@ -206,6 +212,7 @@ async def test_ryse_smart_bridge_four_shades_setup(hass):
), ),
EntityTestInfo( EntityTestInfo(
entity_id="sensor.rzss_ryse_shade_battery", entity_id="sensor.rzss_ryse_shade_battery",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
friendly_name="RZSS RYSE Shade Battery", friendly_name="RZSS RYSE Shade Battery",
unique_id="homekit-00:00:00:00:00:00-5-64", unique_id="homekit-00:00:00:00:00:00-5-64",
unit_of_measurement=PERCENTAGE, unit_of_measurement=PERCENTAGE,

View File

@ -9,6 +9,7 @@ from homeassistant.components.cover import (
SUPPORT_OPEN, SUPPORT_OPEN,
SUPPORT_SET_POSITION, SUPPORT_SET_POSITION,
) )
from homeassistant.components.sensor import SensorStateClass
from homeassistant.const import ( from homeassistant.const import (
CONCENTRATION_PARTS_PER_MILLION, CONCENTRATION_PARTS_PER_MILLION,
PERCENTAGE, PERCENTAGE,
@ -75,6 +76,7 @@ async def test_velux_cover_setup(hass):
EntityTestInfo( EntityTestInfo(
entity_id="sensor.velux_sensor_temperature_sensor", entity_id="sensor.velux_sensor_temperature_sensor",
friendly_name="VELUX Sensor Temperature sensor", friendly_name="VELUX Sensor Temperature sensor",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unique_id="homekit-a11b111-8", unique_id="homekit-a11b111-8",
unit_of_measurement=TEMP_CELSIUS, unit_of_measurement=TEMP_CELSIUS,
state="18.9", state="18.9",
@ -82,6 +84,7 @@ async def test_velux_cover_setup(hass):
EntityTestInfo( EntityTestInfo(
entity_id="sensor.velux_sensor_humidity_sensor", entity_id="sensor.velux_sensor_humidity_sensor",
friendly_name="VELUX Sensor Humidity sensor", friendly_name="VELUX Sensor Humidity sensor",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unique_id="homekit-a11b111-11", unique_id="homekit-a11b111-11",
unit_of_measurement=PERCENTAGE, unit_of_measurement=PERCENTAGE,
state="58", state="58",
@ -89,6 +92,7 @@ async def test_velux_cover_setup(hass):
EntityTestInfo( EntityTestInfo(
entity_id="sensor.velux_sensor_carbon_dioxide_sensor", entity_id="sensor.velux_sensor_carbon_dioxide_sensor",
friendly_name="VELUX Sensor Carbon Dioxide sensor", friendly_name="VELUX Sensor Carbon Dioxide sensor",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unique_id="homekit-a11b111-14", unique_id="homekit-a11b111-14",
unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION, unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
state="400", state="400",

View File

@ -3,7 +3,7 @@ from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes from aiohomekit.model.services import ServicesTypes
from aiohomekit.protocol.statuscodes import HapStatusCode from aiohomekit.protocol.statuscodes import HapStatusCode
from homeassistant.components.sensor import SensorDeviceClass from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from tests.components.homekit_controller.common import Helper, setup_test_component from tests.components.homekit_controller.common import Helper, setup_test_component
@ -79,6 +79,7 @@ async def test_temperature_sensor_read_state(hass, utcnow):
assert state.state == "20" assert state.state == "20"
assert state.attributes["device_class"] == SensorDeviceClass.TEMPERATURE assert state.attributes["device_class"] == SensorDeviceClass.TEMPERATURE
assert state.attributes["state_class"] == SensorStateClass.MEASUREMENT
async def test_temperature_sensor_not_added_twice(hass, utcnow): async def test_temperature_sensor_not_added_twice(hass, utcnow):
@ -146,7 +147,7 @@ async def test_light_level_sensor_read_state(hass, utcnow):
async def test_carbon_dioxide_level_sensor_read_state(hass, utcnow): async def test_carbon_dioxide_level_sensor_read_state(hass, utcnow):
"""Test reading the state of a HomeKit carbon dioxide sensor accessory.""" """Test reading the state of a HomeKit carbon dioxide sensor accessory."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_carbon_dioxide_level_sensor_service, suffix="co2" hass, create_carbon_dioxide_level_sensor_service, suffix="carbon_dioxide"
) )
state = await helper.async_update( state = await helper.async_update(