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

Tesla integration ignores configured scan_interval... at first #37340

Closed
boekabart opened this issue Jul 2, 2020 · 10 comments · Fixed by #37434
Closed

Tesla integration ignores configured scan_interval... at first #37340

boekabart opened this issue Jul 2, 2020 · 10 comments · Fixed by #37434

Comments

@boekabart
Copy link

The problem

After starting Home Assistant, the tesla component initialises and starts polling the general vehicles api every minute, to see if the car(s) are online (OK).
If a car is online, it immediately polls its vehicles_data (OK).
And then keeps polling the vehicles api (OK).
Then every 660 seconds, regardless of the configured scan_interval, it checks the vehicle_data again (NOK).
I have scan_interval configured to 1800 because empirical data shows that otherwise, my car will never fall asleep.
In my case, after half an hour (coincidence?) or to be precise, 1830 ish seconds, hass refreshes the API key.
Immediately after that, it logs 'Changing scan interval from 1800 to 1800', but calls the vehicle_data once more at exactly 11 minutes after it last did so.

Exactly 11 minutes after that (12:42:25), it says it 'skips' the update with state online, and one more minute later (12:43:25), the vehicle is offline (asleep).

So it seems that the setting does work, but that it takes a while (until the token refresh, of which I'm not sure what triggers it) for it to actually be used.

Note that 660 seconds might be a too-low default; especially if some other application (Tezlab, Teslafi, ABRP or so) are also polling the vehicle, this may very well to a nover-sleeping car.

Environment

  • Home Assistant Core release with the issue: 0.112
  • Last working Home Assistant Core release (if known): unknwon
  • Operating environment (OS/Container/Supervised/Core): R.Pi 3+ (OS+Core)
  • Integration causing this issue: Tesla
  • Link to integration documentation on our website: https://www.home-assistant.io/integrations/tesla/

Problem-relevant configuration.yaml

tesla:
  username: my_email_address@gmail.com
  password: !secret tesla_pass
  scan_interval: 1800

Traceback/Error logs

11:57:04 DEBUG (MainThread) [teslajsonpy.connection] Requesting new oauth token using password	 token
11:57:42 DEBUG (MainThread) [teslajsonpy.connection] Connecting with existing access token	 token
11:57:50 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles/vehicleId_obfuscated/vehicle_data None	vehicle_data
11:57:52 DEBUG (MainThread) [homeassistant.components.tesla] Connected to the Tesla API.
11:57:52 DEBUG (MainThread) [homeassistant.components.tesla] Loading sensor
11:57:52 DEBUG (MainThread) [homeassistant.components.tesla] Loading lock
11:57:52 DEBUG (MainThread) [homeassistant.components.tesla] Loading climate
11:57:52 DEBUG (MainThread) [homeassistant.components.tesla] Loading binary_sensor
11:57:52 DEBUG (MainThread) [homeassistant.components.tesla] Loading device_tracker
11:57:52 DEBUG (MainThread) [homeassistant.components.tesla] Loading switch
12:09:02 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles/vehicleId_obfuscated/vehicle_data None	vehicle_data
12:20:06 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles/vehicleId_obfuscated/vehicle_data None	vehicle_data
12:28:19 DEBUG (MainThread) [teslajsonpy.connection] Requesting new oauth token using refresh_token	 token
12:28:21 DEBUG (MainThread) [homeassistant.components.tesla] Saving new tokens in config_entry	 token
12:28:21 DEBUG (MainThread) [homeassistant.components.tesla] Changing scan_interval from 1800 to 1800	interval
12:31:16 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles/vehicleId_obfuscated/vehicle_data None	vehicle_data

Additional information

@boekabart
Copy link
Author

I can confirm that bringing the car back online after a longer time (by opening the tesla app) immediately triggers an update (OK), and that keeping the car awake using the Tesla app, does not lead to another poll after 11 minutes. So indeed, AFTER that token update, the 1800 seconds does take effect. But not right away after startup!

@probot-home-assistant
Copy link

Hey there @zabuldon, @alandtse, mind taking a look at this issue as its been labeled with an integration (tesla) you are listed as a codeowner for? Thanks!
(message by CodeOwnersMention)

@boekabart
Copy link
Author

boekabart commented Jul 2, 2020

What the? it suddenly reverted back to 660 again: Here are the polls, the poll at 13:48 is OK (30 min), but the one at 13:59 wasn't supposed to happen!

13:18:06 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles/00000000000000000/vehicle_data None
13:19:06 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:20:06 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:21:07 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:22:08 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:23:09 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:24:10 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:25:11 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:26:12 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:27:12 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:28:14 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:29:15 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:30:15 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:31:17 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:32:18 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:33:19 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:34:20 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:35:21 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:36:21 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:37:23 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:38:30 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:39:33 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:40:34 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:41:36 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:42:38 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:43:40 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:44:42 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:45:44 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:46:46 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:47:48 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:48:15 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles/00000000000000000/vehicle_data None
13:48:50 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:49:52 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:50:54 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:51:56 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:52:58 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:54:00 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:55:02 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:56:04 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:57:06 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:58:08 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:59:10 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
13:59:22 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles/00000000000000000/vehicle_data None
14:00:12 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:01:14 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:02:16 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:03:18 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:04:20 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:05:21 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:06:24 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:07:26 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:08:28 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:09:30 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:10:24 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles/00000000000000000/vehicle_data None
14:10:32 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:11:34 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:12:36 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:13:38 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:14:40 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:15:42 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:16:44 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:17:46 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:18:46 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:19:50 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:20:52 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:21:26 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles/00000000000000000/vehicle_data None
14:21:54 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:22:56 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:23:58 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:25:00 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:26:02 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:27:04 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:28:06 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:29:08 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:30:10 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:31:10 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:32:12 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:32:29 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles/00000000000000000/vehicle_data None
14:33:14 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:34:16 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:35:17 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:36:18 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:37:19 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:38:20 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:39:21 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:40:22 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:41:23 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None
14:42:24 DEBUG (MainThread) [teslajsonpy.connection] get: https://owner-api.teslamotors.com/api/1/vehicles None

@alandtse
Copy link
Contributor

alandtse commented Jul 3, 2020

Thanks. I'll take a look to see why the value would reset. It should only go to 660 if it can't find the setting, so perhaps when we refresh the token it's overwriting the value so it's lost. Can you confirm in your storage/core.config_entries that the option is being saved properly even when it's reverting to 660?

For the default, we probably won't change it just because it may conflict with people using other tracking items. We assume advanced users are doing what you are doing and upping the value to work with their other items. For default, we just assume people only use the HA app and the Tesla app. Of course, if testing shows that 660 doesn't work as a default, we can continue to up it but again, there's a downside to delaying it.

As to why we reset the oauth token, on reboot the component doesn't know how long the token has been in play, so assumes it needs to reset it after set default. We could probably rewrite the component to save when the token was granted so it avoids that in the future. However, that would only hide the issue you found of the random reset. :) I'll fix your issue first before trying to fix the oauth refresh.

@alandtse
Copy link
Contributor

alandtse commented Jul 3, 2020

Also, are you filtering out the teslajsonpy.controller? That will provide more information on what state it's in.

@alandtse
Copy link
Contributor

alandtse commented Jul 3, 2020

Ok, it's likely because it's currently trying to go back to sleep after being in a driving/active state. There's some hardcoded values in the controller to handle the immediate case of driving and letting a car that just parked go back to sleep. The user set scan interval won't be checked during this period while it's in this adaptive state. Again the controller may be saying what's going on and this period should only have lasted for five minutes after the car goes inactive so there may be a bug.

We can probably change the sleep period to allow the scan interval to override it if it's larger. However, will need the controller logs to know if that's what is really happening.

@alandtse
Copy link
Contributor

alandtse commented Jul 3, 2020

Fix staged. Would want the controller logs to confirm though. Or you can try loading it manually.

@boekabart
Copy link
Author

boekabart commented Jul 3, 2020

storage/core.config_entries:

            {
                "connection_class": "cloud_poll",
                "data": {
                    "access_token": "360af....0d39c7cd",
                    "token": "310b1686d....5ddfc2fd1ce4af"
                },
                "domain": "tesla",
                "entry_id": "603dde34080343baa488e807d81cc8e5",
                "options": {
                    "scan_interval": 1800
                },
                "source": "import",
                "system_options": {
                    "disable_new_entities": false
                },
                "title": ".....@gmail.com",
                "unique_id": null,
                "version": 1
            },

I've done a grep tesla on my entire log file and properly (I hope) obfuscated all sensitive data [log removed].

@alandtse
Copy link
Contributor

alandtse commented Jul 3, 2020

Ok I've confirmed it. You can delete your logs. Thanks.

alandtse added a commit to alandtse/home-assistant that referenced this issue Jul 3, 2020
@alandtse alandtse mentioned this issue Jul 3, 2020
20 tasks
bdraco pushed a commit that referenced this issue Jul 3, 2020
* Bump teslajsonpy to 0.9.1
closes #37340

* Bump teslajsonpy to 0.9.2
balloob pushed a commit that referenced this issue Jul 3, 2020
* Bump teslajsonpy to 0.9.1
closes #37340

* Bump teslajsonpy to 0.9.2
@boekabart
Copy link
Author

Presuming it's in 112.2, it works like a charm. The car now falls asleep nicely... about 18 minutes after waking it with the Tesla app, which immediately causes Hass to fetch data from it (OK)

So in MY case, not sure why, but the car needs a break in polling of > 15 minutes in order to fall asleep.

I had to disable the A-Better-Route-planner integration, as it polls the car every 15 minutes, and that caused the car never to fall asleep. When I disconnected that app yesterday afternoon, it fell asleep within 20 minutes, after NOT having slept in days.

So I really think some due diligence is in place: did Tesla maybe change this 'timeout'?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants