]> git.basschouten.com Git - openhab-addons.git/blob
10ce819421d82eee7725366293d3dfe26e9c1621
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.lightbulb.BrightnessCharacteristic;
28 import io.github.hapjava.characteristics.impl.television.ClosedCaptionsCharacteristic;
29 import io.github.hapjava.characteristics.impl.television.CurrentMediaStateCharacteristic;
30 import io.github.hapjava.characteristics.impl.television.PictureModeCharacteristic;
31 import io.github.hapjava.characteristics.impl.television.PowerModeCharacteristic;
32 import io.github.hapjava.characteristics.impl.television.RemoteKeyCharacteristic;
33 import io.github.hapjava.characteristics.impl.television.SleepDiscoveryModeCharacteristic;
34 import io.github.hapjava.characteristics.impl.television.SleepDiscoveryModeEnum;
35 import io.github.hapjava.characteristics.impl.television.TargetMediaStateCharacteristic;
36 import io.github.hapjava.services.impl.TelevisionService;
37
38 /**
39  * Implements Television
40  * 
41  * This is a little different in that we don't implement the accessory interface.
42  * This is because several of the "mandatory" characteristics we don't require,
43  * and wait until all optional attributes are added and if they don't exist
44  * it will create "default" values for them.
45  *
46  * @author Cody Cutrer - Initial contribution
47  */
48 @NonNullByDefault({})
49 public class HomekitTelevisionImpl extends AbstractHomekitAccessoryImpl {
50
51     public HomekitTelevisionImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
52             HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
53         super(taggedItem, mandatoryCharacteristics, updater, settings);
54     }
55
56     @Override
57     public void init() throws HomekitException {
58         super.init();
59
60         // these charactereristics are technically mandatory, but we provide defaults if they're not provided
61         var activeIdentifierCharacteristic = getCharacteristic(ActiveIdentifierCharacteristic.class)
62                 .orElseGet(() -> new ActiveIdentifierCharacteristic(() -> CompletableFuture.completedFuture(1), v -> {
63                 }, v -> {
64                 }, () -> {
65                 }));
66         var configuredNameCharacteristic = getCharacteristic(ConfiguredNameCharacteristic.class)
67                 .orElseGet(() -> new ConfiguredNameCharacteristic(() -> getName(), v -> {
68                 }, v -> {
69                 }, () -> {
70                 }));
71         var remoteKeyCharacteristic = getCharacteristic(RemoteKeyCharacteristic.class)
72                 .orElseGet(() -> new RemoteKeyCharacteristic((v) -> {
73                 }));
74         var sleepDiscoveryModeCharacteristic = getCharacteristic(SleepDiscoveryModeCharacteristic.class)
75                 .orElseGet(() -> new SleepDiscoveryModeCharacteristic(
76                         () -> CompletableFuture.completedFuture(SleepDiscoveryModeEnum.ALWAYS_DISCOVERABLE), v -> {
77                         }, () -> {
78                         }));
79
80         var service = new TelevisionService(getCharacteristic(ActiveCharacteristic.class).get(),
81                 activeIdentifierCharacteristic, configuredNameCharacteristic, remoteKeyCharacteristic,
82                 sleepDiscoveryModeCharacteristic);
83
84         getCharacteristic(BrightnessCharacteristic.class).ifPresent(c -> service.addOptionalCharacteristic(c));
85         getCharacteristic(PowerModeCharacteristic.class).ifPresent(c -> service.addOptionalCharacteristic(c));
86         getCharacteristic(ClosedCaptionsCharacteristic.class).ifPresent(c -> service.addOptionalCharacteristic(c));
87         getCharacteristic(CurrentMediaStateCharacteristic.class).ifPresent(c -> service.addOptionalCharacteristic(c));
88         getCharacteristic(TargetMediaStateCharacteristic.class).ifPresent(c -> service.addOptionalCharacteristic(c));
89         getCharacteristic(PictureModeCharacteristic.class).ifPresent(c -> service.addOptionalCharacteristic(c));
90
91         getServices().add(service);
92     }
93 }