From 1cb238ec2ad7afbd9253d11aa0e3f9710052d787 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Tue, 24 Oct 2023 22:53:54 +0200 Subject: [PATCH] Fixes for todo card (#18388) --- src/data/todo.ts | 5 +++-- src/panels/lovelace/cards/hui-todo-list-card.ts | 14 ++++++++------ src/panels/todo/ha-panel-todo.ts | 5 +---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/data/todo.ts b/src/data/todo.ts index 833f39067de5..fc4d9974eaf3 100644 --- a/src/data/todo.ts +++ b/src/data/todo.ts @@ -2,6 +2,7 @@ import { HomeAssistant, ServiceCallResponse } from "../types"; import { computeDomain } from "../common/entity/compute_domain"; import { computeStateName } from "../common/entity/compute_state_name"; import { isUnavailableState } from "./entity"; +import { stringCompare } from "../common/string/compare"; export interface TodoList { entity_id: string; @@ -33,12 +34,12 @@ export const getTodoLists = (hass: HomeAssistant): TodoList[] => computeDomain(entityId) === "todo" && !isUnavailableState(hass.states[entityId].state) ) - .sort() .map((entityId) => ({ ...hass.states[entityId], entity_id: entityId, name: computeStateName(hass.states[entityId]), - })); + })) + .sort((a, b) => stringCompare(a.name, b.name, hass.locale.language)); export interface TodoItems { items: TodoItem[]; diff --git a/src/panels/lovelace/cards/hui-todo-list-card.ts b/src/panels/lovelace/cards/hui-todo-list-card.ts index 8a3112f0cda0..9bed15e5e11a 100644 --- a/src/panels/lovelace/cards/hui-todo-list-card.ts +++ b/src/panels/lovelace/cards/hui-todo-list-card.ts @@ -73,7 +73,7 @@ export class HuiTodoListCard @state() private _entityId?: string; - @state() private _items: Record = {}; + @state() private _items?: Record; @state() private _uncheckedItems?: TodoItem[]; @@ -96,8 +96,6 @@ export class HuiTodoListCard this._config = config; this._entityId = config.entity; - this._uncheckedItems = []; - this._checkedItems = []; } protected checkConfig(config: TodoListCardConfig): void { @@ -112,13 +110,17 @@ export class HuiTodoListCard } public willUpdate( - _changedProperties: PropertyValueMap | Map + changedProperties: PropertyValueMap | Map ): void { if (!this.hasUpdated) { if (!this._entityId) { this._entityId = this.getEntityId(); } this._fetchData(); + } else if (changedProperties.has("_entityId") || !this._items) { + this._uncheckedItems = []; + this._checkedItems = []; + this._fetchData(); } } @@ -338,7 +340,7 @@ export class HuiTodoListCard } private _completeItem(ev): void { - const item = this._items[ev.target.itemId]; + const item = this._items![ev.target.itemId]; updateItem(this.hass!, this._entityId!, { ...item, status: ev.target.checked @@ -350,7 +352,7 @@ export class HuiTodoListCard private _saveEdit(ev): void { // If name is not empty, update the item otherwise remove it if (ev.target.value) { - const item = this._items[ev.target.itemId]; + const item = this._items![ev.target.itemId]; updateItem(this.hass!, this._entityId!, { ...item, summary: ev.target.value, diff --git a/src/panels/todo/ha-panel-todo.ts b/src/panels/todo/ha-panel-todo.ts index ebdfa5817329..f3fc794247bc 100644 --- a/src/panels/todo/ha-panel-todo.ts +++ b/src/panels/todo/ha-panel-todo.ts @@ -14,7 +14,6 @@ import { customElement, property, state } from "lit/decorators"; import memoizeOne from "memoize-one"; import { isComponentLoaded } from "../../common/config/is_component_loaded"; import { storage } from "../../common/decorators/storage"; -import { computeDomain } from "../../common/entity/compute_domain"; import { computeStateName } from "../../common/entity/compute_state_name"; import "../../components/ha-button"; import "../../components/ha-icon-button"; @@ -87,9 +86,7 @@ class PanelTodo extends LitElement { super.willUpdate(changedProperties); if (!this.hasUpdated && !this._entityId) { - this._entityId = Object.keys(this.hass.states).find( - (entityId) => computeDomain(entityId) === "todo" - ); + this._entityId = getTodoLists(this.hass)[0]?.entity_id; } else if (!this.hasUpdated) { this._createCard(); }