|
7 | 7 | from homeassistant.const import (LENGTH_KILOMETERS, PERCENTAGE, POWER_WATT, |
8 | 8 | PRESSURE_BAR, SPEED_KILOMETERS_PER_HOUR, |
9 | 9 | TEMP_CELSIUS, TIME_SECONDS) |
| 10 | +from homeassistant.helpers.entity import EntityCategory |
10 | 11 |
|
11 | 12 | from custom_components.stromer.coordinator import StromerDataUpdateCoordinator |
| 13 | +from datetime import datetime, timezone |
12 | 14 |
|
13 | 15 | from .const import DOMAIN |
14 | 16 | from .entity import StromerEntity |
|
133 | 135 | device_class=None, |
134 | 136 | state_class=SensorStateClass.MEASUREMENT, |
135 | 137 | ), |
| 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 | + ), |
136 | 159 | ) |
137 | 160 |
|
138 | 161 |
|
@@ -172,7 +195,22 @@ def __init__( |
172 | 195 | self._attr_unique_id = f"{device_id}-{description.key}" |
173 | 196 | self._attr_name = (f"{coordinator.data.bike_name} {description.name}").lstrip() |
174 | 197 |
|
| 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 | + |
175 | 210 | @property |
176 | | - def state(self): |
| 211 | + def native_value(self): |
177 | 212 | """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 | + |
178 | 216 | return self._coordinator.data.bikedata.get(self._ent) |
0 commit comments