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

Allow older vehicles to sleep in Teslemetry #117229

Draft
wants to merge 10 commits into
base: dev
Choose a base branch
from

Conversation

Bre77
Copy link
Contributor

@Bre77 Bre77 commented May 11, 2024

Proposed change

Tesla vehicles manufactured before 2021 cannot sleep if they are polled by API constantly. This PR adds logic to the coordinator to introduce a 15 minute wait after 15 minutes in inactivity. This 15 minute break is enough time to let the vehicle sleep, and then the coordinator can revert back to frequent polling so it knows when the vehicle wakes up.

The age of the vehicle is determined the 10th character of the VIN, as this is a code for the year it was manufactured.

Library bump: Teslemetry/python-tesla-fleet-api@v0.4.9...v0.5.12

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@@ -62,21 +65,42 @@ def __init__(
)
self.api = api
self.data = flatten(product)
self.pre2021 = product["vin"][9] <= "L"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense for the "age check" to live in the library instead?
Seems a bit weird to parse the VIN in the coordinator.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll add this line as a prop in the library.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still here?

@Bre77 Bre77 marked this pull request as draft May 11, 2024 21:41
@Bre77 Bre77 marked this pull request as ready for review May 12, 2024 02:32
@Bre77 Bre77 marked this pull request as draft May 18, 2024 21:48
@Bre77 Bre77 marked this pull request as ready for review May 20, 2024 00:49
@Bre77 Bre77 requested a review from TheJulianJES May 20, 2024 04:07
@Bre77 Bre77 marked this pull request as draft May 24, 2024 10:35
@Bre77
Copy link
Contributor Author

Bre77 commented May 25, 2024

@coderabbitai review

Copy link

coderabbitai bot commented May 25, 2024

Walkthrough

The update introduces enhancements to the TeslemetryVehicleDataCoordinator class, including new attributes and logic to better handle pre-2021 Tesla vehicles' sleep behavior. Additionally, the tesla-fleet-api dependency was updated to version 0.5.12, and VIN values in test fixtures were standardized. New tests were added to validate vehicle sleep behavior, ensuring efficient data retrieval.

Changes

File Path Change Summary
homeassistant/components/teslemetry/coordinator.py Enhanced TeslemetryVehicleDataCoordinator to handle pre-2021 vehicles, added pre2021 and last_active attributes, and updated update logic and exception handling.
homeassistant/components/teslemetry/manifest.json Updated tesla-fleet-api version from 0.4.9 to 0.5.12.
requirements_all.txt Updated tesla-fleet-api version from 0.4.9 to 0.5.12.
requirements_test_all.txt Updated tesla-fleet-api version from 0.4.9 to 0.5.12.
tests/components/teslemetry/fixtures/products.json Changed vin field value to "LRWXF7EK4KC700000".
tests/components/teslemetry/fixtures/vehicle_data.json Updated vin field to "LRWXF7EK4KC700000".
tests/components/teslemetry/fixtures/vehicle_data_alt.json Changed vin to "LRWXF7EK4KC700000" and is_user_present to true.
tests/components/teslemetry/snapshots/test_climate.ambr Updated unique_id field values to "LRWXF7EK4KC700000-driver_temp".
tests/components/teslemetry/snapshots/test_select.ambr Changed unique_id values for seat heater and steering wheel heat level components.
tests/components/teslemetry/snapshots/test_sensor.ambr Updated unique_id values for various states and measurements to "LRWXF7EK4KC700000".
tests/components/teslemetry/test_init.py Renamed "Tessie" to "Teslemetry" in the docstring, imported constants, and added test_vehicle_sleep function.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
homeassistant/components/teslemetry/coordinator.py (1)

Line range hint 53-104: Effectively handles sleep logic for pre-2021 vehicles. Consider moving VIN parsing to the library for cleaner separation of concerns.

@Bre77 Bre77 marked this pull request as ready for review May 25, 2024 23:00

async def _async_update_data(self) -> dict[str, Any]:
"""Update vehicle data using Teslemetry API."""

self.update_interval = VEHICLE_INTERVAL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're already setting this in __init__?

@@ -4,7 +4,7 @@
"id": 1234,
"user_id": 1234,
"vehicle_id": 1234,
"vin": "VINVINVIN",
"vin": "LRWXF7EK4KC700000",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a new car instead of modifying the existing (so there's another set of tests specifically for "old" cars?

@home-assistant home-assistant bot marked this pull request as draft May 26, 2024 10:27
@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

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

Successfully merging this pull request may close these issues.

None yet

3 participants