forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[miio] add miot protocol & conditions (openhab#7404)
* [miio] add miot protocol & conditions * Add miot protocol handling openhab#7276 * Add first 2 miot devices (airpurifiers mb3 & ma4) * Add conditional commands * Change brightness to dimmers openhab#4388 * Apply conditions for dimmers * Remove obsolete (pre)parameters from actions Signed-off-by: Marcel Verpaalen <[email protected]>
- Loading branch information
1 parent
a3e4822
commit 49b75f8
Showing
25 changed files
with
2,223 additions
and
215 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
....binding.miio/src/main/java/org/openhab/binding/miio/internal/basic/ActionConditions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* Copyright (c) 2010-2020 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.miio.internal.basic; | ||
|
||
import java.util.Map; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonPrimitive; | ||
|
||
/** | ||
* Conditional Execution of rules | ||
* | ||
* @author Marcel Verpaalen - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class ActionConditions { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ActionConditions.class); | ||
|
||
/** | ||
* Check if it matches the firmware version. | ||
* | ||
* @param condition | ||
* @param deviceVariables | ||
* @param value | ||
* @return value in case firmware is matching, return null if not | ||
*/ | ||
private static @Nullable JsonElement firmwareCheck(MiIoDeviceActionCondition condition, | ||
Map<String, Object> deviceVariables, @Nullable JsonElement value) { | ||
// TODO: placeholder for firmware version check to allow for firmware dependent actions | ||
return value; | ||
} | ||
|
||
/** | ||
* Check if the value is a valid brightness between 1-99. | ||
* If <1 returns Off, if >99 returns On to activate the power On/Off switch | ||
* | ||
* @param value | ||
* @return | ||
*/ | ||
private static @Nullable JsonElement brightness(@Nullable JsonElement value) { | ||
if (value != null && value.isJsonPrimitive() && value.getAsJsonPrimitive().isNumber()) { | ||
int intVal = value.getAsInt(); | ||
if (intVal > 99) { | ||
return new JsonPrimitive("on"); | ||
} | ||
if (intVal < 1) { | ||
return new JsonPrimitive("off"); | ||
} | ||
} else { | ||
LOGGER.debug("Could not parse brightness. Value '{}' is not an int", value); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Check if the value is a valid brightness between 1-99 which can be send to brightness channel. | ||
* If not returns a null | ||
* | ||
* @param value | ||
* @return | ||
*/ | ||
private static @Nullable JsonElement brightnessExists(@Nullable JsonElement value) { | ||
if (value != null && value.isJsonPrimitive() && value.getAsJsonPrimitive().isNumber()) { | ||
int intVal = value.getAsInt(); | ||
if (intVal > 0 && intVal < 99) { | ||
return value; | ||
} | ||
} else { | ||
LOGGER.debug("Could not parse brightness. Value '{}' is not an int", value); | ||
} | ||
return null; | ||
} | ||
|
||
public static @Nullable JsonElement executeAction(MiIoDeviceActionCondition condition, | ||
Map<String, Object> deviceVariables, @Nullable JsonElement value) { | ||
switch (condition.getName().toUpperCase()) { | ||
case "FIRMWARE": | ||
return firmwareCheck(condition, deviceVariables, value); | ||
case "BRIGHTNESSEXISTING": | ||
return brightnessExists(value); | ||
case "BRIGHTNESSONOFF": | ||
return brightness(value); | ||
default: | ||
LOGGER.debug("Condition {} not found. Returning '{}'", condition, | ||
value != null ? value.toString() : ""); | ||
return value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...miio/src/main/java/org/openhab/binding/miio/internal/basic/MiIoDeviceActionCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Copyright (c) 2010-2020 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.miio.internal.basic; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonNull; | ||
import com.google.gson.annotations.Expose; | ||
import com.google.gson.annotations.SerializedName; | ||
|
||
/** | ||
* Mapping actions conditions | ||
* | ||
* @author Marcel Verpaalen - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class MiIoDeviceActionCondition { | ||
|
||
@SerializedName("name") | ||
@Expose | ||
private @Nullable String name; | ||
@SerializedName("parameters") | ||
@Expose | ||
private @Nullable JsonElement parameters; | ||
|
||
public String getName() { | ||
final @Nullable String command = this.name; | ||
return command != null ? command : ""; | ||
} | ||
|
||
public void setName(String command) { | ||
this.name = command; | ||
} | ||
|
||
public JsonElement getParameters() { | ||
final JsonElement parameter = this.parameters; | ||
return parameter != null ? parameter : JsonNull.INSTANCE; | ||
} | ||
|
||
public void setParameters(JsonArray parameters) { | ||
this.parameters = parameters; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "MiIoDeviceActionCondition [condition=" + name + ",parameters=" + getParameters().toString() + "]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.