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.haywardomnilogic.internal;
15 import java.util.HashMap;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.haywardomnilogic.internal.handler.HaywardBridgeHandler;
20 import org.openhab.core.library.types.DecimalType;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.library.types.QuantityType;
23 import org.openhab.core.library.types.StringType;
24 import org.openhab.core.library.unit.ImperialUnits;
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.Channel;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.binding.BaseThingHandler;
33 import org.openhab.core.types.Command;
34 import org.openhab.core.types.State;
37 * The {@link HaywardThingHandler} is a subclass of the BaseThingHandler and a Super
38 * Class to each Hayward Thing Handler
40 * @author Matt Myers - Initial contribution
44 public abstract class HaywardThingHandler extends BaseThingHandler {
46 public HaywardThingHandler(Thing thing) {
51 public void initialize() {
52 updateStatus(ThingStatus.ONLINE);
56 public void handleCommand(ChannelUID channelUID, Command command) {
59 public abstract void getTelemetry(String xmlResponse) throws HaywardException;
61 public void setStateDescriptions() throws HaywardException {
64 public State toState(String type, String channelID, String value) throws NumberFormatException {
67 return new DecimalType(value);
69 return OnOffType.from(Integer.parseInt(value) > 0);
70 case "Number:Dimensionless":
72 case HaywardBindingConstants.CHANNEL_CHLORINATOR_AVGSALTLEVEL:
73 return new QuantityType<>(Integer.parseInt(value), Units.PARTS_PER_MILLION);
74 case HaywardBindingConstants.CHANNEL_CHLORINATOR_INSTANTSALTLEVEL:
75 return new QuantityType<>(Integer.parseInt(value), Units.PARTS_PER_MILLION);
76 case HaywardBindingConstants.CHANNEL_CHLORINATOR_TIMEDPERCENT:
77 return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
78 case HaywardBindingConstants.CHANNEL_FILTER_LASTSPEED:
79 return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
80 case HaywardBindingConstants.CHANNEL_FILTER_SPEEDPERCENT:
81 return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
82 case HaywardBindingConstants.CHANNEL_FILTER_SPEEDRPM:
83 case HaywardBindingConstants.CHANNEL_PUMP_LASTSPEED:
84 return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
85 case HaywardBindingConstants.CHANNEL_PUMP_SPEEDPERCENT:
86 return new QuantityType<>(Integer.parseInt(value), Units.PERCENT);
87 case HaywardBindingConstants.CHANNEL_PUMP_SPEEDRPM:
89 return StringType.valueOf(value);
90 case "Number:Temperature":
91 Bridge bridge = getBridge();
93 HaywardBridgeHandler bridgehandler = (HaywardBridgeHandler) bridge.getHandler();
94 if (bridgehandler != null) {
95 if ("Standard".equals(bridgehandler.account.units)) {
96 return new QuantityType<>(Integer.parseInt(value), ImperialUnits.FAHRENHEIT);
98 return new QuantityType<>(Integer.parseInt(value), SIUnits.CELSIUS);
102 // default to imperial if no bridge
103 return new QuantityType<>(Integer.parseInt(value), ImperialUnits.FAHRENHEIT);
105 return StringType.valueOf(value);
109 public String cmdToString(Command command) {
110 if (command == OnOffType.OFF) {
112 } else if (command == OnOffType.ON) {
114 } else if (command instanceof DecimalType decimalCommand) {
115 return decimalCommand.toString();
116 } else if (command instanceof QuantityType quantityCommand) {
117 return quantityCommand.format("%1.0f");
119 return command.toString();
123 public Map<String, State> updateData(String channelID, String data) {
124 Map<String, State> channelStates = new HashMap<>();
125 Channel chan = getThing().getChannel(channelID);
127 String acceptedItemType = chan.getAcceptedItemType();
128 if (acceptedItemType != null) {
129 State state = toState(acceptedItemType, channelID, data);
130 updateState(chan.getUID(), state);
131 channelStates.put(channelID, state);
134 return channelStates;