Skip to content

Commit

Permalink
Merge pull request #1 from natekspencer/time-since-last-watering
Browse files Browse the repository at this point in the history
Add time since last watering sensor
  • Loading branch information
natekspencer authored Sep 28, 2024
2 parents ca40128 + bddeb8d commit 49235c9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
3 changes: 2 additions & 1 deletion custom_components/planta/pyplanta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 29 additions & 3 deletions custom_components/planta/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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__)
Expand Down Expand Up @@ -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,
),
)


Expand Down Expand Up @@ -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:
Expand All @@ -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()
3 changes: 2 additions & 1 deletion custom_components/planta/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
}
},
"scheduled_watering": { "name": "Scheduled watering" },
"size": { "name": "Size" }
"size": { "name": "Size" },
"time_since_last_watering": { "name": "Time since last watering" }
}
}
}
3 changes: 2 additions & 1 deletion custom_components/planta/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
}
},
"scheduled_watering": { "name": "Scheduled watering" },
"size": { "name": "Size" }
"size": { "name": "Size" },
"time_since_last_watering": { "name": "Time since last watering" }
}
}
}

0 comments on commit 49235c9

Please sign in to comment.