-
Notifications
You must be signed in to change notification settings - Fork 191
feat: add DayTripInfoEntity for per-trip sensor data #1698
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
Open
janfrederik
wants to merge
1
commit into
Hyundai-Kia-Connect:master
Choose a base branch
from
flaksit:feat/day-trip-info
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
|
|
||
| import logging | ||
| from typing import Final | ||
| from datetime import date | ||
| from datetime import date, datetime, timedelta | ||
|
|
||
| from hyundai_kia_connect_api import Vehicle | ||
|
|
||
|
|
@@ -428,6 +428,14 @@ async def async_setup_entry( | |
| entities.append( | ||
| VehicleEntity(coordinator, coordinator.vehicle_manager.vehicles[vehicle_id]) | ||
| ) | ||
| # day_trip_info starts None and is populated by the coordinator's | ||
| # first update — register unconditionally so the entity exists even | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this mean it will show up for people with cars that don't support it? |
||
| # when no trip data has been fetched yet (TRIP-01). | ||
| entities.append( | ||
| DayTripInfoEntity( | ||
| coordinator, coordinator.vehicle_manager.vehicles[vehicle_id] | ||
| ) | ||
| ) | ||
| async_add_entities(entities) | ||
| return True | ||
|
|
||
|
|
@@ -588,3 +596,93 @@ def state_attributes(self): | |
| @property | ||
| def unique_id(self): | ||
| return f"{DOMAIN}-todays-daily-driving-stats-{self.vehicle.id}" | ||
|
|
||
|
|
||
| class DayTripInfoEntity(SensorEntity, HyundaiKiaConnectEntity): | ||
| """Per-trip sensor for today (TRIP-01). | ||
|
|
||
| State is the number of trips today; attributes carry the full per-trip list | ||
| (start time, drive/idle time, distance, avg/max speed). The underlying | ||
| ``vehicle.day_trip_info`` is populated by the coordinator on each cached-state | ||
| poll cycle via ``VehicleManager.update_day_trip_info()``; before the first | ||
| successful fetch (or when the trip endpoint is unsupported / returns | ||
| NoDataFound for this region/firmware) the value is ``None`` — both | ||
| ``state`` and ``state_attributes`` handle that case so the entity remains | ||
| available with state ``0`` instead of going unavailable. | ||
| """ | ||
|
|
||
| _attr_translation_key = "day_trip_info" | ||
| _attr_icon = "mdi:calendar" | ||
|
|
||
| def __init__(self, coordinator, vehicle: Vehicle): | ||
| super().__init__(coordinator, vehicle) | ||
|
|
||
| @property | ||
| def state(self): | ||
| if self.vehicle.day_trip_info is None: | ||
| return 0 | ||
| return len(self.vehicle.day_trip_info.trip_list) | ||
|
|
||
| @property | ||
| def state_attributes(self): | ||
| if self.vehicle.day_trip_info is None: | ||
| return {} | ||
| info = self.vehicle.day_trip_info | ||
| date_iso = _iso_date(info.yyyymmdd) | ||
| summary = info.summary | ||
| return { | ||
| "date": date_iso, | ||
| "summary": { | ||
| "drive_time": summary.drive_time, | ||
| "idle_time": summary.idle_time, | ||
| "distance": summary.distance, | ||
| "avg_speed": summary.avg_speed, | ||
| "max_speed": summary.max_speed, | ||
| } | ||
| if summary is not None | ||
| else None, | ||
| "trip_list": [ | ||
| { | ||
| "start_time": _iso_datetime(date_iso, t.hhmmss), | ||
| "end_time": _iso_datetime_add( | ||
| date_iso, t.hhmmss, (t.drive_time or 0) + (t.idle_time or 0) | ||
| ), | ||
| "drive_time": t.drive_time, | ||
| "idle_time": t.idle_time, | ||
| "distance": t.distance, | ||
| "avg_speed": t.avg_speed, | ||
| "max_speed": t.max_speed, | ||
| } | ||
| for t in info.trip_list | ||
| ], | ||
| } | ||
|
|
||
| @property | ||
| def unique_id(self): | ||
| return f"{DOMAIN}-day-trip-info-{self.vehicle.id}" | ||
|
|
||
|
|
||
| def _iso_date(yyyymmdd): | ||
| """Convert YYYYMMDD packed string to ISO YYYY-MM-DD, or None if unset.""" | ||
| if not yyyymmdd or len(yyyymmdd) != 8: | ||
| return None | ||
| return f"{yyyymmdd[0:4]}-{yyyymmdd[4:6]}-{yyyymmdd[6:8]}" | ||
|
|
||
|
|
||
| def _iso_datetime(date_iso, hhmmss): | ||
| """Combine an ISO date with a packed HHMMSS string into ISO 8601.""" | ||
| if not date_iso or not hhmmss or len(hhmmss) != 6: | ||
| return None | ||
| return f"{date_iso}T{hhmmss[0:2]}:{hhmmss[2:4]}:{hhmmss[4:6]}" | ||
|
|
||
|
|
||
| def _iso_datetime_add(date_iso, hhmmss, minutes): | ||
| """Return the naive ISO 8601 timestamp `minutes` after (date_iso, hhmmss).""" | ||
| start_iso = _iso_datetime(date_iso, hhmmss) | ||
| if start_iso is None: | ||
| return None | ||
| try: | ||
| start = datetime.fromisoformat(start_iso) | ||
| except (TypeError, ValueError): | ||
| return None | ||
| return (start + timedelta(minutes=int(minutes))).isoformat(timespec="seconds") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on this I wonder if we should have setting to turn this on and off? As well do we want to be polling this every time using API calls?