ChrisHaPunkt / ha-stihl-imow

Using https://github.com/ChrisHaPunkt/stihl-imow-webapi to provide STIHL iMow lawn mower information to Home Assistant
19 stars 1 forks source link

Start mowing command does not work for iMow model 632 #19

Closed jwmaas closed 1 year ago

jwmaas commented 1 year ago

We discussed before, but I figured out now why my model 632 does not start mowing when issued command, by eavesdropping on the App traffic to the Stihl server.

This model needs a “startMowing” command, and does not recognise the command that is currently programmed “startMowingFromPoint”. In fact there is no such option in my App at all.

I have added START_MOWING_NOW to reference to “startMowing” in the IMowActions() class in your api/ common/actions.py.

There are two ways to execute start mowing for the 632 model:

With a default date/endtime, approximately 2 hours after current time at a 10 minute interval (00, 10, 20, 30, 40, 50) : The action_object should look like this: please note “startMowing” rather than “startMowingFromPoint”

{“actionName”:“startMowing”,“actionValue”:“16 digit externalid,2023-08-12 20:50”}

With an endtime about 2 hours after now, and starttime a little later than now at a 10 minute interval: {“actionName”:“startMowing”,“actionValue”:“16 digit externalid,2023-08-12 21:00,2023-08-12 19:00”} I have started to experiment with this and have it working now for the test script. The function call in the testscript is (after adding the new START_MOWING_NOW command:

await mower.intent(IMowActions.START_MOWING_NOW,0,'2023-08-14 14:00') I have added a 0 as default startpoint, because the code in init.py refers to it. And I wanted to minimise the changes to your code.

To make this work I needed to change two lines of code in imow/api/init.py:

In line 363 I eliminated the quotes around the 0: (I think the startpoint is an integer not a string):

if "-" in str(duration) and startpoint == 0: In line 370 I added some code to ensure the startpoint does not get added to the action_object, because it will result in 432 error for 632 model:

if imow_action == IMowActions.START_MOWING or imow_action == IMowActions.START_MOWING_NOW

This now works for my iMow 632 model at least in the test script that you provided in GitHub. These are changes to be made in de imow api files, which I cannot do from here. Next step is to figure out what is needed in the HA integration to make sure the right action object is generated when HA passes its command. I believe it would be sufficient to just add the new command to HA. If we then fill in the appropriate date/time string under "duration" and leave "startpoint" blank, we should be good.

ChrisHaPunkt commented 1 year ago

Hi Jan, very valuable information. Thanks for your research. If I can spend some time I will look into integrating your findings into the API and HA integration. ( we are on vacation this week, so I'm not able to do work here ;-) )

ChrisHaPunkt commented 1 year ago

Hi @jwmaas, I'm currently on this. Because I'm not able to test the validity of the issued calls, can you give me a little more information about the following:

ChrisHaPunkt commented 1 year ago

Hi @jwmaas, please have a look into the following updated stihl-imow-webapi branch: https://github.com/ChrisHaPunkt/stihl-imow-webapi/tree/imow-600

It would be nice if you could test if the code from this updated branch is working with your mower as expected. I updated the readme and drastically increased the debug output with many more information. To use this, have a look into the updated Readme.md File.

Basically I implemented an additional action based on your findings. I decided to not use START_MOWING_NOW to have a clean reference on the issued string to the api and keep things consitent. We now have the following Actions available:

The following block shows the generated action_objects depending on the used IMowAction in intent call.

await mower.intent(IMowActions.START_MOWING, starttime="2023-08-12 20:50")
 # INFO:imow:{'actionName': 'startMowing', 'actionValue': '0000000442952635,2023-08-12 20:50'}  

await mower.intent(IMowActions.START_MOWING, endtime="2023-08-12 22:50")
 # INFO:imow:{'actionName': 'startMowing', 'actionValue': '0000000442952635,2023-08-21 15:54,2023-08-12 22:50'}  

await mower.intent(IMowActions.START_MOWING, starttime="2023-08-12 20:50", endtime="2023-08-12 22:50")
 # INFO:imow:{'actionName': 'startMowing', 'actionValue': '0000000442952635,2023-08-12 20:50,2023-08-12 22:50'}  

await mower.intent(IMowActions.START_MOWING_FROM_POINT, duration=50)
 # INFO:imow:{'actionName': 'startMowingFromPoint', 'actionValue': '0000000442952635,5.0,0'}  

await mower.intent(IMowActions.START_MOWING_FROM_POINT, startpoint=2)
 # INFO:imow:{'actionName': 'startMowingFromPoint', 'actionValue': '0000000442952635,3.0,2'}  

await mower.intent(IMowActions.START_MOWING_FROM_POINT, duration=50, startpoint=2)
 # INFO:imow:{'actionName': 'startMowingFromPoint', 'actionValue': '0000000442952635,5.0,2'}  

Happy Testing :)

jwmaas commented 1 year ago

Thanks for your efforts! Will test the different commands. Cannot do it tomorrow, wednesday is soonest. The command that worked was 0 as starting point and date/time as second parameter. I will let you know what works. Met groet, Jan Willem+31 651 119 718On 21 Aug 2023, at 16:15, ChrisHaPunkt @.***> wrote: Please have a look into the following updated stihl-imow-webapi branch: https://github.com/ChrisHaPunkt/stihl-imow-webapi/tree/imow-600 It would be nice if you could test if the code from this updated branch is working with your mower as expected. I updated the readme and drastically increased the debug output with many more information. To use this, have a look into the updated Readme.md File. Basically I implemented an additional action based on your findings. I decided to not use START_MOWING_NOW to have a clean reference on the issued string to the api and keep things consitent. We now have the following Actions available:

IMowActions.START_MOWING IMowActions.START_MOWING_FROM_POINT

The following block shows the generated action_objects depending on the used IMowAction in intent call. await mower.intent(IMowActions.START_MOWING, starttime="2023-08-12 20:50")

INFO:imow:{'actionName': 'startMowing', 'actionValue': '0000000442952635,2023-08-12 20:50'}

await mower.intent(IMowActions.START_MOWING, endtime="2023-08-12 22:50")

INFO:imow:{'actionName': 'startMowing', 'actionValue': '0000000442952635,2023-08-21 15:54,2023-08-12 22:50'}

await mower.intent(IMowActions.START_MOWING, starttime="2023-08-12 20:50", endtime="2023-08-12 22:50")

INFO:imow:{'actionName': 'startMowing', 'actionValue': '0000000442952635,2023-08-12 20:50,2023-08-12 22:50'}

await mower.intent(IMowActions.START_MOWING_FROM_POINT, duration=50)

INFO:imow:{'actionName': 'startMowingFromPoint', 'actionValue': '0000000442952635,5.0,0'}

await mower.intent(IMowActions.START_MOWING_FROM_POINT, startpoint=2)

INFO:imow:{'actionName': 'startMowingFromPoint', 'actionValue': '0000000442952635,3.0,2'}

await mower.intent(IMowActions.START_MOWING_FROM_POINT, duration=50, startpoint=2)

INFO:imow:{'actionName': 'startMowingFromPoint', 'actionValue': '0000000442952635,5.0,2'}

Happy Testing :)

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>

ChrisHaPunkt commented 1 year ago

No pressure ;-) But it would be nice if you, as well, could get some answers to my questions above regarding Intervall and, are the arguments considered start or end time in the cases.

jwmaas commented 1 year ago

Yes I will certainly. So far I have only used 00 10 20 30 40 50 fir time in minutes. I will try others too to see what happensMet groet, Jan Willem+31 651 119 718On 22 Aug 2023, at 00:08, ChrisHaPunkt @.***> wrote: No pressure ;-) But it would be nice if you, as well, could get some answers to my questions above regarding Intervall and, are the arguments considered start or end time in the cases.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>

jwmaas commented 1 year ago

I have done some more testing today. I did not get the current version of “iMow-webapi” to work from the test script when entering the startMowing command.

However, I did get “start_Mowing" to work when forcing the action_value in the init.py code in line 449.

There are two versions that worked:

  1. A version that only has mower_external_id and endtime. action_value = f"{mower_external_id},{'2023-08-23 21:45’}” I believe in your code when only endtime is given, you add a starttime automatically in line 435, but that is not necessary. I also think endtime and starttime are in the wrong order. Endtime comes first and is required and starttime is optional and when given is second (which is somehow logical if it is optional and endtime is required. And
  2. A version that has mower_external_id, endtime and starttime, in that order action_value = f"{mower_external_id},{'2023-08-23 21:45’},{'2023-08-23 21:30’}” Please note that endtime needs to be later in time than the starttime. I have also tried just entering “startMowing” and mower_external_id without endtime nor starttime. However this generates an error from the api. So this option does not work.

What I meant with the interval minutes 00, 10, 20 etcetera is that in the iMow app, when I want to start the mower an endtime is proposed by default. It is always a rounded number in 10 minute intervals. But I have discovered that these nice intervals are not necessary. Any HH:MM will work as long as it is after the current date and time.

Looking at the code, my amateur python hunch is that “endtime” needs to go into first_action_value_param, not “starttime” And equally if “starttime” is given, it needs to go into first_action_value_param. You do not need to insert a starttime if it is not provided by the user.

I’d like to compliment on all the code that you have written. I now understand python a little after following Harvards CD50 Python course, and you have truly done an impressive job. And so neat too!

Hope this feedback is useful. Do not hesitate to get back to me with any questions.

Jan Willem

On 22 Aug 2023, at 00:08, ChrisHaPunkt @.***> wrote:

No pressure ;-) But it would be nice if you, as well, could get some answers to my questions above regarding Intervall and, are the arguments considered start or end time in the cases.

— Reply to this email directly, view it on GitHub https://github.com/ChrisHaPunkt/ha-stihl-imow/issues/19#issuecomment-1687109523, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD3NVUTU6E64NIS7BX4GCPDXWPL4ZANCNFSM6AAAAAA3P5P7CE. You are receiving this because you were mentioned.

ChrisHaPunkt commented 1 year ago

Hi Jan,

I changed the order for start- and endtime. So if you use the following command:

await mower.intent(IMowActions.START_MOWING, endtime="2023-08-12 22:50")

something like this is created: {'actionName': 'startMowing', 'actionValue': '<extid>,2023-08-12 22:50'}

Antoher example when using start- and endtime is:

await mower.intent(IMowActions.START_MOWING, starttime="2023-08-12 20:50:30", endtime="2023-08-12 22:50:30")

the result will be created like this: {'actionName': 'startMowing', 'actionValue': '<extid>,2023-08-12 22:50,2023-08-12 20:50'}

if only starttime is given, an error will be raised.

Please make sure that you are using latest changes in the imow-600 brach. I.e download the branches zip file or checkout/pull the branch using git.

Chris

jwmaas commented 1 year ago

Chris, well done!

Test 1: with only endtime given works as expected from the testcode, exactly as you specified here below Test 2: also works as given below. The order of starttime and endtime does not matter, because your code puts them in the right order for the action object. So this is now fixed. Hurray!

I wonder if it is possible to mimic the app, when no endtime nor starttime is given. The app then provides a default endtime 2 hours (rounded) after the current date/time. Would it be possible to include this third option in the code? You had something similar for starttime, but that does not work in the api. But for endtime it will work and will be very useful, because the average HA user will find it difficult to programmatically add current date/time plus two hours in a automation as an endtime. No sweat if your time does not permit.

What is next step now? Is that including this module in Home Assistant and adding the command to the allowed list of HA commands? I have looked at the code, but do not understand HA well enough to figure it out very precisely. Maybe it is just adding the startMowing command to services.yaml and including endtime and starttime as options?

By the way, I checked into GitHub and wanted to download this branch into my Github Desktop, but this does not work for some reason. It asks for a password. Anyway, I did the work around, but eventually would like to start making suggested code via Github. Very unfamiliar steps for me. No idea how this works.

Let me know when I can do a Home Assistant test!

Many thanks, Jan Willem

On 25 Aug 2023, at 16:26, ChrisHaPunkt @.***> wrote:

Hi Jan,

I changed the order for start- and endtime. So if you use the following command:

await mower.intent(IMowActions.START_MOWING, endtime="2023-08-12 22:50") something like this is created: {'actionName': 'startMowing', 'actionValue': ',2023-08-12 22:50'}

Antoher example when using start- and endtime could be:

await mower.intent(IMowActions.START_MOWING, starttime="2023-08-12 20:50:30", endtime="2023-08-12 22:50:30") the result will be created like this: {'actionName': 'startMowing', 'actionValue': ',2023-08-12 22:50,2023-08-12 20:50'}

if only starttime is given, an error will be raised.

Please make sure that you are using latest changes in the imow-600 brach. I.e download the branches zip file https://github.com/ChrisHaPunkt/stihl-imow-webapi/archive/refs/heads/imow-600.zip or checkout the branch using git.

Chris

— Reply to this email directly, view it on GitHub https://github.com/ChrisHaPunkt/ha-stihl-imow/issues/19#issuecomment-1693449546, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD3NVURG4UYQK63UO7QJSY3XXCY2DANCNFSM6AAAAAA3P5P7CE. You are receiving this because you were mentioned.

ChrisHaPunkt commented 1 year ago

Nice to read, thanks :)

I created some defaults with 2 hours if only an endtime or no times at all are specified.

For the last use case, with no times given, we need to be aware, that determing the absolute "now" depends on the configured runtime timezone. I.e. if we are currently in "UTC+2" and the configured machines python timezone is UTC the new default endtime from our relative point of view is UTC+2, which is our relative "now" and not 2 hours from our "now" into the future. I checked, that should not be a problem for HA, because the HA containers python is aware of the local timezone.

The defaults added if times are not specified, see here here

Next steps are creating a new version from this updated webapi and afterwars making the HA Intergration use the new version. I already prepared this here

jwmaas commented 1 year ago

Hello Chris,

Overdelivering this is called!! Going beyond the call of duty.

When no ending and no start time is given, the code works like a charm in practice. I could already see it in the new code. Just a starttime is added functionality which does not exist in the App. But now it does and soon in HA! It also works. Well done.

I recognise the time issue which you described below. I have read a paper once about date and times in programming and how complicated this can get. I am not an IT professional, but I can see how this simple use case already requires some solid thinking.

I do not remember how to force Home Assistant to the new version of the webapi. But I guess there will be a HACS update available at some point. Do I delete the current integration, restart and then install the updated integration?

Best, Jan Willem

On 26 Aug 2023, at 10:39, ChrisHaPunkt @.***> wrote:

Nice to read, thanks :)

I created some defaults with 2 hours if only an endtime or no times at all are specified.

For the last use case, with no times given, we need to be aware, that determing the absolute "now" depends on the configured runtime timezone. I.e. if we are currently in "UTC+2" and the configured machines python timezone is UTC the new default endtime from our relative point of view is UTC+2, which is our relative "now" and not 2 hours from our "now" into the future. I checked, that should not be a problem for HA, because the HA containers python is aware of the local timezone.

The defaults added if times are not specified, see here here https://github.com/ChrisHaPunkt/stihl-imow-webapi/pull/14/commits/c421225d46727f65fe8020774860f0602df85978#diff-88d7e39b63e1473605f993ea62700524436597159de0690c990afb2239bd0f4dR447 Next steps are creating a new version from this updated webapi and afterwars making the HA Intergration use the new version. I already prepared this here https://github.com/ChrisHaPunkt/ha-stihl-imow/pull/20 — Reply to this email directly, view it on GitHub https://github.com/ChrisHaPunkt/ha-stihl-imow/issues/19#issuecomment-1694229612, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD3NVUVIRXTKT42QMUJEF3TXXGY3HANCNFSM6AAAAAA3P5P7CE. You are receiving this because you were mentioned.

ChrisHaPunkt commented 1 year ago

I created an updated version of die imow HA Integration which uses the new webapi. Feel free to test: https://github.com/ChrisHaPunkt/ha-stihl-imow/releases/tag/v1.0.0rc32

ChrisHaPunkt commented 1 year ago

The update should be available in HACS now if you hit the "Update information" button in the STIHL iMow repo

ChrisHaPunkt commented 1 year ago

Just update via HACS and restart HA.

jwmaas commented 1 year ago

Hi Chris, Update works like a charm.

I have tried starttime, endtime, both times and no times.

Using the HA services menu I figured there may be a way to use the default 30, 60, 90 mow times for startMowing command as well. But maybe this overly complicates things.

We can safely say that this is very successfully completed. Very happy here.

Another feature I use a lot is the Active Times schedule in the App, but it is cumbersome to use.

For each day of the week I have two active slots: from 11.00-18.00 and from 18:15-23.00. This is because sometimes during the day workers are present to work on the house or kids play in the garden. I then switch off the day time and switch on the evening time.

When I have time again, I will listen in on the app traffic to see which commands are being sent, when I switch the times on and off. I think these situations are very specific to each user and likely not easy to program, would require downloading the time slots per day and creating switches for each time slot. I my mind I see a nice Home Assistant cards with active times and switches, much more user friendly than the Stihl App.

And last, I just wonder if programming in python is your profession. It seems to be quite easy for you. It is all quite neat and tidy.

Many thanks, Jan Willem

On 26 Aug 2023, at 13:18, ChrisHaPunkt @.***> wrote:

The update should be available in HACS now if you hit the "Update information" button in the STIHL iMow repo

— Reply to this email directly, view it on GitHub https://github.com/ChrisHaPunkt/ha-stihl-imow/issues/19#issuecomment-1694268313, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD3NVUQ35A2HHFX4BODY6PDXXHLRDANCNFSM6AAAAAA3P5P7CE. You are receiving this because you were mentioned.

ChrisHaPunkt commented 1 year ago

For now, I would like to keep the separation between duration,startpoint and starttime, endtime with its differeent actions. Simply, because I cannot spend more time right now. Of course it's possible to add more logic and all of you are welcome to contribute the logic here via fork -> pull request ;-) https://github.com/ChrisHaPunkt/stihl-imow-webapi

The idea with active times and its toggles is a nice one. We should keep that in mind.

jwmaas commented 1 year ago

Fully understand. The number of users is limited and your time too. In addition, I have just thought of an approach in Home Assistant. This entails creating a daily automation which switches automatic mode on and off on the desired times, while in the app the whole schedule is programmed to on. That should do the trick and is much easier and customized. Thanks gor all your efforts!Met groet, Jan Willem+31 651 119 718On 26 Aug 2023, at 16:22, ChrisHaPunkt @.***> wrote: For now, I would like to keep the separation between duration,startpoint and starttime, endtime with its differeent actions. Simply, because I cannot spend more time right now. Of course it's possible to add more logic and all of you are welcome to contribute the logic here via fork -> pull request ;-) https://github.com/ChrisHaPunkt/stihl-imow-webapi The idea with active times and its toggles is a nice one. We should keep that in mind.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>