8000 Add support for icons in todo list items by rokam · Pull Request #146061 · home-assistant/core · GitHub
[go: up one dir, main page]

Skip to content

Add support for icons in todo list items #146061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions homeassistant/components/local_todo/store.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Local storage for the Local To-do integration."""

import asyncio
import json
from pathlib import Path

from homeassistant.core import HomeAssistant
Expand All @@ -14,9 +15,14 @@ def __init__(self, hass: HomeAssistant, path: Path) -> None:
self._hass = hass
self._path = path
self._lock = asyncio.Lock()
self._lock_icons = asyncio.Lock()
self._icons_path = self._path.with_name(self._path.stem + "_icons.json")
self._icons: dict[str, str] = {}

async def async_load(self) -> str:
"""Load the calendar from disk."""
async with self._lock_icons:
await self._hass.async_add_executor_job(self._load_icons)
async with self._lock:
return await self._hass.async_add_executor_job(self._load)

Expand All @@ -30,7 +36,33 @@ async def async_store(self, ics_content: str) -> None:
"""Persist the calendar to storage."""
async with self._lock:
await self._hass.async_add_executor_job(self._store, ics_content)
async with self._lock_icons:
await self._hass.async_add_executor_job(self._store_icons)

def _store(self, ics_content: str) -> None:
"""Persist the calendar to storage."""
self._path.write_text(ics_content)

def _load_icons(self) -> None:
"""Load the icon mapping from disk."""
if not self._icons_path.exists():
self._icons = {}
return
content = self._icons_path.read_text()
self._icons = json.loads(content) if content else {}

def _store_icons(self) -> None:
"""Persist the icon mapping to storage."""
data = json.dumps(self._icons)
self._icons_path.write_text(data)

def get_icon(self, uid: str) -> str | None:
"""Get the icon for a given UID."""
return self._icons.get(uid)

def set_icon(self, uid: str, icon: str | None) -> None:
"""Set or remove the icon for a given UID."""
if icon:
self._icons[uid] = icon
elif uid in self._icons:
del self._icons[uid]
8000
5 changes: 5 additions & 0 deletions homeassistant/components/local_todo/todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class LocalTodoListEntity(TodoListEntity):
| TodoListEntityFeature.SET_DUE_DATETIME_ON_ITEM
| TodoListEntityFeature.SET_DUE_DATE_ON_ITEM
| TodoListEntityFeature.SET_DESCRIPTION_ON_ITEM
| TodoListEntityFeature.SET_ICON_ON_ITEM
)
_attr_should_poll = False

Expand Down Expand Up @@ -148,6 +149,7 @@ async def async_update(self) -> None:
TodoItem(
uid=item.uid,
summary=item.summary or "",
icon=self._store.get_icon(item.uid),
status=ICS_TODO_STATUS_MAP.get(
item.status or TodoStatus.NEEDS_ACTION,
TodoItemStatus.NEEDS_ACTION,
Expand All @@ -164,6 +166,7 @@ async def async_create_todo_item(self, item: TodoItem) -> None:
async with self._calendar_lock:
todo_store = self._new_todo_store()
await self.hass.async_add_executor_job(todo_store.add, todo)
self._store.set_icon(todo.uid, item.icon)
await self.async_save()
await self.async_update_ha_state(force_refresh=True)

Expand All @@ -173,6 +176,7 @@ async def async_update_todo_item(self, item: TodoItem) -> None:
async with self._calendar_lock:
todo_store = self._new_todo_store()
await self.hass.async_add_executor_job(todo_store.edit, todo.uid, todo)
self._store.set_icon(todo.uid, item.icon)
await self.async_save()
await self.async_update_ha_state(force_refresh=True)

Expand All @@ -182,6 +186,7 @@ async def async_delete_todo_items(self, uids: list[str]) -> None:
async with self._calendar_lock:
for uid in uids:
store.delete(uid)
self._store.set_icon(uid, None)
await self.async_save()
await self.async_update_ha_state(force_refresh=True)

Expand Down
10 changes: 10 additions & 0 deletions homeassistant/components/todo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
ATTR_DUE,
ATTR_DUE_DATE,
ATTR_DUE_DATETIME,
ATTR_ICON,
ATTR_ITEM,
ATTR_RENAME,
ATTR_STATUS,
Expand Down Expand Up @@ -89,6 +90,12 @@ class TodoItemFieldDescription:
todo_item_field=ATTR_DESCRIPTION,
required_feature=TodoListEntityFeature.SET_DESCRIPTION_ON_ITEM,
),
TodoItemFieldDescription(
service_field=ATTR_ICON,
validation=vol.Any(cv.icon, None),
todo_item_field=ATTR_ICON,
required_feature=TodoListEntityFeature.SET_ICON_ON_ITEM,
),
]

TODO_ITEM_FIELD_SCHEMA = {
Expand Down Expand Up @@ -218,6 +225,9 @@ class TodoItem:
uid: str | None = None
"""A unique identifier for the To-do item."""

icon: str | None = None
"""An icon that represents the To-do item."""

status: TodoItemStatus | None = None
"""A status or confirmation of the To-do item."""

Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/todo/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
ATTR_DUE_DATE = "due_date"
ATTR_DUE_DATETIME = "due_datetime"
ATTR_DESCRIPTION = "description"
ATTR_ICON = "icon"
ATTR_ITEM = "item"
ATTR_RENAME = "rename"
ATTR_STATUS = "status"
Expand All @@ -44,6 +45,7 @@ class TodoListEntityFeature(IntFlag):
SET_DUE_DATE_ON_ITEM = 16
SET_DUE_DATETIME_ON_ITEM = 32
SET_DESCRIPTION_ON_ITEM = 64
SET_ICON_ON_ITEM = 128


class TodoItemStatus(StrEnum):
Expand Down
14 changes: 14 additions & 0 deletions homeassistant/components/todo/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ add_item:
example: "Submit income tax return"
selector:
text:
icon:
example: "mdi:calculator"
selector:
icon:
filter:
supported_features:
- todo.TodoListEntityFeature.SET_ICON_ON_ITEM
due_date:
filter:
supported_features:
Expand Down Expand Up @@ -62,6 +69,13 @@ update_item:
example: "Something else"
selector:
text:
icon:
example: "mdi:calculator"
selector:
icon:
filter:
supported_features:
- todo.TodoListEntityFeature.SET_ICON_ON_ITEM
status:
example: "needs_action"
selector:
Expand Down
8 changes: 8 additions & 0 deletions homeassistant/components/todo/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
"name": "Item name",
"description": "The name that represents the to-do item."
},
"icon": {
"name": "Icon",
"description": "An icon to represent the to-do item. If not specified, no icon will be used."
},
"due_date": {
"name": "Due date",
"description": "The date the to-do item is expected to be completed."
Expand All @@ -50,6 +54,10 @@
"name": "Rename item",
"description": "The new name for the to-do item"
},
"icon": {
"name": "Icon",
"description": "An icon to represent the to-do item. If not specified, no icon will be used."
},
"status": {
"name": "Set status",
"description": "A status or confirmation of the to-do item."
Expand Down
Loading
0