Custom HA integration (domain meshcore_agent) that lets MeshCore nodes manage a todo list over mesh messages: - todo / todo list, todo add, todo done, todo uncheck, todo remove - remind <n> <when> / remind off <n> (relative, clock, weekday, absolute) - replies via meshcore.send_message / send_channel_message - reminders scheduled with async_track_point_in_time and delivered to the sender who set them - todo list also exposed as a HA todo entity - config flow with sender allowlist, allow-all toggle, reply length cap - pure-Python core (core/) covered by pytest; adapter flow tested with stubbed hass
116 lines
3.6 KiB
Python
116 lines
3.6 KiB
Python
"""Todo platform: exposes the mesh todo list as a Home Assistant todo list.
|
|
|
|
The same items reachable over the mesh (`todo add`, `todo done`, ...)
|
|
appear in the Home Assistant UI and can be edited from there too.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from homeassistant.components.todo import (
|
|
TodoItem,
|
|
TodoItemStatus,
|
|
TodoListEntity,
|
|
TodoListEntityFeature,
|
|
)
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.core import HomeAssistant, callback
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
|
from homeassistant.helpers.dispatcher import (
|
|
async_dispatcher_connect,
|
|
)
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
from homeassistant.util import dt as dt_util
|
|
|
|
from . import MeshCoreAgent
|
|
from .const import DOMAIN, SIGNAL_STORE_UPDATED
|
|
from .core.store import TodoItem as CoreTodoItem
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
entry: ConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
agent: MeshCoreAgent = hass.data[DOMAIN][entry.entry_id]
|
|
async_add_entities([MeshCoreTodoList(agent, entry)])
|
|
|
|
|
|
def _to_ha_item(core_item: CoreTodoItem) -> TodoItem:
|
|
due = None
|
|
if core_item.reminder is not None:
|
|
due = dt_util.parse_datetime(core_item.reminder.at)
|
|
return TodoItem(
|
|
uid=core_item.id,
|
|
item=core_item.text,
|
|
status=(
|
|
TodoItemStatus.COMPLETED
|
|
if core_item.is_completed
|
|
else TodoItemStatus.NEEDS_ACTION
|
|
),
|
|
due=due,
|
|
)
|
|
|
|
|
|
class MeshCoreTodoList(TodoListEntity):
|
|
"""A todo list backed by the mesh agent's store."""
|
|
|
|
_attr_has_entity_name = True
|
|
_attr_translation_key = "todos"
|
|
_attr_supported_features = (
|
|
TodoListEntityFeature.CREATE_TODO_ITEM
|
|
| TodoListEntityFeature.UPDATE_TODO_ITEM
|
|
| TodoListEntityFeature.DELETE_TODO_ITEM
|
|
)
|
|
|
|
def __init__(self, agent: MeshCoreAgent, entry: ConfigEntry) -> None:
|
|
self._agent = agent
|
|
self._entry = entry
|
|
self._attr_unique_id = entry.entry_id
|
|
self._attr_device_info = DeviceInfo(
|
|
identifiers={(DOMAIN, entry.entry_id)},
|
|
name=entry.title,
|
|
)
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
self.async_on_remove(
|
|
async_dispatcher_connect(
|
|
self.hass,
|
|
SIGNAL_STORE_UPDATED.format(entry_id=self._entry.entry_id),
|
|
self._store_updated,
|
|
)
|
|
)
|
|
|
|
@callback
|
|
def _store_updated(self) -> None:
|
|
self.async_write_ha_state()
|
|
|
|
@property
|
|
def todo_items(self) -> list[TodoItem]:
|
|
return [_to_ha_item(i) for i in self._agent.todo_store.ordered()]
|
|
|
|
async def async_create_item(self, item: TodoItem) -> None:
|
|
self._agent.todo_store.add(item.item)
|
|
await self._agent.async_persist()
|
|
|
|
async def async_update_item(self, item: TodoItem) -> None:
|
|
if item.uid is None:
|
|
return
|
|
core_item = self._agent.todo_store.get(item.uid)
|
|
if core_item is None:
|
|
return
|
|
if item.item is not None:
|
|
core_item.text = item.item
|
|
if item.status is not None:
|
|
if item.status == TodoItemStatus.COMPLETED:
|
|
self._agent.todo_store.complete(core_item)
|
|
else:
|
|
self._agent.todo_store.reopen(core_item)
|
|
await self._agent.async_persist()
|
|
|
|
async def async_delete_items(self, uids: list[str]) -> None:
|
|
for uid in uids:
|
|
core_item = self._agent.todo_store.get(uid)
|
|
if core_item is not None:
|
|
self._agent.todo_store.remove(core_item)
|
|
await self._agent.async_persist()
|