]> git.basschouten.com Git - openhab-addons.git/blob
aad63fad18d8e23f648f3548777ccfa1a0e60b65
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.homekit.internal.accessories;
14
15 import java.util.List;
16 import java.util.concurrent.CompletableFuture;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
20 import org.openhab.io.homekit.internal.HomekitException;
21 import org.openhab.io.homekit.internal.HomekitSettings;
22 import org.openhab.io.homekit.internal.HomekitTaggedItem;
23
24 import io.github.hapjava.characteristics.impl.common.ActiveCharacteristic;
25 import io.github.hapjava.characteristics.impl.common.ActiveIdentifierCharacteristic;
26 import io.github.hapjava.characteristics.impl.common.ConfiguredNameCharacteristic;
27 import io.github.hapjava.characteristics.impl.common.NameCharacteristic;
28 import io.github.hapjava.characteristics.impl.lightbulb.BrightnessCharacteristic;
29 import io.github.hapjava.characteristics.impl.television.ClosedCaptionsCharacteristic;
30 import io.github.hapjava.characteristics.impl.television.CurrentMediaStateCharacteristic;
31 import io.github.hapjava.characteristics.impl.television.PictureModeCharacteristic;
32 import io.github.hapjava.characteristics.impl.television.PowerModeCharacteristic;
33 import io.github.hapjava.characteristics.impl.television.RemoteKeyCharacteristic;
34 import io.github.hapjava.characteristics.impl.television.SleepDiscoveryModeCharacteristic;
35 import io.github.hapjava.characteristics.impl.television.SleepDiscoveryModeEnum;
36 import io.github.hapjava.characteristics.impl.television.TargetMediaStateCharacteristic;
37 import io.github.hapjava.services.impl.TelevisionService;
38
39 /**
40  * Implements Television
41  * 
42  * This is a little different in that we don't implement the accessory interface.
43  * This is because several of the "mandatory" characteristics we don't require,
44  * and wait until all optional attributes are added and if they don't exist
45  * it will create "default" values for them.
46  *
47  * @author Cody Cutrer - Initial contribution
48  */
49 @NonNullByDefault({})
50 public class HomekitTelevisionImpl extends AbstractHomekitAccessoryImpl {
51
52     public HomekitTelevisionImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
53             HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
54         super(taggedItem, mandatoryCharacteristics, updater, settings);
55     }
56
57     @Override
58     public void init() throws HomekitException {
59         super.init();
60
61         // these charactereristics are technically mandatory, but we provide defaults if they're not provided
62         var activeIdentifierCharacteristic = getCharacteristic(ActiveIdentifierCharacteristic.class)
63                 .orElseGet(() -> new ActiveIdentifierCharacteristic(() -> CompletableFuture.completedFuture(1), v -> {
64                 }, v -> {
65                 }, () -> {
66                 }));
67         var configuredNameCharacteristic = getCharacteristic(ConfiguredNameCharacteristic.class)
68                 .orElseGet(() -> new ConfiguredNameCharacteristic(() -> getName(), v -> {
69                 }, v -> {
70                 }, () -> {
71                 }));
72         var remoteKeyCharacteristic = getCharacteristic(RemoteKeyCharacteristic.class)
73                 .orElseGet(() -> new RemoteKeyCharacteristic((v) -> {
74                 }));
75         var sleepDiscoveryModeCharacteristic = getCharacteristic(SleepDiscoveryModeCharacteristic.class)
76                 .orElseGet(() -> new SleepDiscoveryModeCharacteristic(
77                         () -> CompletableFuture.completedFuture(SleepDiscoveryModeEnum.ALWAYS_DISCOVERABLE), v -> {
78                         }, () -> {
79                         }));
80
81         var service = new TelevisionService(getCharacteristic(ActiveCharacteristic.class).get(),
82                 activeIdentifierCharacteristic, configuredNameCharacteristic, remoteKeyCharacteristic,
83                 sleepDiscoveryModeCharacteristic);
84
85         getCharacteristic(NameCharacteristic.class).ifPresent(c -> service.addOptionalCharacteristic(c));
86         getCharacteristic(BrightnessCharacteristic.class).ifPresent(c -> service.addOptionalCharacteristic(c));
87         getCharacteristic(PowerModeCharacteristic.class).ifPresent(c -> service.addOptionalCharacteristic(c));
88         getCharacteristic(ClosedCaptionsCharacteristic.class).ifPresent(c -> service.addOptionalCharacteristic(c));
89         getCharacteristic(CurrentMediaStateCharacteristic.class).ifPresent(c -> service.addOptionalCharacteristic(c));
90         getCharacteristic(TargetMediaStateCharacteristic.class).ifPresent(c -> service.addOptionalCharacteristic(c));
91         getCharacteristic(PictureModeCharacteristic.class).ifPresent(c -> service.addOptionalCharacteristic(c));
92
93         getServices().add(service);
94     }
95 }