From 829f4e347883a7b3b7b5038e87d362c88232a4bc Mon Sep 17 00:00:00 2001
From: gvzdus <>
Date: Thu, 27 Apr 2023 10:16:32 +0000
Subject: [PATCH] 47_OBIS: Added support for Tibber Pulse with HTTP-Hack
git-svn-id: https://svn.fhem.de/fhem/trunk@27490 2b470e98-0d58-463d-a4d8-8e2adae1ed80
---
fhem/CHANGED | 1 +
fhem/FHEM/47_OBIS.pm | 69 +++++++++++++++++++++++++++++++++++++++-----
2 files changed, 63 insertions(+), 7 deletions(-)
diff --git a/fhem/CHANGED b/fhem/CHANGED
index a12f91ccc..42daaa1c5 100644
--- a/fhem/CHANGED
+++ b/fhem/CHANGED
@@ -1,5 +1,6 @@
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide.
# Do not insert empty lines here, update check depends on it.
+ - feature: 47_OBIS: Support for Tibber Pulse with HTTP-Hack
- feature: AutomowerConnectFamily: add definiton of zones, possibillity to
highlight path section, some fixes
- bugfix: 72_FRITZBOX: Dumper Fehler
diff --git a/fhem/FHEM/47_OBIS.pm b/fhem/FHEM/47_OBIS.pm
index 374b1f341..a3079b9e4 100644
--- a/fhem/FHEM/47_OBIS.pm
+++ b/fhem/FHEM/47_OBIS.pm
@@ -22,6 +22,7 @@ use Time::HiRes qw(gettimeofday usleep);
use Scalar::Util qw(looks_like_number);
use POSIX qw{strftime};
use DevIo;
+use HttpUtils;
no warnings 'portable'; # Support for 64-bit ints required
my %OBIS_channels = (
@@ -110,7 +111,7 @@ sub OBIS_Initialize($)
$hash->{GetFn} = "OBIS_Get";
$hash->{UndefFn} = "OBIS_Undef";
$hash->{AttrFn} = "OBIS_Attr";
- $hash->{AttrList}= "do_not_notify:1,0 interval offset_feed offset_energy IODev channels directions alignTime pollingMode:on,off extChannels:on,off,auto unitReadings:on,off ignoreUnknown:on,off valueBracket:first,second,both resetAfterNoDataTime createPreValues:on,off ".
+ $hash->{AttrList}= "do_not_notify:1,0 interval offset_feed offset_energy IODev channels directions alignTime pollingMode:on,off extChannels:on,off,auto unitReadings:on,off ignoreUnknown:on,off valueBracket:first,second,both resetAfterNoDataTime createPreValues:on,off httpAuthorization ".
$readingFnAttributes;
}
@@ -195,7 +196,17 @@ sub OBIS_Define($$)
Log3 $hash, 5, "OBIS ($name) - Opening device...";
if (! -f $dev) {
- DevIo_OpenDev($hash, 0, "OBIS_Init", $hash->{helper}{NETDEV} ? "OBIS_Callback" : undef);
+ if ($dev !~ /^https?:\/\/.*/i ) {
+ DevIo_OpenDev($hash, 0, "OBIS_Init", $hash->{helper}{NETDEV} ? "OBIS_Callback" : undef);
+ } else {
+ $hash->{helper}{LASTDATA} = "";
+ HttpUtils_NonblockingGet({
+ hash => $hash,
+ header => $hash->{helper}{HTTPAUTH},
+ url => $dev,
+ callback => \&OBIS_HttpCallback
+ });
+ }
} else {
# Debug mode: Open a FHEM debug session and process it...
Log3 $hash, 1, "OBIS ($name) - Replaying session";
@@ -279,14 +290,24 @@ sub OBIS_GetUpdate($)
{
my ($hash) = @_;
my $name = $hash->{NAME};
+ my $dev = $hash->{DeviceName};
my $type= $hash->{MeterType};
RemoveInternalTimer($hash, "OBIS_GetUpdate");
$hash->{helper}{EoM}=-1;
if ($hash->{helper}{DEVICES}[1] eq "") {return undef;}
if( $init_done ) {
- DevIo_SimpleWrite($hash,$hash->{helper}{DEVICES}[0],undef) ;
- Log3 $hash, 4, "OBIS ($name) - Wrote $hash->{helper}{DEVICES}[0]";
+ if ($dev =~ /^https?:\/\/.*/i ) {
+ HttpUtils_NonblockingGet({
+ hash => $hash,
+ url => $dev,
+ header => $hash->{helper}{HTTPAUTH},
+ callback => \&OBIS_HttpCallback
+ });
+ } else {
+ DevIo_SimpleWrite($hash,$hash->{helper}{DEVICES}[0],undef) ;
+ Log3 $hash, 4, "OBIS ($name) - Wrote $hash->{helper}{DEVICES}[0]";
+ }
}
my $t=OBIS_adjustAlign($hash,AttrVal($name,"alignTime",undef),$hash->{helper}{DEVICES}[1]);
Log3 ($hash,5,"OBIS ($name) - Internal timer set to ".FmtDateTime($t)) if ($hash->{helper}{DEVICES}[1]>0);
@@ -539,6 +560,25 @@ sub OBIS_trySMLdecode($$)
return ($newMsg,$remainingSML);
}
+sub OBIS_HttpCallback($$$)
+{
+ my ($param, $err, $data) = @_;
+ my $hash = $param->{hash};
+ my $name = $hash->{NAME};
+ if ($err) {
+ Log3 $hash,1,"OBIS ($name) - Error $err";
+ } else {
+ Log3 $hash,5,"OBIS ($name) - Got data, len " . length $data;
+ if ($hash->{helper}{LASTDATA} eq $data) {
+ Log3 $hash,4,"OBIS ($name) - HTTP data unchanged";
+ } else {
+ $hash->{helper}{LASTDATA} = $data;
+ OBIS_Parse($hash, $data);
+ }
+ }
+}
+
+
sub OBIS_Parse($$)
{
my ($hash, $buf) = @_;
@@ -841,6 +881,11 @@ Log3 $name, 3, "OBIS ($name) - Attr $aName Val $aVal, dopoll = $dopoll";
}
}
}
+ if ($aName eq "httpAuthorization") {
+ return "OBIS ($name) - $name: attr httpAuthorization must be in the format user:password, e.g. for Tibber Pulse admin:ABCD-1234"
+ if ($aVal!~/^\w+:.*$/);
+ $hash->{helper}{HTTPAUTH} = "Authorization: Basic " . encode_base64($aVal);
+ }
}
return undef;
}
@@ -939,7 +984,8 @@ sub OBIS_CRC16($$) {
define <name> OBIS device|none [MeterType]
<device> specifies the serial port or hostname/ip-address:port
- to communicate with the smartmeter.
+ to communicate with the smartmeter. For Tibber Pulse and requesting via HTTP,
+ specify the URL in the LAN.
In case of a serial device and with Linux:
httpAuthorization
define <name> OBIS device|none [MeterType]
resetAfterNoDataTime
httpAuthorization