Skip to content

Commit

Permalink
Merge pull request #9 from CoMPaTech/last_update
Browse files Browse the repository at this point in the history
Add last update sensors
  • Loading branch information
CoMPaTech authored Apr 26, 2022
2 parents 50cd3df + eeb2410 commit 4d34d03
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions custom_components/stromer/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/stromer/manifest.json
Original file line number Diff line number Diff line change
@@ -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"],
Expand Down
40 changes: 39 additions & 1 deletion custom_components/stromer/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
),
)


Expand Down Expand Up @@ -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)

0 comments on commit 4d34d03

Please sign in to comment.