Skip to content

Commit 8e14590

Browse files
♻️ refactoring to TypeScript (wip)
1 parent 1dba2b5 commit 8e14590

File tree

15 files changed

+489
-128
lines changed

15 files changed

+489
-128
lines changed

hacs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "Today Card",
33
"content_in_root": false,
4-
"filename": "dist/ha-today-card.js",
4+
"filename": "ha-today-card.js",
55
"homeassistant": "2025.2.0",
66
"hacs": "2.0.0"
77
}

package-lock.json

Lines changed: 353 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"version": "0.0.1",
3-
"source": "src/index.js",
3+
"source": "src/index.ts",
44
"module": "dist/ha-today-card.js",
55
"targets": {
66
"module": {
@@ -17,7 +17,9 @@
1717
},
1818
"dependencies": {
1919
"@mdi/js": "^7.4.47",
20+
"custom-card-helpers": "^1.9.0",
2021
"dayjs": "^1.11.13",
22+
"home-assistant-js-websocket": "^9.4.0",
2123
"lit": "^3.2.1",
2224
"superstruct": "^2.0.2"
2325
}

src/const.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/const.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const TIME_FORMAT: string = 'HH:mm';
2+
3+
export const HA_API_DATE_FORMAT: string = 'YYYY-MM-DDTHH:mm:ss';
Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,63 @@
1-
import {getEvents} from "../functions/calendar.js";
2-
import {processEditorEntities} from "../functions/config.js";
31
import styles from "bundle-text:./card.css";
4-
import {html, LitElement, unsafeCSS} from 'lit';
2+
import {CSSResult, html, LitElement, nothing, TemplateResult, unsafeCSS} from 'lit';
3+
import {customElement, state} from 'lit/decorators.js';
54
import {assert} from "superstruct";
5+
import {getEvents} from "../functions/calendar.js";
66
import {computeCssColor} from "../functions/colors";
7+
import {processEditorEntities} from "../functions/config.js";
78
import localize from "../localization/localize";
8-
import {cardConfigStruct} from "../structs/config";
9-
10-
export class TodayCard extends LitElement
11-
{
12-
13-
static styles = unsafeCSS(styles);
14-
_initialized = false;
15-
16-
static get properties()
17-
{
18-
return {
19-
_config: {state: true},
20-
_content: {state: true},
21-
};
22-
}
9+
import {CardConfig} from "../structs/config";
10+
import {HomeAssistant} from "custom-card-helpers";
11+
import CalendarEvent from "../structs/event";
2312

24-
_hass;
13+
@customElement('today-card')
14+
export class TodayCard extends LitElement {
15+
// @property({ attribute: false }) public hass!: HomeAssistant;
16+
@state() private config: CardConfig;
17+
@state() private content: TemplateResult;
18+
private initialized: boolean = false;
19+
private _hass?: HomeAssistant;
2520

26-
set hass(hass)
27-
{
21+
set hass(hass: HomeAssistant) {
2822
this._hass = hass;
2923
}
3024

31-
static getConfigElement()
32-
{
25+
static getConfigElement(): HTMLElement {
3326
return document.createElement("today-card-editor");
3427
}
3528

36-
static getStubConfig(hass, entities, entitiesFallback)
37-
{
29+
static getStubConfig(
30+
hass: HomeAssistant,
31+
entities: string[],
32+
entitiesFallback: string[]
33+
): Partial<CardConfig> {
3834
let calendarEntities = entities.filter((entityId) => entityId.startsWith('calendar.'));
3935

4036
if (calendarEntities.length < 1) {
4137
calendarEntities = entitiesFallback.filter((entityId) => entityId.startsWith('calendar.'));
4238
}
4339

4440
return {
45-
entities: calendarEntities,
46-
title: localize(this._hass, 'config.stub.title'),
41+
type: "custom:today-card",
42+
title: localize(hass, 'config.stub.title'),
4743
advance: 0,
4844
show_all_day_events: true,
4945
show_past_events: false,
46+
entities: calendarEntities,
5047
};
5148
}
5249

53-
setConfig(config)
54-
{
55-
assert(config, cardConfigStruct);
56-
this._config = {...config, entities: processEditorEntities(config.entities, true)};
57-
this._content = "";
50+
setConfig(config: CardConfig) {
51+
assert(config, CardConfig);
52+
this.config = {...config, entities: processEditorEntities(config.entities, true)};
53+
this.content = html``;
5854
}
5955

60-
async updateEvents()
61-
{
56+
async updateEvents() {
6257
console.log('Updating events');
63-
const events = await getEvents(this._config, this._hass);
58+
const events = await getEvents(this.config, this._hass);
6459

65-
let eventsHtml = events.map((event) => {
60+
let eventsHtml = events.map((event: CalendarEvent) => {
6661
return html`
6762
<div class="event">
6863
<div class="indicator" style="background-color: ${computeCssColor(event.color)}"></div>
@@ -74,27 +69,30 @@ export class TodayCard extends LitElement
7469
`;
7570
});
7671

77-
this._content = html`
72+
this.content = html`
7873
<div class="events">
7974
${eventsHtml}
8075
</div>
8176
`;
8277
}
8378

84-
render()
85-
{
79+
render() {
8680
console.log('Rendering');
87-
if (!this._initialized) {
81+
if (!this.initialized) {
8882
this.updateEvents();
89-
this._initialized = true;
83+
this.initialized = true;
9084
}
9185

9286
return html`
93-
<ha-card header="${this._config.title}">
87+
<ha-card header="${this.config.title}">
9488
<div class="card-content">
95-
${this._content}
89+
${this.content}
9690
</div>
9791
</ha-card>
9892
`;
9993
}
94+
95+
static get styles(): CSSResult {
96+
return unsafeCSS(styles);
97+
}
10098
}

src/elements/editor.js renamed to src/elements/editor.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import styles from "bundle-text:./editor.css";
22
import {html, LitElement, nothing, unsafeCSS} from "lit";
3+
import {customElement} from "lit/decorators.js";
34
import {assert} from "superstruct";
45
import {fireEvent, processEditorEntities} from "../functions/config.js";
56
import {loadHaComponents} from "../functions/hacks";
6-
import {cardConfigStruct} from "../structs/config";
7+
import {CardConfig} from "../structs/config";
78

89
const FORM_SCHEMA = [
910
{
@@ -35,6 +36,7 @@ const FORM_SCHEMA = [
3536
},
3637
];
3738

39+
@customElement('today-card-editor')
3840
export class TodayCardEditor extends LitElement
3941
{
4042

@@ -57,7 +59,7 @@ export class TodayCardEditor extends LitElement
5759

5860
setConfig(config)
5961
{
60-
assert(config, cardConfigStruct);
62+
assert(config, CardConfig);
6163

6264
let entities = processEditorEntities(config.entities, false);
6365
this._config = {...config, entities: entities};

src/elements/entity-editor.js renamed to src/elements/entity-editor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {mdiClose} from "@mdi/js";
22
import styles from "bundle-text:./entity-editor.css";
33
import {html, LitElement, nothing, unsafeCSS} from "lit";
4+
import {customElement} from "lit/decorators.js";
45
import {repeat} from "lit/directives/repeat";
56
import {fireEvent} from "../functions/config.js";
67

8+
@customElement('today-card-entities-editor')
79
export class TodayCardEntitiesEditor extends LitElement
810
{
911
static styles = unsafeCSS(styles);

src/index.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {version} from "../package.json";
2+
import "./elements/card";
3+
import "./elements/editor";
4+
import "./elements/entity-editor";
5+
6+
(window as any).customCards = (window as any).customCards || [];
7+
(window as any).customCards.push({
8+
type: "today-card",
9+
name: "Today",
10+
description: "Show today's schedule",
11+
});
12+
13+
console.info(
14+
`%c🗓️ Today Card v${version}`,
15+
"font-weight: 700;",
16+
);

0 commit comments

Comments
 (0)