Skip to content

Commit

Permalink
Better entity states
Browse files Browse the repository at this point in the history
Entity state will now report last event name until mail piece is delivered in which case it will report status description for consistent reporting.
  • Loading branch information
jampez77 committed Oct 7, 2024
1 parent 4d32988 commit 1f5aed5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
3 changes: 3 additions & 0 deletions custom_components/royalmail/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@
CONF_PRODUCT_NAME = "productName"
PRODUCT_NAME = "ProductName"
CONF_SUMMARY = "summary"
CONF_STATUS_DESCRIPTION = "statusDescription"
CONF_DELIVERIES_TODAY = "deliveriesToday"
CONF_LAST_EVENT_CODE = "lastEventCode"
CONF_LAST_EVENT_NAME = "lastEventName"
CONF_LAST_EVENT_DATE_TIME = "lastEventDateTime"
DELIVERY_TRANSIT_EVENTS = [
"EVNSR",
Expand All @@ -59,6 +61,7 @@
"EVNRT",
"EVOCO",
"RSRXS",
"EVNDA",
]
DELIVERY_FAILED = ["EVKNR"]
DELIVERY_DELIVERED_EVENTS = ["EVKSP", "EVKOP", "EVKSF"]
Expand Down
2 changes: 1 addition & 1 deletion custom_components/royalmail/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"issue_tracker": "https://github.com/jampez77/RoyalMail/issues",
"requirements": [],
"ssdp": [],
"version": "2024.10.1",
"version": "2024.10.2",
"zeroconf": []
}
22 changes: 16 additions & 6 deletions custom_components/royalmail/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
from .const import (
CONF_LAST_EVENT_CODE,
CONF_LAST_EVENT_DATE_TIME,
CONF_LAST_EVENT_NAME,
CONF_MAILPIECE_ID,
CONF_MP_DETAILS,
CONF_OUT_FOR_DELIVERY,
CONF_PARCELS,
CONF_STATUS_DESCRIPTION,
CONF_SUMMARY,
DELIVERY_DELIVERED_EVENTS,
DELIVERY_TODAY_EVENTS,
Expand Down Expand Up @@ -156,7 +158,7 @@ async def async_setup_entry(
await remove_unavailable_entities(hass)


async def remove_unavailable_entities(hass):
async def remove_unavailable_entities(hass: HomeAssistant):
"""Remove entities no longer provided by the integration."""
# Access the entity registry
registry = er.async_get(hass)
Expand All @@ -183,7 +185,7 @@ def __init__(
name: str,
parcels: list,
parcels_out_for_delivery: list,
):
) -> None:
"""Init."""
self.total_parcels = parcels
self.parcels_out_for_delivery = parcels_out_for_delivery
Expand All @@ -203,12 +205,12 @@ def __init__(
self.attrs: dict[str, Any] = {}

@property
def name(self):
def name(self) -> None:
"""Name."""
return self._name

@property
def state(self):
def state(self) -> None:
"""State."""
return self._state

Expand Down Expand Up @@ -339,8 +341,16 @@ def get_state(self) -> str:

value = self.data.get(self.entity_description.key)

if CONF_SUMMARY in self.data and "statusDescription" in self.data[CONF_SUMMARY]:
value = self.data[CONF_SUMMARY]["statusDescription"]
if (
CONF_SUMMARY in self.data
and CONF_LAST_EVENT_NAME in self.data[CONF_SUMMARY]
and CONF_LAST_EVENT_CODE in self.data[CONF_SUMMARY]
):
lastEventCode = self.data[CONF_SUMMARY][CONF_LAST_EVENT_CODE]
if lastEventCode in DELIVERY_DELIVERED_EVENTS:
value = self.data[CONF_SUMMARY][CONF_STATUS_DESCRIPTION]
else:
value = self.data[CONF_SUMMARY][CONF_LAST_EVENT_NAME]

return value

Expand Down

0 comments on commit 1f5aed5

Please sign in to comment.