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 missing device class attributes to homekit_controller sensors #32175

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from aiohomekit.model.characteristics import CharacteristicsTypes

from homeassistant.components.binary_sensor import (
DEVICE_CLASS_MOTION,
DEVICE_CLASS_OPENING,
DEVICE_CLASS_SMOKE,
BinarySensorDevice,
)
Expand Down Expand Up @@ -32,7 +34,7 @@ def _update_motion_detected(self, value):
@property
def device_class(self):
"""Define this binary_sensor as a motion sensor."""
return "motion"
return DEVICE_CLASS_MOTION

@property
def is_on(self):
Expand All @@ -55,6 +57,11 @@ def get_characteristic_types(self):
def _update_contact_state(self, value):
self._state = value

@property
def device_class(self):
"""Define this binary_sensor as a opening sensor."""
return DEVICE_CLASS_OPENING

@property
def is_on(self):
"""Return true if the binary sensor is on/open."""
Expand Down
18 changes: 18 additions & 0 deletions homeassistant/components/homekit_controller/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from homeassistant.const import (
CONCENTRATION_PARTS_PER_MILLION,
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_ILLUMINANCE,
DEVICE_CLASS_TEMPERATURE,
TEMP_CELSIUS,
)
from homeassistant.core import callback
Expand Down Expand Up @@ -31,6 +34,11 @@ def get_characteristic_types(self):
"""Define the homekit characteristics the entity is tracking."""
return [CharacteristicsTypes.RELATIVE_HUMIDITY_CURRENT]

@property
def device_class(self) -> str:
"""Return the device class of the sensor."""
return DEVICE_CLASS_HUMIDITY

@property
def name(self):
"""Return the name of the device."""
Expand Down Expand Up @@ -67,6 +75,11 @@ def get_characteristic_types(self):
"""Define the homekit characteristics the entity is tracking."""
return [CharacteristicsTypes.TEMPERATURE_CURRENT]

@property
def device_class(self) -> str:
"""Return the device class of the sensor."""
return DEVICE_CLASS_TEMPERATURE

@property
def name(self):
"""Return the name of the device."""
Expand Down Expand Up @@ -103,6 +116,11 @@ def get_characteristic_types(self):
"""Define the homekit characteristics the entity is tracking."""
return [CharacteristicsTypes.LIGHT_LEVEL_CURRENT]

@property
def device_class(self) -> str:
"""Return the device class of the sensor."""
return DEVICE_CLASS_ILLUMINANCE

@property
def name(self):
"""Return the name of the device."""
Expand Down
12 changes: 11 additions & 1 deletion tests/components/homekit_controller/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes

from homeassistant.components.binary_sensor import (
DEVICE_CLASS_MOTION,
DEVICE_CLASS_OPENING,
DEVICE_CLASS_SMOKE,
)

from tests.components.homekit_controller.common import setup_test_component

MOTION_DETECTED = ("motion", "motion-detected")
Expand Down Expand Up @@ -29,6 +35,8 @@ async def test_motion_sensor_read_state(hass, utcnow):
state = await helper.poll_and_get_state()
assert state.state == "on"

assert state.attributes["device_class"] == DEVICE_CLASS_MOTION


def create_contact_sensor_service(accessory):
"""Define contact characteristics."""
Expand All @@ -50,6 +58,8 @@ async def test_contact_sensor_read_state(hass, utcnow):
state = await helper.poll_and_get_state()
assert state.state == "on"

assert state.attributes["device_class"] == DEVICE_CLASS_OPENING


def create_smoke_sensor_service(accessory):
"""Define smoke sensor characteristics."""
Expand All @@ -71,4 +81,4 @@ async def test_smoke_sensor_read_state(hass, utcnow):
state = await helper.poll_and_get_state()
assert state.state == "on"

assert state.attributes["device_class"] == "smoke"
assert state.attributes["device_class"] == DEVICE_CLASS_SMOKE
15 changes: 15 additions & 0 deletions tests/components/homekit_controller/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes

from homeassistant.const import (
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_ILLUMINANCE,
DEVICE_CLASS_TEMPERATURE,
)

from tests.components.homekit_controller.common import setup_test_component

TEMPERATURE = ("temperature", "temperature.current")
Expand Down Expand Up @@ -75,6 +82,8 @@ async def test_temperature_sensor_read_state(hass, utcnow):
state = await helper.poll_and_get_state()
assert state.state == "20"

assert state.attributes["device_class"] == DEVICE_CLASS_TEMPERATURE


async def test_humidity_sensor_read_state(hass, utcnow):
"""Test reading the state of a HomeKit humidity sensor accessory."""
Expand All @@ -90,6 +99,8 @@ async def test_humidity_sensor_read_state(hass, utcnow):
state = await helper.poll_and_get_state()
assert state.state == "20"

assert state.attributes["device_class"] == DEVICE_CLASS_HUMIDITY


async def test_light_level_sensor_read_state(hass, utcnow):
"""Test reading the state of a HomeKit temperature sensor accessory."""
Expand All @@ -105,6 +116,8 @@ async def test_light_level_sensor_read_state(hass, utcnow):
state = await helper.poll_and_get_state()
assert state.state == "20"

assert state.attributes["device_class"] == DEVICE_CLASS_ILLUMINANCE


async def test_carbon_dioxide_level_sensor_read_state(hass, utcnow):
"""Test reading the state of a HomeKit carbon dioxide sensor accessory."""
Expand Down Expand Up @@ -137,6 +150,8 @@ async def test_battery_level_sensor(hass, utcnow):
assert state.state == "20"
assert state.attributes["icon"] == "mdi:battery-20"

assert state.attributes["device_class"] == DEVICE_CLASS_BATTERY


async def test_battery_charging(hass, utcnow):
"""Test reading the state of a HomeKit battery's charging state."""
Expand Down