Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions custom_components/parcelapp/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
UPDATE_INTERVAL_SECONDS,
MIN_UPDATE_INTERVAL_SECONDS,
MAX_UPDATE_INTERVAL_SECONDS,
DEFAULT_FILTER_MODE,
FILTER_MODE_RECENT,
FILTER_MODE_ACTIVE,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -114,6 +117,7 @@ async def async_step_init(
new_account_token = user_input.get("account_token")
new_update_interval_minutes = user_input.get("update_interval_minutes", UPDATE_INTERVAL_SECONDS // 60)
new_update_interval = new_update_interval_minutes * 60
new_filter_mode = user_input.get("filter_mode", DEFAULT_FILTER_MODE)

# Check if the API key or account token has changed
if new_api_key != self.config_entry.data.get(
Expand Down Expand Up @@ -154,6 +158,7 @@ async def async_step_init(
"api_key": new_api_key,
"account_token": new_account_token,
"update_interval": new_update_interval,
"filter_mode": new_filter_mode,
}
)

Expand All @@ -165,6 +170,9 @@ def _create_schema(self) -> vol.Schema:
"update_interval", UPDATE_INTERVAL_SECONDS
)
current_interval_minutes = current_interval_seconds // 60
current_filter_mode = self.config_entry.options.get(
"filter_mode", DEFAULT_FILTER_MODE
)

return vol.Schema(
{
Expand All @@ -188,6 +196,18 @@ def _create_schema(self) -> vol.Schema:
unit_of_measurement="minutes",
)
),
vol.Optional(
"filter_mode",
default=current_filter_mode,
): selector.SelectSelector(
selector.SelectSelectorConfig(
options=[
selector.SelectOptionDict(value=FILTER_MODE_RECENT, label="Recent (includes completed)"),
selector.SelectOptionDict(value=FILTER_MODE_ACTIVE, label="Active (excludes completed)"),
],
mode=selector.SelectSelectorMode.DROPDOWN,
)
),
}
)

Expand Down
3 changes: 3 additions & 0 deletions custom_components/parcelapp/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
MIN_UPDATE_INTERVAL_SECONDS = 300
MAX_UPDATE_INTERVAL_SECONDS = 1800
CARRIER_CODE_ENDPOINT = "https://api.parcel.app/external/supported_carriers.json"
FILTER_MODE_RECENT = "recent"
FILTER_MODE_ACTIVE = "active"
DEFAULT_FILTER_MODE = FILTER_MODE_RECENT
DELIVERY_STATUS_CODES = {
-1: "None",
0: "Completed delivery.",
Expand Down
11 changes: 9 additions & 2 deletions custom_components/parcelapp/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import DOMAIN, PARCEL_URL, UPDATE_INTERVAL_SECONDS, CARRIER_CODE_ENDPOINT
from .const import (
DOMAIN,
PARCEL_URL,
UPDATE_INTERVAL_SECONDS,
CARRIER_CODE_ENDPOINT,
DEFAULT_FILTER_MODE,
)

_LOGGER = logging.getLogger(__name__)
type ParcelConfigEntry = ConfigEntry[ParcelUpdateCoordinator]
Expand All @@ -28,6 +34,7 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
update_interval_seconds = entry.options.get(
"update_interval", UPDATE_INTERVAL_SECONDS
)
self.filter_mode = entry.options.get("filter_mode", DEFAULT_FILTER_MODE)

super().__init__(
hass,
Expand All @@ -40,7 +47,7 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:

async def _async_update_data(self) -> dict[str, Any]:
"""Fetch data from the API and return the top value."""
API_URL = f"{PARCEL_URL}?filter_mode=recent"
API_URL = f"{PARCEL_URL}?filter_mode={self.filter_mode}"
carrier_codes_updated = self.carrier_codes["carrier_codes_updated"]
try:
updated = datetime.strptime(carrier_codes_updated, "%Y-%m-%d %H:%M:%S.%f")
Expand Down
6 changes: 4 additions & 2 deletions custom_components/parcelapp/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
"data": {
"api_key": "API Key (Required)",
"account_token": "Account Token (Optional) | BETA",
"update_interval_minutes": "Update Interval"
"update_interval_minutes": "Update Interval",
"filter_mode": "Filter Mode"
},
"data_description": {
"update_interval_minutes": "How often to check for parcel updates (in minutes). API rate limit: 20 requests per hour."
"update_interval_minutes": "How often to check for parcel updates (in minutes). API rate limit: 20 requests per hour.",
"filter_mode": "Choose which parcels to retrieve. 'Recent' includes recently completed deliveries. 'Active' shows only parcels that are still in transit or pending."
}
}
}
Expand Down
Loading