diff --git a/README.md b/README.md index 73ba668..7f54a3f 100644 --- a/README.md +++ b/README.md @@ -41,13 +41,9 @@ trigger: - platform: state entity_id: binary_sensor.stromer_bike_lock to: 'on' - - platform: numeric_state - entity_id: sensor.s_eve_speed - for: - hours: 0 - minutes: 10 - seconds: 0 - below: '5' + - platform: template + value_template: >- + {{ now().timestamp() - as_timestamp(states.sensor.stromer_last_status_push.state) > 600 }} condition: [] action: - service: homeassistant.turn_off @@ -66,6 +62,9 @@ trigger: - platform: state entity_id: binary_sensor.stromer_bike_lock to: 'off' + - platform: template + value_template: >- + {{ now().timestamp() - as_timestamp(states.sensor.stromer_last_status_push.state) > 600 }} condition: [] action: - service: homeassistant.turn_on diff --git a/custom_components/stromer/coordinator.py b/custom_components/stromer/coordinator.py index 4103457..a59e95f 100644 --- a/custom_components/stromer/coordinator.py +++ b/custom_components/stromer/coordinator.py @@ -31,6 +31,10 @@ async def _async_update_data(self) -> StromerData: try: await self.stromer.stromer_update() + # Rewrite position["rcvts"] as this key exists in status + if "rcvts" in self.stromer.position: + self.stromer.position["rcvts_pos"] = self.stromer.position.pop("rcvts") + bike_data = self.stromer.bike bike_data.update(self.stromer.status) bike_data.update(self.stromer.position) diff --git a/custom_components/stromer/manifest.json b/custom_components/stromer/manifest.json index 04f260a..cd89967 100644 --- a/custom_components/stromer/manifest.json +++ b/custom_components/stromer/manifest.json @@ -1,7 +1,7 @@ { "domain": "stromer", "name": "Stromer e-bike", - "version": "0.0.7", + "version": "0.1.0", "documentation": "https://github.com/CoMPaTech/stromer", "requirements": [], "codeowners": ["@CoMPaTech"], diff --git a/custom_components/stromer/sensor.py b/custom_components/stromer/sensor.py index a15f04a..c6b2fad 100644 --- a/custom_components/stromer/sensor.py +++ b/custom_components/stromer/sensor.py @@ -7,8 +7,10 @@ from homeassistant.const import (LENGTH_KILOMETERS, PERCENTAGE, POWER_WATT, PRESSURE_BAR, SPEED_KILOMETERS_PER_HOUR, TEMP_CELSIUS, TIME_SECONDS) +from homeassistant.helpers.entity import EntityCategory from custom_components.stromer.coordinator import StromerDataUpdateCoordinator +from datetime import datetime, timezone from .const import DOMAIN from .entity import StromerEntity @@ -133,6 +135,27 @@ device_class=None, state_class=SensorStateClass.MEASUREMENT, ), + SensorEntityDescription( + key="rcvts", + name="Last status push", + native_unit_of_measurement=None, + device_class=SensorDeviceClass.TIMESTAMP, + entity_category=EntityCategory.DIAGNOSTIC, + ), + SensorEntityDescription( + key="rcvts_pos", + name="Last position push", + native_unit_of_measurement=None, + device_class=SensorDeviceClass.TIMESTAMP, + entity_category=EntityCategory.DIAGNOSTIC, + ), + SensorEntityDescription( + key="timets", + name="Last position time", + native_unit_of_measurement=None, + device_class=SensorDeviceClass.TIMESTAMP, + entity_category=EntityCategory.DIAGNOSTIC, + ), ) @@ -172,7 +195,22 @@ def __init__( self._attr_unique_id = f"{device_id}-{description.key}" self._attr_name = (f"{coordinator.data.bike_name} {description.name}").lstrip() + @staticmethod + def _ensure_timezone(timestamp: datetime | None) -> datetime | None: + """Calculate days left until domain expires.""" + if timestamp is None: + return None + + # If timezone info isn't provided by the Whois, assume UTC. + if timestamp.tzinfo is None: + return timestamp.replace(tzinfo=timezone.utc) + + return timestamp + @property - def state(self): + def native_value(self): """Return the state of the sensor.""" + if self.entity_description.device_class == SensorDeviceClass.TIMESTAMP: + return self._ensure_timezone(datetime.fromtimestamp(int(self._coordinator.data.bikedata.get(self._ent)))) + return self._coordinator.data.bikedata.get(self._ent)