GiorgioBertolotti / TadoJava

Java wrapper library for Tado APIs

Home Page:https://bertolotti.dev/TadoJava

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add setPresence

Clon1998 opened this issue · comments

Add the setPresence API call.
TadoState can be set as AWAY or HOME.

The presence can only be set to AWAY/HOME if all GeoFencing Devices are outside/inside the Fence.
In case that you try to set it to AWAY and there are still Devices in the GeoFence, the API will response with an Error (The Err Msg. provided by the API is actually wrong/switch around xD)
In case update Presence was successful, no JSON respone is provided.
Example Impl:

private boolean _setPresence(int homeId, TadoState tadoState, int attempt) throws  TadoException {
		boolean toReturn = false;
		try {
			Map<String, String> headers = new HashMap<>();
			headers.put("Authorization", "Bearer " + this.bearer);
			headers.put("Content-type", "application/json;charset=UTF-8");
			JSONObject toPut = new JSONObject();
			toPut.put("homePresence", tadoState.getPresence());
			String jsonResponse = doPutRequest(
					"https://my.tado.com/api/v2/homes/" + homeId + "/presence", toPut.toString(),
					headers);
			if (jsonResponse != "") {
				JSONObject json = new JSONObject(jsonResponse);
				checkException(json);
			}
			debugMessage("setPresenc response: " + jsonResponse);
			toReturn = true;
		} catch (IOException e) {
			e.printStackTrace();
			toReturn = false;
		} catch (TadoException e) {
			if (attempt > 1) {
				throw e;
			} else {
				refresh();
				toReturn = _setPresence(homeId, tadoState, attempt + 1);
			}
		}
		return toReturn;
	}

Hey @Clon1998 thanks for adding the issue, I didn't see this API on the documentation, unfortunately I couldn't try it out on my device because it says I've auto-assist enabled, so let me know if it works, thanks again!

@GiorgioBertolotti i can confirm that 0.6 is working.
Guess if you have Tado Premium it doenst work...

Maybe its worth adding a comment mentioning that the API return msg in case of the fencing errors i mentioned earlier are wrong.

Example:
Tado is set in HOME and some mobile Devices are inside the Fence.
Trying to run this Code/API call:
home.setState(new TadoState("AWAY"),connector);
API return msg:
"Tried to update to HOME though all mobile devices are detected outside the home fence"

And if you try to set it to HOME and no device is inside the fence you will get the Error msg saying sth like.
Tried to update to AWAY though all mobile devices are detected inside the home fence

And if you try to set the same state as it currently is in, the API will return nothing!

@Clon1998 Thanks for reporting this behaviour, I'll add it to the API documentation soon!
Have a nice day!