Skip to content

Commit

Permalink
[homekit] add support for air quality sensor (openhab#8125)
Browse files Browse the repository at this point in the history
* add air quality sensor
* add example to README

Signed-off-by: Eugen Freiter <[email protected]>
Signed-off-by: CSchlipp <[email protected]>
  • Loading branch information
yfre authored and CSchlipp committed Jul 26, 2020
1 parent 6b01750 commit 5e24f7e
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 6 deletions.
34 changes: 28 additions & 6 deletions bundles/org.openhab.io.homekit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ A full list of supported accessory types can be found in the table *below*.

| Accessory Tag | Mandatory Characteristics | Optional Characteristics | Supported OH items | Description |
|:---------------------|:----------------------------|:-----------------------------|:-------------------------|:-----------------------------------------------------------------|
| AirQualitySensor | | | | Air Quality Sensor which can measure different parameters |
| | AirQuality | | String | Air quality state, possible values (UNKNOWN,EXCELLENT,GOOD,FAIR,INFERIOR,POOR). Custom mapping can be defined at item level, e.g. [EXCELLENT="BEST", POOR="BAD"] |
| | | OzoneDensity | Number | Ozone density in micrograms/m3, max 1000 |
| | | NitrogenDioxideDensity | Number | NO2 density in micrograms/m3, max 1000 |
| | | SulphurDioxideDensity | Number | SO2 density in micrograms/m3, max 1000 |
| | | PM25Density | Number | PM2.5 micrometer particulate density in micrograms/m3, max 1000 |
| | | PM10Density | Number | PM10 micrometer particulate density in micrograms/m3, max 1000 |
| | | VOCDensity | Number | VOC Density in micrograms/m3, max 1000 |
| | | Name | String | Name of the sensor |
| | | ActiveStatus | Switch, Contact | Working status |
| | | FaultStatus | Switch, Contact | Fault status |
| | | TamperedStatus | Switch, Contact | Tampered status |
| | | BatteryLowStatus | Switch, Contact | Battery status |
| LeakSensor | | | | Leak Sensor |
| | LeakDetectedState | | Switch, Contact | Leak sensor state (ON=Leak Detected, OFF=no leak) |
| | | Name | String | Name of the sensor |
Expand All @@ -138,35 +151,35 @@ A full list of supported accessory types can be found in the table *below*.
| | | TamperedStatus | Switch, Contact | Tampered status |
| | | BatteryLowStatus | Switch, Contact | Battery status |
| MotionSensor | | | | Motion Sensor |
| | MotionDetectedState | | Switc, Contact | Motion sensor state (ON=motion detected, OFF=no motion) |
| | MotionDetectedState | | Switch, Contact | Motion sensor state (ON=motion detected, OFF=no motion) |
| | | Name | String | Name of the sensor |
| | | ActiveStatus | Switch, Contact | Working status |
| | | FaultStatus | Switch, Contact | Fault status |
| | | TamperedStatus | Switch, Contact | Tampered status |
| | | BatteryLowStatus | Switch, Contact | Battery status |
| OccupancySensor | | | | Occupancy Sensor |
| | OccupancyDetectedState | | SwitchItem, Contact Item | Occupancy sensor state (ON=occupied, OFF=not occupied |
| OccupancySensor | | | | Occupancy Sensor |
| | OccupancyDetectedState | | Switch, Contact | Occupancy sensor state (ON=occupied, OFF=not occupied) |
| | | Name | String | Name of the sensor |
| | | ActiveStatus | Switch, Contact | Working status |
| | | FaultStatus | Switch, Contact | Fault status |
| | | TamperedStatus | Switch, Contact | Tampered status |
| | | BatteryLowStatus | Switch, Contact | Battery status |
| ContactSensor | | | | Contact Sensor,An accessory with on/off state that can be viewed in HomeKit but not changed such as a contact sensor for a door or window |
| | ContactSensorState | | SwitchItem, Contact Item | Contact sensor state (ON=open, OFF=closed) |
| | ContactSensorState | | Switch, Contact | Contact sensor state (ON=open, OFF=closed) |
| | | Name | String | Name of the sensor |
| | | ActiveStatus | Switch, Contact | Working status |
| | | FaultStatus | Switch, Contact | Fault status |
| | | TamperedStatus | Switch, Contact | Tampered status |
| | | BatteryLowStatus | Switch, Contact | Battery status |
| SmokeSensor | | | | Smoke Sensor |
| | SmokeDetectedState | | SwitchItem, Contact Item | Smoke sensor state (ON=smoke detected, OFF=no smoke) |
| | SmokeDetectedState | | Switch, Contact | Smoke sensor state (ON=smoke detected, OFF=no smoke) |
| | | Name | String | Name of the sensor |
| | | ActiveStatus | Switch, Contact | Working status |
| | | FaultStatus | Switch, Contact | Fault status |
| | | TamperedStatus | Switch, Contact | Tampered status |
| | | BatteryLowStatus | Switch, Contact | Battery status |
| LightSensor | | | | Light sensor |
| | LightLevel | | Number | Light level in lux |
| | LightLevel | | Number | Light level in lux |
| | | Name | String | Name of the sensor |
| | | ActiveStatus | Switch, Contact | Working status |
| | | FaultStatus | Switch, Contact | Fault status |
Expand Down Expand Up @@ -402,6 +415,15 @@ Switch contactsensor_active "Contact Sensor Active"
Switch contactsensor_fault "Contact Sensor Fault" (gContactSensor) {homekit="ContactSensor.FaultStatus"}
Switch contactsensor_tampered "Contact Sensor Tampered" (gContactSensor) {homekit="ContactSensor.TamperedStatus"}
Group gAirQualitySensor "Air Quality Sensor" {homekit="AirQualitySensor"}
String airquality "Air Quality" (gAirQualitySensor) {homekit="AirQuality"}
Number ozone "Ozone Density" (gAirQualitySensor) {homekit="OzoneDensity"}
Number voc "VOC Density" (gAirQualitySensor) {homekit="VOCDensity"}
Number nitrogen "Nitrogen Density" (gAirQualitySensor) {homekit="NitrogenDioxideDensity"}
Number sulphur "Sulphur Density" (gAirQualitySensor) {homekit="SulphurDioxideDensity"}
Number pm25 "PM25 Density" (gAirQualitySensor) {homekit="PM25Density"}
Number pm10 "PM10 Density" (gAirQualitySensor) {homekit="PM10Density"}
Group gSecuritySystem "Security System Group" {homekit="SecuritySystem"}
String security_current_state "Security Current State" (gSecuritySystem) {homekit="SecuritySystem.CurrentSecuritySystemState"}
String security_target_state "Security Target State" (gSecuritySystem) {homekit="SecuritySystem.TargetSecuritySystemState"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public enum HomekitAccessoryType {
GARAGE_DOOR_OPENER("GarageDoorOpener"),
HEATER_COOLER("HeaterCooler"),
LIGHT_SENSOR("LightSensor"),
AIR_QUALITY_SENSOR("AirQualitySensor"),
DUMMY("Dummy"),
@Deprecated()
BLINDS("Blinds"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ public enum HomekitCharacteristicType {
COOLING_THRESHOLD_TEMPERATURE("CoolingThresholdTemperature"),
HEATING_THRESHOLD_TEMPERATURE("HeatingThresholdTemperature"),

AIR_QUALITY("AirQuality"),
OZONE_DENSITY("OzoneDensity"),
NITROGEN_DIOXIDE_DENSITY("NitrogenDioxideDensity"),
SULPHUR_DIOXIDE_DENSITY("SulphurDioxideDensity"),
PM25_DENSITY("PM25Density"),
PM10_DENSITY("PM10Density"),
VOC_DENSITY("VOCDensity"),

@Deprecated()
OLD_BATTERY_LEVEL("homekit:BatteryLevel"),
@Deprecated()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class HomekitAccessoryFactory {
put(CONTACT_SENSOR, new HomekitCharacteristicType[] { CONTACT_SENSOR_STATE });
put(SMOKE_SENSOR, new HomekitCharacteristicType[] { SMOKE_DETECTED_STATE });
put(HUMIDITY_SENSOR, new HomekitCharacteristicType[] { RELATIVE_HUMIDITY });
put(AIR_QUALITY_SENSOR, new HomekitCharacteristicType[] { AIR_QUALITY });
put(SWITCH, new HomekitCharacteristicType[] { ON_STATE });
put(CARBON_DIOXIDE_SENSOR, new HomekitCharacteristicType[] { CARBON_DIOXIDE_DETECTED_STATE });
put(CARBON_MONOXIDE_SENSOR, new HomekitCharacteristicType[] { CARBON_MONOXIDE_DETECTED_STATE });
Expand Down Expand Up @@ -111,6 +112,7 @@ public class HomekitAccessoryFactory {
put(CONTACT_SENSOR, HomekitContactSensorImpl.class);
put(SMOKE_SENSOR, HomekitSmokeSensorImpl.class);
put(HUMIDITY_SENSOR, HomekitHumiditySensorImpl.class);
put(AIR_QUALITY_SENSOR, HomekitAirQualitySensorImpl.class);
put(SWITCH, HomekitSwitchImpl.class);
put(CARBON_DIOXIDE_SENSOR, HomekitCarbonDioxideSensorImpl.class);
put(CARBON_MONOXIDE_SENSOR, HomekitCarbonMonoxideSensorImpl.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.io.homekit.internal.accessories;

import static org.openhab.io.homekit.internal.HomekitCharacteristicType.AIR_QUALITY;

import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
import org.openhab.io.homekit.internal.HomekitSettings;
import org.openhab.io.homekit.internal.HomekitTaggedItem;

import io.github.hapjava.accessories.AirQualityAccessory;
import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
import io.github.hapjava.characteristics.impl.airquality.AirQualityEnum;
import io.github.hapjava.services.impl.AirQualityService;

/**
* Air Quality sensor accessory.
*
* @author Eugen Freiter - Initial contribution
*/
public class HomekitAirQualitySensorImpl extends AbstractHomekitAccessoryImpl implements AirQualityAccessory {
private final Map<AirQualityEnum, String> qualityStateMapping = new EnumMap<AirQualityEnum, String>(
AirQualityEnum.class) {
{
put(AirQualityEnum.UNKNOWN, "UNKNOWN");
put(AirQualityEnum.EXCELLENT, "EXCELLENT");
put(AirQualityEnum.GOOD, "GOOD");
put(AirQualityEnum.FAIR, "FAIR");
put(AirQualityEnum.INFERIOR, "INFERIOR");
put(AirQualityEnum.POOR, "POOR");
}
};

public HomekitAirQualitySensorImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
super(taggedItem, mandatoryCharacteristics, updater, settings);
updateMapping(AIR_QUALITY, qualityStateMapping);
getServices().add(new AirQualityService(this));
}

@Override
public CompletableFuture<AirQualityEnum> getAirQuality() {
return CompletableFuture
.completedFuture(getKeyFromMapping(AIR_QUALITY, qualityStateMapping, AirQualityEnum.UNKNOWN));
}

@Override
public void subscribeAirQuality(final HomekitCharacteristicChangeCallback callback) {
subscribe(AIR_QUALITY, callback);
}

@Override
public void unsubscribeAirQuality() {
unsubscribe(AIR_QUALITY);
}
}
Loading

0 comments on commit 5e24f7e

Please sign in to comment.