Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2217 from steintore/master
Browse files Browse the repository at this point in the history
Samsung Air Conditioner fix for #2182.
  • Loading branch information
teichsta committed Mar 3, 2015
2 parents 45b4158 + 5de765d commit 777ab73
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,13 @@ public void shouldCheckIfItIsCorrectResponseCommand() {

@Test
public void shouldParseXMLStatusResponseCorrectly() throws SAXException {
String response = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><Response Type=\"DeviceState\" Status=\"Okay\"><DeviceState><Device DUID=\"7825AD1243BA\" GroupID=\"AC\" ModelID=\"AC\" ><Attr ID=\"AC_FUN_ENABLE\" Type=\"RW\" Value=\"Enable\"/><Attr ID=\"AC_FUN_POWER\" Type=\"RW\" Value=\"On\"/><Attr ID=\"AC_FUN_SUPPORTED\" Type=\"R\" Value=\"0\"/><Attr ID=\"AC_FUN_OPMODE\" Type=\"RW\" Value=\"Heat\"/><Attr ID=\"AC_FUN_TEMPSET\" Type=\"RW\" Value=\"20\"/><Attr ID=\"AC_FUN_COMODE\" Type=\"RW\" Value=\"Off\"/><Attr ID=\"AC_FUN_ERROR\" Type=\"RW\" Value=\"00000000\"/><Attr ID=\"AC_FUN_TEMPNOW\" Type=\"R\" Value=\"21\"/><Attr ID=\"AC_FUN_SLEEP\" Type=\"RW\" Value=\"0\"/><Attr ID=\"AC_FUN_WINDLEVEL\" Type=\"RW\" Value=\"Auto\"/><Attr ID=\"AC_FUN_DIRECTION\" Type=\"RW\" Value=\"Fixed\"/><Attr ID=\"AC_ADD_AUTOCLEAN\" Type=\"RW\" Value=\"Off\"/><Attr ID=\"AC_ADD_APMODE_END\" Type=\"W\" Value=\"0\"/><Attr ID=\"AC_ADD_STARTWPS\" Type=\"RW\" Value=\"Direct\"/><Attr ID=\"AC_ADD_SPI\" Type=\"RW\" Value=\"Off\"/><Attr ID=\"AC_SG_WIFI\" Type=\"W\" Value=\"Connected\"/><Attr ID=\"AC_SG_INTERNET\" Type=\"W\" Value=\"Connected\"/><Attr ID=\"AC_ADD2_VERSION\" Type=\"RW\" Value=\"0\"/><Attr ID=\"AC_SG_MACHIGH\" Type=\"W\" Value=\"0\"/><Attr ID=\"AC_SG_MACMID\" Type=\"W\" Value=\"0\"/><Attr ID=\"AC_SG_MACLOW\" Type=\"W\" Value=\"0\"/><Attr ID=\"AC_SG_VENDER01\" Type=\"W\" Value=\"0\"/><Attr ID=\"AC_SG_VENDER02\" Type=\"W\" Value=\"0\"/><Attr ID=\"AC_SG_VENDER03\" Type=\"W\" Value=\"0\"/></Device></DeviceState></Response>";
String response = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><Response Type=\"DeviceState\" Status=\"Okay\"><DeviceState><Device DUID=\"7825AD1243BA\" GroupID=\"AC\" ModelID=\"AC\" ><Attr ID=\"AC_FUN_ENABLE\" Type=\"RW\" Value=\"Enable\"/><Attr ID=\"AC_FUN_POWER\" Type=\"RW\" Value=\"On\"/><Attr ID=\"AC_FUN_SUPPORTED\" Type=\"R\" Value=\"0\"/><Attr ID=\"AC_FUN_OPMODE\" Type=\"RW\" Value=\"Heat\"/><Attr ID=\"AC_FUN_TEMPSET\" Type=\"RW\" Value=\"20\"/><Attr ID=\"AC_FUN_COMODE\" Type=\"RW\" Value=\"Off\"/><Attr ID=\"AC_FUN_ERROR\" Type=\"RW\" Value=\"45010D00\"/><Attr ID=\"AC_FUN_TEMPNOW\" Type=\"R\" Value=\"21\"/><Attr ID=\"AC_FUN_SLEEP\" Type=\"RW\" Value=\"0\"/><Attr ID=\"AC_FUN_WINDLEVEL\" Type=\"RW\" Value=\"Auto\"/><Attr ID=\"AC_FUN_DIRECTION\" Type=\"RW\" Value=\"Fixed\"/><Attr ID=\"AC_ADD_AUTOCLEAN\" Type=\"RW\" Value=\"Off\"/><Attr ID=\"AC_ADD_APMODE_END\" Type=\"W\" Value=\"0\"/><Attr ID=\"AC_ADD_STARTWPS\" Type=\"RW\" Value=\"Direct\"/><Attr ID=\"AC_ADD_SPI\" Type=\"RW\" Value=\"Off\"/><Attr ID=\"AC_SG_WIFI\" Type=\"W\" Value=\"Connected\"/><Attr ID=\"AC_SG_INTERNET\" Type=\"W\" Value=\"Connected\"/><Attr ID=\"AC_ADD2_VERSION\" Type=\"RW\" Value=\"0\"/><Attr ID=\"AC_SG_MACHIGH\" Type=\"W\" Value=\"0\"/><Attr ID=\"AC_SG_MACMID\" Type=\"W\" Value=\"0\"/><Attr ID=\"AC_SG_MACLOW\" Type=\"W\" Value=\"0\"/><Attr ID=\"AC_SG_VENDER01\" Type=\"W\" Value=\"0\"/><Attr ID=\"AC_SG_VENDER02\" Type=\"W\" Value=\"0\"/><Attr ID=\"AC_SG_VENDER03\" Type=\"W\" Value=\"0\"/></Device></DeviceState></Response>";
Map<CommandEnum,String> result = ResponseParser.parseStatusResponse(response);

assertEquals("On", result.get(CommandEnum.AC_FUN_POWER));
assertEquals("21", result.get(CommandEnum.AC_FUN_TEMPNOW));
assertEquals("20", result.get(CommandEnum.AC_FUN_TEMPSET));
assertEquals("45010D00", result.get(CommandEnum.AC_FUN_ERROR));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
public interface SamsungAcBindingProvider extends BindingProvider {
String getAirConditionerInstance(String itemname);
CommandEnum getProperty(String itemname);
String getItemName(CommandEnum property);
String getItemName(String acName, CommandEnum property);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,21 @@
* @since 1.6.0
*/
public enum CommandEnum {
AC_FUN_OPMODE, AC_FUN_TEMPSET, AC_FUN_WINDLEVEL, AC_FUN_TEMPNOW, AC_FUN_POWER, AC_FUN_COMODE, AC_FUN_DIRECTION, AC_FUN_ENABLE, AC_FUN_SLEEP;

AC_FUN_OPMODE, AC_FUN_TEMPSET, AC_FUN_WINDLEVEL,
AC_FUN_TEMPNOW, AC_FUN_POWER, AC_FUN_COMODE,
AC_FUN_DIRECTION, AC_FUN_ENABLE, AC_FUN_SLEEP,
AC_FUN_ERROR, AC_SG_VENDER01, AC_SG_VENDER02, AC_SG_VENDER03,
AC_ADD2_VERSION, AC_SG_INTERNET,
AC_SG_WIFI, AC_ADD_SPI, AC_ADD_STARTWPS, AC_ADD_APMODE_END,
AC_ADD_AUTOCLEAN, AC_FUN_SUPPORTED,
AC_ADD_SETKWH, AC_ADD_CLEAR_FILTER_ALARM,
AC_OUTDOOR_TEMP, AC_COOL_CAPABILITY,
AC_WARM_CAPABILITY, AC_ADD2_USEDWATT,
AC_SG_MACHIGH, AC_SG_MACMID, AC_SG_MACLOW,
AC_ADD2_PANEL_VERSION, AC_ADD2_OUT_VERSION,
AC_FUN_MODEL, AC_ADD2_OPTIONCODE, AC_ADD2_USEDPOWER,
AC_ADD2_USEDTIME, AC_ADD2_CLEAR_POWERTIME, AC_ADD2_FILTERTIME,
AC_ADD2_FILTER_USE_TIME
}

enum ConvenientModeEnum {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.binding.openhab.samsungac.communicator.AirConditioner;
import org.binding.openhab.samsungac.communicator.SsdpDiscovery;
import org.openhab.binding.samsungac.SamsungAcBindingProvider;
import org.openhab.core.binding.AbstractActiveBinding;
import org.openhab.core.binding.BindingProvider;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.osgi.service.cm.ConfigurationException;
Expand Down Expand Up @@ -99,6 +101,8 @@ private String getCmdStringFromEnumValue(Command command,
String cmd = null;
switch (property) {
case AC_FUN_POWER:
case AC_ADD_SPI:
case AC_ADD_AUTOCLEAN:
cmd = "ON".equals(command.toString()) ? "On" : "Off";
break;
case AC_FUN_WINDLEVEL:
Expand All @@ -114,8 +118,7 @@ private String getCmdStringFromEnumValue(Command command,
cmd = DirectionEnum.getFromValue(command).toString();
break;
case AC_FUN_TEMPSET:
cmd = command.toString();
break;
case AC_FUN_ERROR:
default:
cmd = command.toString();
break;
Expand Down Expand Up @@ -159,11 +162,11 @@ private CommandEnum getProperty(String itemName) {
return null;
}

private String getItemName(CommandEnum property) {
private String getItemName(String acName, CommandEnum property) {
for (BindingProvider provider : providers) {
if (provider instanceof SamsungAcBindingProvider) {
SamsungAcBindingProvider acProvider = (SamsungAcBindingProvider) provider;
return acProvider.getItemName(property);
return acProvider.getItemName(acName, property);
}
}
return null;
Expand All @@ -176,9 +179,18 @@ public void updated(Dictionary<String, ?> config)
throws ConfigurationException {
Enumeration<String> keys = config.keys();

String refreshIntervalString = (String) config.get("refresh");
if (StringUtils.isNotBlank(refreshIntervalString)) {
refreshInterval = Long.parseLong(refreshIntervalString);
logger.info("Refresh interval set to " + refreshIntervalString + " ms");
} else {
logger.info("No refresh interval configured, using default: " + refreshInterval + " ms");
}

Map<String, AirConditioner> hosts = new HashMap<String, AirConditioner>();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
logger.debug("Configuration key is: " + key);
if ("service.pid".equals(key)) {
continue;
}
Expand Down Expand Up @@ -236,8 +248,9 @@ protected void execute() {
for (Map.Entry<String, AirConditioner> entry : nameHostMapper
.entrySet()) {
AirConditioner host = entry.getValue();
String acName = entry.getKey();
if (host.isConnected()) {
getAndUpdateStatusForAirConditioner(host);
getAndUpdateStatusForAirConditioner(acName, host);
} else {
reconnectToAirConditioner(entry.getKey(), host);
}
Expand All @@ -258,17 +271,19 @@ private void reconnectToAirConditioner(String key, AirConditioner host) {
}
}

private void getAndUpdateStatusForAirConditioner(AirConditioner host) {
private void getAndUpdateStatusForAirConditioner(String acName, AirConditioner host) {
Map<CommandEnum, String> status = new HashMap<CommandEnum, String>();
try {
logger.debug("Getting status for ac: '" + acName + "'");
status = host.getStatus();
} catch (Exception e) {
logger.debug("Could not get status.. returning..");
return;
}

for (CommandEnum cmd : status.keySet()) {
String item = getItemName(cmd);
logger.debug("Trying to find item for: " + acName + " and cmd: " + cmd.toString());
String item = getItemName(acName, cmd);
String value = status.get(cmd);
if (item != null && value != null) {
updateItemWithValue(cmd, item, value);
Expand All @@ -283,6 +298,8 @@ private void updateItemWithValue(CommandEnum cmd, String item, String value) {
postUpdate(item, DecimalType.valueOf(value));
break;
case AC_FUN_POWER:
case AC_ADD_SPI:
case AC_ADD_AUTOCLEAN:
postUpdate(
item,
value.toUpperCase().equals("ON") ? OnOffType.ON
Expand Down Expand Up @@ -312,9 +329,9 @@ private void updateItemWithValue(CommandEnum cmd, String item, String value) {
.toString(DirectionEnum
.valueOf(value).value)));
break;
case AC_FUN_ERROR:
default:
logger.debug("Not implementation for updating: '"
+ cmd + "'");
postUpdate(item, StringType.valueOf(value));
break;
}
}
Expand All @@ -323,6 +340,8 @@ private void postUpdate(String item, State state) {
if (item != null && state != null) {
logger.debug(item + " gets updated to: " + state);
eventPublisher.postUpdate(item, state);
} else {
logger.debug("Could not update item: '" + item + "' with state: '" + state.toString() + "'");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.openhab.core.binding.BindingConfig;
import org.openhab.core.items.Item;
import org.openhab.core.library.items.NumberItem;
import org.openhab.core.library.items.StringItem;
import org.openhab.core.library.items.SwitchItem;
import org.openhab.model.item.binding.AbstractGenericBindingProvider;
import org.openhab.model.item.binding.BindingConfigParseException;
Expand All @@ -34,6 +35,7 @@
* Number ac_set_temp "Set temp [%.1f]" {samsungac="[<AC_NAME>|AC_FUN_TEMPSET]"}
* Number ac_direction "Direction" {samsungac="[<AC_NAME>|AC_FUN_DIRECTION]"}
* Number ac_windlevel "Windlevel" {samsungac="[<AC_NAME>|AC_FUN_WINDLEVEL]"}
* String ac_error "Error" {samsungac="[<AC_NAME>|AC_FUN_ERROR]"}
* </pre>
*
* @author Stein Tore Tøsse
Expand All @@ -60,10 +62,10 @@ public String getBindingType() {
*/
public void validateItemType(Item item, String bindingConfig)
throws BindingConfigParseException {
if (!(item instanceof SwitchItem) && !(item instanceof NumberItem)) {
if (!(item instanceof SwitchItem) && !(item instanceof NumberItem) && !(item instanceof StringItem)) {
throw new BindingConfigParseException("item '" + item.getName()
+ "' is of type '" + item.getClass().getSimpleName()
+ "', but only Number and Switchs items are allowed.");
+ "', but only Number, Strings and Switchs items are allowed.");
}
}

Expand Down Expand Up @@ -93,17 +95,17 @@ private SamsungAcBindingConfig parseBindingConfig(Item item,
return new SamsungAcBindingConfig(acInstance, item.getName(), property);
}

public BindingConfig getItem(CommandEnum property) {
public BindingConfig getItem(String acName, CommandEnum property) {
for (BindingConfig config : bindingConfigs.values()) {
SamsungAcBindingConfig con = (SamsungAcBindingConfig) config;
if (property.equals(con.getProperty()))
if (property.equals(con.getProperty()) && con.acInstance.equals(acName) )
return con;
}
return null;
}

public String getItemName(CommandEnum property) {
SamsungAcBindingConfig con = (SamsungAcBindingConfig) getItem(property);
public String getItemName(String acName, CommandEnum property) {
SamsungAcBindingConfig con = (SamsungAcBindingConfig) getItem(acName, property);
if (con != null && property.equals(con.getProperty()))
return con.getItemName();
return null;
Expand Down

0 comments on commit 777ab73

Please sign in to comment.