Skip to content

Commit

Permalink
Add missing device class attributes to homekit_controller sensors (#3…
Browse files Browse the repository at this point in the history
…2175)

* Add some missing device class attributes to homekit_controller sensors

* Add classes for binary sensors
  • Loading branch information
Jc2k authored Feb 25, 2020
1 parent 24652d8 commit dd13e99
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
9 changes: 8 additions & 1 deletion homeassistant/components/homekit_controller/binary_sensor.py
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

0 comments on commit dd13e99

Please sign in to comment.