Each cost line is now an entity rather than an attribute: subtotal, box price, add-ons, tax and delivery fee, plus a total and free-delivery-remaining for the open order, a delivery-day sensor, and a binary sensor that turns off at the cutoff. Entities share a base class and declare a scope, so a description states only the field it reads. Also parses the driver tip, Bounty savings and the free-delivery threshold. The threshold is carried across orders because the progress bar only renders on carts that have not met it. Adds LICENSE, hacs.json, a CI workflow, a pre-publish audit script, and a test that cross-checks every entity's translation_key against both translation files. Manifest URLs now point at GitHub rather than a private forge.
63 lines
1.9 KiB
Python
63 lines
1.9 KiB
Python
"""Binary sensors for Fresh Harvest."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from homeassistant.components.binary_sensor import (
|
|
BinarySensorEntity,
|
|
BinarySensorEntityDescription,
|
|
)
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from . import FreshHarvestConfigEntry
|
|
from .coordinator import FreshHarvestCoordinator
|
|
from .entity import FreshHarvestEntity
|
|
|
|
DESCRIPTION = BinarySensorEntityDescription(
|
|
key="order_open",
|
|
translation_key="order_open",
|
|
icon="mdi:cart-arrow-right",
|
|
)
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
entry: FreshHarvestConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
"""Set up the binary sensor platform."""
|
|
async_add_entities([FreshHarvestOrderOpen(entry.runtime_data, entry)])
|
|
|
|
|
|
class FreshHarvestOrderOpen(FreshHarvestEntity, BinarySensorEntity):
|
|
"""Whether any upcoming order can still be changed.
|
|
|
|
The single most useful thing to automate on: it goes off when the cutoff
|
|
passes, which is the last moment to add something to the box.
|
|
"""
|
|
|
|
entity_description = DESCRIPTION
|
|
|
|
def __init__(
|
|
self, coordinator: FreshHarvestCoordinator, entry: FreshHarvestConfigEntry
|
|
) -> None:
|
|
super().__init__(coordinator, entry, DESCRIPTION.key)
|
|
|
|
@property
|
|
def is_on(self) -> bool:
|
|
"""Return true while an order is still customizable."""
|
|
return self.target("open_order") is not None
|
|
|
|
@property
|
|
def extra_state_attributes(self) -> dict[str, str | None] | None:
|
|
"""Surface which order is open and until when."""
|
|
order = self.target("open_order")
|
|
if order is None:
|
|
return None
|
|
return {
|
|
"delivery_date": order.delivery_date.isoformat()
|
|
if order.delivery_date
|
|
else None,
|
|
"shop_window": order.shop_window,
|
|
}
|