Skip to content

Commit 4d34d03

Browse files
authored
Merge pull request #9 from CoMPaTech/last_update
Add last update sensors
2 parents 50cd3df + eeb2410 commit 4d34d03

File tree

4 files changed

+50
-9
lines changed

4 files changed

+50
-9
lines changed

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,9 @@ trigger:
4141
- platform: state
4242
entity_id: binary_sensor.stromer_bike_lock
4343
to: 'on'
44-
- platform: numeric_state
45-
entity_id: sensor.s_eve_speed
46-
for:
47-
hours: 0
48-
minutes: 10
49-
seconds: 0
50-
below: '5'
44+
- platform: template
45+
value_template: >-
46+
{{ now().timestamp() - as_timestamp(states.sensor.stromer_last_status_push.state) > 600 }}
5147
condition: []
5248
action:
5349
- service: homeassistant.turn_off
@@ -66,6 +62,9 @@ trigger:
6662
- platform: state
6763
entity_id: binary_sensor.stromer_bike_lock
6864
to: 'off'
65+
- platform: template
66+
value_template: >-
67+
{{ now().timestamp() - as_timestamp(states.sensor.stromer_last_status_push.state) > 600 }}
6968
condition: []
7069
action:
7170
- service: homeassistant.turn_on

custom_components/stromer/coordinator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ async def _async_update_data(self) -> StromerData:
3131
try:
3232
await self.stromer.stromer_update()
3333

34+
# Rewrite position["rcvts"] as this key exists in status
35+
if "rcvts" in self.stromer.position:
36+
self.stromer.position["rcvts_pos"] = self.stromer.position.pop("rcvts")
37+
3438
bike_data = self.stromer.bike
3539
bike_data.update(self.stromer.status)
3640
bike_data.update(self.stromer.position)

custom_components/stromer/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"domain": "stromer",
33
"name": "Stromer e-bike",
4-
"version": "0.0.7",
4+
"version": "0.1.0",
55
"documentation": "https://github.com/CoMPaTech/stromer",
66
"requirements": [],
77
"codeowners": ["@CoMPaTech"],

custom_components/stromer/sensor.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
from homeassistant.const import (LENGTH_KILOMETERS, PERCENTAGE, POWER_WATT,
88
PRESSURE_BAR, SPEED_KILOMETERS_PER_HOUR,
99
TEMP_CELSIUS, TIME_SECONDS)
10+
from homeassistant.helpers.entity import EntityCategory
1011

1112
from custom_components.stromer.coordinator import StromerDataUpdateCoordinator
13+
from datetime import datetime, timezone
1214

1315
from .const import DOMAIN
1416
from .entity import StromerEntity
@@ -133,6 +135,27 @@
133135
device_class=None,
134136
state_class=SensorStateClass.MEASUREMENT,
135137
),
138+
SensorEntityDescription(
139+
key="rcvts",
140+
name="Last status push",
141+
native_unit_of_measurement=None,
142+
device_class=SensorDeviceClass.TIMESTAMP,
143+
entity_category=EntityCategory.DIAGNOSTIC,
144+
),
145+
SensorEntityDescription(
146+
key="rcvts_pos",
147+
name="Last position push",
148+
native_unit_of_measurement=None,
149+
device_class=SensorDeviceClass.TIMESTAMP,
150+
entity_category=EntityCategory.DIAGNOSTIC,
151+
),
152+
SensorEntityDescription(
153+
key="timets",
154+
name="Last position time",
155+
native_unit_of_measurement=None,
156+
device_class=SensorDeviceClass.TIMESTAMP,
157+
entity_category=EntityCategory.DIAGNOSTIC,
158+
),
136159
)
137160

138161

@@ -172,7 +195,22 @@ def __init__(
172195
self._attr_unique_id = f"{device_id}-{description.key}"
173196
self._attr_name = (f"{coordinator.data.bike_name} {description.name}").lstrip()
174197

198+
@staticmethod
199+
def _ensure_timezone(timestamp: datetime | None) -> datetime | None:
200+
"""Calculate days left until domain expires."""
201+
if timestamp is None:
202+
return None
203+
204+
# If timezone info isn't provided by the Whois, assume UTC.
205+
if timestamp.tzinfo is None:
206+
return timestamp.replace(tzinfo=timezone.utc)
207+
208+
return timestamp
209+
175210
@property
176-
def state(self):
211+
def native_value(self):
177212
"""Return the state of the sensor."""
213+
if self.entity_description.device_class == SensorDeviceClass.TIMESTAMP:
214+
return self._ensure_timezone(datetime.fromtimestamp(int(self._coordinator.data.bikedata.get(self._ent))))
215+
178216
return self._coordinator.data.bikedata.get(self._ent)

0 commit comments

Comments
 (0)