-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49f88a9
commit d9b988d
Showing
14 changed files
with
590 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { HomeAssistant, ServiceCallResponse } from "../types"; | ||
import { computeDomain } from "../common/entity/compute_domain"; | ||
import { computeStateName } from "../common/entity/compute_state_name"; | ||
import { isUnavailableState } from "./entity"; | ||
|
||
export enum TodoItemStatus { | ||
NeedsAction = "needs-action", | ||
Completed = "completed", | ||
} | ||
|
||
export interface TodoList { | ||
entity_id: string; | ||
name: string; | ||
} | ||
|
||
export interface TodoItem { | ||
uid?: string; | ||
summary: string; | ||
status: TodoItemStatus; | ||
} | ||
|
||
export const enum TodoListEntityFeature { | ||
CREATE_TODO_ITEM = 1, | ||
DELETE_TODO_ITEM = 2, | ||
UPDATE_TODO_ITEM = 4, | ||
MOVE_TODO_ITEM = 8, | ||
} | ||
|
||
export const getTodoLists = (hass: HomeAssistant): TodoList[] => | ||
Object.keys(hass.states) | ||
.filter( | ||
(entityId) => | ||
computeDomain(entityId) === "todo" && | ||
!isUnavailableState(hass.states[entityId].state) | ||
) | ||
.sort() | ||
.map((entityId) => ({ | ||
...hass.states[entityId], | ||
entity_id: entityId, | ||
name: computeStateName(hass.states[entityId]), | ||
})); | ||
|
||
export interface TodoItems { | ||
items: TodoItem[]; | ||
} | ||
|
||
export const fetchItems = async ( | ||
hass: HomeAssistant, | ||
entityId: string | ||
): Promise<TodoItem[]> => { | ||
const result = await hass.callWS<TodoItems>({ | ||
type: "todo/item/list", | ||
entity_id: entityId, | ||
}); | ||
return result.items; | ||
}; | ||
|
||
export const updateItem = ( | ||
hass: HomeAssistant, | ||
entity_id: string, | ||
item: TodoItem | ||
): Promise<ServiceCallResponse> => | ||
hass.callService("todo", "update_item", item, { entity_id }); | ||
|
||
export const createItem = ( | ||
hass: HomeAssistant, | ||
entity_id: string, | ||
summary: string | ||
): Promise<ServiceCallResponse> => | ||
hass.callService( | ||
"todo", | ||
"create_item", | ||
{ | ||
summary, | ||
}, | ||
{ entity_id } | ||
); | ||
|
||
export const deleteItem = ( | ||
hass: HomeAssistant, | ||
entity_id: string, | ||
uid: string | ||
): Promise<ServiceCallResponse> => | ||
hass.callService( | ||
"todo", | ||
"delete_item", | ||
{ | ||
uid, | ||
}, | ||
{ entity_id } | ||
); | ||
|
||
export const moveItem = ( | ||
hass: HomeAssistant, | ||
entity_id: string, | ||
uid: string, | ||
pos: number | ||
): Promise<void> => | ||
hass.callWS({ | ||
type: "todo/item/move", | ||
entity_id, | ||
uid, | ||
pos, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.