2023-04-13 16:50:12 +02:00
#!/usr/bin/env perl
use 5.024 ;
use strict ;
use warnings ;
use autodie ;
2023-09-24 14:59:55 +02:00
use Config::Tiny ;
2023-04-13 16:50:12 +02:00
use Getopt::Long ;
use HTTP::Tiny ;
use JSON::PP ;
use Data::Dumper ;
2023-09-24 14:59:55 +02:00
GetOptions (
"config=s" = > \ ( my $ CONFIG ) ,
) ;
my $ USAGE = << "END_USAGE" ;
Usage: $ 0 [ - c config ]
Options:
- c | - - config text ( no default )
END_USAGE
my @ config_files = $ CONFIG || grep { - e } ( './_tibber.conf' , './.tibber.conf' , './tibber.conf' , "$ENV{'HOME'}/_tibber.conf" , "$ENV{'HOME'}/.tibber.conf" , "$ENV{'HOME'}/tibber.conf" ) ;
my $ config = Config::Tiny - > read ( $ config_files [ - 1 ] , 'utf8' ) ;
my $ TOKEN = $ config - > { $ APPLICATION } - > { token } || die ( "$USAGE\nError: TOKEN not found.\n" ) ;
2023-04-13 16:50:12 +02:00
my $ http = HTTP::Tiny - > new ;
my % HEADERS = ( "Content-Type" = > "application/json" , "Authorization" = > "Bearer $TOKEN" ) ;
my $ uri = "https://api.tibber.com/v1-beta/gql" ;
my $ request = HTTP::Tiny - > new ( 'default_headers' = > \ % HEADERS ) ;
my $ json = JSON::PP - > new ;
my $ body = $ json - > encode ( { query = > "{viewer{homes{currentSubscription{priceInfo{current{total energy tax startsAt} today{total energy tax startsAt} tomorrow{total energy tax startsAt}}}}}}" } ) ;
#print "$body\n";
my $ response = $ request - > post ( $ uri , { 'content' = > $ body } ) ;
#print "$response->{'status'} $response->{'reason'}\n";
#print "$response->{'content'}\n";
my $ data = $ json - > decode ( $ response - > { 'content' } ) - > { 'data' } - > { 'viewer' } - > { 'homes' } [ 0 ] - > { 'currentSubscription' } ;
print "*** priceInfo ***\n" ;
print "current: " ;
if ( ! length $ data - > { 'priceInfo' } - > { 'current' } ) {
print "price not set\n" ;
} else {
print "$data->{'priceInfo'}->{'current'}->{'total'} (Energy: $data->{'priceInfo'}->{'current'}->{'energy'}; Tax: $data->{'priceInfo'}->{'current'}->{'tax'})\n" ;
#print Dumper $data->{'priceInfo'}->{'current'};
}
print "today: " ;
if ( ! length $ data - > { 'priceInfo' } - > { 'today' } [ 0 ] ) {
print "price not set\n" ;
} else {
print "$data->{'priceInfo'}->{'today'}[0]->{'total'} (Energy: $data->{'priceInfo'}->{'today'}[0]->{'energy'}; Tax: $data->{'priceInfo'}->{'today'}[0]->{'tax'})\n" ;
#print Dumper $data->{'priceInfo'}->{'today'}[0];
}
print "tomorrow: " ;
if ( ! length $ data - > { 'priceInfo' } - > { 'tomorrow' } [ 0 ] ) {
print "price set at 13:00\n" ;
} else {
print "$data->{'priceInfo'}->{'tomorrow'}[0]->{'total'} (Energy: $data->{'priceInfo'}->{'tomorrow'}[0]->{'energy'}; Tax: $data->{'priceInfo'}->{'tomorrow'}[0]->{'tax'})\n" ;
#print Dumper $data->{'priceInfo'}->{'tomorrow'}[0];
}