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
14 package org.openhab.binding.haywardomnilogic.internal;
16 import java.util.HashMap;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.haywardomnilogic.internal.handler.HaywardBridgeHandler;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.library.types.QuantityType;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.library.unit.ImperialUnits;
26 import org.openhab.core.library.unit.SIUnits;
27 import org.openhab.core.library.unit.Units;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.Channel;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.binding.BaseThingHandler;
34 import org.openhab.core.types.Command;
35 import org.openhab.core.types.State;
38 * The {@link HaywarThingHandler} is a subclass of the BaseThingHandler and a Super
39 * Class to each Hayward Thing Handler
41 * @author Matt Myers - Initial contribution
45 public abstract class HaywardThingHandler extends BaseThingHandler {
47 public HaywardThingHandler(Thing thing) {
52 public void initialize() {
53 updateStatus(ThingStatus.ONLINE);
57 public void handleCommand(ChannelUID channelUID, Command command) {
60 public abstract void getTelemetry(String xmlResponse) throws HaywardException;
62 public void setStateDescriptions() throws HaywardException {
65 public State toState(String type, String channelID, String value) throws NumberFormatException {
68 return new DecimalType(value);
70 return Integer.parseInt(value) > 0 ? OnOffType.ON : OnOffType.OFF;
71 case "Number:Dimensionless":
73 case HaywardBindingConstants.CHANNEL_CHLORINATOR_AVGSALTLEVEL:
74 return new QuantityType<>(Integer.parseInt(value), Units.PARTS_PER_MILLION);
75 case HaywardBindingConstants.CHANNEL_CHLORINATOR_INSTANTSALTLEVEL:
76 return new QuantityType<>(Integer.parseInt(value), Units.PARTS_PER_MILLION);
77 case HaywardBindingConstants.CHANNEL_CHLORINATOR_TIMEDPERCENT:
78 return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
79 case HaywardBindingConstants.CHANNEL_FILTER_LASTSPEED:
80 return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
81 case HaywardBindingConstants.CHANNEL_FILTER_SPEEDPERCENT:
82 return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
83 case HaywardBindingConstants.CHANNEL_FILTER_SPEEDRPM:
84 case HaywardBindingConstants.CHANNEL_PUMP_LASTSPEED:
85 return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
86 case HaywardBindingConstants.CHANNEL_PUMP_SPEEDPERCENT:
87 return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
88 case HaywardBindingConstants.CHANNEL_PUMP_SPEEDRPM:
90 return StringType.valueOf(value);
91 case "Number:Temperature":
92 Bridge bridge = getBridge();
94 HaywardBridgeHandler bridgehandler = (HaywardBridgeHandler) bridge.getHandler();
95 if (bridgehandler != null) {
96 if ("Standard".equals(bridgehandler.account.units)) {
97 return new QuantityType<>(Integer.parseInt(value), ImperialUnits.FAHRENHEIT);
99 return new QuantityType<>(Integer.parseInt(value), SIUnits.CELSIUS);
103 // default to imperial if no bridge
104 return new QuantityType<>(Integer.parseInt(value), ImperialUnits.FAHRENHEIT);
106 return StringType.valueOf(value);
110 public String cmdToString(Command command) {
111 if (command == OnOffType.OFF) {
113 } else if (command == OnOffType.ON) {
115 } else if (command instanceof DecimalType decimalCommand) {
116 return decimalCommand.toString();
117 } else if (command instanceof QuantityType quantityCommand) {
118 return quantityCommand.format("%1.0f");
120 return command.toString();
124 public Map<String, State> updateData(String channelID, String data) {
125 Map<String, State> channelStates = new HashMap<>();
126 Channel chan = getThing().getChannel(channelID);
128 String acceptedItemType = chan.getAcceptedItemType();
129 if (acceptedItemType != null) {
130 State state = toState(acceptedItemType, channelID, data);
131 updateState(chan.getUID(), state);
132 channelStates.put(channelID, state);
135 return channelStates;