diff --git a/homeassistant/components/homekit_controller/binary_sensor.py b/homeassistant/components/homekit_controller/binary_sensor.py index 939c6055e10..29fddf99189 100644 --- a/homeassistant/components/homekit_controller/binary_sensor.py +++ b/homeassistant/components/homekit_controller/binary_sensor.py @@ -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, } diff --git a/homeassistant/components/homekit_controller/const.py b/homeassistant/components/homekit_controller/const.py index 394750c0688..2a8a106a296 100644 --- a/homeassistant/components/homekit_controller/const.py +++ b/homeassistant/components/homekit_controller/const.py @@ -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", diff --git a/tests/components/homekit_controller/test_binary_sensor.py b/tests/components/homekit_controller/test_binary_sensor.py index 460d14d0d48..e9ba4420176 100644 --- a/tests/components/homekit_controller/test_binary_sensor.py +++ b/tests/components/homekit_controller/test_binary_sensor.py @@ -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)