diff --git a/custom_components/planta/pyplanta/__init__.py b/custom_components/planta/pyplanta/__init__.py index 120d15b..53084ce 100644 --- a/custom_components/planta/pyplanta/__init__.py +++ b/custom_components/planta/pyplanta/__init__.py @@ -103,7 +103,8 @@ async def _refresh_token(self) -> None: async with self._lock: if self._is_token_valid(): return - self._client.headers.pop("Authorization") + if "Authorization" in self._client.headers: + self._client.headers.pop("Authorization") resp = await self._request( "POST", "https://securetoken.googleapis.com/v1/token", diff --git a/custom_components/planta/sensor.py b/custom_components/planta/sensor.py index 670f469..1d21f32 100644 --- a/custom_components/planta/sensor.py +++ b/custom_components/planta/sensor.py @@ -3,7 +3,7 @@ from __future__ import annotations from dataclasses import dataclass -from datetime import datetime +from datetime import datetime, timedelta, timezone import logging from homeassistant.components.sensor import ( @@ -12,12 +12,13 @@ SensorEntityDescription, SensorStateClass, ) -from homeassistant.const import EntityCategory, UnitOfLength +from homeassistant.const import EntityCategory, UnitOfLength, UnitOfTime from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.event import async_track_time_interval from . import PlantaConfigEntry -from .coordinator import PlantaCoordinator +from .coordinator import PlantaCoordinator, PlantaPlantCoordinator from .entity import PlantaEntity _LOGGER = logging.getLogger(__name__) @@ -118,6 +119,16 @@ class PlantaSensorEntityDescription(SensorEntityDescription): device_class=SensorDeviceClass.TIMESTAMP, entity_category=EntityCategory.DIAGNOSTIC, ), + PlantaSensorEntityDescription( + key="time_since_last_watering", + field="watering", + translation_key="time_since_last_watering", + device_class=SensorDeviceClass.DURATION, + entity_category=EntityCategory.DIAGNOSTIC, + native_unit_of_measurement=UnitOfTime.SECONDS, + suggested_unit_of_measurement=UnitOfTime.DAYS, + state_class=SensorStateClass.MEASUREMENT, + ), ) @@ -158,10 +169,21 @@ class PlantaHistorySensorEntity(PlantaEntity, SensorEntity): """Planta history sensor entity.""" entity_description: PlantaSensorEntityDescription + coordinator: PlantaPlantCoordinator async def async_added_to_hass(self) -> None: self._handle_coordinator_update() await super().async_added_to_hass() + if self.entity_description.device_class == SensorDeviceClass.DURATION: + self.async_on_remove( + async_track_time_interval( + self.hass, self._update_entity_state, timedelta(minutes=15) + ) + ) + + async def _update_entity_state(self, now: datetime | None = None) -> None: + """Update the state of the entity.""" + self._handle_coordinator_update() @callback def _handle_coordinator_update(self) -> None: @@ -170,4 +192,8 @@ def _handle_coordinator_update(self) -> None: return value = self.coordinator.data["stats"][self.entity_description.field]["latest"] self._attr_native_value = datetime.fromisoformat(value) if value else None + if self.entity_description.device_class == SensorDeviceClass.DURATION: + self._attr_native_value = ( + datetime.now(timezone.utc) - self._attr_native_value + ).total_seconds() super()._handle_coordinator_update() diff --git a/custom_components/planta/strings.json b/custom_components/planta/strings.json index 0db4d7e..d10d626 100755 --- a/custom_components/planta/strings.json +++ b/custom_components/planta/strings.json @@ -84,7 +84,8 @@ } }, "scheduled_watering": { "name": "Scheduled watering" }, - "size": { "name": "Size" } + "size": { "name": "Size" }, + "time_since_last_watering": { "name": "Time since last watering" } } } } diff --git a/custom_components/planta/translations/en.json b/custom_components/planta/translations/en.json index 448cabd..97f335c 100755 --- a/custom_components/planta/translations/en.json +++ b/custom_components/planta/translations/en.json @@ -84,7 +84,8 @@ } }, "scheduled_watering": { "name": "Scheduled watering" }, - "size": { "name": "Size" } + "size": { "name": "Size" }, + "time_since_last_watering": { "name": "Time since last watering" } } } }