Skip to content

Commit

Permalink
Create a read-only todo list containing medications that need to be r…
Browse files Browse the repository at this point in the history
…efilled
  • Loading branch information
c99koder committed Oct 9, 2024
1 parent 0ceea9a commit a7e3353
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
4 changes: 2 additions & 2 deletions custom_components/medisafe/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
NAME = "Medisafe"
DOMAIN = "medisafe"
DOMAIN_DATA = f"{DOMAIN}_data"
VERSION = "0.0.4"
VERSION = "0.1.0"

ATTRIBUTION = "Data provided by https://medisafe.com/"
ISSUE_URL = "https://github.com/c99koder/ha-medisafe/issues"

# Platforms
PLATFORMS = ["sensor"]
PLATFORMS = ["sensor", "todo"]

# Configuration and options
CONF_USERNAME = "username"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/medisafe/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
],
"iot_class": "cloud_polling",
"requirements": [],
"version": "0.0.4"
"version": "0.1.0"
}
77 changes: 77 additions & 0 deletions custom_components/medisafe/todo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Copyright (C) 2024 Sam Steele
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging

from datetime import date

from homeassistant.components.todo import TodoListEntity, TodoItem, TodoItemStatus
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import ATTRIBUTION
from .const import CONF_USERNAME
from .const import DOMAIN

_LOGGER: logging.Logger = logging.getLogger(__package__)


async def async_setup_entry(hass, entry, async_add_devices):
coordinator = entry.runtime_data.coordinator
await coordinator.async_config_entry_first_refresh()

async_add_devices([MedisafeTodoListEntity(coordinator, entry)])


class MedisafeTodoListEntity(CoordinatorEntity, TodoListEntity):
_attr_has_entity_name = True
_attr_icon = "mdi:pill"

def __init__(self, coordinator, config_entry):
super().__init__(coordinator)
self.config_entry = config_entry
self._attr_unique_id = "medication_refills"

@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"]:
item = TodoItem()
item.summary = medication["name"]
item.uid = medication["uuid"]
item.status = TodoItemStatus.NEEDS_ACTION
if medication['pillsLeft'].is_integer():
item.description = f"{int(medication['pillsLeft'])} pills remaining"
else:
item.description = f"{medication['pillsLeft']} pills remaining"
todo_list.append(item)

return todo_list

@property
def name(self):
return "Medication Refills"

@property
def available(self):
return "medications" in self.coordinator.data

@property
def extra_state_attributes(self):
return {
"account": self.config_entry.data.get(CONF_USERNAME),
"attribution": ATTRIBUTION,
"integration": DOMAIN,
}

0 comments on commit a7e3353

Please sign in to comment.