Add Carbon Monoxide binary sensor to Homekit Controller (#39889)

pull/40177/head
RogerSelwyn 2020-09-10 12:25:14 +01:00 committed by GitHub
parent 14f7f5ba45
commit 162c39258e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import logging
from aiohomekit.model.characteristics import CharacteristicsTypes
from homeassistant.components.binary_sensor import (
DEVICE_CLASS_GAS,
DEVICE_CLASS_MOISTURE,
DEVICE_CLASS_MOTION,
DEVICE_CLASS_OCCUPANCY,
@ -72,6 +73,24 @@ class HomeKitSmokeSensor(HomeKitEntity, BinarySensorEntity):
return self.service.value(CharacteristicsTypes.SMOKE_DETECTED) == 1
class HomeKitCarbonMonoxideSensor(HomeKitEntity, BinarySensorEntity):
"""Representation of a Homekit BO sensor."""
@property
def device_class(self) -> str:
"""Return the class of this sensor."""
return DEVICE_CLASS_GAS
def get_characteristic_types(self):
"""Define the homekit characteristics the entity is tracking."""
return [CharacteristicsTypes.CARBON_MONOXIDE_DETECTED]
@property
def is_on(self):
"""Return true if CO is currently detected."""
return self.service.value(CharacteristicsTypes.CARBON_MONOXIDE_DETECTED) == 1
class HomeKitOccupancySensor(HomeKitEntity, BinarySensorEntity):
"""Representation of a Homekit occupancy sensor."""
@ -112,6 +131,7 @@ ENTITY_TYPES = {
"motion": HomeKitMotionSensor,
"contact": HomeKitContactSensor,
"smoke": HomeKitSmokeSensor,
"carbon-monoxide": HomeKitCarbonMonoxideSensor,
"occupancy": HomeKitOccupancySensor,
"leak": HomeKitLeakSensor,
}

View File

@ -28,6 +28,7 @@ HOMEKIT_ACCESSORY_DISPATCH = {
"temperature": "sensor",
"battery": "sensor",
"smoke": "binary_sensor",
"carbon-monoxide": "binary_sensor",
"leak": "binary_sensor",
"fan": "fan",
"fanv2": "fan",

View File

@ -3,6 +3,7 @@ from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes
from homeassistant.components.binary_sensor import (
DEVICE_CLASS_GAS,
DEVICE_CLASS_MOISTURE,
DEVICE_CLASS_MOTION,
DEVICE_CLASS_OCCUPANCY,
@ -15,6 +16,7 @@ from tests.components.homekit_controller.common import setup_test_component
MOTION_DETECTED = ("motion", "motion-detected")
CONTACT_STATE = ("contact", "contact-state")
SMOKE_DETECTED = ("smoke", "smoke-detected")
CARBON_MONOXIDE_DETECTED = ("carbon-monoxide", "carbon-monoxide.detected")
OCCUPANCY_DETECTED = ("occupancy", "occupancy-detected")
LEAK_DETECTED = ("leak", "leak-detected")
@ -88,6 +90,29 @@ async def test_smoke_sensor_read_state(hass, utcnow):
assert state.attributes["device_class"] == DEVICE_CLASS_SMOKE
def create_carbon_monoxide_sensor_service(accessory):
"""Define carbon monoxide sensor characteristics."""
service = accessory.add_service(ServicesTypes.CARBON_MONOXIDE_SENSOR)
cur_state = service.add_char(CharacteristicsTypes.CARBON_MONOXIDE_DETECTED)
cur_state.value = 0
async def test_carbon_monoxide_sensor_read_state(hass, utcnow):
"""Test that we can read the state of a HomeKit contact accessory."""
helper = await setup_test_component(hass, create_carbon_monoxide_sensor_service)
helper.characteristics[CARBON_MONOXIDE_DETECTED].value = 0
state = await helper.poll_and_get_state()
assert state.state == "off"
helper.characteristics[CARBON_MONOXIDE_DETECTED].value = 1
state = await helper.poll_and_get_state()
assert state.state == "on"
assert state.attributes["device_class"] == DEVICE_CLASS_GAS
def create_occupancy_sensor_service(accessory):
"""Define occupancy characteristics."""
service = accessory.add_service(ServicesTypes.OCCUPANCY_SENSOR)