[radiothermostat] Add job to set thermostat clock automatically from the binding (#10819)

* add thermostat clock sync job

Signed-off-by: Michael Lobstein <michael.lobstein@gmail.com>
This commit is contained in:
mlobstein
2021-06-07 13:58:19 -05:00
committed by GitHub
parent d9841ef0de
commit 6141d69da3
5 changed files with 56 additions and 3 deletions

View File

@@ -40,6 +40,7 @@ public class RadioThermostatBindingConstants {
public static final String LOCAL = "local";
public static final String PROPERTY_IP = "hostName";
public static final String PROPERTY_ISCT80 = "isCT80";
public static final String JSON_TIME = "{\"day\":%s,\"hour\":%s,\"minute\":%s}";
public static final String KEY_ERROR = "error";
@@ -48,6 +49,7 @@ public class RadioThermostatBindingConstants {
public static final String RUNTIME_RESOURCE = "tstat/datalog";
public static final String HUMIDITY_RESOURCE = "tstat/humidity";
public static final String REMOTE_TEMP_RESOURCE = "tstat/remote_temp";
public static final String TIME_RESOURCE = "tstat/time";
// List of all Thing Type UIDs
public static final ThingTypeUID THING_TYPE_RTHERM = new ThingTypeUID(BINDING_ID, "rtherm");

View File

@@ -28,5 +28,6 @@ public class RadioThermostatConfiguration {
public @Nullable Integer logRefresh;
public boolean isCT80 = false;
public boolean disableLogs = false;
public boolean clockSync = false;
public String setpointMode = "temporary";
}

View File

@@ -19,6 +19,7 @@ import java.text.NumberFormat;
import java.text.ParseException;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@@ -86,11 +87,13 @@ public class RadioThermostatHandler extends BaseThingHandler implements RadioThe
private @Nullable ScheduledFuture<?> refreshJob;
private @Nullable ScheduledFuture<?> logRefreshJob;
private @Nullable ScheduledFuture<?> clockSyncJob;
private int refreshPeriod = DEFAULT_REFRESH_PERIOD;
private int logRefreshPeriod = DEFAULT_LOG_REFRESH_PERIOD;
private boolean isCT80 = false;
private boolean disableLogs = false;
private boolean clockSync = false;
private String setpointCmdKeyPrefix = "t_";
public RadioThermostatHandler(Thing thing, RadioThermostatStateDescriptionProvider stateDescriptionProvider,
@@ -111,8 +114,9 @@ public class RadioThermostatHandler extends BaseThingHandler implements RadioThe
final Integer logRefresh = config.logRefresh;
this.isCT80 = config.isCT80;
this.disableLogs = config.disableLogs;
this.clockSync = config.clockSync;
if (hostName == null || hostName.equals("")) {
if (hostName == null || "".equals(hostName)) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Thermostat Host Name must be specified");
return;
@@ -145,12 +149,18 @@ public class RadioThermostatHandler extends BaseThingHandler implements RadioThe
channels.removeIf(c -> (c.getUID().getId().equals(PROGRAM_MODE)));
updateThing(editThing().withChannels(channels).build());
}
updateStatus(ThingStatus.UNKNOWN);
startAutomaticRefresh();
if (!this.disableLogs || this.isCT80) {
startAutomaticLogRefresh();
}
updateStatus(ThingStatus.UNKNOWN);
if (this.clockSync) {
scheduleClockSyncJob();
}
}
@Override
@@ -174,6 +184,35 @@ public class RadioThermostatHandler extends BaseThingHandler implements RadioThe
}
}
/**
* Schedule the clock sync job
*/
private void scheduleClockSyncJob() {
ScheduledFuture<?> clockSyncJob = this.clockSyncJob;
if (clockSyncJob == null || clockSyncJob.isCancelled()) {
clockSyncJob = null;
this.clockSyncJob = scheduler.scheduleWithFixedDelay(this::syncThermostatClock, 1, 60, TimeUnit.MINUTES);
}
}
/**
* Sync the thermostat's clock with the host system clock
*/
private void syncThermostatClock() {
Calendar c = Calendar.getInstance();
// The thermostat week starts as Monday = 0, subtract 2 since in standard DoW Monday = 2
int thermDayOfWeek = c.get(Calendar.DAY_OF_WEEK) - 2;
// Sunday will be -1, so add 7 to make it 6
if (thermDayOfWeek < 0) {
thermDayOfWeek += 7;
}
connector.sendCommand(null, null,
String.format(JSON_TIME, thermDayOfWeek, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE)),
TIME_RESOURCE);
}
/**
* Start the job to periodically update humidity and runtime date from the thermostat
*/
@@ -214,6 +253,12 @@ public class RadioThermostatHandler extends BaseThingHandler implements RadioThe
logRefreshJob.cancel(true);
this.logRefreshJob = null;
}
ScheduledFuture<?> clockSyncJob = this.clockSyncJob;
if (clockSyncJob != null) {
clockSyncJob.cancel(true);
this.clockSyncJob = null;
}
}
public void handleRawCommand(@Nullable String rawCommand) {

View File

@@ -68,7 +68,11 @@
<option value="temporary">Temporary</option>
</options>
</parameter>
<parameter name="clockSync" type="boolean">
<label>Enable Thermostat Clock Sync</label>
<description>Optional Flag to snyc the thermostat's clock with the host system clock</description>
<default>false</default>
</parameter>
</config-description>
</thing-type>