2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.shelly.internal.api2;
15 import static org.openhab.binding.shelly.internal.ShellyBindingConstants.*;
16 import static org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.*;
17 import static org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.*;
18 import static org.openhab.binding.shelly.internal.util.ShellyUtils.*;
20 import java.util.ArrayList;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.shelly.internal.api.ShellyApiException;
26 import org.openhab.binding.shelly.internal.api.ShellyDeviceProfile;
27 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyInputState;
28 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySensorSleepMode;
29 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySensorTmp;
30 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsDevice;
31 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsInput;
32 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellySettingsStatus;
33 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyStatusSensor;
34 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyStatusSensor.ShellySensorAccel;
35 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyStatusSensor.ShellySensorBat;
36 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyStatusSensor.ShellySensorHum;
37 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyStatusSensor.ShellySensorLux;
38 import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyStatusSensor.ShellySensorState;
39 import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2NotifyEvent;
40 import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2RpcNotifyEvent;
41 import org.openhab.binding.shelly.internal.config.ShellyThingConfiguration;
42 import org.openhab.binding.shelly.internal.handler.ShellyBluSensorHandler;
43 import org.openhab.binding.shelly.internal.handler.ShellyComponents;
44 import org.openhab.binding.shelly.internal.handler.ShellyThingInterface;
45 import org.openhab.binding.shelly.internal.handler.ShellyThingTable;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * {@link ShellyBluApi} implementsBLU interface
52 * @author Markus Michels - Initial contribution
55 public class ShellyBluApi extends Shelly2ApiRpc {
56 private static final Logger logger = LoggerFactory.getLogger(ShellyBluApi.class);
57 private boolean connected = false; // true = BLU devices has connected
58 private ShellySettingsStatus deviceStatus = new ShellySettingsStatus();
59 private int lastPid = -1;
61 private static final Map<String, String> MAP_INPUT_EVENT_TYPE = Map.of( //
62 SHELLY2_EVENT_1PUSH, SHELLY_BTNEVENT_1SHORTPUSH, //
63 SHELLY2_EVENT_2PUSH, SHELLY_BTNEVENT_2SHORTPUSH, //
64 SHELLY2_EVENT_3PUSH, SHELLY_BTNEVENT_3SHORTPUSH, //
65 SHELLY2_EVENT_LPUSH, SHELLY_BTNEVENT_LONGPUSH, //
66 SHELLY2_EVENT_LSPUSH, SHELLY_BTNEVENT_LONGSHORTPUSH, //
67 SHELLY2_EVENT_SLPUSH, SHELLY_BTNEVENT_SHORTLONGPUSH, //
68 "1", SHELLY_BTNEVENT_1SHORTPUSH, //
69 "2", SHELLY_BTNEVENT_2SHORTPUSH, //
70 "3", SHELLY_BTNEVENT_3SHORTPUSH, //
71 "4", SHELLY_BTNEVENT_LONGPUSH);
74 * Regular constructor - called by Thing handler
76 * @param thingName Symbolic thing name
77 * @param thing Thing Handler (ThingHandlerInterface)
79 public ShellyBluApi(String thingName, ShellyThingTable thingTable, ShellyThingInterface thing) {
80 super(thingName, thingTable, thing);
82 ShellyInputState input = new ShellyInputState();
83 deviceStatus.inputs = new ArrayList<>();
87 deviceStatus.inputs.add(input);
91 public void initialize() throws ShellyApiException {
100 public boolean isInitialized() {
105 public void setConfig(String thingName, ShellyThingConfiguration config) {
106 this.thingName = thingName;
107 this.config = config;
111 public ShellySettingsDevice getDeviceInfo() throws ShellyApiException {
112 ShellySettingsDevice info = new ShellySettingsDevice();
113 info.hostname = !config.serviceName.isEmpty() ? config.serviceName : "";
116 info.mac = config.deviceAddress;
123 public ShellyDeviceProfile getDeviceProfile(String thingType, @Nullable ShellySettingsDevice devInfo)
124 throws ShellyApiException {
125 ShellyDeviceProfile profile = thing != null ? getProfile() : new ShellyDeviceProfile();
127 if (devInfo != null) {
128 profile.device = devInfo;
130 profile.isBlu = true;
131 profile.settingsJson = "{}";
132 profile.thingName = thingName;
133 profile.name = getString(profile.settings.name);
134 if (profile.gateway.isEmpty()) {
135 profile.gateway = getThing().getProperty(PROPERTY_GW_DEVICE);
138 profile.device = getDeviceInfo();
139 if (config.serviceName.isEmpty()) {
140 config.serviceName = getString(profile.device.hostname);
143 // for now we have no API to get this information
144 profile.fwDate = profile.fwVersion = profile.status.update.oldVersion = "";
145 profile.status.hasUpdate = profile.status.update.hasUpdate = false;
147 if (profile.hasBattery) {
148 profile.settings.sleepMode = new ShellySensorSleepMode();
149 profile.settings.sleepMode.unit = "m";
150 profile.settings.sleepMode.period = 720;
153 if (profile.isButton) {
154 ShellySettingsInput settings = new ShellySettingsInput();
155 profile.numInputs = 1;
156 settings.btnType = SHELLY_BTNT_MOMENTARY;
158 if (profile.settings.inputs != null) {
159 profile.settings.inputs.set(0, settings);
161 profile.settings.inputs = new ArrayList<>();
162 profile.settings.inputs.add(settings);
164 profile.status = deviceStatus;
168 throw new ShellyApiException("BLU Device not yet connected");
171 profile.initialized = true;
176 public ShellySettingsStatus getStatus() throws ShellyApiException {
178 throw new ShellyApiException("Thing is not yet initialized -> status not available");
184 public ShellyStatusSensor getSensorStatus() throws ShellyApiException {
186 throw new ShellyApiException("Thing is not yet initialized -> sensor data not available");
193 public void onNotifyEvent(Shelly2RpcNotifyEvent message) {
194 logger.trace("{}: ShellyEvent received: {}", thingName, gson.toJson(message));
196 boolean updated = false;
197 ShellyBluSensorHandler t = (ShellyBluSensorHandler) thing;
199 logger.debug("{}: Thing is not initialized -> ignore event", thingName);
204 ShellyDeviceProfile profile = getProfile();
215 for (Shelly2NotifyEvent e : message.params.events) {
216 logger.debug("{}: BluEvent received: {}", thingName, gson.toJson(message));
217 String event = getString(e.event);
218 if (event.startsWith(SHELLY2_EVENT_BLUPREFIX)) {
219 logger.debug("{}: BLU event {} received from address {}, pid={}", thingName, event,
220 getString(e.data.addr), getInteger(e.data.pid));
221 if (e.data.pid != null) {
222 int pid = e.data.pid;
223 if (pid == lastPid) {
224 logger.debug("{}: Duplicate packet for PID={} received, ignore", thingName, pid);
229 getThing().getProfile().gateway = message.src;
233 case SHELLY2_EVENT_BLUSCAN:
234 if (e.data == null || e.data.addr == null) {
235 logger.debug("{}: Inconsistent BLU scan result ignored: {}", thingName,
236 gson.toJson(message));
239 logger.debug("{}: BLU Device discovered", thingName);
240 if (e.data.name != null) {
241 profile.settings.name = ShellyDeviceProfile.buildBluServiceName(e.data.name, e.data.addr);
244 case SHELLY2_EVENT_BLUDATA:
245 if (e.data == null || e.data.addr == null || e.data.pid == null) {
246 logger.debug("{}: Inconsistent BLU packet ignored: {}", thingName, gson.toJson(message));
250 if (e.data.battery != null) {
251 if (sensorData.bat == null) {
252 sensorData.bat = new ShellySensorBat();
254 sensorData.bat.value = (double) e.data.battery;
256 if (e.data.rssi != null) {
257 deviceStatus.wifiSta.rssi = e.data.rssi;
259 if (e.data.windowState != null) {
260 if (sensorData.sensor == null) {
261 sensorData.sensor = new ShellySensorState();
263 sensorData.sensor.isValid = true;
264 sensorData.sensor.state = e.data.windowState == 1 ? SHELLY_API_DWSTATE_OPEN
265 : SHELLY_API_DWSTATE_CLOSE;
267 if (e.data.illuminance != null) {
268 if (sensorData.lux == null) {
269 sensorData.lux = new ShellySensorLux();
271 sensorData.lux.isValid = true;
272 sensorData.lux.value = (double) e.data.illuminance;
274 if (e.data.temperature != null) {
275 if (sensorData.tmp == null) {
276 sensorData.tmp = new ShellySensorTmp();
278 sensorData.tmp.units = SHELLY_TEMP_CELSIUS;
279 sensorData.tmp.tC = e.data.temperature;
280 sensorData.tmp.isValid = true;
282 if (e.data.humidity != null) {
283 if (sensorData.hum == null) {
284 sensorData.hum = new ShellySensorHum();
286 sensorData.hum.value = e.data.humidity;
288 if (e.data.rotation != null) {
289 if (sensorData.accel == null) {
290 sensorData.accel = new ShellySensorAccel();
292 sensorData.accel.tilt = e.data.rotation.intValue();
294 if (e.data.motionState != null) {
295 sensorData.motion = e.data.motionState == 1;
298 if (e.data.buttonEvent != null) {
299 ShellyInputState input = deviceStatus.inputs != null ? deviceStatus.inputs.get(0)
300 : new ShellyInputState();
301 input.event = mapValue(MAP_INPUT_EVENT_TYPE, e.data.buttonEvent + "");
303 deviceStatus.inputs.set(0, input);
304 // sensorData.inputs.set(0, input);
306 String group = getProfile().getInputGroup(0);
307 String suffix = profile.getInputSuffix(0);
308 t.updateChannel(group, CHANNEL_STATUS_EVENTTYPE + suffix, getStringType(input.event));
309 t.updateChannel(group, CHANNEL_STATUS_EVENTCOUNT + suffix, getDecimal(input.eventCount));
310 t.triggerButton(profile.getInputGroup(0), 0, input.event);
312 updated |= ShellyComponents.updateDeviceStatus(t, deviceStatus);
313 updated |= ShellyComponents.updateSensors(getThing(), deviceStatus);
316 super.onNotifyEvent(message);
319 } catch (ShellyApiException e) {
320 logger.debug("{}: Unable to process event", thingName, e);