-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a widget that indicates activity state for a given zone
- Loading branch information
Showing
17 changed files
with
431 additions
and
9 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 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,65 @@ | ||
import { ExtendedHomeyAPIV3Local, ExtendedZone } from "homey-api"; | ||
import Homey from "homey/lib/Homey"; | ||
import handleZoneAutocomplete from "../utils/handleZoneAutocomplete"; | ||
import ZonesDb from "./ZonesDb"; | ||
import getIconForZone, { getZoneImageSource } from "./../utils/getIconForZone"; | ||
|
||
export default class WidgetZoneActivityState { | ||
|
||
private static instance: WidgetZoneActivityState | null = null; | ||
|
||
widget: unknown; | ||
|
||
private constructor(private homey: Homey, private homeyApi: ExtendedHomeyAPIV3Local, private zonesDb: ZonesDb, private log: (...args: unknown[]) => void, private error: (...args: unknown[]) => void) { | ||
// @ts-expect-error Ignore the error for the non-existing method / property | ||
this.widget = this.homey.dashboards.getWidget('zone-activity-state'); | ||
} | ||
|
||
public static async initialize(homey: Homey, homeyApi: ExtendedHomeyAPIV3Local, zonesDb: ZonesDb, log: (...args: unknown[]) => void, error: (...args: unknown[]) => void): Promise<WidgetZoneActivityState> { | ||
if (WidgetZoneActivityState.instance === null) { | ||
WidgetZoneActivityState.instance = new WidgetZoneActivityState(homey, homeyApi, zonesDb, log, error); | ||
await WidgetZoneActivityState.instance.setup(); | ||
} | ||
return WidgetZoneActivityState.instance; | ||
} | ||
|
||
private async setup(): Promise<void> { | ||
// @ts-expect-error Ignore the error for the non-existing method / property | ||
this.widget.registerSettingAutocompleteListener('zone', async (query: string) => { | ||
const result = (await handleZoneAutocomplete(query, this.zonesDb)) | ||
// Remap icon to image for all results | ||
.map((zone) => { | ||
zone.image = zone.icon; | ||
return zone; | ||
}); | ||
return result; | ||
}); | ||
|
||
this.homeyApi.zones.on('zone.update', async (zone: ExtendedZone) => { | ||
const data = await this.transformZone(zone); | ||
this.homey.api.realtime(`zone-update-${zone.id}`, data); | ||
}); | ||
} | ||
|
||
public async getZone(zoneId: string): Promise<unknown> { | ||
const zone = await this.zonesDb.getZone(zoneId); | ||
if (zone === null) | ||
return null; | ||
|
||
return await this.transformZone(zone); | ||
} | ||
|
||
private async transformZone(zone: ExtendedZone): Promise<unknown> { | ||
const result = { | ||
id: zone.id, | ||
iconSrc: await getZoneImageSource(zone.icon), | ||
icon: getIconForZone(zone.icon), | ||
name: zone.name, | ||
active: zone.active, | ||
activeLastUpdated: zone.activeLastUpdated, | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -1,4 +1,14 @@ | ||
{ | ||
"any_type": "Any type", | ||
"any_sensor": "Any sensor" | ||
"any_sensor": "Any sensor", | ||
"active": "Active", | ||
"last_active": "Last active", | ||
"hour_ago": "hour ago", | ||
"hours_ago": "hours ago", | ||
"minute_ago": "minute ago", | ||
"minutes_ago": "minutes ago", | ||
"second_ago": "second ago", | ||
"seconds_ago": "seconds ago", | ||
"widget_error": "An error occurred", | ||
"contact_dev": "Please contact the developer" | ||
} |
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 |
---|---|---|
@@ -1,4 +1,14 @@ | ||
{ | ||
"any_type": "Elk type", | ||
"any_sensor": "Elke sensor" | ||
"any_sensor": "Elke sensor", | ||
"active": "Actief", | ||
"last_active": "Laatst actief", | ||
"hours_ago": "uur geleden", | ||
"hour_ago": "uur geleden", | ||
"minutes_ago": "minuten geleden", | ||
"minute_ago": "minuut geleden", | ||
"second_ago": "seconde geleden", | ||
"seconds_ago": "seconden geleden", | ||
"widget_error": "Er is een fout is opgetreden", | ||
"contact_dev": "Neem contact op met de ontwikkelaar" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
import Homey from "homey/lib/Homey"; | ||
|
||
class ZoneActivityStateWidget { | ||
public async getZone({ homey, query }: ApiRequest) : Promise<string> { | ||
//@ts-expect-error: Ignore the error for the non-existing method | ||
const result = await homey.app.widget.getZone(query.zoneId); | ||
homey.app.log('Widget initialized,', { widgetInstanceId: query.widgetInstanceId, zoneId: query.zoneId, result }); | ||
return result; | ||
} | ||
|
||
public async log({ homey, body }: ApiRequest) : Promise<void> { | ||
const message = `[${this.constructor.name}]: ${body[0]}`; | ||
const args = body.slice(1); | ||
homey.app.log(message, ...args); | ||
} | ||
} | ||
|
||
module.exports = new ZoneActivityStateWidget(); | ||
|
||
type ApiRequest = { homey: Homey; query: any, body: any, params: unknown }; | ||
Check warning on line 21 in widgets/zone-activity-state/api.ts GitHub Actions / eslint
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.