diff --git a/custom_components/parcelapp/config_flow.py b/custom_components/parcelapp/config_flow.py index ea0e035..8d2f90f 100644 --- a/custom_components/parcelapp/config_flow.py +++ b/custom_components/parcelapp/config_flow.py @@ -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__) @@ -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( @@ -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, } ) @@ -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( { @@ -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, + ) + ), } ) diff --git a/custom_components/parcelapp/const.py b/custom_components/parcelapp/const.py index dfb32f4..e163231 100644 --- a/custom_components/parcelapp/const.py +++ b/custom_components/parcelapp/const.py @@ -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.", diff --git a/custom_components/parcelapp/coordinator.py b/custom_components/parcelapp/coordinator.py index f757c6e..3020ca3 100644 --- a/custom_components/parcelapp/coordinator.py +++ b/custom_components/parcelapp/coordinator.py @@ -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] @@ -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, @@ -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") diff --git a/custom_components/parcelapp/translations/en.json b/custom_components/parcelapp/translations/en.json index 935135c..4f6c6ea 100644 --- a/custom_components/parcelapp/translations/en.json +++ b/custom_components/parcelapp/translations/en.json @@ -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." } } }