2 * Copyright (c) 2010-2023 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.mystrom.internal;
15 import static org.openhab.binding.mystrom.internal.MyStromBindingConstants.*;
16 import static org.openhab.core.library.unit.SIUnits.CELSIUS;
17 import static org.openhab.core.library.unit.Units.PERCENT;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.eclipse.jetty.http.HttpMethod;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.types.Command;
31 import com.google.gson.JsonParseException;
35 * @author Stefan Navratil - Initial Contribution
40 public class MyStromPIRHandler extends AbstractMyStromHandler {
42 private static class MyStromReport {
45 public boolean motion;
46 public float temperature;
49 public MyStromPIRHandler(Thing thing, HttpClient httpClient) {
50 super(thing, httpClient);
52 sendHttpRequest(HttpMethod.POST, "/api/v1/settings/pir",
53 "{\"backoff_time\":" + config.getBackoffTime() + ",\"led_enable\":" + config.getLedEnable() + "}");
54 } catch (MyStromException e) {
55 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
60 public void handleCommand(ChannelUID channelUID, Command command) {
64 protected void pollDevice() {
65 MyStromReport report = getReport();
67 updateState(CHANNEL_MOTION, OnOffType.from(report.motion));
68 updateState(CHANNEL_TEMPERATURE, QuantityType.valueOf(report.temperature, CELSIUS));
69 // The Default Light thresholds are from 30 to 300.
70 updateState(CHANNEL_LIGHT, QuantityType.valueOf(report.light / 3, PERCENT));
74 private @Nullable MyStromReport getReport() {
76 String json = sendHttpRequest(HttpMethod.GET, "/api/v1/sensors", null);
77 MyStromReport report = gson.fromJson(json, MyStromReport.class);
78 updateStatus(ThingStatus.ONLINE);
80 } catch (MyStromException | JsonParseException e) {
81 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());