2 * Copyright (c) 2010-2021 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.boschshc.internal.devices.twinguard;
15 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.*;
17 import javax.measure.quantity.Dimensionless;
18 import javax.measure.quantity.Temperature;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.boschshc.internal.devices.BoschSHCHandler;
22 import org.openhab.binding.boschshc.internal.devices.twinguard.dto.AirQualityLevelState;
23 import org.openhab.core.library.types.QuantityType;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.library.unit.SIUnits;
26 import org.openhab.core.library.unit.Units;
27 import org.openhab.core.thing.Bridge;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.RefreshType;
35 import com.google.gson.JsonElement;
36 import com.google.gson.JsonSyntaxException;
39 * The {@link BoschSHCHandler} is responsible for handling commands for the TwinGuard handler.
41 * @author Stefan Kästle - Initial contribution
44 public class BoschTwinguardHandler extends BoschSHCHandler {
46 public BoschTwinguardHandler(Thing thing) {
51 public void handleCommand(ChannelUID channelUID, Command command) {
52 Bridge bridge = this.getBridge();
55 logger.debug("Handle command for: {} - {}", channelUID.getThingUID(), command);
57 if (command instanceof RefreshType) {
58 AirQualityLevelState state = this.getState("AirQualityLevel", AirQualityLevelState.class);
60 updateAirQualityState(state);
64 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Bridge is NUL");
68 void updateAirQualityState(AirQualityLevelState state) {
69 updateState(CHANNEL_TEMPERATURE, new QuantityType<Temperature>(state.temperature, SIUnits.CELSIUS));
70 updateState(CHANNEL_TEMPERATURE_RATING, new StringType(state.temperatureRating));
71 updateState(CHANNEL_HUMIDITY, new QuantityType<Dimensionless>(state.humidity, Units.PERCENT));
72 updateState(CHANNEL_HUMIDITY_RATING, new StringType(state.humidityRating));
73 updateState(CHANNEL_PURITY, new QuantityType<Dimensionless>(state.purity, Units.PARTS_PER_MILLION));
74 updateState(CHANNEL_AIR_DESCRIPTION, new StringType(state.description));
75 updateState(CHANNEL_PURITY_RATING, new StringType(state.purityRating));
76 updateState(CHANNEL_COMBINED_RATING, new StringType(state.combinedRating));
80 public void processUpdate(String id, JsonElement state) throws JsonSyntaxException {
81 logger.debug("Twinguard: received update: {} {}", id, state);
83 AirQualityLevelState parsed = GSON.fromJson(state, AirQualityLevelState.class);
85 logger.warn("Received unknown update in in-wall switch: {}", state);
89 logger.debug("Parsed switch state of {}: {}", this.getBoschID(), parsed);
90 updateAirQualityState(parsed);