Skip to content
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

[WIP] Generic todo-list lovelace card #2785

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
end_of_line = lf
insert_final_newline = true

[*.{js,py}]
charset = utf-8

[*.py]
indent_style = space
indent_size = 4

[*.{js,json,ts}]
indent_style = space
indent_size = 2
164 changes: 164 additions & 0 deletions src/data/todo-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { HomeAssistant } from "../types";
import { filter } from "minimatch";

export interface ToDoListItem {
id?: string;
name?: string;
complete?: boolean;
}

export interface ToDoAPI {
fetchItems(
hass: HomeAssistant,
listId: string,
showCompleted: boolean
): Promise<ToDoListItem[]>;

addItem(
hass: HomeAssistant,
listId: string,
item: ToDoListItem
): Promise<ToDoListItem>;

updateItem(
hass: HomeAssistant,
listId: string,
itemId: string,
item: ToDoListItem
): Promise<ToDoListItem>;

clearItems(hass: HomeAssistant, listId: string): Promise<void>;
}

export class ShoppingListAPI implements ToDoAPI {
public listIdRequired(): boolean {
return false;
}

public fetchItems(
hass: HomeAssistant,
_listId: string,
showCompleted: boolean
): Promise<ToDoListItem[]> {
return hass
.callWS({
type: "shopping_list/items",
})
.then(
(items: any): ToDoListItem[] => {
return items.filter((item) => showCompleted || !item.complete);
}
);
}

public updateItem(
hass: HomeAssistant,
_listId: string,
itemId: string,
item: ToDoListItem
): Promise<ToDoListItem> {
return hass.callWS({
type: "shopping_list/items/update",
...item,
item_id: itemId,
});
}

public clearItems(hass: HomeAssistant, _listId: string): Promise<void> {
return hass.callWS({
type: "shopping_list/items/clear",
});
}

public addItem(
hass: HomeAssistant,
_listId: string,
item: ToDoListItem
): Promise<ToDoListItem> {
return hass.callWS({
type: "shopping_list/items/add",
name: item.name,
});
}
}

export class WunderListAPI implements ToDoAPI {
public listIdRequired(): boolean {
return false;
}

public fetchItems(
hass: HomeAssistant,
listId: string,
showCompleted: boolean
): Promise<ToDoListItem[]> {
return hass
.callWS({
type: "wunderlist/tasks/list",
list_id: listId,
show_completed: showCompleted,
})
.then(
(items: any): ToDoListItem[] => {
return items.map(this.mapToToDoListItem);
}
);
}

public updateItem(
hass: HomeAssistant,
_listId: string,
itemId: string,
item: ToDoListItem
): Promise<ToDoListItem> {
const wlItem = this.mapToWunderListItem(item);
return hass.callWS({
type: "wunderlist/tasks/update",
task_id: itemId,
task: wlItem,
});
}

public clearItems(hass: HomeAssistant): Promise<void> {
throw new Error("Not yet implemented!");
}

public addItem(
hass: HomeAssistant,
listId: string,
item: ToDoListItem
): Promise<ToDoListItem> {
const wlItem = this.mapToWunderListItem(item);
return hass.callWS({
type: "wunderlist/tasks/add",
list_id: listId,
task: wlItem,
});
}

private mapToToDoListItem(wlItem: any): ToDoListItem {
return {
name: wlItem.title,
complete: wlItem.completed,
id: wlItem.id,
};
}

private mapToWunderListItem(tdItem: ToDoListItem): any {
return {
title: tdItem.name,
completed: tdItem.complete,
id: tdItem.id,
};
}
}

export function createToDoListAPI(provider: string) {
if (provider === "shopping-list") {
return new ShoppingListAPI();
} else if (provider === "wunderlist") {
return new WunderListAPI();
} else {
throw new Error(`Unknown provider: ${provider}`);
}
}