Skip to content

Commit

Permalink
[tibber] Tibber Binding initial contribution (openhab#6457)
Browse files Browse the repository at this point in the history
Signed-off-by: Stian Kjoglum <[email protected]>
  • Loading branch information
kjoglum authored and andrewfg committed Aug 31, 2020
1 parent 675bdda commit 04ffc1b
Show file tree
Hide file tree
Showing 16 changed files with 1,081 additions and 0 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
/bundles/org.openhab.binding.telegram/ @ZzetT
/bundles/org.openhab.binding.tellstick/ @jarlebh
/bundles/org.openhab.binding.tesla/ @kgoderis
/bundles/org.openhab.binding.tibber/ @kjoglum
/bundles/org.openhab.binding.tplinksmarthome/ @Hilbrand
/bundles/org.openhab.binding.tradfri/ @cweitkamp @kaikreuzer
/bundles/org.openhab.binding.unifi/ @mgbowman
Expand Down
5 changes: 5 additions & 0 deletions bom/openhab-addons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,11 @@
<artifactId>org.openhab.binding.tesla</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.binding.tibber</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.binding.tplinksmarthome</artifactId>
Expand Down
32 changes: 32 additions & 0 deletions bundles/org.openhab.binding.tibber/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions bundles/org.openhab.binding.tibber/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.openhab.binding.tibber</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
13 changes: 13 additions & 0 deletions bundles/org.openhab.binding.tibber/NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
This content is produced and maintained by the openHAB project.

* Project home: https://www.openhab.org

== Declared Project Licenses

This program and the accompanying materials are made available under the terms
of the Eclipse Public License 2.0 which is available at
https://www.eclipse.org/legal/epl-2.0/.

== Source Code

https://github.com/openhab/openhab-addons
136 changes: 136 additions & 0 deletions bundles/org.openhab.binding.tibber/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# Tibber Binding

The Tibber Binding connects to the [Tibber API](https://developer.tibber.com), and enables users to retrieve electricity data:

* Default: Frequent polls are performed to retrieve electricity price and cost/consumption information
* Optional: For users having Tibber Pulse, a websocket connection is established to retrieve live measurements

Refresh time (poll frequency) is set manually as part of setup, minimum 1 minute.

Tibber Pulse will automatically be detected by the Binding if present and associated with the token/HomeID used for setup.

## Supported Things

Provided one have a Tibber User Account, the Tibber API is recognized as a thing in openHAB using the Tibber Binding.

Tibber Pulse is optional, but will enable live measurements.

The channels (i.e. measurements) associated with the Binding:

Tibber Default:

| Channel ID | Description | Read-only |
|--------------------|-----------------------------------------|-----------|
| Current Total | Current Total Price (energy + tax) | True |
| Starts At | Current Price Timestamp | True |
| Daily Cost | Daily Cost (last/previous day) | True |
| Daily Consumption | Daily Consumption (last/previous day) | True |
| Daily From | Timestamp (daily from) | True |
| Daily To | Timestamp (daily to) | True |
| Hourly Cost | Hourly Cost (last/previous hour) | True |
| Hourly Consumption | Hourly Consumption (last/previous hour) | True |
| Hourly From | Timestamp (hourly from) | True |
| Hourly To | Timestamp (hourly to) | True |

Tibber Pulse (optional):

| Channel ID | Description | Read-only |
|-------------------------|------------------------------------------|-----------|
| Timestamp | Timestamp for live measurements | True |
| Power | Live Power Consumption | True |
| Last Meter Consumption | Last Recorded Meter Consumption | True |
| Accumulated Consumption | Accumulated Consumption since Midnight | True |
| Accumulated Cost | Accumulated Cost since Midnight | True |
| Currency | Currency of Cost | True |
| Min Power | Min Power Consumption since Midnight | True |
| Average Power | Average Power Consumption since Midnight | True |
| Max Power | Max Power Consumption since Midnight | True |
| Voltage 1-3 | Voltage per Phase | True |
| Current 1-3 | Current per Phase | True |
| Power Production | Live Power Production | True |
| Accumulated Production | Accumulated Production since Midnight | True |
| Min Power Production | Min Power Production since Midnight | True |
| Max Power Production | Max Power Production since Midnight | True |


## Binding Configuration

To access and initiate the Tibber Binding, a Tibber user account is required.

The following input is required for initialization:

* Tibber token
* Tibber HomeId
* Refresh Interval (min 1 minute)

Note: Tibber token is retrieved from your Tibber account:
[Tibber Account](https://developer.tibber.com/settings/accesstoken)

Note: Tibber HomeId is retrieved from [www.developer.com](https://developer.tibber.com/explorer):

* Sign in (Tibber user account) and "load" personal token.
* Copy query from below and paste into the Tibber API Explorer, and run query.
* If Tibber Pulse is connected, the Tibber API Explorer will report "true" for "realTimeConsumptionEnabled"
* Copy HomeId from Tibber API Explorer, without quotation marks, and paste into Paper UI configuration


```
{
viewer {
homes {
id
features {
realTimeConsumptionEnabled
}
}
}
}
```

If user have multiple HomeIds / Pulse, separate Things have to be created for the different/desired HomeIds in Paper UI.


## Thing Configuration

When Tibber Binding is installed, Tibber API should be autodiscovered in Paper UI.

Retrieve personal token and HomeId from description above, and initialize/start Binding from Paper UI.

Tibber API will be autodiscovered if provided input is correct.


## Full Example

demo.items:

```
Number:Dimensionless TibberAPICurrentTotal "Current Total Price [%.2f NOK]" {channel="tibber:tibberapi:7cfae492:current_total"}
DateTime TibberAPICurrentStartsAt "Timestamp - Current Price" {channel="tibber:tibberapi:7cfae492:current_startsAt"}
DateTime TibberAPIDailyFrom "Timestamp - Daily From" {channel="tibber:tibberapi:7cfae492:daily_from"}
DateTime TibberAPIDailyTo "Timestamp - Daily To" {channel="tibber:tibberapi:7cfae492:daily_to"}
Number:Dimensionless TibberAPIDailyCost "Total Daily Cost [%.2f NOK]" {channel="tibber:tibberapi:7cfae492:daily_cost"}
Number:Energy TibberAPIDailyConsumption "Total Daily Consumption [%.2f kWh]" {channel="tibber:tibberapi:7cfae492:daily_consumption"}
DateTime TibberAPIHourlyFrom "Timestamp - Hourly From" {channel="tibber:tibberapi:7cfae492:hourly_from"}
DateTime TibberAPIHourlyTo "Timestamp - Hourly To" {channel="tibber:tibberapi:7cfae492:hourly_to"}
Number:Dimensionless TibberAPIHourlyCost "Total Hourly Cost [%.2f NOK]" {channel="tibber:tibberapi:7cfae492:hourly_cost"}
Number:Energy TibberAPIHourlyConsumption "Total Hourly Consumption [%.2f kWh]" {channel="tibber:tibberapi:7cfae492:hourly_consumption"}
DateTime TibberAPILiveTimestamp "Timestamp - Live Measurement" {channel="tibber:tibberapi:7cfae492:live_timestamp"}
Number:Power TibberAPILivePower "Live Power Consumption [W]" {channel="tibber:tibberapi:7cfae492:live_power"}
Number:Energy TibberAPILiveLastMeterConsumption "Last Meter Consumption [%.2f kWh]" {channel="tibber:tibberapi:7cfae492:live_lastMeterConsumption"}
Number:Energy TibberAPILiveAccumulatedConsumption "Accumulated Consumption [%.2f kWh]" {channel="tibber:tibberapi:7cfae492:live_accumulatedConsumption"}
Number:Dimensionless TibberAPILiveAccumulatedCost "Accumulated Cost [%.2f NOK]" {channel="tibber:tibberapi:7cfae492:live_accumulatedCost"}
String TibberAPILiveCurrency "Currency" {channel="tibber:tibberapi:7cfae492:live_currency"}
Number:Power TibberAPILiveMinPower "Min Power Consumption [W]" {channel="tibber:tibberapi:7cfae492:live_minPower"}
Number:Power TibberAPILiveAveragePower "Average Power Consumption [W]" {channel="tibber:tibberapi:7cfae492:live_averagePower"}
Number:Power TibberAPILiveMaxPower "Max Power Consumption [W]" {channel="tibber:tibberapi:7cfae492:live_maxPower"}
Number:ElectricPotential TibberAPILiveVoltage1 "Live Voltage Phase 1 [V]" {channel="tibber:tibberapi:7cfae492:live_voltage1"}
Number:ElectricPotential TibberAPILiveVoltage2 "Live Voltage Phase 2 [V]" {channel="tibber:tibberapi:7cfae492:live_voltage2"}
Number:ElectricPotential TibberAPILiveVoltage3 "Live Voltage Phase 3 [V]" {channel="tibber:tibberapi:7cfae492:live_voltage3"}
Number:ElectricCurrent TibberAPILiveCurrent1 "Live Current Phase 1 [V]" {channel="tibber:tibberapi:7cfae492:live_current1"}
Number:ElectricCurrent TibberAPILiveCurrent2 "Live Current Phase 2 [V]" {channel="tibber:tibberapi:7cfae492:live_current2"}
Number:ElectricCurrent TibberAPILiveCurrent3 "Live Current Phase 3 [V]" {channel="tibber:tibberapi:7cfae492:live_current3"}
Number:Power TibberAPILivePowerProduction "Live Power Production [W]" {channel="tibber:tibberapi:7cfae492:live_powerProduction"}
Number:Power TibberAPILiveMinPowerproduction "Min Power Production [W]" {channel="tibber:tibberapi:7cfae492:live_minPowerproduction"}
Number:Power TibberAPILiveMaxPowerproduction "Power consumption/production" {channel="tibber:tibberapi:7cfae492:live_maxPowerproduction"}
Number:Energy TibberAPILiveAccumulatedProduction "Accumulated Production [%.2f kWh]" {channel="tibber:tibberapi:7cfae492:live_accumulatedProduction"}
```
16 changes: 16 additions & 0 deletions bundles/org.openhab.binding.tibber/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.addons.reactor.bundles</artifactId>
<version>2.5.4-SNAPSHOT</version>
</parent>

<artifactId>org.openhab.binding.tibber</artifactId>

<name>openHAB Add-ons :: Bundles :: Tibber Binding</name>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<features name="org.openhab.binding.tibber-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0">
<repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${ohc.version}/xml/features</repository>

<feature name="openhab-binding-tibber" description="tibber Binding" version="${project.version}">
<feature>openhab-runtime-base</feature>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.tibber/${project.version}</bundle>
</feature>
</features>
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.tibber.internal;

import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.core.thing.ThingTypeUID;

/**
* The {@link TibberBindingConstants} class defines common constants, which are
* used across the whole binding.
*
* @author Stian Kjoglum - Initial contribution
*/
@NonNullByDefault
public class TibberBindingConstants {

public static final String BINDING_ID = "tibber";
public static final String JSON_CONTENT_TYPE = "application/json";

// Tibber base URL for queries and mutations
public static final String BASE_URL = "https://api.tibber.com/v1-beta/gql";

// Tibber websocket endpoint for live subscription
public static final String SUBSCRIPTION_URL = "wss://api.tibber.com/v1-beta/gql/subscriptions";

// List of all Thing Type UIDs
public static final ThingTypeUID TIBBER_THING_TYPE = new ThingTypeUID(BINDING_ID, "tibberapi");

// List of all Channel ids - Used for queries
public static final String CURRENT_TOTAL = "current_total";
public static final String CURRENT_STARTSAT = "current_startsAt";
public static final String DAILY_FROM = "daily_from";
public static final String DAILY_TO = "daily_to";
public static final String DAILY_COST = "daily_cost";
public static final String DAILY_CONSUMPTION = "daily_consumption";
public static final String HOURLY_FROM = "hourly_from";
public static final String HOURLY_TO = "hourly_to";
public static final String HOURLY_COST = "hourly_cost";
public static final String HOURLY_CONSUMPTION = "hourly_consumption";
public static final String LIVE_TIMESTAMP = "live_timestamp";
public static final String LIVE_POWER = "live_power";
public static final String LIVE_LASTMETERCONSUMPTION = "live_lastMeterConsumption";
public static final String LIVE_ACCUMULATEDCONSUMPTION = "live_accumulatedConsumption";
public static final String LIVE_ACCUMULATEDCOST = "live_accumulatedCost";
public static final String LIVE_CURRENCY = "live_currency";
public static final String LIVE_MINPOWER = "live_minPower";
public static final String LIVE_AVERAGEPOWER = "live_averagePower";
public static final String LIVE_MAXPOWER = "live_maxPower";
public static final String LIVE_VOLTAGE1 = "live_voltage1";
public static final String LIVE_VOLTAGE2 = "live_voltage2";
public static final String LIVE_VOLTAGE3 = "live_voltage3";
public static final String LIVE_CURRENT1 = "live_current1";
public static final String LIVE_CURRENT2 = "live_current2";
public static final String LIVE_CURRENT3 = "live_current3";
public static final String LIVE_POWERPRODUCTION = "live_powerProduction";
public static final String LIVE_ACCUMULATEDPRODUCTION = "live_accumulatedProduction";
public static final String LIVE_MINPOWERPRODUCTION = "live_minPowerproduction";
public static final String LIVE_MAXPOWERPRODUCTION = "live_maxPowerproduction";

// Lift of all config ids
public static final String CONFIG_BRIDGE_TOKEN = "token";
public static final String CONFIG_BRIDGE_REFRESH = "refresh";

public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream.of(TIBBER_THING_TYPE)
.collect(Collectors.toSet());

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.tibber.internal;

import static org.openhab.binding.tibber.internal.TibberBindingConstants.*;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory;
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory;
import org.openhab.binding.tibber.internal.handler.TibberHandler;
import org.osgi.service.component.annotations.Component;

/**
* The {@link TibberHandlerFactory} is responsible for creating things and thing
* handlers.
*
* @author Stian Kjoglum - Initial contribution
*/
@NonNullByDefault
@Component(configurationPid = "binding.tibber", service = ThingHandlerFactory.class)
public class TibberHandlerFactory extends BaseThingHandlerFactory {

@Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
}

@Override
protected @Nullable ThingHandler createHandler(Thing thing) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (thingTypeUID.equals(TIBBER_THING_TYPE)) {
return new TibberHandler(thing);
} else {
return null;
}
}
}
Loading

0 comments on commit 04ffc1b

Please sign in to comment.