fix relay id for devices with more than 1 power meter, fix (#15247)

initialization if device doesn't support scripts (Pro4); fix resetMeter
for EM50 (has 2 power meters); removed channel sensors#open; removed
device#voltage updates for Plus/Pro, needs to be verified

Signed-off-by: Markus Michels <markus7017@gmail.com>
This commit is contained in:
Markus Michels 2023-07-16 14:38:40 +02:00 committed by GitHub
parent 4c3b398398
commit 00da6457f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 142 additions and 146 deletions

View File

@ -173,6 +173,7 @@ public class ShellyBindingConstants {
public static final String CHANNEL_EMETER_VOLTAGE = "voltage";
public static final String CHANNEL_EMETER_CURRENT = "current";
public static final String CHANNEL_EMETER_PFACTOR = "powerFactor";
public static final String CHANNEL_EMETER_RESETTOTAL = "resetTotals";
public static final String CHANNEL_GROUP_SENSOR = "sensors";
public static final String CHANNEL_SENSOR_TEMP = "temperature";
@ -187,7 +188,6 @@ public class ShellyBindingConstants {
public static final String CHANNEL_SENSOR_SMOKE = "smoke";
public static final String CHANNEL_SENSOR_MUTE = "mute";
public static final String CHANNEL_SENSOR_STATE = "state";
public static final String CHANNEL_SENSOR_OPEN = "open";
public static final String CHANNEL_SENSOR_VALVE = "valve";
public static final String CHANNEL_SENSOR_SSTATE = "status"; // Shelly Gas
public static final String CHANNEL_SENSOR_MOTION_ACT = "motionActive";
@ -260,7 +260,8 @@ public class ShellyBindingConstants {
public static final String CHANNEL_DEVST_ACCUWATTS = "accumulatedWatts";
public static final String CHANNEL_DEVST_ACCUTOTAL = "accumulatedWTotal";
public static final String CHANNEL_DEVST_ACCURETURNED = "accumulatedReturned";
public static final String CHANNEL_DEVST_RESETTOTAL = "resetTotals";
public static final String CHANNEL_DEVST_RESETTOTAL = CHANNEL_EMETER_RESETTOTAL;
public static final String CHANNEL_DEVST_CHARGER = "charger";
public static final String CHANNEL_DEVST_UPDATE = "updateAvailable";
public static final String CHANNEL_DEVST_SELFTTEST = "selfTest";

View File

@ -264,11 +264,6 @@ public class Shelly2ApiClient extends ShellyHttpClient {
}
}
if (rs.voltage != null) {
if (status.voltage == null || rs.voltage > status.voltage) {
status.voltage = rs.voltage;
}
}
if (rs.errors != null) {
for (String error : rs.errors) {
sr.overpower = rstatus.overpower = SHELLY2_ERROR_OVERPOWER.equals(error);
@ -318,8 +313,8 @@ public class Shelly2ApiClient extends ShellyHttpClient {
if (relay.isValid && relay.id != null && relay.id.intValue() == id.intValue()) {
return idx;
}
idx++;
}
idx++;
}
return -1;
}
@ -330,7 +325,8 @@ public class Shelly2ApiClient extends ShellyHttpClient {
return;
}
sm.isValid = sm.power != null || sm.total != null;
emeter.isValid = emeter.current != null || emeter.voltage != null || emeter.power != null;
emeter.isValid = emeter.current != null || emeter.voltage != null || emeter.power != null
|| emeter.total != null;
status.meters.set(id, sm);
status.emeters.set(id, emeter);
relayStatus.meters.set(id, sm);
@ -548,9 +544,6 @@ public class Shelly2ApiClient extends ShellyHttpClient {
}
}
if (cs.voltage != null) {
if (status.voltage == null || cs.voltage > status.voltage) {
status.voltage = cs.voltage;
}
emeter.voltage = cs.voltage;
}
if (cs.current != null) {

View File

@ -365,122 +365,133 @@ public class Shelly2ApiRpc extends Shelly2ApiClient implements ShellyApiInterfac
}
protected void installScript(String script, boolean install) throws ShellyApiException {
ShellyScriptListResponse scriptList = apiRequest(
new Shelly2RpcRequest().withMethod(SHELLYRPC_METHOD_SCRIPT_LIST), ShellyScriptListResponse.class);
Integer ourId = -1;
String code = "";
try {
ShellyScriptListResponse scriptList = apiRequest(
new Shelly2RpcRequest().withMethod(SHELLYRPC_METHOD_SCRIPT_LIST), ShellyScriptListResponse.class);
Integer ourId = -1;
String code = "";
if (install) {
logger.debug("{}: Install or restart script {} on Shelly Device", thingName, script);
}
boolean running = false, upload = false;
for (ShellyScriptListEntry s : scriptList.scripts) {
if (s.name.startsWith(script)) {
ourId = s.id;
running = s.running;
logger.debug("{}: Script {} is already installed, id={}", thingName, script, ourId);
break;
if (install) {
logger.debug("{}: Install or restart script {} on Shelly Device", thingName, script);
}
}
if (!install) {
if (ourId != -1) {
startScript(ourId, false);
enableScript(script, false);
logger.debug("{}: Script {} was disabledd, id={}", thingName, script, ourId);
}
return;
}
// get script code from bundle resources
String file = BUNDLE_RESOURCE_SCRIPTS + "/" + script;
ClassLoader cl = Shelly2ApiRpc.class.getClassLoader();
if (cl != null) {
try (InputStream inputStream = cl.getResourceAsStream(file)) {
if (inputStream != null) {
code = new BufferedReader(new InputStreamReader(inputStream)).lines()
.collect(Collectors.joining("\n"));
boolean running = false, upload = false;
for (ShellyScriptListEntry s : scriptList.scripts) {
if (s.name.startsWith(script)) {
ourId = s.id;
running = s.running;
logger.debug("{}: Script {} is already installed, id={}", thingName, script, ourId);
break;
}
} catch (IOException | UncheckedIOException e) {
logger.debug("{}: Installation of script {} failed: Unable to read {} from bundle resources!",
thingName, script, file, e);
}
}
boolean restart = false;
if (ourId == -1) {
// script not installed -> install it
upload = true;
} else {
try {
// verify that the same code version is active (avoid unnesesary flash updates)
ShellyScriptResponse rsp = apiRequest(
new Shelly2RpcRequest().withMethod(SHELLYRPC_METHOD_SCRIPT_GETCODE).withId(ourId),
ShellyScriptResponse.class);
if (!rsp.data.trim().equals(code.trim())) {
logger.debug("{}: A script version was found, update to newest one", thingName);
if (!install) {
if (ourId != -1) {
startScript(ourId, false);
enableScript(script, false);
logger.debug("{}: Script {} was disabledd, id={}", thingName, script, ourId);
}
return;
}
// get script code from bundle resources
String file = BUNDLE_RESOURCE_SCRIPTS + "/" + script;
ClassLoader cl = Shelly2ApiRpc.class.getClassLoader();
if (cl != null) {
try (InputStream inputStream = cl.getResourceAsStream(file)) {
if (inputStream != null) {
code = new BufferedReader(new InputStreamReader(inputStream)).lines()
.collect(Collectors.joining("\n"));
}
} catch (IOException | UncheckedIOException e) {
logger.debug("{}: Installation of script {} failed: Unable to read {} from bundle resources!",
thingName, script, file, e);
}
}
boolean restart = false;
if (ourId == -1) {
// script not installed -> install it
upload = true;
} else {
try {
// verify that the same code version is active (avoid unnesesary flash updates)
ShellyScriptResponse rsp = apiRequest(
new Shelly2RpcRequest().withMethod(SHELLYRPC_METHOD_SCRIPT_GETCODE).withId(ourId),
ShellyScriptResponse.class);
if (!rsp.data.trim().equals(code.trim())) {
logger.debug("{}: A script version was found, update to newest one", thingName);
upload = true;
} else {
logger.debug("{}: Same script version was found, restart", thingName);
restart = true;
}
} catch (ShellyApiException e) {
logger.debug("{}: Unable to read current script code -> force update (deviced returned: {})",
thingName, e.getMessage());
upload = true;
} else {
logger.debug("{}: Same script version was found, restart", thingName);
restart = true;
}
} catch (ShellyApiException e) {
logger.debug("{}: Unable to read current script code -> force update (deviced returned: {})", thingName,
e.getMessage());
}
if (restart || (running && upload)) {
// first stop running script
startScript(ourId, false);
running = false;
}
if (upload && ourId != -1) {
// Delete existing script
logger.debug("{}: Delete existing script", thingName);
apiRequest(new Shelly2RpcRequest().withMethod(SHELLYRPC_METHOD_SCRIPT_DELETE).withId(ourId));
}
if (upload) {
logger.debug("{}: Script will be installed...", thingName);
// Create new script, get id
ShellyScriptResponse rsp = apiRequest(
new Shelly2RpcRequest().withMethod(SHELLYRPC_METHOD_SCRIPT_CREATE).withName(script),
ShellyScriptResponse.class);
ourId = rsp.id;
logger.debug("{}: Script has been created, id={}", thingName, ourId);
upload = true;
}
}
if (restart || (running && upload)) {
// first stop running script
startScript(ourId, false);
running = false;
}
if (upload && ourId != -1) {
// Delete existing script
logger.debug("{}: Delete existing script", thingName);
apiRequest(new Shelly2RpcRequest().withMethod(SHELLYRPC_METHOD_SCRIPT_DELETE).withId(ourId));
}
if (upload) {
// Put script code for generated id
ShellyScriptPutCodeParams parms = new ShellyScriptPutCodeParams();
parms.id = ourId;
parms.append = false;
int length = code.length(), processed = 0, chunk = 1;
do {
int nextlen = Math.min(1024, length - processed);
parms.code = code.substring(processed, processed + nextlen);
logger.debug("{}: Uploading chunk {} of script (total {} chars, {} processed)", thingName, chunk,
length, processed);
apiRequest(SHELLYRPC_METHOD_SCRIPT_PUTCODE, parms, String.class);
processed += nextlen;
chunk++;
parms.append = true;
} while (processed < length);
running = false;
}
if (enableScript(script, true) && upload) {
logger.info("{}: Script {} was {} installed successful", thingName, thingName, script);
}
if (upload) {
logger.debug("{}: Script will be installed...", thingName);
// Create new script, get id
ShellyScriptResponse rsp = apiRequest(
new Shelly2RpcRequest().withMethod(SHELLYRPC_METHOD_SCRIPT_CREATE).withName(script),
ShellyScriptResponse.class);
ourId = rsp.id;
logger.debug("{}: Script has been created, id={}", thingName, ourId);
upload = true;
if (!running) {
running = startScript(ourId, true);
}
if (!discovery) {
logger.info("{}: Script {} {}", thingName, script,
running ? "was successfully (re)started" : "failed to start");
}
} catch (ShellyApiException e) {
ShellyApiResult res = e.getApiResult();
if (res.httpCode == HttpStatus.NOT_FOUND_404) { // Shely 4Pro
logger.debug("{}: Script {} was not installed, device doesn't support scripts", thingName, script);
} else {
logger.debug("{}: Unable to install script {}: {}", thingName, script, res.toString());
}
}
if (upload) {
// Put script code for generated id
ShellyScriptPutCodeParams parms = new ShellyScriptPutCodeParams();
parms.id = ourId;
parms.append = false;
int length = code.length(), processed = 0, chunk = 1;
do {
int nextlen = Math.min(1024, length - processed);
parms.code = code.substring(processed, processed + nextlen);
logger.debug("{}: Uploading chunk {} of script (total {} chars, {} processed)", thingName, chunk,
length, processed);
apiRequest(SHELLYRPC_METHOD_SCRIPT_PUTCODE, parms, String.class);
processed += nextlen;
chunk++;
parms.append = true;
} while (processed < length);
running = false;
}
if (enableScript(script, true)) {
logger.info("{}: Script {} was {} installed successful", thingName, thingName, script);
}
if (!running) {
running = startScript(ourId, true);
}
logger.info("{}: Script {} {}", thingName, script,
running ? "was successfully (re)started" : "failed to start");
}
private boolean startScript(int ourId, boolean start) {

View File

@ -429,8 +429,6 @@ public class ShellyComponents {
toQuantityType((double) bminutes, DIGITS_NONE, Units.MINUTE));
updated |= thingHandler.updateChannel(CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_MODE, getStringType(
getBool(t.targetTemp.enabled) ? SHELLY_TRV_MODE_AUTO : SHELLY_TRV_MODE_MANUAL));
updated |= thingHandler.updateChannel(CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_OPEN,
getOpenClosed(t.windowOpen));
int pid = getBool(t.schedule) ? getInteger(t.profile) : 0;
updated |= thingHandler.updateChannel(CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_SCHEDULE,

View File

@ -133,10 +133,12 @@ public class ShellyRelayHandler extends ShellyBaseHandler {
logger.debug("{}: Set Auto-OFF timer to {}", thingName, command);
api.setAutoTimer(rIndex, SHELLY_TIMER_AUTOOFF, getNumber(command).doubleValue());
break;
case CHANNEL_DEVST_RESETTOTAL:
logger.debug("{}: Reset Meter Totals", thingName);
api.resetMeterTotal(0); // currently there is only 1 emdata component
updateChannel(groupName, CHANNEL_DEVST_RESETTOTAL, OnOffType.OFF);
case CHANNEL_EMETER_RESETTOTAL:
String id = substringAfter(groupName, CHANNEL_GROUP_METER);
int mIdx = id.isEmpty() ? 0 : Integer.parseInt(id) - 1;
logger.debug("{}: Reset Meter Totals for meter {}", thingName, mIdx + 1);
api.resetMeterTotal(mIdx); // currently there is only 1 emdata component
updateChannel(groupName, CHANNEL_EMETER_RESETTOTAL, OnOffType.OFF);
break;
}
return true;

View File

@ -201,6 +201,7 @@ public class ShellyChannelDefinitions {
.add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_VOLTAGE, "meterVoltage", ITEMT_VOLT))
.add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_CURRENT, "meterCurrent", ITEMT_AMP))
.add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_PFACTOR, "meterPowerFactor", ITEMT_NUMBER))
.add(new ShellyChannel(m, CHGR_METER, CHANNEL_EMETER_RESETTOTAL, "meterResetTotals", ITEMT_SWITCH))
// Sensors
.add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_TEMP, "sensorTemp", ITEMT_TEMP))
@ -209,7 +210,6 @@ public class ShellyChannelDefinitions {
.add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_ILLUM, "sensorIllumination", ITEMT_STRING))
.add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_VOLTAGE, "sensorADC", ITEMT_VOLT))
.add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_STATE, "sensorContact", ITEMT_CONTACT))
.add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_OPEN, "sensorOpen", ITEMT_CONTACT))
.add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_SSTATE, "sensorState", ITEMT_STRING))
.add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_TILT, "sensorTilt", ITEMT_ANGLE))
.add(new ShellyChannel(m, CHGR_SENSOR, CHANNEL_SENSOR_MOTION, "sensorMotion", ITEMT_SWITCH))
@ -304,9 +304,13 @@ public class ShellyChannelDefinitions {
&& ((status.temperature != null && getDouble(status.temperature) != SHELLY_API_INVTEMP)
|| (status.tmp != null && getDouble(status.tmp.tC) != SHELLY_API_INVTEMP))) {
// Only some devices report the internal device temp
addChannel(thing, add,
!profile.isLight && (status.temperature != null || (status.tmp != null && !profile.isSensor)),
CHGR_DEVST, CHANNEL_DEVST_ITEMP);
boolean hasTemp = !profile.isLight
&& (status.temperature != null || (status.tmp != null && !profile.isSensor));
if (hasTemp && profile.isGen2 && (profile.numMeters > 0 && !profile.hasRelays)) // Shely Plus PM Mini
{
hasTemp = false;
}
addChannel(thing, add, hasTemp, CHGR_DEVST, CHANNEL_DEVST_ITEMP);
}
addChannel(thing, add, profile.settings.sleepTime != null, CHGR_SENSOR, CHANNEL_SENSOR_SLEEPTIME);
@ -315,7 +319,7 @@ public class ShellyChannelDefinitions {
addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_ACCUWATTS);
addChannel(thing, add, accuChannel, CHGR_DEVST, CHANNEL_DEVST_ACCUTOTAL);
addChannel(thing, add, accuChannel && (status.emeters != null), CHGR_DEVST, CHANNEL_DEVST_ACCURETURNED);
addChannel(thing, add, profile.is3EM || profile.isEM50, CHGR_DEVST, CHANNEL_DEVST_RESETTOTAL); // 3EM
addChannel(thing, add, profile.is3EM, CHGR_DEVST, CHANNEL_DEVST_RESETTOTAL); // 3EM
addChannel(thing, add, status.voltage != null || profile.settings.supplyVoltage != null, CHGR_DEVST,
CHANNEL_DEVST_VOLTAGE);
addChannel(thing, add,
@ -480,6 +484,10 @@ public class ShellyChannelDefinitions {
addChannel(thing, newChannels, emeter.current != null, group, CHANNEL_EMETER_CURRENT);
addChannel(thing, newChannels, emeter.pf != null, group, CHANNEL_EMETER_PFACTOR); // EM has no PF. but power
addChannel(thing, newChannels, true, group, CHANNEL_LAST_UPDATE);
ShellyThingInterface handler = (ShellyThingInterface) thing.getHandler();
if (handler != null) {
addChannel(thing, newChannels, handler.getProfile().isEM50, group, CHANNEL_DEVST_RESETTOTAL); // 3EM
}
return newChannels;
}
@ -543,7 +551,6 @@ public class ShellyChannelDefinitions {
addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_PROFILE);
addChannel(thing, newChannels, true, CHANNEL_GROUP_CONTROL, CHANNEL_CONTROL_SCHEDULE);
addChannel(thing, newChannels, true, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_STATE);
addChannel(thing, newChannels, true, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_OPEN);
}
// Battery

View File

@ -116,7 +116,7 @@ thing-type.shelly.shellypro2pm-relay.description= Shelly Pro 2PM - Dual Relay Sw
thing-type.shelly.shellypro2pm-roller.description = Shelly Pro 2PM - Roller Control with Power Meter
thing-type.shelly.shellypro3.description = Shelly Pro 3 - 3xRelay Switch
thing-type.shelly.shellypro3em.description = Shelly Pro 3EM - 3xPower Meter
thing-type.shelly.shellyproem50.description = Shelly Pro EM-50 - 3xPower Meter + 1xOutput
thing-type.shelly.shellyproem50.description = Shelly Pro EM-50 - 2xPower Meter + 1xOutput
thing-type.shelly.shellypro4pm.description = Shelly Pro 4PM - 4xRelay Switch with Power Meter
# BLU devices
@ -299,7 +299,7 @@ channel-type.shelly.lastPower1.label = Last Power
channel-type.shelly.lastPower1.description = Last power consumption #1 - one rounded minute
channel-type.shelly.meterTotal.label = Total Energy Consumption
channel-type.shelly.meterTotal.description = Total energy consumption in kW/h since the device powered up (resets on restart)
channel-type.shelly.meterResetTotals.label = Reset Totals
channel-type.shelly.meterResetTotals.label = Reset Energy Measurements
channel-type.shelly.meterResetTotals.description = Resets totals measurement data
channel-type.shelly.meterReturned.label = Total Returned Energy
channel-type.shelly.meterReturned.description = Total returned energy in kW/h
@ -411,10 +411,6 @@ channel-type.shelly.sensorContact.label = State
channel-type.shelly.sensorContact.description = State of the contact (open/closed)
channel-type.shelly.sensorContact.state.option.OPEN = Open
channel-type.shelly.sensorContact.state.option.CLOSED = Closed
channel-type.shelly.sensorOpen.label = Open
channel-type.shelly.sensorOpen.description = OPEN or CLOSED
channel-type.shelly.sensorOpen.state.option.OPEN = Open
channel-type.shelly.sensorOpen.state.option.CLOSED = Closed
channel-type.shelly.sensorState.label = Sensor State
channel-type.shelly.sensorState.description = Sensor State (Warm-Up/Normal/Fault/Unknown)
channel-type.shelly.sensorState.state.option.warmup = Warm-Up

View File

@ -243,18 +243,6 @@
</state>
</channel-type>
<channel-type id="sensorOpen">
<item-type>Contact</item-type>
<label>@text/channel-type.shelly.sensorOpen.label</label>
<description>@text/channel-type.shelly.sensorOpen.description</description>
<state pattern="%s" readOnly="true">
<options>
<option value="OPEN">@text/channel-type.shelly.sensorOpen.state.option.OPEN</option>
<option value="CLOSED">@text/channel-type.shelly.sensorOpen.state.option.CLOSED</option>
</options>
</state>
</channel-type>
<channel-type id="sensorState">
<item-type>String</item-type>
<label>@text/channel-type.shelly.sensorState.label</label>