Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[automower] Implementation of complete automower API #17545

Open
wants to merge 35 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
c381e14
started implementation of complete automower API
MikeTheTux Oct 12, 2024
1445a1f
workaround for bug in automower API (issue created)
MikeTheTux Oct 12, 2024
3a68f00
WorkAreas and StayOutZones
MikeTheTux Oct 12, 2024
d788084
QuantityType instead of DecimalType
MikeTheTux Oct 12, 2024
4fa132d
relative statistics and patterns
MikeTheTux Oct 12, 2024
0e7d284
reverted logic
MikeTheTux Oct 12, 2024
3858e80
full calendar read support
MikeTheTux Oct 12, 2024
7f1d520
finally ...
MikeTheTux Oct 12, 2024
f74213b
new mowerZoneId config parameter
MikeTheTux Oct 13, 2024
eb132a5
cleaned up time zone handling
MikeTheTux Oct 13, 2024
a38b63f
implementation of CalendarTask POST
MikeTheTux Oct 13, 2024
b120aec
implemented sendAutomowerSettings
MikeTheTux Oct 13, 2024
b2f789a
sendAutomowerSettings
MikeTheTux Oct 13, 2024
cdbfce7
sendAutomowerSettings()
MikeTheTux Oct 13, 2024
7a292c8
re-trigger build
MikeTheTux Oct 13, 2024
0f62224
added CHANNEL_STATUS_WORK_AREA_ID channels
MikeTheTux Oct 17, 2024
7c9ad9d
implemented sendAutomowerConfirmError()
MikeTheTux Oct 17, 2024
5702549
fixed updateProperties()
MikeTheTux Oct 17, 2024
887f527
added sendAutomowerStayOutZones
MikeTheTux Oct 18, 2024
3f24b27
added sendAutomowerWorkArea()
MikeTheTux Oct 19, 2024
34c5d7b
refactored actions
MikeTheTux Oct 21, 2024
be2285b
clean-ups
MikeTheTux Oct 21, 2024
119a33d
updated changelog
MikeTheTux Oct 21, 2024
320f6e5
own review + findings
MikeTheTux Oct 22, 2024
159b828
robustness improvements
MikeTheTux Nov 9, 2024
9607dad
updated README.md
MikeTheTux Nov 10, 2024
cc07d5e
added actions to README.md
MikeTheTux Nov 10, 2024
c2269c5
README.md
MikeTheTux Nov 10, 2024
ab0fe97
testing, fixes
MikeTheTux Nov 11, 2024
db81cca
mvn i18n:generate-default-translations
MikeTheTux Nov 13, 2024
e20c4be
further improvements after testing
MikeTheTux Nov 13, 2024
9889aff
API release 2024-10-14 (https://developer.husqvarnagroup.cloud/apis/a…
MikeTheTux Nov 18, 2024
9f84daf
ResetCuttingBladeUsageTime with QuantityType
MikeTheTux Nov 18, 2024
33280d8
added error message
MikeTheTux Nov 21, 2024
55def92
static channels + channel groups
MikeTheTux Nov 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
286 changes: 164 additions & 122 deletions bundles/org.openhab.binding.automower/README.md

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.HeadlightMode;
import org.openhab.binding.automower.internal.things.AutomowerCommand;
import org.openhab.binding.automower.internal.things.AutomowerHandler;
import org.openhab.core.automation.annotation.ActionInput;
Expand Down Expand Up @@ -131,4 +132,113 @@ public void resumeSchedule() {
public static void resumeSchedule(ThingActions actions) {
((AutomowerActions) actions).resumeSchedule();
}

@RuleAction(label = "@text/action-confirm-error-label", description = "@text/action-confirm-error-desc")
public void confirmError() {
AutomowerHandler automowerHandler = handler;
if (automowerHandler == null) {
logger.warn("Automower Action service ThingHandler is null!");
} else {
automowerHandler.sendAutomowerConfirmError();
}
}

public static void confirmError(ThingActions actions) {
((AutomowerActions) actions).confirmError();
}

@RuleAction(label = "@text/action-reset-cutting-blade-usage-time-label", description = "@text/action-reset-cutting-blade-usage-time-desc")
public void resetCuttingBladeUsageTime() {
AutomowerHandler automowerHandler = handler;
if (automowerHandler == null) {
logger.warn("Automower Action service ThingHandler is null!");
} else {
automowerHandler.sendAutomowerResetCuttingBladeUsageTime();
}
}

public static void resetCuttingBladeUsageTime(ThingActions actions) {
((AutomowerActions) actions).resetCuttingBladeUsageTime();
}

@RuleAction(label = "@text/action-set-settings-label", description = "@text/action-set-settings-desc")
public void setSettings(
@ActionInput(name = "cutting-height", label = "@text/action-input-cutting-height-label", description = "@text/action-input-cutting-height-desc") byte cuttingHeight,
@ActionInput(name = "headlight-mode", label = "@text/action-input-headlight-mode-label", description = "@text/action-input-headlight-mode-desc") String headlightMode) {
AutomowerHandler automowerHandler = handler;
if (automowerHandler == null) {
logger.warn("Automower Action service ThingHandler is null!");
} else {
try {
automowerHandler.sendAutomowerSettings(cuttingHeight, HeadlightMode.valueOf(headlightMode));
} catch (IllegalArgumentException e) {
logger.warn("Invalid HeadlightMode: {}, Error: {}", headlightMode, e.getMessage());
}
}
}

public static void setSettings(ThingActions actions, byte cuttingHeight, String headlightMode) {
((AutomowerActions) actions).setSettings(cuttingHeight, headlightMode);
}

@RuleAction(label = "@text/action-set-work-area-label", description = "@text/action-set-work-area-desc")
public void setWorkArea(
@ActionInput(name = "workarea-id", label = "@text/action-input-workarea-id-label", description = "@text/action-input-workarea-id-desc") long workAreaId,
@ActionInput(name = "enable", label = "@text/action-input-enable-label", description = "@text/action-input-enable-desc") boolean enable,
@ActionInput(name = "cutting-height", label = "@text/action-input-cutting-height-label", description = "@text/action-input-cutting-height-desc") byte cuttingHeight) {
AutomowerHandler automowerHandler = handler;
if (automowerHandler == null) {
logger.warn("Automower Action service ThingHandler is null!");
} else {
automowerHandler.sendAutomowerWorkArea(workAreaId, enable, cuttingHeight);
}
}

public static void setWorkArea(ThingActions actions, long workAreaId, boolean enable, byte cuttingHeight) {
((AutomowerActions) actions).setWorkArea(workAreaId, enable, cuttingHeight);
}

@RuleAction(label = "@text/action-set-stayoutzone-label", description = "@text/action-set-stayoutzone-desc")
public void setStayOutZone(
@ActionInput(name = "zone-id", label = "@text/action-input-zone-id-label", description = "@text/action-input-zone-id-desc") String zoneId,
@ActionInput(name = "enable", label = "@text/action-input-enable-label", description = "@text/action-input-enable-desc") boolean enable) {
AutomowerHandler automowerHandler = handler;
if (automowerHandler == null) {
logger.warn("Automower Action service ThingHandler is null!");
} else {
automowerHandler.sendAutomowerStayOutZone(zoneId, enable);
}
}

public static void setStayOutZone(ThingActions actions, String zoneId, boolean enable) {
((AutomowerActions) actions).setStayOutZone(zoneId, enable);
}

@RuleAction(label = "@text/action-set-calendartask-label", description = "@text/action-set-calendartask-desc")
public void setCalendarTask(
@ActionInput(name = "workarea-id", label = "@text/action-input-workarea-id-label", description = "@text/action-input-workarea-id-desc") Long workAreaId,
@ActionInput(name = "start", label = "@text/action-input-start-label", description = "@text/action-input-start-desc") short[] start,
@ActionInput(name = "duration", label = "@text/action-input-duration-label", description = "@text/action-input-duration-desc") short[] duration,
@ActionInput(name = "monday", label = "@text/action-input-monday-label", description = "@text/action-input-monday-desc") boolean[] monday,
@ActionInput(name = "tuesday", label = "@text/action-input-tuesday-label", description = "@text/action-input-tuesday-desc") boolean[] tuesday,
@ActionInput(name = "wednesday", label = "@text/action-input-wednesday-label", description = "@text/action-input-wednesday-desc") boolean[] wednesday,
@ActionInput(name = "thursday", label = "@text/action-input-thursday-label", description = "@text/action-input-thursday-desc") boolean[] thursday,
@ActionInput(name = "friday", label = "@text/action-input-friday-label", description = "@text/action-input-friday-desc") boolean[] friday,
@ActionInput(name = "saturday", label = "@text/action-input-saturday-label", description = "@text/action-input-saturday-desc") boolean[] saturday,
@ActionInput(name = "sunday", label = "@text/action-input-sunday-label", description = "@text/action-input-sunday-desc") boolean[] sunday) {
AutomowerHandler automowerHandler = handler;
if (automowerHandler == null) {
logger.warn("Automower Action service ThingHandler is null!");
} else {
automowerHandler.sendAutomowerCalendarTask(workAreaId, start, duration, monday, tuesday, wednesday,
thursday, friday, saturday, sunday);
}
}

public static void setCalendarTask(ThingActions actions, Long workAreaId, short[] start, short[] duration,
boolean[] monday, boolean[] tuesday, boolean[] wednesday, boolean[] thursday, boolean[] friday,
boolean[] saturday, boolean[] sunday) {
((AutomowerActions) actions).setCalendarTask(workAreaId, start, duration, monday, tuesday, wednesday, thursday,
friday, saturday, sunday);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,30 @@

import java.io.IOException;
import java.time.Instant;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jetty.client.HttpClient;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.AutomowerConnectApi;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.Calendar;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.CalendarTask;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.Mower;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerCalendar;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerCalendardRequest;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerCommand;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerCommandAttributes;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerCommandRequest;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerListResult;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerSettings;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerSettingsRequest;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerStayOutZone;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerStayOutZoneAttributes;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerStayOutZoneRequest;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerWorkArea;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerWorkAreaAttributes;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerWorkAreaRequest;
import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.Settings;
import org.openhab.binding.automower.internal.rest.exceptions.AutomowerCommunicationException;
import org.openhab.binding.automower.internal.things.AutomowerCommand;
import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
Expand Down Expand Up @@ -103,4 +117,109 @@ public void sendAutomowerCommand(String id, AutomowerCommand command, long comma
request.setData(mowerCommand);
automowerApi.sendCommand(appKey, authenticate().getAccessToken(), id, request);
}

/**
* Sends a calendarTask to the automower
*
* @param id The id of the mower
* @param hasWorkAreas Work area capability of the mower
* @param workAreaId The Id of the work area this calendar belongs to (or null, if there is no work area support)
* @param calendarTasks The calendar that should be sent. It is using the same json structure (start, duration, ...)
* as provided when reading the channel
* @throws AutomowerCommunicationException In case the query cannot be executed successfully
*/
public void sendAutomowerCalendarTask(String id, boolean hasWorkAreas, Long workAreaId,
List<CalendarTask> calendarTasks) throws AutomowerCommunicationException {
Calendar calendar = new Calendar();
calendar.setTasks(calendarTasks);

MowerCalendar mowerCalendar = new MowerCalendar();
mowerCalendar.setType("calendar");
mowerCalendar.setAttributes(calendar);

MowerCalendardRequest calendarRequest = new MowerCalendardRequest();
calendarRequest.setData(mowerCalendar);

automowerApi.sendCalendar(appKey, authenticate().getAccessToken(), id, hasWorkAreas, workAreaId,
calendarRequest);
}

/**
* Sends Settings to the automower
*
* @param id The id of the mower
* @param settings The Settings that should be sent. It is using the same json structure
* as provided when reading the channel
* @throws AutomowerCommunicationException In case the query cannot be executed successfully
*/
public void sendAutomowerSettings(String id, Settings settings) throws AutomowerCommunicationException {
MowerSettings mowerSettings = new MowerSettings();
mowerSettings.setType("settings");
mowerSettings.setAttributes(settings);

MowerSettingsRequest settingsRequest = new MowerSettingsRequest();
settingsRequest.setData(mowerSettings);

automowerApi.sendSettings(appKey, authenticate().getAccessToken(), id, settingsRequest);
}

/**
* Confirm current non fatal error on the mower
*
* @param id The id of the mower
* @throws AutomowerCommunicationException In case the query cannot be executed successfully
*/
public void sendAutomowerConfirmError(String id) throws AutomowerCommunicationException {
automowerApi.sendConfirmError(appKey, authenticate().getAccessToken(), id);
}

/**
* Reset the cutting blade usage time
*
* @param id The id of the mower
* @throws AutomowerCommunicationException In case the query cannot be executed successfully
*/
public void sendAutomowerResetCuttingBladeUsageTime(String id) throws AutomowerCommunicationException {
automowerApi.sendResetCuttingBladeUsageTime(appKey, authenticate().getAccessToken(), id);
}

/**
* Enable or disable stay out zone
*
* @param id The id of the mower
* @param zoneId The id of the stay out zone
* @param zoneAttributes The new zone status
* @throws AutomowerCommunicationException In case the query cannot be executed successfully
*/
public void sendAutomowerStayOutZone(String id, String zoneId, MowerStayOutZoneAttributes zoneAttributes)
throws AutomowerCommunicationException {
MowerStayOutZone zoneData = new MowerStayOutZone();
zoneData.setType("stayOutZone");
zoneData.setId(zoneId);
zoneData.setAttributes(zoneAttributes);
MowerStayOutZoneRequest zoneRequest = new MowerStayOutZoneRequest();
zoneRequest.setData(zoneData);

automowerApi.sendStayOutZone(appKey, authenticate().getAccessToken(), id, zoneId, zoneRequest);
}

/**
* Update a work area setting
*
* @param id The id of the mower
* @param workAreaId The id of the work area
* @param workAreaAttributes The new work area status
* @throws AutomowerCommunicationException In case the query cannot be executed successfully
*/
public void sendAutomowerWorkArea(String id, long workAreaId, MowerWorkAreaAttributes workAreaAttributes)
throws AutomowerCommunicationException {
MowerWorkArea workAreaData = new MowerWorkArea();
workAreaData.setType("workArea");
workAreaData.setId(workAreaId);
workAreaData.setAttributes(workAreaAttributes);
MowerWorkAreaRequest workAreaRequest = new MowerWorkAreaRequest();
workAreaRequest.setData(workAreaData);

automowerApi.sendWorkArea(appKey, authenticate().getAccessToken(), id, workAreaId, workAreaRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ protected void startScan() {
properties.put(AutomowerBindingConstants.AUTOMOWER_MODEL, mower.getAttributes().getSystem().getModel());
properties.put(AutomowerBindingConstants.AUTOMOWER_NAME, mower.getAttributes().getSystem().getName());

properties.put(AutomowerBindingConstants.AUTOMOWER_CAN_CONFIRM_ERROR,
(mower.getAttributes().getCapabilities().canConfirmError() ? "yes" : "no"));
properties.put(AutomowerBindingConstants.AUTOMOWER_HAS_HEADLIGHTS,
(mower.getAttributes().getCapabilities().hasHeadlights() ? "yes" : "no"));
properties.put(AutomowerBindingConstants.AUTOMOWER_HAS_POSITION,
(mower.getAttributes().getCapabilities().hasPosition() ? "yes" : "no"));
properties.put(AutomowerBindingConstants.AUTOMOWER_HAS_STAY_OUT_ZONES,
(mower.getAttributes().getCapabilities().hasStayOutZones() ? "yes" : "no"));
properties.put(AutomowerBindingConstants.AUTOMOWER_HAS_WORK_AREAS,
(mower.getAttributes().getCapabilities().hasWorkAreas() ? "yes" : "no"));

DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(mowerThingUid)
.withThingType(thingTypeUID).withProperties(properties).withBridge(bridgeUID)
.withRepresentationProperty(AutomowerBindingConstants.AUTOMOWER_ID)
Expand Down
Loading