diff --git a/custom_components/medisafe/__init__.py b/custom_components/medisafe/__init__.py index 6703581..0a09e24 100644 --- a/custom_components/medisafe/__init__.py +++ b/custom_components/medisafe/__init__.py @@ -83,8 +83,7 @@ def get_medication(self, uuid): if "medications" not in self.data: _LOGGER.error("Medisafe has no data yet") return None - - if "medications" in self.data: + else: for medication in self.data["medications"]: if medication["uuid"] == uuid: return medication diff --git a/custom_components/medisafe/sensor.py b/custom_components/medisafe/sensor.py index b94bebe..822af4a 100644 --- a/custom_components/medisafe/sensor.py +++ b/custom_components/medisafe/sensor.py @@ -30,15 +30,15 @@ async def async_setup_entry(hass, entry, async_add_devices): entities = [] - if coordinator.data is not None and "medications" in coordinator.data: - _LOGGER.info(f"Got {len(coordinator.data['medications'])} medications") - for ent in coordinator.data["medications"]: - if "pillsLeft" not in ent: - _LOGGER.debug(f"Missing pillsLeft: {ent['name']} with UUID {ent['uuid']}") - elif ent["treatmentStatus"] != 1: - _LOGGER.debug(f"Inactive medication: {ent['name']} with UUID {ent['uuid']}") + if coordinator.data is not None and "groups" in coordinator.data: + _LOGGER.info(f"Got {len(coordinator.data['groups'])} medications") + for ent in coordinator.data["groups"]: + if "refill" not in ent or "currentNumberOfPills" not in ent["refill"]: + _LOGGER.debug(f"Missing currentNumberOfPills: {ent['medicine']['name']} with UUID {ent['uuid']}") + elif ent["status"] != "ACTIVE": + _LOGGER.debug(f"Inactive medication: {ent['medicine']['name']} with UUID {ent['uuid']}") else: - _LOGGER.debug(f"Adding: {ent['name']} with UUID {ent['uuid']}") + _LOGGER.debug(f"Adding: {ent['medicine']['name']} with UUID {ent['uuid']}") entities.append(MedisafeMedicationEntity(coordinator, entry, ent["uuid"])) entities.append(MedisafeStatusCountEntity(coordinator, entry, "taken")) diff --git a/custom_components/medisafe/todo.py b/custom_components/medisafe/todo.py index 8153251..4a1b39d 100644 --- a/custom_components/medisafe/todo.py +++ b/custom_components/medisafe/todo.py @@ -38,24 +38,24 @@ class MedisafeTodoListEntity(CoordinatorEntity, TodoListEntity): def __init__(self, coordinator, config_entry): super().__init__(coordinator) self.config_entry = config_entry - self._attr_unique_id = "medication_refills" + self._attr_unique_id = f"medication_refills_{self.config_entry.entry_id}" @property def todo_items(self) -> list[TodoItem]: todo_list = [] - if self.coordinator.data is not None and "medications" in self.coordinator.data: - for medication in self.coordinator.data["medications"]: - if "pillsLeft" in medication and "pillsReminder" in medication and "treatmentStatus" in medication: - if medication["treatmentStatus"] == 1 and medication["pillsLeft"] <= medication["pillsReminder"]: + if self.coordinator.data is not None and "groups" in self.coordinator.data: + for group in self.coordinator.data["groups"]: + if "refill" in group and "refillReminder" in group["refill"] and "status" in group: + if group["status"] == "ACTIVE" and group["refill"]["currentNumberOfPills"] <= group["refill"]["refillReminder"]["pills"]: item = TodoItem() - item.summary = medication["name"] - item.uid = medication["uuid"] + item.summary = group["medicine"]["name"] + item.uid = group["uuid"] item.status = TodoItemStatus.NEEDS_ACTION - if medication['pillsLeft'].is_integer(): - item.description = f"{int(medication['pillsLeft'])} pills remaining" + if group["refill"]["currentNumberOfPills"].is_integer(): + item.description = f"{int(group['refill']['currentNumberOfPills'])} pills remaining" else: - item.description = f"{medication['pillsLeft']} pills remaining" + item.description = f"{group['refill']['currentNumberOfPills']} pills remaining" todo_list.append(item) return todo_list