Skip to content

Commit

Permalink
better detection of inactive medications
Browse files Browse the repository at this point in the history
  • Loading branch information
c99koder committed Oct 9, 2024
1 parent a7e3353 commit 45fe97f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
3 changes: 1 addition & 2 deletions custom_components/medisafe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions custom_components/medisafe/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
20 changes: 10 additions & 10 deletions custom_components/medisafe/todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 45fe97f

Please sign in to comment.