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
14 changes: 8 additions & 6 deletions custom_components/toyota_na/const.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from toyota_na.vehicle.base_vehicle import VehicleFeatures

from homeassistant.components.binary_sensor import BinarySensorDeviceClass
from homeassistant.components.sensor import SensorStateClass
from homeassistant.const import PERCENTAGE, UnitOfPressure
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.const import PERCENTAGE, UnitOfPressure, UnitOfTime

from toyota_na.vehicle.base_vehicle import RemoteRequestCommand

Expand Down Expand Up @@ -305,7 +305,8 @@
"electric": True,
},
{
"state_class": SensorStateClass.MEASUREMENT,
"device_class": SensorDeviceClass.TIMESTAMP,
"state_class": None,
"icon": "mdi:gauge",
"feature": VehicleFeatures.LastTimeStamp,
"name": "Last Update Timestamp",
Expand All @@ -314,7 +315,8 @@
"electric": False,
},
{
"state_class": SensorStateClass.MEASUREMENT,
"device_class": SensorDeviceClass.TIMESTAMP,
"state_class": None,
"icon": "mdi:gauge",
"feature": VehicleFeatures.LastTirePressureTimeStamp,
"name": "Last Tire Pressure Update Timestamp",
Expand Down Expand Up @@ -345,7 +347,7 @@
"icon": "mdi:clock-outline",
"feature": VehicleFeatures.RemainingChargeTime,
"name": "Remaining Charge Time",
"unit": "",
"unit": UnitOfTime.MINUTES,
"subscription": True,
"electric": True,
},
Expand All @@ -368,7 +370,7 @@
"electric": True,
},
{
"state_class": SensorStateClass.MEASUREMENT,
"state_class": None,
"icon": "mdi:ev-plug-type1",
"feature": VehicleFeatures.ConnectorStatus,
"name": "Connector Status",
Expand Down
10 changes: 8 additions & 2 deletions custom_components/toyota_na/patch_seventeen_cy.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,16 @@ def _parse_electric_status(self, electric_status: dict) -> None:
self._features[VehicleFeatures.ChargeDistanceAC] = ToyotaNumeric(chargeInfo.get("evDistanceAC"), chargeInfo.get("evDistanceUnit"))
self._features[VehicleFeatures.ChargeLevel] = ToyotaNumeric(chargeInfo.get("chargeRemainingAmount"), "%")
self._features[VehicleFeatures.PlugStatus] = ToyotaNumeric(chargeInfo.get("plugStatus"), "")
self._features[VehicleFeatures.RemainingChargeTime] = ToyotaNumeric(chargeInfo.get("remainingChargeTime"), "")
self._features[VehicleFeatures.RemainingChargeTime] = ToyotaNumeric(chargeInfo.get("remainingChargeTime"), "min")
self._features[VehicleFeatures.EvTravelableDistance] = ToyotaNumeric(chargeInfo.get("evTravelableDistance"), "")
self._features[VehicleFeatures.ChargeType] = ToyotaNumeric(chargeInfo.get("chargeType"), "")
self._features[VehicleFeatures.ConnectorStatus] = ToyotaNumeric(chargeInfo.get("connectorStatus"), "")
raw_status = chargeInfo.get("connectorStatus")
connector_status = {
2: "Disconnected",
4: "Unlocked",
5: "Locked",
}.get(raw_status if isinstance(raw_status, int) else int(raw_status) if raw_status is not None and str(raw_status).isdigit() else raw_status, raw_status)
self._features[VehicleFeatures.ConnectorStatus] = ToyotaNumeric(connector_status, "")
self._features[VehicleFeatures.ChargingStatus] = ToyotaOpening(chargeInfo.get("connectorStatus") != 5)

#
Expand Down
10 changes: 8 additions & 2 deletions custom_components/toyota_na/patch_seventeen_cy_plus.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,16 @@ def _parse_electric_status(self, electric_status: dict) -> None:
self._features[VehicleFeatures.ChargeDistanceAC] = ToyotaNumeric(chargeInfo.get("evDistanceAC"), chargeInfo.get("evDistanceUnit"))
self._features[VehicleFeatures.ChargeLevel] = ToyotaNumeric(chargeInfo.get("chargeRemainingAmount"), "%")
self._features[VehicleFeatures.PlugStatus] = ToyotaNumeric(chargeInfo.get("plugStatus"), "")
self._features[VehicleFeatures.RemainingChargeTime] = ToyotaNumeric(chargeInfo.get("remainingChargeTime"), "")
self._features[VehicleFeatures.RemainingChargeTime] = ToyotaNumeric(chargeInfo.get("remainingChargeTime"), "min")
self._features[VehicleFeatures.EvTravelableDistance] = ToyotaNumeric(chargeInfo.get("evTravelableDistance"), "")
self._features[VehicleFeatures.ChargeType] = ToyotaNumeric(chargeInfo.get("chargeType"), "")
self._features[VehicleFeatures.ConnectorStatus] = ToyotaNumeric(chargeInfo.get("connectorStatus"), "")
raw_status = chargeInfo.get("connectorStatus")
connector_status = {
2: "Disconnected",
4: "Unlocked",
5: "Locked",
}.get(raw_status if isinstance(raw_status, int) else int(raw_status) if raw_status is not None and str(raw_status).isdigit() else raw_status, raw_status)
self._features[VehicleFeatures.ConnectorStatus] = ToyotaNumeric(connector_status, "")
self._features[VehicleFeatures.ChargingStatus] = ToyotaOpening(chargeInfo.get("connectorStatus") != 5)

#
Expand Down
16 changes: 13 additions & 3 deletions custom_components/toyota_na/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from toyota_na.vehicle.base_vehicle import ToyotaVehicle, VehicleFeatures
from toyota_na.vehicle.entity_types.ToyotaNumeric import ToyotaNumeric

from homeassistant.components.sensor import SensorStateClass
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfLength
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -43,7 +43,8 @@ async def async_setup_entry(
cast(VehicleFeatures, feature_sensor["feature"]),
cast(str, entity_config["icon"]),
cast(str, entity_config["unit"]),
cast(SensorStateClass, entity_config["state_class"]),
cast(SensorStateClass, entity_config.get("state_class")),
cast(SensorDeviceClass, entity_config.get("device_class")),
coordinator,
entity_config["name"],
vehicle.vin,
Expand All @@ -62,23 +63,32 @@ def __init__(
vehicle_feature: VehicleFeatures,
icon: str,
unit_of_measurement: str,
state_class: Union[SensorStateClass, str],
state_class: Union[SensorStateClass, str, None],
device_class: Union[SensorDeviceClass, str, None],
*args: Any,
):
super().__init__(*args)
self._icon = icon
self._state_class = state_class
self._unit_of_measurement = unit_of_measurement
self._vehicle_feature = vehicle_feature
self._device_class = device_class

@property
def icon(self) -> str:
return self._icon

@property
def device_class(self):
return self._device_class

@property
def state(self):
feat = cast(ToyotaNumeric, self.feature(self._vehicle_feature))
if feat:
if self._device_class == SensorDeviceClass.TIMESTAMP and feat.value is not None:
import datetime
return datetime.datetime.fromtimestamp(feat.value, datetime.timezone.utc)
return feat.value

@property
Expand Down