Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
235 changes: 118 additions & 117 deletions README.md

Large diffs are not rendered by default.

18 changes: 0 additions & 18 deletions custom_components/rivian/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

from collections.abc import Mapping
from typing import Any

from homeassistant.components.binary_sensor import BinarySensorEntity
Expand Down Expand Up @@ -76,20 +75,3 @@ def is_on(self) -> bool:
result = val in values
return result if not self.entity_description.negate else not result
return STATE_UNAVAILABLE

@property
def extra_state_attributes(self) -> Mapping[str, Any] | None:
"""Return the state attributes of the device."""
if self._aggregate:
return None
try:
entity = self.coordinator.data[self.entity_description.field]
if entity is None:
return "Binary Sensor Unavailable"
return {
"value": entity["value"],
"last_update": entity["timeStamp"],
"history": str(entity["history"]),
}
except KeyError:
return None
16 changes: 15 additions & 1 deletion custom_components/rivian/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,15 @@
icon="mdi:bluetooth",
entity_category=EntityCategory.DIAGNOSTIC,
),
RivianSensorEntityDescription(
key="vehicle_state_last_update_time",
translation_key="vehicle_state_last_update_time",
field=None,
device_class=SensorDeviceClass.TIMESTAMP,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
value_fn=lambda coor: coor.last_update_time,
),
),
"R1S": (
RivianSensorEntityDescription(
Expand Down Expand Up @@ -1033,7 +1042,12 @@
}

VEHICLE_STATE_API_FIELDS: Final[set[str]] = {
*(description.field for sensor in SENSORS.values() for description in sensor),
*(
description.field
for sensor in SENSORS.values()
for description in sensor
if description.field
),
*(
field
for sensors in BINARY_SENSORS.values()
Expand Down
16 changes: 10 additions & 6 deletions custom_components/rivian/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ class VehicleCoordinator(RivianDataUpdateCoordinator[dict[str, Any]]):

key = "vehicleState"
_update_interval_seconds = 15 * 60 # 15 minutes
_last_update_time: datetime | None = None

def __init__(
self,
Expand All @@ -271,6 +272,11 @@ def __init__(
self._unsub_handler: Coroutine[None, None, None] | None = None
self._awake = asyncio.Event()

@property
def last_update_time(self) -> datetime | None:
"""Return the last update time, if any."""
return self._last_update_time

async def _async_update_data(self) -> dict[str, Any]:
"""Get the latest data from Rivian."""
if not self.data or not self.last_update_success:
Expand Down Expand Up @@ -313,14 +319,13 @@ def _process_new_data(self, data: dict[str, Any]) -> None:

def _build_vehicle_info_dict(self, vijson: dict[str, Any]) -> dict[str, Any]:
"""Take the json output of vehicle_info and build a dictionary."""
items = {
k: v | ({"history": {v["value"]}} if "value" in v else {})
for k, v in vijson.items()
if v
}
items = {k: v for k, v in vijson.items() if v}

if items:
_LOGGER.debug("Vehicle %s updated: %s", self.vehicle_id, redact(items))
self._last_update_time = datetime.fromisoformat(
max(v["timeStamp"] for k, v in items.items())
)

if power_state := items.get("powerState"):
if power_state.get("value") == "sleep":
Expand All @@ -342,7 +347,6 @@ def _build_vehicle_info_dict(self, vijson: dict[str, Any]) -> dict[str, Any]:
value = items[key].get("value")
if str(value).lower() in INVALID_SENSOR_STATES and key in prev_items:
new_data[key] = prev_items[key]
new_data[key]["history"] |= prev_items.get(key, {}).get("history", set())

return new_data

Expand Down
18 changes: 2 additions & 16 deletions custom_components/rivian/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

from collections.abc import Mapping
from typing import Any

from homeassistant.components.device_tracker import SourceType, TrackerEntity
Expand All @@ -26,14 +25,12 @@ async def async_setup_entry(
vehicles: dict[str, Any] = data[ATTR_VEHICLE]
coordinators: dict[str, VehicleCoordinator] = data[ATTR_COORDINATOR][ATTR_VEHICLE]

entities = [
async_add_entities(
RivianDeviceEntity(
coordinators[vehicle_id], entry, LOCATION_DESCRIPTION, vehicle
)
for vehicle_id, vehicle in vehicles.items()
]

async_add_entities(entities)
)


class RivianDeviceEntity(RivianVehicleEntity, TrackerEntity):
Expand Down Expand Up @@ -73,17 +70,6 @@ def source_type(self) -> SourceType:
"""Return the source type, eg gps or router, of the device."""
return SourceType.GPS

# @property
# def location_accuracy(self) -> int:
# return self._tracker_data[6]

@property
def extra_state_attributes(self) -> Mapping[str, Any]:
"""Return the state attributes of the device."""
return {
"last_update": self._tracker_data["timeStamp"],
}

@callback
def _handle_coordinator_update(self) -> None:
"""Respond to a DataUpdateCoordinator update."""
Expand Down
19 changes: 0 additions & 19 deletions custom_components/rivian/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,6 @@ def native_value(self) -> str | None:
self.options.append(rval)
return rval

@property
def extra_state_attributes(self) -> Mapping[str, Any] | None:
"""Return the state attributes of the device."""
try:
entity = self.coordinator.data[self.entity_description.field]
if entity is None:
return None
if self.entity_description.value_lambda is None:
return {
"last_update": entity["timeStamp"],
}
return {
"native_value": entity["value"],
"last_update": entity["timeStamp"],
"history": str(entity["history"]),
}
except KeyError:
return None


class RivianChargingSensorEntity(RivianChargingEntity, SensorEntity):
"""Representation of a Rivian charging sensor entity."""
Expand Down
3 changes: 3 additions & 0 deletions custom_components/rivian/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
"plugged_in": "Plugged in",
"charging": "Charging"
}
},
"vehicle_state_last_update_time": {
"name": "Vehicle state last update time"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/rivian/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
"plugged_in": "Plugged in",
"charging": "Charging"
}
},
"vehicle_state_last_update_time": {
"name": "Vehicle state last update time"
}
}
},
Expand Down