[homekit] add support for additional homekit accessories (#12536)

Signed-off-by: Eugen Freiter <freiter@gmx.de>
This commit is contained in:
eugen
2022-04-26 19:37:54 +02:00
committed by GitHub
parent d584daf334
commit f310cf7ca0
11 changed files with 634 additions and 30 deletions

View File

@@ -44,10 +44,16 @@ public enum HomekitAccessoryType {
SECURITY_SYSTEM("SecuritySystem"),
OUTLET("Outlet"),
SPEAKER("Speaker"),
SMART_SPEAKER("SmartSpeaker"),
GARAGE_DOOR_OPENER("GarageDoorOpener"),
HEATER_COOLER("HeaterCooler"),
LIGHT_SENSOR("LightSensor"),
AIR_QUALITY_SENSOR("AirQualitySensor"),
BATTERY("Battery"),
FILTER_MAINTENANCE("Filter"),
FAUCET("Faucet"),
MICROPHONE("Microphone"),
SLAT("Slat"),
DUMMY("Dummy");
private static final Map<String, HomekitAccessoryType> TAG_MAP = new HashMap<>();

View File

@@ -62,6 +62,8 @@ public enum HomekitCharacteristicType {
CURRENT_VERTICAL_TILT_ANGLE("CurrentVerticalTiltAngle"),
TARGET_HORIZONTAL_TILT_ANGLE("TargetHorizontalTiltAngle"),
TARGET_VERTICAL_TILT_ANGLE("TargetVerticalTiltAngle"),
CURRENT_TILT_ANGLE("CurrentTiltAngle"),
TARGET_TILT_ANGLE("TargetTiltAngle"),
HUE("Hue"),
BRIGHTNESS("Brightness"),
@@ -107,7 +109,22 @@ public enum HomekitCharacteristicType {
SULPHUR_DIOXIDE_DENSITY("SulphurDioxideDensity"),
PM25_DENSITY("PM25Density"),
PM10_DENSITY("PM10Density"),
VOC_DENSITY("VOCDensity");
VOC_DENSITY("VOCDensity"),
BATTERY_LEVEL("BatteryLevel"),
BATTERY_CHARGING_STATE("BatteryChargingState"),
CURRENT_SLAT_STATE("CurrentSlatState"),
CURRENT_MEDIA_STATE("CurrentMediaState"),
TARGET_MEDIA_STATE("TargetMediaState"),
CONFIGURED_NAME("ConfiguredName"),
ACTIVE("Active"),
FILTER_CHANGE_INDICATION("FilterChangeIndication"),
FILTER_LIFE_LEVEL("FilterLifeLevel"),
FILTER_RESET_INDICATION("FilterResetIndication");
private static final Map<String, HomekitCharacteristicType> TAG_MAP = new HashMap<>();

View File

@@ -89,12 +89,18 @@ public class HomekitAccessoryFactory {
new HomekitCharacteristicType[] { SECURITY_SYSTEM_CURRENT_STATE, SECURITY_SYSTEM_TARGET_STATE });
put(OUTLET, new HomekitCharacteristicType[] { ON_STATE, INUSE_STATUS });
put(SPEAKER, new HomekitCharacteristicType[] { MUTE });
put(SMART_SPEAKER, new HomekitCharacteristicType[] { CURRENT_MEDIA_STATE, TARGET_MEDIA_STATE });
put(GARAGE_DOOR_OPENER,
new HomekitCharacteristicType[] { CURRENT_DOOR_STATE, TARGET_DOOR_STATE, OBSTRUCTION_STATUS });
put(HEATER_COOLER, new HomekitCharacteristicType[] { ACTIVE_STATUS, CURRENT_HEATER_COOLER_STATE,
TARGET_HEATER_COOLER_STATE, CURRENT_TEMPERATURE });
put(WINDOW, new HomekitCharacteristicType[] { CURRENT_POSITION, TARGET_POSITION, POSITION_STATE });
put(DOOR, new HomekitCharacteristicType[] { CURRENT_POSITION, TARGET_POSITION, POSITION_STATE });
put(BATTERY, new HomekitCharacteristicType[] { BATTERY_LEVEL, BATTERY_LOW_STATUS });
put(FILTER_MAINTENANCE, new HomekitCharacteristicType[] { FILTER_CHANGE_INDICATION });
put(SLAT, new HomekitCharacteristicType[] { CURRENT_SLAT_STATE });
put(FAUCET, new HomekitCharacteristicType[] { ACTIVE_STATUS });
put(MICROPHONE, new HomekitCharacteristicType[] { MUTE });
}
};
@@ -122,10 +128,16 @@ public class HomekitAccessoryFactory {
put(SECURITY_SYSTEM, HomekitSecuritySystemImpl.class);
put(OUTLET, HomekitOutletImpl.class);
put(SPEAKER, HomekitSpeakerImpl.class);
put(SMART_SPEAKER, HomekitSmartSpeakerImpl.class);
put(GARAGE_DOOR_OPENER, HomekitGarageDoorOpenerImpl.class);
put(DOOR, HomekitDoorImpl.class);
put(WINDOW, HomekitWindowImpl.class);
put(HEATER_COOLER, HomekitHeaterCoolerImpl.class);
put(BATTERY, HomekitBatteryImpl.class);
put(FILTER_MAINTENANCE, HomekitFilterMaintenanceImpl.class);
put(SLAT, HomekitSlatImpl.class);
put(FAUCET, HomekitFaucetImpl.class);
put(MICROPHONE, HomekitMicrophoneImpl.class);
}
};

View File

@@ -0,0 +1,107 @@
/**
* Copyright (c) 2010-2022 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.BATTERY_CHARGING_STATE;
import static org.openhab.io.homekit.internal.HomekitCharacteristicType.BATTERY_LEVEL;
import static org.openhab.io.homekit.internal.HomekitCharacteristicType.BATTERY_LOW_STATUS;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.library.types.DecimalType;
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.BatteryAccessory;
import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
import io.github.hapjava.characteristics.impl.battery.ChargingStateEnum;
import io.github.hapjava.characteristics.impl.battery.StatusLowBatteryEnum;
import io.github.hapjava.services.impl.BatteryService;
/**
* Implements battery services for chargeable and non-chargeable batteries
*
* @author Eugen Freiter - Initial contribution
*/
public class HomekitBatteryImpl extends AbstractHomekitAccessoryImpl implements BatteryAccessory {
private static final String BATTERY_TYPE = "chargeable";
private final BooleanItemReader lowBatteryReader;
private BooleanItemReader chargingBatteryReader;
private final boolean isChargeable;
public HomekitBatteryImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
super(taggedItem, mandatoryCharacteristics, updater, settings);
lowBatteryReader = createBooleanReader(BATTERY_LOW_STATUS);
final String batteryTypeConfig = getAccessoryConfiguration(BATTERY_TYPE, "false");
isChargeable = "true".equalsIgnoreCase(batteryTypeConfig) || "yes".equalsIgnoreCase(batteryTypeConfig);
if (isChargeable) {
chargingBatteryReader = createBooleanReader(BATTERY_CHARGING_STATE);
}
getServices().add(new BatteryService(this));
}
@Override
public CompletableFuture<Integer> getBatteryLevel() {
final @Nullable DecimalType state = getStateAs(BATTERY_LEVEL, DecimalType.class);
return CompletableFuture.completedFuture(state != null ? state.intValue() : 0);
}
@Override
public CompletableFuture<StatusLowBatteryEnum> getLowBatteryState() {
return CompletableFuture
.completedFuture(lowBatteryReader.getValue() ? StatusLowBatteryEnum.LOW : StatusLowBatteryEnum.NORMAL);
}
@Override
public CompletableFuture<ChargingStateEnum> getChargingState() {
return CompletableFuture.completedFuture(isChargeable
? chargingBatteryReader.getValue() ? ChargingStateEnum.CHARGING : ChargingStateEnum.NOT_CHARGING
: ChargingStateEnum.NOT_CHARABLE); // the mapping to NOT_CHARABLE is correct, there is a typo in java
// HAP
}
@Override
public void subscribeBatteryLevel(final HomekitCharacteristicChangeCallback callback) {
subscribe(BATTERY_LEVEL, callback);
}
@Override
public void subscribeLowBatteryState(final HomekitCharacteristicChangeCallback callback) {
subscribe(BATTERY_LOW_STATUS, callback);
}
@Override
public void subscribeBatteryChargingState(final HomekitCharacteristicChangeCallback callback) {
subscribe(BATTERY_CHARGING_STATE, callback);
}
@Override
public void unsubscribeBatteryLevel() {
unsubscribe(BATTERY_LEVEL);
}
@Override
public void unsubscribeLowBatteryState() {
unsubscribe(BATTERY_LOW_STATUS);
}
@Override
public void unsubscribeBatteryChargingState() {
unsubscribe(BATTERY_CHARGING_STATE);
}
}

View File

@@ -33,6 +33,7 @@ import org.openhab.core.items.Item;
import org.openhab.core.library.items.ColorItem;
import org.openhab.core.library.items.DimmerItem;
import org.openhab.core.library.items.NumberItem;
import org.openhab.core.library.items.StringItem;
import org.openhab.core.library.items.SwitchItem;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.HSBType;
@@ -40,6 +41,7 @@ import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.OpenClosedType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.unit.ImperialUnits;
import org.openhab.core.library.unit.SIUnits;
import org.openhab.core.types.State;
@@ -72,6 +74,9 @@ import io.github.hapjava.characteristics.impl.carbondioxidesensor.CarbonDioxideL
import io.github.hapjava.characteristics.impl.carbondioxidesensor.CarbonDioxidePeakLevelCharacteristic;
import io.github.hapjava.characteristics.impl.carbonmonoxidesensor.CarbonMonoxideLevelCharacteristic;
import io.github.hapjava.characteristics.impl.carbonmonoxidesensor.CarbonMonoxidePeakLevelCharacteristic;
import io.github.hapjava.characteristics.impl.common.ActiveCharacteristic;
import io.github.hapjava.characteristics.impl.common.ActiveEnum;
import io.github.hapjava.characteristics.impl.common.ConfiguredNameCharacteristic;
import io.github.hapjava.characteristics.impl.common.NameCharacteristic;
import io.github.hapjava.characteristics.impl.common.ObstructionDetectedCharacteristic;
import io.github.hapjava.characteristics.impl.common.StatusActiveCharacteristic;
@@ -90,10 +95,14 @@ import io.github.hapjava.characteristics.impl.fan.SwingModeCharacteristic;
import io.github.hapjava.characteristics.impl.fan.SwingModeEnum;
import io.github.hapjava.characteristics.impl.fan.TargetFanStateCharacteristic;
import io.github.hapjava.characteristics.impl.fan.TargetFanStateEnum;
import io.github.hapjava.characteristics.impl.filtermaintenance.FilterLifeLevelCharacteristic;
import io.github.hapjava.characteristics.impl.filtermaintenance.ResetFilterIndicationCharacteristic;
import io.github.hapjava.characteristics.impl.lightbulb.BrightnessCharacteristic;
import io.github.hapjava.characteristics.impl.lightbulb.ColorTemperatureCharacteristic;
import io.github.hapjava.characteristics.impl.lightbulb.HueCharacteristic;
import io.github.hapjava.characteristics.impl.lightbulb.SaturationCharacteristic;
import io.github.hapjava.characteristics.impl.slat.CurrentTiltAngleCharacteristic;
import io.github.hapjava.characteristics.impl.slat.TargetTiltAngleCharacteristic;
import io.github.hapjava.characteristics.impl.thermostat.CoolingThresholdTemperatureCharacteristic;
import io.github.hapjava.characteristics.impl.thermostat.HeatingThresholdTemperatureCharacteristic;
import io.github.hapjava.characteristics.impl.valve.RemainingDurationCharacteristic;
@@ -135,6 +144,8 @@ public class HomekitCharacteristicFactory {
put(TARGET_HORIZONTAL_TILT_ANGLE,
HomekitCharacteristicFactory::createTargetHorizontalTiltAngleCharacteristic);
put(TARGET_VERTICAL_TILT_ANGLE, HomekitCharacteristicFactory::createTargetVerticalTiltAngleCharacteristic);
put(CURRENT_TILT_ANGLE, HomekitCharacteristicFactory::createCurrentTiltAngleCharacteristic);
put(TARGET_TILT_ANGLE, HomekitCharacteristicFactory::createTargetTiltAngleCharacteristic);
put(HUE, HomekitCharacteristicFactory::createHueCharacteristic);
put(BRIGHTNESS, HomekitCharacteristicFactory::createBrightnessCharacteristic);
put(SATURATION, HomekitCharacteristicFactory::createSaturationCharacteristic);
@@ -156,6 +167,10 @@ public class HomekitCharacteristicFactory {
put(PM25_DENSITY, HomekitCharacteristicFactory::createPM25DensityCharacteristic);
put(PM10_DENSITY, HomekitCharacteristicFactory::createPM10DensityCharacteristic);
put(VOC_DENSITY, HomekitCharacteristicFactory::createVOCDensityCharacteristic);
put(FILTER_LIFE_LEVEL, HomekitCharacteristicFactory::createFilterLifeLevelCharacteristic);
put(FILTER_RESET_INDICATION, HomekitCharacteristicFactory::createFilterResetCharacteristic);
put(ACTIVE, HomekitCharacteristicFactory::createActiveCharacteristic);
put(CONFIGURED_NAME, HomekitCharacteristicFactory::createConfiguredNameCharacteristic);
}
};
@@ -512,6 +527,20 @@ public class HomekitCharacteristicFactory {
getUnsubscriber(taggedItem, TARGET_HORIZONTAL_TILT_ANGLE, updater));
}
private static CurrentTiltAngleCharacteristic createCurrentTiltAngleCharacteristic(HomekitTaggedItem taggedItem,
HomekitAccessoryUpdater updater) {
return new CurrentTiltAngleCharacteristic(getAngleSupplier(taggedItem, 0),
getSubscriber(taggedItem, CURRENT_TILT_ANGLE, updater),
getUnsubscriber(taggedItem, CURRENT_TILT_ANGLE, updater));
}
private static TargetTiltAngleCharacteristic createTargetTiltAngleCharacteristic(HomekitTaggedItem taggedItem,
HomekitAccessoryUpdater updater) {
return new TargetTiltAngleCharacteristic(getAngleSupplier(taggedItem, 0), setAngleConsumer(taggedItem),
getSubscriber(taggedItem, TARGET_TILT_ANGLE, updater),
getUnsubscriber(taggedItem, TARGET_TILT_ANGLE, updater));
}
private static HueCharacteristic createHueCharacteristic(HomekitTaggedItem taggedItem,
HomekitAccessoryUpdater updater) {
return new HueCharacteristic(() -> {
@@ -783,4 +812,36 @@ public class HomekitCharacteristicFactory {
VOCDensityCharacteristic.DEFAULT_MIN_VALUE)),
getSubscriber(taggedItem, VOC_DENSITY, updater), getUnsubscriber(taggedItem, VOC_DENSITY, updater));
}
private static FilterLifeLevelCharacteristic createFilterLifeLevelCharacteristic(HomekitTaggedItem taggedItem,
HomekitAccessoryUpdater updater) {
return new FilterLifeLevelCharacteristic(getDoubleSupplier(taggedItem, 0),
getSubscriber(taggedItem, FILTER_LIFE_LEVEL, updater),
getUnsubscriber(taggedItem, FILTER_LIFE_LEVEL, updater));
}
private static ResetFilterIndicationCharacteristic createFilterResetCharacteristic(HomekitTaggedItem taggedItem,
HomekitAccessoryUpdater updater) {
return new ResetFilterIndicationCharacteristic(
(value) -> ((SwitchItem) taggedItem.getItem()).send(OnOffType.ON));
}
private static ActiveCharacteristic createActiveCharacteristic(HomekitTaggedItem taggedItem,
HomekitAccessoryUpdater updater) {
return new ActiveCharacteristic(
() -> getEnumFromItem(taggedItem, ActiveEnum.ACTIVE, ActiveEnum.INACTIVE, ActiveEnum.INACTIVE),
(value) -> setValueFromEnum(taggedItem, value, ActiveEnum.ACTIVE, ActiveEnum.INACTIVE),
getSubscriber(taggedItem, ACTIVE, updater), getUnsubscriber(taggedItem, ACTIVE, updater));
}
private static ConfiguredNameCharacteristic createConfiguredNameCharacteristic(HomekitTaggedItem taggedItem,
HomekitAccessoryUpdater updater) {
return new ConfiguredNameCharacteristic(() -> {
final State state = taggedItem.getItem().getState();
return CompletableFuture
.completedFuture(state instanceof UnDefType ? taggedItem.getName() : state.toString());
}, (value) -> ((StringItem) taggedItem.getItem()).send(new StringType(value)),
getSubscriber(taggedItem, CONFIGURED_NAME, updater),
getUnsubscriber(taggedItem, CONFIGURED_NAME, updater));
}
}

View File

@@ -0,0 +1,63 @@
/**
* Copyright (c) 2010-2022 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.ACTIVE;
import java.util.List;
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.FaucetAccessory;
import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
import io.github.hapjava.services.impl.FaucetService;
/**
* Implements Faucet using an Item that provides an On/Off state
*
* @author Eugen Freiter - Initial contribution
*/
class HomekitFaucetImpl extends AbstractHomekitAccessoryImpl implements FaucetAccessory {
private final BooleanItemReader activeReader;
public HomekitFaucetImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
super(taggedItem, mandatoryCharacteristics, updater, settings);
activeReader = createBooleanReader(ACTIVE);
this.getServices().add(new FaucetService(this));
}
@Override
public CompletableFuture<Boolean> isActive() {
return CompletableFuture.completedFuture(activeReader.getValue());
}
@Override
public CompletableFuture<Void> setActive(boolean state) {
activeReader.setValue(state);
return CompletableFuture.completedFuture(null);
}
@Override
public void subscribeActive(HomekitCharacteristicChangeCallback callback) {
subscribe(ACTIVE, callback);
}
@Override
public void unsubscribeActive() {
unsubscribe(ACTIVE);
}
}

View File

@@ -0,0 +1,60 @@
/**
* Copyright (c) 2010-2022 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.FILTER_CHANGE_INDICATION;
import java.util.List;
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.FilterMaintenanceAccessory;
import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
import io.github.hapjava.characteristics.impl.filtermaintenance.FilterChangeIndicationEnum;
import io.github.hapjava.services.impl.FilterMaintenanceService;
/**
* Implements filter maintenance indicator
*
* @author Eugen Freiter - Initial contribution
*/
public class HomekitFilterMaintenanceImpl extends AbstractHomekitAccessoryImpl implements FilterMaintenanceAccessory {
private BooleanItemReader filterChangeIndication;
public HomekitFilterMaintenanceImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
super(taggedItem, mandatoryCharacteristics, updater, settings);
filterChangeIndication = createBooleanReader(FILTER_CHANGE_INDICATION);
getServices().add(new FilterMaintenanceService(this));
}
@Override
public CompletableFuture<FilterChangeIndicationEnum> getFilterChangeIndication() {
return CompletableFuture
.completedFuture(filterChangeIndication.getValue() ? FilterChangeIndicationEnum.CHANGE_NEEDED
: FilterChangeIndicationEnum.NO_CHANGE_NEEDED);
}
@Override
public void subscribeFilterChangeIndication(final HomekitCharacteristicChangeCallback callback) {
subscribe(FILTER_CHANGE_INDICATION, callback);
}
@Override
public void unsubscribeFilterChangeIndication() {
unsubscribe(FILTER_CHANGE_INDICATION);
}
}

View File

@@ -0,0 +1,62 @@
/**
* Copyright (c) 2010-2022 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 java.util.List;
import java.util.concurrent.CompletableFuture;
import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
import org.openhab.io.homekit.internal.HomekitCharacteristicType;
import org.openhab.io.homekit.internal.HomekitSettings;
import org.openhab.io.homekit.internal.HomekitTaggedItem;
import io.github.hapjava.accessories.MicrophoneAccessory;
import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
import io.github.hapjava.services.impl.MicrophoneService;
/**
* Implements microphone using an item that provides an On/Off state for Mute.
*
* @author Eugen Freiter - Initial contribution
*/
public class HomekitMicrophoneImpl extends AbstractHomekitAccessoryImpl implements MicrophoneAccessory {
private final BooleanItemReader muteReader;
public HomekitMicrophoneImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
super(taggedItem, mandatoryCharacteristics, updater, settings);
muteReader = createBooleanReader(HomekitCharacteristicType.MUTE);
getServices().add(new MicrophoneService(this));
}
@Override
public CompletableFuture<Boolean> isMuted() {
return CompletableFuture.completedFuture(muteReader.getValue());
}
@Override
public CompletableFuture<Void> setMute(boolean state) {
muteReader.setValue(state);
return CompletableFuture.completedFuture(null);
}
@Override
public void subscribeMuteState(HomekitCharacteristicChangeCallback callback) {
subscribe(HomekitCharacteristicType.MUTE, callback);
}
@Override
public void unsubscribeMuteState() {
unsubscribe(HomekitCharacteristicType.MUTE);
}
}

View File

@@ -0,0 +1,74 @@
/**
* Copyright (c) 2010-2022 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.CURRENT_SLAT_STATE;
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.SlatAccessory;
import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
import io.github.hapjava.characteristics.impl.slat.CurrentSlatStateEnum;
import io.github.hapjava.characteristics.impl.slat.SlatTypeEnum;
import io.github.hapjava.services.impl.SlatService;
/**
*
* @author Eugen Freiter - Initial contribution
*/
public class HomekitSlatImpl extends AbstractHomekitAccessoryImpl implements SlatAccessory {
private static final String CONFIG_TYPE = "type";
private final Map<CurrentSlatStateEnum, String> currentSlatStateMapping;
private final SlatTypeEnum slatType;
public HomekitSlatImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
HomekitAccessoryUpdater updater, HomekitSettings settings) {
super(taggedItem, mandatoryCharacteristics, updater, settings);
final String slatTypeConfig = getAccessoryConfiguration(CONFIG_TYPE, "horizontal");
slatType = "horizontal".equalsIgnoreCase(slatTypeConfig) ? SlatTypeEnum.HORIZONTAL : SlatTypeEnum.VERTICAL;
currentSlatStateMapping = new EnumMap<>(CurrentSlatStateEnum.class);
currentSlatStateMapping.put(CurrentSlatStateEnum.FIXED, "FIXED");
currentSlatStateMapping.put(CurrentSlatStateEnum.SWINGING, "SWINGING");
currentSlatStateMapping.put(CurrentSlatStateEnum.JAMMED, "JAMMED");
updateMapping(CURRENT_SLAT_STATE, currentSlatStateMapping);
getServices().add(new SlatService(this));
}
@Override
public CompletableFuture<CurrentSlatStateEnum> getSlatState() {
return CompletableFuture.completedFuture(
getKeyFromMapping(CURRENT_SLAT_STATE, currentSlatStateMapping, CurrentSlatStateEnum.FIXED));
}
@Override
public void subscribeSlatState(final HomekitCharacteristicChangeCallback callback) {
subscribe(CURRENT_SLAT_STATE, callback);
}
@Override
public void unsubscribeSlatState() {
unsubscribe(CURRENT_SLAT_STATE);
}
@Override
public CompletableFuture<SlatTypeEnum> getSlatType() {
return CompletableFuture.completedFuture(slatType);
}
}

View File

@@ -0,0 +1,99 @@
/**
* Copyright (c) 2010-2022 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.CURRENT_MEDIA_STATE;
import static org.openhab.io.homekit.internal.HomekitCharacteristicType.TARGET_MEDIA_STATE;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import org.openhab.core.library.items.StringItem;
import org.openhab.core.library.types.StringType;
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.SmartSpeakerAccessory;
import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
import io.github.hapjava.characteristics.impl.television.CurrentMediaStateEnum;
import io.github.hapjava.characteristics.impl.television.TargetMediaStateEnum;
import io.github.hapjava.services.impl.SmartSpeakerService;
/**
*
* @author Eugen Freiter - Initial contribution
*/
public class HomekitSmartSpeakerImpl extends AbstractHomekitAccessoryImpl implements SmartSpeakerAccessory {
private final Map<CurrentMediaStateEnum, String> currentMediaState;
private final Map<TargetMediaStateEnum, String> targetMediaState;
public HomekitSmartSpeakerImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
HomekitAccessoryUpdater updater, HomekitSettings settings) {
super(taggedItem, mandatoryCharacteristics, updater, settings);
currentMediaState = new EnumMap<>(CurrentMediaStateEnum.class);
currentMediaState.put(CurrentMediaStateEnum.STOP, "STOP");
currentMediaState.put(CurrentMediaStateEnum.PLAY, "PLAY");
currentMediaState.put(CurrentMediaStateEnum.PAUSE, "PAUSE");
currentMediaState.put(CurrentMediaStateEnum.UNKNOWN, "UNKNOWN");
updateMapping(CURRENT_MEDIA_STATE, currentMediaState);
targetMediaState = new EnumMap<>(TargetMediaStateEnum.class);
targetMediaState.put(TargetMediaStateEnum.STOP, "STOP");
targetMediaState.put(TargetMediaStateEnum.PLAY, "PLAY");
targetMediaState.put(TargetMediaStateEnum.PAUSE, "PAUSE");
updateMapping(TARGET_MEDIA_STATE, targetMediaState);
getServices().add(new SmartSpeakerService(this));
}
@Override
public CompletableFuture<CurrentMediaStateEnum> getCurrentMediaState() {
return CompletableFuture.completedFuture(
getKeyFromMapping(CURRENT_MEDIA_STATE, currentMediaState, CurrentMediaStateEnum.UNKNOWN));
}
@Override
public void subscribeCurrentMediaState(final HomekitCharacteristicChangeCallback callback) {
subscribe(CURRENT_MEDIA_STATE, callback);
}
@Override
public void unsubscribeCurrentMediaState() {
unsubscribe(CURRENT_MEDIA_STATE);
}
@Override
public CompletableFuture<TargetMediaStateEnum> getTargetMediaState() {
return CompletableFuture
.completedFuture(getKeyFromMapping(TARGET_MEDIA_STATE, targetMediaState, TargetMediaStateEnum.STOP));
}
@Override
public CompletableFuture<Void> setTargetMediaState(final TargetMediaStateEnum targetState) {
getItem(TARGET_MEDIA_STATE, StringItem.class)
.ifPresent(item -> item.send(new StringType(targetMediaState.get(targetState))));
return CompletableFuture.completedFuture(null);
}
@Override
public void subscribeTargetMediaState(final HomekitCharacteristicChangeCallback callback) {
subscribe(TARGET_MEDIA_STATE, callback);
}
@Override
public void unsubscribeTargetMediaState() {
unsubscribe(TARGET_MEDIA_STATE);
}
}