Add payment date (editable datetime) and next payment date (sensor) entities#7
Conversation
…or) entities Agent-Logs-Url: https://github.com/CodekExplor/Bills-ha/sessions/f9f71eb1-9017-497a-9751-1fa57f0fce91 Co-authored-by: CodekExplor <24770587+CodekExplor@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds two new per-bill entities to the oplats Home Assistant integration to track the last payment timestamp (editable datetime) and expose the next payment due date (sensor), extending the existing “confirm payment” flow.
Changes:
- Adds an editable
datetimeentity (data_platnosci) backed by HA Store via the coordinator. - Adds a read-only date sensor (
data_kolejnej_platnosci) exposing the current configured due date asSensorDeviceClass.DATE. - Wires up new entity IDs, platform registration, and UI translations.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| custom_components/oplats/translations/pl.json | Adds Polish entity-name translations for the new datetime and sensor entities. |
| custom_components/oplats/strings.json | Adds base (English) entity-name strings for the new datetime and sensor entities. |
| custom_components/oplats/sensor.py | Registers and implements NextPaymentDateSensor (DATE device class). |
| custom_components/oplats/datetime.py | Adds PaymentDateEntity (DateTimeEntity) with manual write-through to coordinator. |
| custom_components/oplats/coordinator.py | Adds async_set_payment_confirmed_date() to persist manual payment datetime updates. |
| custom_components/oplats/const.py | Introduces constants for the new entity IDs. |
| custom_components/oplats/init.py | Registers the datetime platform for config entry forwarding. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| import logging | ||
| from datetime import datetime | ||
|
|
||
| from homeassistant.components.datetime import DateTimeEntity | ||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.core import HomeAssistant, callback | ||
| from homeassistant.helpers.entity import DeviceInfo | ||
| from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
|
||
| from .const import DATETIME_PAYMENT_DATE, DOMAIN | ||
| from .coordinator import BillCoordinator | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) |
There was a problem hiding this comment.
logging / _LOGGER are defined but never used in this new module. Please remove the unused import/logger or add logging where it provides value, to avoid lint noise and keep the file minimal.
| async def async_set_payment_confirmed_date(self, dt: datetime) -> None: | ||
| """Ręcznie ustaw datę wykonanej płatności.""" | ||
| self._payment_confirmed_date = dt.isoformat() | ||
| await self.async_save() | ||
| self._notify_listeners() | ||
| _LOGGER.info( | ||
| "Ręcznie ustawiono datę płatności dla '%s' na %s.", | ||
| self.bill_name, | ||
| dt.isoformat(), | ||
| ) |
There was a problem hiding this comment.
dt.isoformat() will preserve whatever timezone awareness the UI passes in (often tz-aware), but this integration currently also writes naive datetimes elsewhere (e.g. datetime.now().isoformat() in async_confirm_payment). Mixing naive and tz-aware ISO strings can lead to Home Assistant warnings and inconsistent display/parsing. Consider normalizing dt using Home Assistant time helpers (e.g. convert to local/UTC consistently) before storing, and align async_confirm_payment to the same approach so the stored format is uniform.
| def native_value(self) -> datetime | None: | ||
| """Zwróć datę wykonanej płatności.""" | ||
| raw = self._coordinator.payment_confirmed_date | ||
| if not raw: | ||
| return None | ||
| try: | ||
| return datetime.fromisoformat(raw) | ||
| except (ValueError, TypeError): | ||
| return None |
There was a problem hiding this comment.
Using datetime.fromisoformat() here can return a naive datetime (because existing stored values are written with datetime.now().isoformat()), and it will also fail to parse some valid ISO-8601 forms (e.g. strings ending with Z). For Home Assistant DateTimeEntity, prefer using HA datetime utilities (e.g. homeassistant.util.dt.parse_datetime) and ensure the returned native_value is timezone-aware and in the expected timezone.
Adds two new per-bill entities tracking payment history and the upcoming due date, completing the payment lifecycle tracking alongside the existing confirm-payment button.
New entities
data_platnosci(datetimeplatform) — editableDateTimeEntitystoring the last completed payment timestamp. Auto-set by the "Potwierdź płatność" button; can be overridden manually from the HA UI. Persisted via HA Store.data_kolejnej_platnosci(sensor,SensorDeviceClass.DATE) — read-only sensor exposing the next due date (termin_platnosci). Advances automatically on payment confirmation per the configured recurrence cycle.Changes
const.py— addedDATETIME_PAYMENT_DATEandSENSOR_NEXT_PAYMENT_DATEconstantscoordinator.py— addedasync_set_payment_confirmed_date(dt)for manual date writes to storagedatetime.py(new) —PaymentDateEntityimplementingDateTimeEntitywithasync_set_valuedelegating to the coordinatorsensor.py— addedNextPaymentDateSensor; returnsdate.fromisoformat(due_date_str)withSensorDeviceClass.DATE__init__.py— registered"datetime"inPLATFORMStranslations/pl.json,strings.json— translations for both new entities