From 0d510f562f795b59851e0cb8f798b7e6043fa40d Mon Sep 17 00:00:00 2001 From: Dan Cunningham Date: Thu, 18 Nov 2021 12:24:10 -0800 Subject: [PATCH 1/4] Fixes breaking API changes to the MyQ binding Signed-off-by: Dan Cunningham --- .../myq/internal/MyQDiscoveryService.java | 7 +- .../binding/myq/internal/dto/AccountDTO.java | 20 +-- .../{AccountInfoDTO.java => AccountsDTO.java} | 10 +- .../binding/myq/internal/dto/ActionDTO.java | 28 ----- .../binding/myq/internal/dto/AddressDTO.java | 26 ---- .../binding/myq/internal/dto/CountryDTO.java | 25 ---- .../binding/myq/internal/dto/DeviceDTO.java | 1 + .../myq/internal/dto/DeviceStateDTO.java | 32 +---- .../binding/myq/internal/dto/TimeZoneDTO.java | 24 ---- .../binding/myq/internal/dto/UsersDTO.java | 23 ---- .../internal/handler/MyQAccountHandler.java | 116 ++++++++++-------- .../handler/MyQGarageDoorHandler.java | 20 +-- .../myq/internal/handler/MyQLampHandler.java | 16 +-- 13 files changed, 105 insertions(+), 243 deletions(-) rename bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/{AccountInfoDTO.java => AccountsDTO.java} (77%) delete mode 100644 bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/ActionDTO.java delete mode 100644 bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/AddressDTO.java delete mode 100644 bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/CountryDTO.java delete mode 100644 bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/TimeZoneDTO.java delete mode 100644 bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/UsersDTO.java diff --git a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/MyQDiscoveryService.java b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/MyQDiscoveryService.java index 883d2988cf736..43d507d0eb1f9 100644 --- a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/MyQDiscoveryService.java +++ b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/MyQDiscoveryService.java @@ -14,11 +14,12 @@ import static org.openhab.binding.myq.internal.MyQBindingConstants.BINDING_ID; +import java.util.List; import java.util.Set; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; -import org.openhab.binding.myq.internal.dto.DevicesDTO; +import org.openhab.binding.myq.internal.dto.DeviceDTO; import org.openhab.binding.myq.internal.handler.MyQAccountHandler; import org.openhab.core.config.discovery.AbstractDiscoveryService; import org.openhab.core.config.discovery.DiscoveryResult; @@ -55,9 +56,9 @@ public Set getSupportedThingTypes() { public void startScan() { MyQAccountHandler accountHandler = this.accountHandler; if (accountHandler != null) { - DevicesDTO devices = accountHandler.devicesCache(); + List devices = accountHandler.devicesCache(); if (devices != null) { - devices.items.forEach(device -> { + devices.forEach(device -> { ThingTypeUID thingTypeUID = new ThingTypeUID(BINDING_ID, device.deviceFamily); if (SUPPORTED_DISCOVERY_THING_TYPES_UIDS.contains(thingTypeUID)) { ThingUID thingUID = new ThingUID(thingTypeUID, accountHandler.getThing().getUID(), diff --git a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/AccountDTO.java b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/AccountDTO.java index 2957918320957..4d461b2d6ed3a 100644 --- a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/AccountDTO.java +++ b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/AccountDTO.java @@ -18,21 +18,7 @@ * @author Dan Cunningham - Initial contribution */ public class AccountDTO { - - public UsersDTO users; - public Boolean admin; - public AccountInfoDTO account; - public String analyticsId; - public String userId; - public String userName; - public String email; - public String firstName; - public String lastName; - public String cultureCode; - public AddressDTO address; - public TimeZoneDTO timeZone; - public Boolean mailingListOptIn; - public Boolean requestAccountLinkInfo; - public String phone; - public Boolean diagnosticDataOptIn; + public String id; + public String name; + public String created_by; } diff --git a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/AccountInfoDTO.java b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/AccountsDTO.java similarity index 77% rename from bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/AccountInfoDTO.java rename to bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/AccountsDTO.java index 193f464d044bb..88fcf9e3fea51 100644 --- a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/AccountInfoDTO.java +++ b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/AccountsDTO.java @@ -12,13 +12,13 @@ */ package org.openhab.binding.myq.internal.dto; +import java.util.List; + /** - * The {@link AccountInfoDTO} entity from the MyQ API + * The {@link AccountsDTO} entity from the MyQ API * * @author Dan Cunningham - Initial contribution */ -public class AccountInfoDTO { - - public String href; - public String id; +public class AccountsDTO { + public List accounts; } diff --git a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/ActionDTO.java b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/ActionDTO.java deleted file mode 100644 index a38aa1e075d1b..0000000000000 --- a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/ActionDTO.java +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Copyright (c) 2010-2021 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.myq.internal.dto; - -/** - * The {@link ActionDTO} entity from the MyQ API - * - * @author Dan Cunningham - Initial contribution - */ -public class ActionDTO { - - public ActionDTO(String actionType) { - super(); - this.actionType = actionType; - } - - public String actionType; -} diff --git a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/AddressDTO.java b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/AddressDTO.java deleted file mode 100644 index 1c4d858ff8b08..0000000000000 --- a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/AddressDTO.java +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Copyright (c) 2010-2021 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.myq.internal.dto; - -/** - * The {@link AddressDTO} entity from the MyQ API - * - * @author Dan Cunningham - Initial contribution - */ -public class AddressDTO { - - public String addressLine1; - public String city; - public String postalCode; - public CountryDTO country; -} diff --git a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/CountryDTO.java b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/CountryDTO.java deleted file mode 100644 index 594a1ee32e7c0..0000000000000 --- a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/CountryDTO.java +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Copyright (c) 2010-2021 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.myq.internal.dto; - -/** - * The {@link CountryDTO} entity from the MyQ API - * - * @author Dan Cunningham - Initial contribution - */ -public class CountryDTO { - - public String code; - public Boolean isEEACountry; - public String href; -} diff --git a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/DeviceDTO.java b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/DeviceDTO.java index 426d691b2fcfb..9cfe9a00407ef 100644 --- a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/DeviceDTO.java +++ b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/DeviceDTO.java @@ -25,6 +25,7 @@ public class DeviceDTO { public String deviceType; public String name; public String createdDate; + public String accountId; public DeviceStateDTO state; public String parentDevice; public String parentDeviceId; diff --git a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/DeviceStateDTO.java b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/DeviceStateDTO.java index 169119b8c6116..331330c361df9 100644 --- a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/DeviceStateDTO.java +++ b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/DeviceStateDTO.java @@ -12,8 +12,6 @@ */ package org.openhab.binding.myq.internal.dto; -import java.util.List; - /** * The {@link DeviceStateDTO} entity from the MyQ API * @@ -23,34 +21,16 @@ public class DeviceStateDTO { public Boolean gdoLockConnected; public Boolean attachedWorkLightErrorPresent; - public String doorState; + public String learnStatus; + public Boolean hasCamera; public String lampState; - public String open; - public String close; + public String batteryBackupState; + public String doorState; public String lastUpdate; - public String passthroughInterval; - public String doorAjarInterval; - public String invalidCredentialWindow; - public String invalidShutoutPeriod; public Boolean isUnattendedOpenAllowed; public Boolean isUnattendedCloseAllowed; - public String auxRelayDelay; - public Boolean useAuxRelay; - public String auxRelayBehavior; - public Boolean rexFiresDoor; - public Boolean commandChannelReportStatus; - public Boolean controlFromBrowser; - public Boolean reportForced; - public Boolean reportAjar; - public Integer maxInvalidAttempts; + public Integer serviceCycleCount; + public Integer absoluteCycleCount; public Boolean online; public String lastStatus; - public String firmwareVersion; - public Boolean homekitCapable; - public Boolean homekitEnabled; - public String learn; - public Boolean learnMode; - public String updatedDate; - public List physicalDevices = null; - public Boolean pendingBootloadAbandoned; } diff --git a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/TimeZoneDTO.java b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/TimeZoneDTO.java deleted file mode 100644 index e324225ecdd11..0000000000000 --- a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/TimeZoneDTO.java +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Copyright (c) 2010-2021 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.myq.internal.dto; - -/** - * The {@link TimeZoneDTO} entity from the MyQ API - * - * @author Dan Cunningham - Initial contribution - */ -public class TimeZoneDTO { - - public String id; - public String name; -} diff --git a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/UsersDTO.java b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/UsersDTO.java deleted file mode 100644 index c988dba883888..0000000000000 --- a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/UsersDTO.java +++ /dev/null @@ -1,23 +0,0 @@ -/** - * Copyright (c) 2010-2021 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.myq.internal.dto; - -/** - * The {@link UsersDTO} entity from the MyQ API - * - * @author Dan Cunningham - Initial contribution - */ -public class UsersDTO { - - public String href; -} diff --git a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQAccountHandler.java b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQAccountHandler.java index a26760a616e2b..4d0bdb66c3aa4 100644 --- a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQAccountHandler.java +++ b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQAccountHandler.java @@ -23,10 +23,12 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; +import java.util.ArrayList; import java.util.Arrays; import java.util.Base64; import java.util.Collection; import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.Random; import java.util.concurrent.CompletableFuture; @@ -47,7 +49,6 @@ import org.eclipse.jetty.client.api.Result; import org.eclipse.jetty.client.util.BufferingResponseListener; import org.eclipse.jetty.client.util.FormContentProvider; -import org.eclipse.jetty.client.util.StringContentProvider; import org.eclipse.jetty.http.HttpMethod; import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.util.Fields; @@ -57,7 +58,8 @@ import org.openhab.binding.myq.internal.MyQDiscoveryService; import org.openhab.binding.myq.internal.config.MyQAccountConfiguration; import org.openhab.binding.myq.internal.dto.AccountDTO; -import org.openhab.binding.myq.internal.dto.ActionDTO; +import org.openhab.binding.myq.internal.dto.AccountsDTO; +import org.openhab.binding.myq.internal.dto.DeviceDTO; import org.openhab.binding.myq.internal.dto.DevicesDTO; import org.openhab.core.auth.client.oauth2.AccessTokenRefreshListener; import org.openhab.core.auth.client.oauth2.AccessTokenResponse; @@ -106,9 +108,13 @@ public class MyQAccountHandler extends BaseBridgeHandler implements AccessTokenR // this should never happen, but lets be safe and give up after so many redirects private static final int LOGIN_MAX_REDIRECTS = 30; /* - * MyQ device and account API endpoint + * MyQ device and account API endpoints */ - private static final String BASE_URL = "https://api.myqdevice.com/api"; + private static final String ACCOUNTS_URL = "https://accounts.myq-cloud.com/api/v6.0/accounts"; + private static final String DEVICES_URL = "https://devices.myq-cloud.com/api/v5.2/Accounts/%s/Devices"; + private static final String CMD_LAMP_URL = "https://account-devices-lamp.myq-cloud.com/api/v5.2/Accounts/%s/lamps/%s/%s"; + private static final String CMD_DOOR_URL = "https://account-devices-gdo.myq-cloud.com/api/v5.2/Accounts/%s/door_openers/%s/%s"; + private static final Integer RAPID_REFRESH_SECONDS = 5; private final Logger logger = LoggerFactory.getLogger(MyQAccountHandler.class); private final Gson gsonUpperCase = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) @@ -118,8 +124,9 @@ public class MyQAccountHandler extends BaseBridgeHandler implements AccessTokenR private final OAuthFactory oAuthFactory; private @Nullable Future normalPollFuture; private @Nullable Future rapidPollFuture; - private @Nullable AccountDTO account; - private @Nullable DevicesDTO devicesCache; + private @Nullable AccountsDTO accounts; + + private List devicesCache = new ArrayList(); private @Nullable OAuthClientService oAuthService; private Integer normalRefreshSeconds = 60; private HttpClient httpClient; @@ -167,10 +174,10 @@ public Collection> getServices() { @Override public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) { - DevicesDTO localDeviceCaches = devicesCache; + List localDeviceCaches = devicesCache; if (localDeviceCaches != null && childHandler instanceof MyQDeviceHandler) { MyQDeviceHandler handler = (MyQDeviceHandler) childHandler; - localDeviceCaches.items.stream() + localDeviceCaches.stream() .filter(d -> ((MyQDeviceHandler) childHandler).getSerialNumber().equalsIgnoreCase(d.serialNumber)) .findFirst().ifPresent(handler::handleDeviceUpdate); } @@ -182,33 +189,42 @@ public void onAccessTokenResponse(AccessTokenResponse tokenResponse) { } /** - * Sends an action to the MyQ API + * Sends a door action to the MyQ API + * + * @param serialNumber + * @param action + */ + public void sendDoorAction(DeviceDTO devivce, String action) { + sendAction(devivce, action, CMD_DOOR_URL); + } + + /** + * Sends a lamp action to the MyQ API * * @param serialNumber * @param action */ - public void sendAction(String serialNumber, String action) { + public void sendLampAction(DeviceDTO devivce, String action) { + sendAction(devivce, action, CMD_LAMP_URL); + } + + private void sendAction(DeviceDTO device, String action, String urlFormat) { if (getThing().getStatus() != ThingStatus.ONLINE) { logger.debug("Account offline, ignoring action {}", action); return; } - AccountDTO localAccount = account; - if (localAccount != null) { - try { - ContentResponse response = sendRequest( - String.format("%s/v5.1/Accounts/%s/Devices/%s/actions", BASE_URL, localAccount.account.id, - serialNumber), - HttpMethod.PUT, new StringContentProvider(gsonLowerCase.toJson(new ActionDTO(action))), - "application/json"); - if (HttpStatus.isSuccess(response.getStatus())) { - restartPolls(true); - } else { - logger.debug("Failed to send action {} : {}", action, response.getContentAsString()); - } - } catch (InterruptedException | MyQCommunicationException | MyQAuthenticationException e) { - logger.debug("Could not send action", e); + try { + ContentResponse response = sendRequest( + String.format(urlFormat, device.accountId, device.serialNumber, action), HttpMethod.PUT, null, + null); + if (HttpStatus.isSuccess(response.getStatus())) { + restartPolls(true); + } else { + logger.debug("Failed to send action {} : {}", action, response.getContentAsString()); } + } catch (InterruptedException | MyQCommunicationException | MyQAuthenticationException e) { + logger.debug("Could not send action", e); } } @@ -217,7 +233,7 @@ HttpMethod.PUT, new StringContentProvider(gsonLowerCase.toJson(new ActionDTO(act * * @return cached MyQ devices */ - public @Nullable DevicesDTO devicesCache() { + public @Nullable List devicesCache() { return devicesCache; } @@ -266,8 +282,8 @@ private void rapidPoll() { private synchronized void fetchData() { try { - if (account == null) { - getAccount(); + if (accounts == null) { + getAccounts(); } getDevices(); } catch (MyQCommunicationException e) { @@ -338,33 +354,37 @@ private AccessTokenResponse login() } } - private void getAccount() throws InterruptedException, MyQCommunicationException, MyQAuthenticationException { - ContentResponse response = sendRequest(BASE_URL + "/v5/My?expand=account", HttpMethod.GET, null, null); - account = parseResultAndUpdateStatus(response, gsonUpperCase, AccountDTO.class); + private void getAccounts() throws InterruptedException, MyQCommunicationException, MyQAuthenticationException { + ContentResponse response = sendRequest(ACCOUNTS_URL, HttpMethod.GET, null, null); + accounts = parseResultAndUpdateStatus(response, gsonLowerCase, AccountsDTO.class); } private void getDevices() throws InterruptedException, MyQCommunicationException, MyQAuthenticationException { - AccountDTO localAccount = account; - if (localAccount == null) { + AccountsDTO localAccounts = accounts; + if (localAccounts == null) { return; } - ContentResponse response = sendRequest( - String.format("%s/v5.1/Accounts/%s/Devices", BASE_URL, localAccount.account.id), HttpMethod.GET, null, - null); - DevicesDTO devices = parseResultAndUpdateStatus(response, gsonLowerCase, DevicesDTO.class); - devicesCache = devices; - devices.items.forEach(device -> { - ThingTypeUID thingTypeUID = new ThingTypeUID(BINDING_ID, device.deviceFamily); - if (SUPPORTED_DISCOVERY_THING_TYPES_UIDS.contains(thingTypeUID)) { - for (Thing thing : getThing().getThings()) { - ThingHandler handler = thing.getHandler(); - if (handler != null - && ((MyQDeviceHandler) handler).getSerialNumber().equalsIgnoreCase(device.serialNumber)) { - ((MyQDeviceHandler) handler).handleDeviceUpdate(device); + + List currentDevices = new ArrayList(); + + for (AccountDTO account : localAccounts.accounts) { + ContentResponse response = sendRequest(String.format(DEVICES_URL, account.id), HttpMethod.GET, null, null); + DevicesDTO devices = parseResultAndUpdateStatus(response, gsonLowerCase, DevicesDTO.class); + currentDevices.addAll(devices.items); + devices.items.forEach(device -> { + ThingTypeUID thingTypeUID = new ThingTypeUID(BINDING_ID, device.deviceFamily); + if (SUPPORTED_DISCOVERY_THING_TYPES_UIDS.contains(thingTypeUID)) { + for (Thing thing : getThing().getThings()) { + ThingHandler handler = thing.getHandler(); + if (handler != null && ((MyQDeviceHandler) handler).getSerialNumber() + .equalsIgnoreCase(device.serialNumber)) { + ((MyQDeviceHandler) handler).handleDeviceUpdate(device); + } } } - } - }); + }); + } + devicesCache = currentDevices; } private synchronized ContentResponse sendRequest(String url, HttpMethod method, @Nullable ContentProvider content, diff --git a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQGarageDoorHandler.java b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQGarageDoorHandler.java index a2422f3d0feeb..0d5ff03cc653c 100644 --- a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQGarageDoorHandler.java +++ b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQGarageDoorHandler.java @@ -40,7 +40,7 @@ */ @NonNullByDefault public class MyQGarageDoorHandler extends BaseThingHandler implements MyQDeviceHandler { - private @Nullable DeviceDTO deviceState; + private @Nullable DeviceDTO device; private String serialNumber; public MyQGarageDoorHandler(Thing thing) { @@ -60,8 +60,8 @@ public void handleCommand(ChannelUID channelUID, Command command) { return; } Bridge bridge = getBridge(); - final DeviceDTO localState = deviceState; - if (bridge != null && localState != null) { + final DeviceDTO localDevice = device; + if (bridge != null && localDevice != null) { BridgeHandler handler = bridge.getHandler(); if (handler != null) { String cmd = null; @@ -78,7 +78,7 @@ public void handleCommand(ChannelUID channelUID, Command command) { cmd = command.toString(); } if (cmd != null) { - ((MyQAccountHandler) handler).sendAction(localState.serialNumber, cmd); + ((MyQAccountHandler) handler).sendDoorAction(localDevice, cmd); } } } @@ -90,9 +90,9 @@ public String getSerialNumber() { } protected void updateState() { - final DeviceDTO localState = deviceState; - if (localState != null) { - String doorState = localState.state.doorState; + final DeviceDTO localDevice = device; + if (localDevice != null) { + String doorState = localDevice.state.doorState; updateState("status", new StringType(doorState)); switch (doorState) { case "open": @@ -112,8 +112,8 @@ protected void updateState() { updateState("rollershutter", UnDefType.UNDEF); break; } - updateState("closeerror", localState.state.isUnattendedCloseAllowed ? OnOffType.OFF : OnOffType.ON); - updateState("openerror", localState.state.isUnattendedOpenAllowed ? OnOffType.OFF : OnOffType.ON); + updateState("closeerror", localDevice.state.isUnattendedCloseAllowed ? OnOffType.OFF : OnOffType.ON); + updateState("openerror", localDevice.state.isUnattendedOpenAllowed ? OnOffType.OFF : OnOffType.ON); } } @@ -122,7 +122,7 @@ public void handleDeviceUpdate(DeviceDTO device) { if (!MyQBindingConstants.THING_TYPE_GARAGEDOOR.getId().equals(device.deviceFamily)) { return; } - deviceState = device; + this.device = device; if (device.state.online) { updateStatus(ThingStatus.ONLINE); updateState(); diff --git a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQLampHandler.java b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQLampHandler.java index e7988bae00ee4..80c537eee5b4b 100644 --- a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQLampHandler.java +++ b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQLampHandler.java @@ -36,7 +36,7 @@ */ @NonNullByDefault public class MyQLampHandler extends BaseThingHandler implements MyQDeviceHandler { - private @Nullable DeviceDTO deviceState; + private @Nullable DeviceDTO device; private String serialNumber; public MyQLampHandler(Thing thing) { @@ -58,11 +58,11 @@ public void handleCommand(ChannelUID channelUID, Command command) { if (command instanceof OnOffType) { Bridge bridge = getBridge(); - final DeviceDTO localState = deviceState; - if (bridge != null && localState != null) { + final DeviceDTO localDevice = device; + if (bridge != null && localDevice != null) { BridgeHandler handler = bridge.getHandler(); if (handler != null) { - ((MyQAccountHandler) handler).sendAction(localState.serialNumber, + ((MyQAccountHandler) handler).sendLampAction(localDevice, command == OnOffType.ON ? "turnon" : "turnoff"); } } @@ -75,9 +75,9 @@ public String getSerialNumber() { } protected void updateState() { - final DeviceDTO localState = deviceState; - if (localState != null) { - String lampState = localState.state.lampState; + final DeviceDTO localDevice = device; + if (localDevice != null) { + String lampState = localDevice.state.lampState; updateState("switch", "on".equals(lampState) ? OnOffType.ON : OnOffType.OFF); } } @@ -87,7 +87,7 @@ public void handleDeviceUpdate(DeviceDTO device) { if (!MyQBindingConstants.THING_TYPE_LAMP.getId().equals(device.deviceFamily)) { return; } - deviceState = device; + this.device = device; if (device.state.online) { updateStatus(ThingStatus.ONLINE); updateState(); From 8adea8621a14a0bbe72483e130d62f01ae9f941c Mon Sep 17 00:00:00 2001 From: Dan Cunningham Date: Thu, 18 Nov 2021 15:35:52 -0800 Subject: [PATCH 2/4] Small cleanups Signed-off-by: Dan Cunningham --- .../org/openhab/binding/myq/internal/dto/AccountDTO.java | 2 +- .../binding/myq/internal/handler/MyQAccountHandler.java | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/AccountDTO.java b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/AccountDTO.java index 4d461b2d6ed3a..e86853d2b4c76 100644 --- a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/AccountDTO.java +++ b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/dto/AccountDTO.java @@ -20,5 +20,5 @@ public class AccountDTO { public String id; public String name; - public String created_by; + public String createdBy; } diff --git a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQAccountHandler.java b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQAccountHandler.java index 4d0bdb66c3aa4..0c33bdd27ec7b 100644 --- a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQAccountHandler.java +++ b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQAccountHandler.java @@ -117,8 +117,6 @@ public class MyQAccountHandler extends BaseBridgeHandler implements AccessTokenR private static final Integer RAPID_REFRESH_SECONDS = 5; private final Logger logger = LoggerFactory.getLogger(MyQAccountHandler.class); - private final Gson gsonUpperCase = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) - .create(); private final Gson gsonLowerCase = new GsonBuilder() .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create(); private final OAuthFactory oAuthFactory; @@ -162,6 +160,7 @@ public void initialize() { @Override public void dispose() { stopPolls(); + OAuthClientService oAuthService = this.oAuthService; if (oAuthService != null) { oAuthService.close(); } @@ -175,7 +174,7 @@ public Collection> getServices() { @Override public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) { List localDeviceCaches = devicesCache; - if (localDeviceCaches != null && childHandler instanceof MyQDeviceHandler) { + if (childHandler instanceof MyQDeviceHandler) { MyQDeviceHandler handler = (MyQDeviceHandler) childHandler; localDeviceCaches.stream() .filter(d -> ((MyQDeviceHandler) childHandler).getSerialNumber().equalsIgnoreCase(d.serialNumber)) From 0da06a2a9a2e46f9fda194c155b0df856934d449 Mon Sep 17 00:00:00 2001 From: Dan Cunningham Date: Fri, 19 Nov 2021 07:52:23 -0800 Subject: [PATCH 3/4] Docs clean up, make sure background scans work Signed-off-by: Dan Cunningham --- .../openhab/binding/myq/internal/MyQDiscoveryService.java | 5 +++++ .../binding/myq/internal/handler/MyQAccountHandler.java | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/MyQDiscoveryService.java b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/MyQDiscoveryService.java index 43d507d0eb1f9..9a2464f4d0a0d 100644 --- a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/MyQDiscoveryService.java +++ b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/MyQDiscoveryService.java @@ -74,6 +74,11 @@ public void startScan() { } } + @Override + public void startBackgroundDiscovery() { + startScan(); + } + @Override public void setThingHandler(ThingHandler handler) { if (handler instanceof MyQAccountHandler) { diff --git a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQAccountHandler.java b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQAccountHandler.java index 0c33bdd27ec7b..7f5f9e8e93bf6 100644 --- a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQAccountHandler.java +++ b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQAccountHandler.java @@ -190,7 +190,7 @@ public void onAccessTokenResponse(AccessTokenResponse tokenResponse) { /** * Sends a door action to the MyQ API * - * @param serialNumber + * @param devivce * @param action */ public void sendDoorAction(DeviceDTO devivce, String action) { @@ -200,7 +200,7 @@ public void sendDoorAction(DeviceDTO devivce, String action) { /** * Sends a lamp action to the MyQ API * - * @param serialNumber + * @param devivce * @param action */ public void sendLampAction(DeviceDTO devivce, String action) { From e373df4c37971ac1f39660ff8ad2fb4a0cf4acb7 Mon Sep 17 00:00:00 2001 From: Dan Cunningham Date: Fri, 19 Nov 2021 15:06:47 -0800 Subject: [PATCH 4/4] Fixed typos Signed-off-by: Dan Cunningham --- .../myq/internal/handler/MyQAccountHandler.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQAccountHandler.java b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQAccountHandler.java index 7f5f9e8e93bf6..c769bcae1a673 100644 --- a/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQAccountHandler.java +++ b/bundles/org.openhab.binding.myq/src/main/java/org/openhab/binding/myq/internal/handler/MyQAccountHandler.java @@ -190,21 +190,21 @@ public void onAccessTokenResponse(AccessTokenResponse tokenResponse) { /** * Sends a door action to the MyQ API * - * @param devivce + * @param device * @param action */ - public void sendDoorAction(DeviceDTO devivce, String action) { - sendAction(devivce, action, CMD_DOOR_URL); + public void sendDoorAction(DeviceDTO device, String action) { + sendAction(device, action, CMD_DOOR_URL); } /** * Sends a lamp action to the MyQ API * - * @param devivce + * @param device * @param action */ - public void sendLampAction(DeviceDTO devivce, String action) { - sendAction(devivce, action, CMD_LAMP_URL); + public void sendLampAction(DeviceDTO device, String action) { + sendAction(device, action, CMD_LAMP_URL); } private void sendAction(DeviceDTO device, String action, String urlFormat) {