]> git.basschouten.com Git - openhab-addons.git/blob
c07138eb864e7ea8fccd1f575a6a49265a359a41
[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.binding.ecovacs.internal.action;
14
15 import static org.openhab.binding.ecovacs.internal.EcovacsBindingConstants.*;
16
17 import java.util.Optional;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.ecovacs.internal.api.commands.PlaySoundCommand;
22 import org.openhab.binding.ecovacs.internal.handler.EcovacsVacuumHandler;
23 import org.openhab.core.automation.annotation.ActionInput;
24 import org.openhab.core.automation.annotation.RuleAction;
25 import org.openhab.core.thing.binding.ThingActions;
26 import org.openhab.core.thing.binding.ThingActionsScope;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * @author Danny Baumann - Initial contribution
33  */
34 @ThingActionsScope(name = "ecovacs")
35 @NonNullByDefault
36 public class EcovacsVacuumActions implements ThingActions {
37     private final Logger logger = LoggerFactory.getLogger(EcovacsVacuumActions.class);
38     private @Nullable EcovacsVacuumHandler handler;
39
40     @Override
41     public void setThingHandler(@Nullable ThingHandler handler) {
42         this.handler = (EcovacsVacuumHandler) handler;
43     }
44
45     @Override
46     public @Nullable ThingHandler getThingHandler() {
47         return handler;
48     }
49
50     @RuleAction(label = "@text/playSoundActionLabel", description = "@text/playSoundActionDesc")
51     public void playSound(
52             @ActionInput(name = "type", label = "@text/actionInputSoundTypeLabel", description = "@text/actionInputSoundTypeDesc") String type) {
53         EcovacsVacuumHandler handler = this.handler;
54         if (handler != null) {
55             Optional<PlaySoundCommand.SoundType> soundType = SOUND_TYPE_MAPPING.findMappedEnumValue(type);
56             if (soundType.isPresent()) {
57                 handler.playSound(new PlaySoundCommand(soundType.get()));
58             } else {
59                 logger.debug("Sound type '{}' is unknown, ignoring", type);
60             }
61         }
62     }
63
64     @RuleAction(label = "@text/playSoundActionLabel", description = "@text/playSoundActionDesc")
65     public void playSoundWithId(
66             @ActionInput(name = "soundId", label = "@text/actionInputSoundIdLabel", description = "@text/actionInputSoundIdDesc") int soundId) {
67         EcovacsVacuumHandler handler = this.handler;
68         if (handler != null) {
69             handler.playSound(new PlaySoundCommand(soundId));
70         }
71     }
72
73     public static void playSound(@Nullable ThingActions actions, String type) {
74         if (actions instanceof EcovacsVacuumActions action) {
75             action.playSound(type);
76         }
77     }
78
79     public static void playSoundWithId(@Nullable ThingActions actions, int soundId) {
80         if (actions instanceof EcovacsVacuumActions action) {
81             action.playSoundWithId(soundId);
82         }
83     }
84 }