mirror of
https://github.com/home-assistant/core.git
synced 2025-07-29 16:17:20 +00:00
Add state class to HKC sensors (#75662)
This commit is contained in:
parent
8d6247446b
commit
759add5184
@ -34,8 +34,6 @@ from . import KNOWN_DEVICES, CharacteristicEntity, HomeKitEntity
|
||||
from .connection import HKDevice
|
||||
from .utils import folded_name
|
||||
|
||||
CO2_ICON = "mdi:molecule-co2"
|
||||
|
||||
|
||||
@dataclass
|
||||
class HomeKitSensorEntityDescription(SensorEntityDescription):
|
||||
@ -200,9 +198,11 @@ SIMPLE_SENSOR: dict[str, HomeKitSensorEntityDescription] = {
|
||||
}
|
||||
|
||||
|
||||
class HomeKitSensor(HomeKitEntity):
|
||||
class HomeKitSensor(HomeKitEntity, SensorEntity):
|
||||
"""Representation of a HomeKit sensor."""
|
||||
|
||||
_attr_state_class = SensorStateClass.MEASUREMENT
|
||||
|
||||
@property
|
||||
def name(self) -> str | None:
|
||||
"""Return the name of the device."""
|
||||
@ -217,7 +217,7 @@ class HomeKitSensor(HomeKitEntity):
|
||||
return full_name
|
||||
|
||||
|
||||
class HomeKitHumiditySensor(HomeKitSensor, SensorEntity):
|
||||
class HomeKitHumiditySensor(HomeKitSensor):
|
||||
"""Representation of a Homekit humidity sensor."""
|
||||
|
||||
_attr_device_class = SensorDeviceClass.HUMIDITY
|
||||
@ -238,7 +238,7 @@ class HomeKitHumiditySensor(HomeKitSensor, SensorEntity):
|
||||
return self.service.value(CharacteristicsTypes.RELATIVE_HUMIDITY_CURRENT)
|
||||
|
||||
|
||||
class HomeKitTemperatureSensor(HomeKitSensor, SensorEntity):
|
||||
class HomeKitTemperatureSensor(HomeKitSensor):
|
||||
"""Representation of a Homekit temperature sensor."""
|
||||
|
||||
_attr_device_class = SensorDeviceClass.TEMPERATURE
|
||||
@ -259,7 +259,7 @@ class HomeKitTemperatureSensor(HomeKitSensor, SensorEntity):
|
||||
return self.service.value(CharacteristicsTypes.TEMPERATURE_CURRENT)
|
||||
|
||||
|
||||
class HomeKitLightSensor(HomeKitSensor, SensorEntity):
|
||||
class HomeKitLightSensor(HomeKitSensor):
|
||||
"""Representation of a Homekit light level sensor."""
|
||||
|
||||
_attr_device_class = SensorDeviceClass.ILLUMINANCE
|
||||
@ -280,10 +280,10 @@ class HomeKitLightSensor(HomeKitSensor, SensorEntity):
|
||||
return self.service.value(CharacteristicsTypes.LIGHT_LEVEL_CURRENT)
|
||||
|
||||
|
||||
class HomeKitCarbonDioxideSensor(HomeKitEntity, SensorEntity):
|
||||
class HomeKitCarbonDioxideSensor(HomeKitSensor):
|
||||
"""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
|
||||
|
||||
def get_characteristic_types(self) -> list[str]:
|
||||
@ -293,7 +293,7 @@ class HomeKitCarbonDioxideSensor(HomeKitEntity, SensorEntity):
|
||||
@property
|
||||
def default_name(self) -> str:
|
||||
"""Return the default name of the device."""
|
||||
return "CO2"
|
||||
return "Carbon Dioxide"
|
||||
|
||||
@property
|
||||
def native_value(self) -> int:
|
||||
@ -301,7 +301,7 @@ class HomeKitCarbonDioxideSensor(HomeKitEntity, SensorEntity):
|
||||
return self.service.value(CharacteristicsTypes.CARBON_DIOXIDE_LEVEL)
|
||||
|
||||
|
||||
class HomeKitBatterySensor(HomeKitSensor, SensorEntity):
|
||||
class HomeKitBatterySensor(HomeKitSensor):
|
||||
"""Representation of a Homekit battery sensor."""
|
||||
|
||||
_attr_device_class = SensorDeviceClass.BATTERY
|
||||
|
@ -7,6 +7,7 @@ service-label-index despite not being linked to a service-label.
|
||||
https://github.com/home-assistant/core/pull/39090
|
||||
"""
|
||||
|
||||
from homeassistant.components.sensor import SensorStateClass
|
||||
from homeassistant.const import PERCENTAGE
|
||||
|
||||
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",
|
||||
friendly_name="Programmable Switch Battery Sensor",
|
||||
unique_id="homekit-111a1111a1a111-5",
|
||||
capabilities={"state_class": SensorStateClass.MEASUREMENT},
|
||||
unit_of_measurement=PERCENTAGE,
|
||||
state="100",
|
||||
),
|
||||
|
@ -46,6 +46,7 @@ async def test_arlo_baby_setup(hass):
|
||||
entity_id="sensor.arlobabya0_battery",
|
||||
unique_id="homekit-00A0000000000-700",
|
||||
friendly_name="ArloBabyA0 Battery",
|
||||
capabilities={"state_class": SensorStateClass.MEASUREMENT},
|
||||
unit_of_measurement=PERCENTAGE,
|
||||
state="82",
|
||||
),
|
||||
@ -53,6 +54,7 @@ async def test_arlo_baby_setup(hass):
|
||||
entity_id="sensor.arlobabya0_humidity",
|
||||
unique_id="homekit-00A0000000000-900",
|
||||
friendly_name="ArloBabyA0 Humidity",
|
||||
capabilities={"state_class": SensorStateClass.MEASUREMENT},
|
||||
unit_of_measurement=PERCENTAGE,
|
||||
state="60.099998",
|
||||
),
|
||||
@ -60,6 +62,7 @@ async def test_arlo_baby_setup(hass):
|
||||
entity_id="sensor.arlobabya0_temperature",
|
||||
unique_id="homekit-00A0000000000-1000",
|
||||
friendly_name="ArloBabyA0 Temperature",
|
||||
capabilities={"state_class": SensorStateClass.MEASUREMENT},
|
||||
unit_of_measurement=TEMP_CELSIUS,
|
||||
state="24.0",
|
||||
),
|
||||
|
@ -36,6 +36,7 @@ async def test_eve_degree_setup(hass):
|
||||
entity_id="sensor.eve_degree_aa11_temperature",
|
||||
unique_id="homekit-AA00A0A00000-22",
|
||||
friendly_name="Eve Degree AA11 Temperature",
|
||||
capabilities={"state_class": SensorStateClass.MEASUREMENT},
|
||||
unit_of_measurement=TEMP_CELSIUS,
|
||||
state="22.7719116210938",
|
||||
),
|
||||
@ -43,6 +44,7 @@ async def test_eve_degree_setup(hass):
|
||||
entity_id="sensor.eve_degree_aa11_humidity",
|
||||
unique_id="homekit-AA00A0A00000-27",
|
||||
friendly_name="Eve Degree AA11 Humidity",
|
||||
capabilities={"state_class": SensorStateClass.MEASUREMENT},
|
||||
unit_of_measurement=PERCENTAGE,
|
||||
state="59.4818115234375",
|
||||
),
|
||||
@ -58,6 +60,7 @@ async def test_eve_degree_setup(hass):
|
||||
entity_id="sensor.eve_degree_aa11_battery",
|
||||
unique_id="homekit-AA00A0A00000-17",
|
||||
friendly_name="Eve Degree AA11 Battery",
|
||||
capabilities={"state_class": SensorStateClass.MEASUREMENT},
|
||||
unit_of_measurement=PERCENTAGE,
|
||||
state="65",
|
||||
),
|
||||
|
@ -1,5 +1,6 @@
|
||||
"""Tests for handling accessories on a Hue bridge via HomeKit."""
|
||||
|
||||
from homeassistant.components.sensor import SensorStateClass
|
||||
from homeassistant.const import PERCENTAGE
|
||||
|
||||
from tests.components.homekit_controller.common import (
|
||||
@ -41,6 +42,7 @@ async def test_hue_bridge_setup(hass):
|
||||
entities=[
|
||||
EntityTestInfo(
|
||||
entity_id="sensor.hue_dimmer_switch_battery",
|
||||
capabilities={"state_class": SensorStateClass.MEASUREMENT},
|
||||
friendly_name="Hue dimmer switch battery",
|
||||
unique_id="homekit-6623462389072572-644245094400",
|
||||
unit_of_measurement=PERCENTAGE,
|
||||
|
@ -5,6 +5,7 @@ from homeassistant.components.cover import (
|
||||
SUPPORT_OPEN,
|
||||
SUPPORT_SET_POSITION,
|
||||
)
|
||||
from homeassistant.components.sensor import SensorStateClass
|
||||
from homeassistant.const import PERCENTAGE
|
||||
|
||||
from tests.components.homekit_controller.common import (
|
||||
@ -54,6 +55,7 @@ async def test_ryse_smart_bridge_setup(hass):
|
||||
EntityTestInfo(
|
||||
entity_id="sensor.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",
|
||||
unit_of_measurement=PERCENTAGE,
|
||||
state="100",
|
||||
@ -80,6 +82,7 @@ async def test_ryse_smart_bridge_setup(hass):
|
||||
EntityTestInfo(
|
||||
entity_id="sensor.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",
|
||||
unit_of_measurement=PERCENTAGE,
|
||||
state="100",
|
||||
@ -129,6 +132,7 @@ async def test_ryse_smart_bridge_four_shades_setup(hass):
|
||||
EntityTestInfo(
|
||||
entity_id="sensor.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",
|
||||
unit_of_measurement=PERCENTAGE,
|
||||
state="89",
|
||||
@ -155,6 +159,7 @@ async def test_ryse_smart_bridge_four_shades_setup(hass):
|
||||
EntityTestInfo(
|
||||
entity_id="sensor.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",
|
||||
unit_of_measurement=PERCENTAGE,
|
||||
state="100",
|
||||
@ -181,6 +186,7 @@ async def test_ryse_smart_bridge_four_shades_setup(hass):
|
||||
EntityTestInfo(
|
||||
entity_id="sensor.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",
|
||||
unit_of_measurement=PERCENTAGE,
|
||||
state="100",
|
||||
@ -206,6 +212,7 @@ async def test_ryse_smart_bridge_four_shades_setup(hass):
|
||||
),
|
||||
EntityTestInfo(
|
||||
entity_id="sensor.rzss_ryse_shade_battery",
|
||||
capabilities={"state_class": SensorStateClass.MEASUREMENT},
|
||||
friendly_name="RZSS RYSE Shade Battery",
|
||||
unique_id="homekit-00:00:00:00:00:00-5-64",
|
||||
unit_of_measurement=PERCENTAGE,
|
||||
|
@ -9,6 +9,7 @@ from homeassistant.components.cover import (
|
||||
SUPPORT_OPEN,
|
||||
SUPPORT_SET_POSITION,
|
||||
)
|
||||
from homeassistant.components.sensor import SensorStateClass
|
||||
from homeassistant.const import (
|
||||
CONCENTRATION_PARTS_PER_MILLION,
|
||||
PERCENTAGE,
|
||||
@ -75,6 +76,7 @@ async def test_velux_cover_setup(hass):
|
||||
EntityTestInfo(
|
||||
entity_id="sensor.velux_sensor_temperature_sensor",
|
||||
friendly_name="VELUX Sensor Temperature sensor",
|
||||
capabilities={"state_class": SensorStateClass.MEASUREMENT},
|
||||
unique_id="homekit-a11b111-8",
|
||||
unit_of_measurement=TEMP_CELSIUS,
|
||||
state="18.9",
|
||||
@ -82,6 +84,7 @@ async def test_velux_cover_setup(hass):
|
||||
EntityTestInfo(
|
||||
entity_id="sensor.velux_sensor_humidity_sensor",
|
||||
friendly_name="VELUX Sensor Humidity sensor",
|
||||
capabilities={"state_class": SensorStateClass.MEASUREMENT},
|
||||
unique_id="homekit-a11b111-11",
|
||||
unit_of_measurement=PERCENTAGE,
|
||||
state="58",
|
||||
@ -89,6 +92,7 @@ async def test_velux_cover_setup(hass):
|
||||
EntityTestInfo(
|
||||
entity_id="sensor.velux_sensor_carbon_dioxide_sensor",
|
||||
friendly_name="VELUX Sensor Carbon Dioxide sensor",
|
||||
capabilities={"state_class": SensorStateClass.MEASUREMENT},
|
||||
unique_id="homekit-a11b111-14",
|
||||
unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
|
||||
state="400",
|
||||
|
@ -3,7 +3,7 @@ from aiohomekit.model.characteristics import CharacteristicsTypes
|
||||
from aiohomekit.model.services import ServicesTypes
|
||||
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
|
||||
|
||||
@ -79,6 +79,7 @@ async def test_temperature_sensor_read_state(hass, utcnow):
|
||||
assert state.state == "20"
|
||||
|
||||
assert state.attributes["device_class"] == SensorDeviceClass.TEMPERATURE
|
||||
assert state.attributes["state_class"] == SensorStateClass.MEASUREMENT
|
||||
|
||||
|
||||
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):
|
||||
"""Test reading the state of a HomeKit carbon dioxide sensor accessory."""
|
||||
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(
|
||||
|
Loading…
x
Reference in New Issue
Block a user