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

ACCESS_TOKEN, Widget #105

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .homeycompose/app.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "com.tibber",
"version": "1.9.18",
"compatibility": ">=8.1.0",
"compatibility": ">=12.1.0",
"platforms": ["local", "cloud"],
"sdk": 3,
"author": {
Expand Down
35 changes: 35 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"restart": true,
"name": "Attach Tibber to HomeyPro 19",
"address": "192.168.1.13",
"port": 9291,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/"
},
{
"type": "node",
"request": "attach",
"restart": true,
"name": "Attach Tibber to HomeyPro 23",
"address": "192.168.1.12",
"port": 9291,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/"
},
{
"type": "node",
"request": "attach",
"restart": true,
"name": "Attach Tibber to HomeyPro 23 Docker",
"address": "192.168.1.42",
"port": 9229,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app/"
}
]
}
113 changes: 112 additions & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"_comment": "This file is generated. Please edit .homeycompose/app.json instead.",
"id": "com.tibber",
"version": "1.9.18",
"compatibility": ">=8.1.0",
"compatibility": ">=12.1.0",
"platforms": [
"local",
"cloud"
Expand Down Expand Up @@ -1156,6 +1156,13 @@
},
"class": "other",
"pair": [
{
"id": "loading",
"template": "loading",
"navigation": {
"next": "login_oauth2"
}
},
{
"id": "login_oauth2",
"template": "login_oauth2"
Expand Down Expand Up @@ -1254,6 +1261,13 @@
},
"class": "other",
"pair": [
{
"id": "loading",
"template": "loading",
"navigation": {
"next": "login_oauth2"
}
},
{
"id": "login_oauth2",
"template": "login_oauth2"
Expand Down Expand Up @@ -1511,6 +1525,13 @@
},
"class": "other",
"pair": [
{
"id": "loading",
"template": "loading",
"navigation": {
"next": "login_oauth2"
}
},
{
"id": "login_oauth2",
"template": "login_oauth2"
Expand Down Expand Up @@ -1706,6 +1727,96 @@
"id": "watty"
}
],
"widgets": {
"price": {
"name": {
"en": "Price",
"de": "Preis"
},
"settings": [
{
"id": "device_home",
"type": "autocomplete",
"title": {
"en": "Home device"
}
},
{
"id": "device_pulse",
"type": "autocomplete",
"title": {
"en": "Pulse device"
}
},
{
"id": "chart_display",
"type": "checkbox",
"value": true,
"title": {
"en": "Display chart"
}
},
{
"id": "chart_zoom",
"type": "checkbox",
"value": true,
"title": {
"en": "Zoom chart"
}
},
{
"id": "chart_colored",
"type": "checkbox",
"value": true,
"title": {
"en": "Colored chart"
}
},
{
"id": "chart_color_style",
"type": "dropdown",
"value": "colored_line_level",
"title": {
"en": "Color style"
},
"values": [
{
"id": "colored_line_level",
"title": {
"en": "Colored line based on price level"
}
},
{
"id": "colored_line_range",
"title": {
"en": "Colored line based on range"
}
},
{
"id": "vertical_segments",
"title": {
"en": "Vertical segments based on price level"
}
},
{
"id": "horizontal_segments",
"title": {
"en": "Horizontal segments based on range"
}
}
]
}
],
"height": 70,
"api": {
"triggerRealtimeData": {
"method": "GET",
"path": "/trigger_realtime_data"
}
},
"id": "price"
}
},
"capabilities": {
"accumulatedCost": {
"type": "number",
Expand Down
52 changes: 52 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { App } from 'homey';
import Homey from 'homey/lib/Homey';
import { setGlobalAttributes } from './lib/newrelic-transaction';
import * as appJson from './app.json';
import { HomeDevice } from './drivers/home/device';

sourceMapSupport.install();

Expand All @@ -15,6 +16,19 @@ class TibberApp extends App {
async onInit() {
this.log('Tibber app is running...');

// Init Debugger
if (process.env.DEBUG === '1') {
// @ts-expect-error
if (this.homey.platform == "local") {
try {
require('inspector').waitForDebugger();
}
catch (error) {
require('inspector').open(9291, '0.0.0.0', true);
}
}
}

const { version: firmwareVersion, platformVersion } = this
.homey as HomeyWithMissingTypings;
const { version: appVersion } = appJson;
Expand All @@ -31,6 +45,7 @@ class TibberApp extends App {
this.homey.settings.set('v', 2);
this.cleanupLogs('*').catch(console.error);
}
this.#initWidgets();
}

async cleanupLogs(prefix: string) {
Expand All @@ -50,6 +65,43 @@ class TibberApp extends App {
async onUninit() {
this.log('Tibber app is stopping');
}

// WIDGET Settings ==============================================================================
async #initWidgets(){
// @ts-expect-error
this.homey.dashboards.getWidget('price').registerSettingAutocompleteListener('device_home', async (query: string, settings: any) => {
let homes: { name: string; id: any; }[] = [];
let devices = this.homey.drivers.getDriver('home').getDevices();
devices.forEach(device => {
homes.push({
name: device.getName(),
id: device.getData().id
})
});
return homes.filter((item) => item.name.toLowerCase().includes(query.toLowerCase()));
});
// @ts-expect-error
this.homey.dashboards.getWidget('price').registerSettingAutocompleteListener('device_pulse', async (query: string, settings: any) => {
let homes: { name: string; id: any; }[] = [];
let devices = this.homey.drivers.getDriver('pulse').getDevices();
devices.forEach(device => {
homes.push({
name: device.getName(),
id: device.getData().id
})
});
return homes.filter((item) => item.name.toLowerCase().includes(query.toLowerCase()));
});
}

// WIDGET API ============================================================================
async apiTriggerRealtimeData(){
// let device = this.homey.drivers.getDriver('home').getDevices()[0] as HomeDevice;
this.homey.drivers.getDriver('home').getDevices().forEach( (device ) => {
(device as HomeDevice).triggerRealtimeData();
});
}

}

// workaround for `The class exported in '<filepath>' must extend Homey.<classname>` error
Expand Down
26 changes: 25 additions & 1 deletion drivers/home/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const deprecatedPriceLevelMap = {
VERY_EXPENSIVE: 'HIGH',
};

class HomeDevice extends Device {
export class HomeDevice extends Device {
#api!: TibberApi;
#deviceLabel!: string;
#insightId!: string;
Expand Down Expand Up @@ -391,6 +391,21 @@ class HomeDevice extends Device {
return;
}

// Realtime event - Widget update
await this.homey.api.realtime("device_update", {
driver_id:'home',
device_id: this.getData().id,
now: now,
currentHour: currentHour,
currentPrice: currentPrice,
lowestToday: this.#prices.lowestToday,
highestToday: this.#prices.highestToday,
pricesToday: this.#prices.today,
hourlyPrices: this.#api.hourlyPrices,
tz: this.homey.clock.getTimezone(),
language: this.homey.i18n.getLanguage()
} );

const shouldUpdate =
currentPrice.startsAt !== this.#prices.latest?.startsAt;

Expand Down Expand Up @@ -835,6 +850,15 @@ class HomeDevice extends Device {
return await this.homey.insights.createLog(name, options);
}
}

async triggerRealtimeData(){
const now = moment();
try {
await this.#handlePrice(now);
} catch (err) {
console.error(err);
}
}
}

module.exports = HomeDevice;
7 changes: 7 additions & 0 deletions drivers/home/driver.compose.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
},
"class": "other",
"pair": [
{
"id": "loading",
"template": "loading",
"navigation": {
"next": "login_oauth2"
}
},
{
"id": "login_oauth2",
"template": "login_oauth2"
Expand Down
14 changes: 13 additions & 1 deletion drivers/home/driver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Driver } from 'homey';
import { Driver, env } from 'homey';
import PairSession from 'homey/lib/PairSession';
import { createListDeviceHandler } from '../../lib/device-helpers';
import { initiateOauth } from '../../lib/oauth';
Expand All @@ -14,6 +14,18 @@ class HomeDriver extends Driver {
onPair(session: PairSession) {
this.#api = new TibberApi(this.log, this.homey.settings);

session.setHandler('showView', async (view) => {
if (view == 'loading'){
if (env.ACCESS_TOKEN != undefined){
// If access token is provided, don't show oAuth popup.
await session.showView('list_devices');
}
else{
await session.showView('login_oauth2');
}
}
});

session.setHandler(
'list_devices',
createListDeviceHandler(
Expand Down
7 changes: 7 additions & 0 deletions drivers/pulse/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ class PulseDevice extends Device {
async subscribeCallback(result: LiveMeasurement) {
this.#resubscribeDebounce();

// Realtime event - Widget update
await this.homey.api.realtime("device_update", {
driver_id:'pulse',
device_id: this.getData().id,
liveMeasurement: result.data?.liveMeasurement
} );

const power = result.data?.liveMeasurement?.power;
const powerProduction = result.data?.liveMeasurement?.powerProduction;
if (powerProduction) this.#prevPowerProduction = powerProduction;
Expand Down
7 changes: 7 additions & 0 deletions drivers/pulse/driver.compose.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@
},
"class": "other",
"pair": [
{
"id": "loading",
"template": "loading",
"navigation": {
"next": "login_oauth2"
}
},
{
"id": "login_oauth2",
"template": "login_oauth2"
Expand Down
Loading