Fix set up issue AGAIN
This commit is contained in:
91
sensor.py
91
sensor.py
@@ -24,6 +24,8 @@ from .const import (
|
||||
CONF_PACK_SIZE,
|
||||
CONF_SCHEDULE,
|
||||
CONF_PRESCRIPTION,
|
||||
CONF_DAYS,
|
||||
CONF_TIMES,
|
||||
CONF_ISSUE_DATE,
|
||||
CONF_EXPIRY_DATE,
|
||||
CONF_DOCTOR,
|
||||
@@ -101,6 +103,43 @@ class MedMateBaseSensor(CoordinatorEntity, SensorEntity):
|
||||
"""Return if entity is available."""
|
||||
return self.coordinator.last_update_success and bool(self.coordinator.data)
|
||||
|
||||
def _get_clean_schedule_attributes(self, schedule_data: dict) -> dict[str, Any]:
|
||||
"""Extract only valid schedule attributes, filtering out form keys."""
|
||||
if not schedule_data:
|
||||
return {}
|
||||
|
||||
clean_schedule = {}
|
||||
|
||||
# Only include expected schedule keys
|
||||
if CONF_DAYS in schedule_data:
|
||||
clean_schedule[CONF_DAYS] = schedule_data[CONF_DAYS]
|
||||
if CONF_TIMES in schedule_data:
|
||||
clean_schedule[CONF_TIMES] = schedule_data[CONF_TIMES]
|
||||
|
||||
return clean_schedule
|
||||
|
||||
def _get_clean_prescription_attributes(self, prescription_data: dict) -> dict[str, Any]:
|
||||
"""Extract only valid prescription attributes."""
|
||||
if not prescription_data:
|
||||
return {}
|
||||
|
||||
clean_attrs = {}
|
||||
|
||||
# Only include expected prescription keys
|
||||
expected_keys = [
|
||||
CONF_ISSUE_DATE,
|
||||
CONF_EXPIRY_DATE,
|
||||
CONF_DOCTOR,
|
||||
CONF_TOTAL_REPEATS,
|
||||
CONF_REPEATS_LEFT,
|
||||
]
|
||||
|
||||
for key in expected_keys:
|
||||
if key in prescription_data:
|
||||
clean_attrs[f"prescription_{key}"] = prescription_data[key]
|
||||
|
||||
return clean_attrs
|
||||
|
||||
|
||||
class MedMateInventorySensor(MedMateBaseSensor):
|
||||
"""Sensor for medicine inventory."""
|
||||
@@ -132,23 +171,23 @@ class MedMateInventorySensor(MedMateBaseSensor):
|
||||
schedule = data.get(CONF_SCHEDULE, {})
|
||||
prescription = data.get(CONF_PRESCRIPTION, {})
|
||||
|
||||
# Build clean attributes - CRITICAL: Only include expected keys
|
||||
attrs = {
|
||||
ATTR_ACTIVE_INGREDIENT: data.get(CONF_ACTIVE_INGREDIENT),
|
||||
ATTR_STRENGTH: data.get(CONF_STRENGTH),
|
||||
ATTR_PACK_SIZE: data.get(CONF_PACK_SIZE),
|
||||
ATTR_SCHEDULE: schedule,
|
||||
ATTR_PRESCRIPTION_ACTIVE: data.get("prescription_active", False),
|
||||
}
|
||||
|
||||
# Add prescription details if available
|
||||
if prescription:
|
||||
attrs.update({
|
||||
ATTR_ISSUE_DATE: prescription.get(CONF_ISSUE_DATE),
|
||||
ATTR_EXPIRY_DATE: prescription.get(CONF_EXPIRY_DATE),
|
||||
ATTR_DOCTOR: prescription.get(CONF_DOCTOR),
|
||||
ATTR_TOTAL_REPEATS: prescription.get(CONF_TOTAL_REPEATS),
|
||||
ATTR_REPEATS_LEFT: prescription.get(CONF_REPEATS_LEFT),
|
||||
})
|
||||
# Add clean schedule data
|
||||
clean_schedule = self._get_clean_schedule_attributes(schedule)
|
||||
if clean_schedule:
|
||||
attrs["schedule_days"] = clean_schedule.get(CONF_DAYS, [])
|
||||
attrs["schedule_times"] = clean_schedule.get(CONF_TIMES, [])
|
||||
|
||||
# Add clean prescription details
|
||||
clean_prescription = self._get_clean_prescription_attributes(prescription)
|
||||
attrs.update(clean_prescription)
|
||||
|
||||
return attrs
|
||||
|
||||
@@ -183,13 +222,16 @@ class MedMateRepeatsSensor(MedMateBaseSensor):
|
||||
data = self.coordinator.data
|
||||
prescription = data.get(CONF_PRESCRIPTION, {})
|
||||
|
||||
return {
|
||||
ATTR_TOTAL_REPEATS: prescription.get(CONF_TOTAL_REPEATS, 0),
|
||||
# Only include expected attributes
|
||||
attrs = {
|
||||
ATTR_PRESCRIPTION_ACTIVE: data.get("prescription_active", False),
|
||||
ATTR_ISSUE_DATE: prescription.get(CONF_ISSUE_DATE),
|
||||
ATTR_EXPIRY_DATE: prescription.get(CONF_EXPIRY_DATE),
|
||||
ATTR_DOCTOR: prescription.get(CONF_DOCTOR),
|
||||
}
|
||||
|
||||
# Add clean prescription data
|
||||
clean_prescription = self._get_clean_prescription_attributes(prescription)
|
||||
attrs.update(clean_prescription)
|
||||
|
||||
return attrs
|
||||
|
||||
|
||||
class MedMateNextDoseSensor(MedMateBaseSensor):
|
||||
@@ -237,8 +279,19 @@ class MedMateNextDoseSensor(MedMateBaseSensor):
|
||||
except (ValueError, TypeError):
|
||||
most_recent_dose = None
|
||||
|
||||
return {
|
||||
ATTR_SCHEDULE: schedule,
|
||||
ATTR_LAST_TAKEN: most_recent_dose,
|
||||
# Build clean attributes
|
||||
attrs = {
|
||||
"dose_due": data.get("dose_due", False),
|
||||
}
|
||||
}
|
||||
|
||||
# Add clean schedule data
|
||||
clean_schedule = self._get_clean_schedule_attributes(schedule)
|
||||
if clean_schedule:
|
||||
attrs["schedule_days"] = clean_schedule.get(CONF_DAYS, [])
|
||||
attrs["schedule_times"] = clean_schedule.get(CONF_TIMES, [])
|
||||
|
||||
# Add last taken info
|
||||
if most_recent_dose:
|
||||
attrs[ATTR_LAST_TAKEN] = most_recent_dose
|
||||
|
||||
return attrs
|
||||
Reference in New Issue
Block a user