our
@ISA
=
qw(Exporter)
;
our
%EXPORT_TAGS
= (
'all'
=> [
qw(
new fetch_Auth_Token fetch_Thermostat_Designation fetch_Ambient_Temperature_C
fetch_Target_Temperature_C fetch_Target_Temperature_high_C fetch_Target_Temperature_low_C
fetch_Away_Temperature_low_C fetch_Ambient_Temperature_F fetch_Away_Temperature_low_F
fetch_Away_Temperature_high_F fetch_Target_Temperature_low_F fetch_Target_Temperature_F
fetch_Target_Temperature_high_F fetch_Temperature_Scale fetch_Locale fetch_Name
fetch_Long_Name fetch_HVAC_Mode fetch_SW_Version
)
] );
our
@EXPORT_OK
= ( @{
$EXPORT_TAGS
{
'all'
} } );
our
@EXPORT
=
qw( $EXPORT_TAGS{'all'})
;
BEGIN
{
if
($^O eq
"MSWin32"
){
use
JSON
qw(decode_json)
;
}
else
{
use
JSON
qw(decode_json)
;
}
}
our
$VERSION
=
'0.03'
;
sub
new {
my
$class
=
shift
;
my
$self
;
$self
->{
'ua'
} = LWP::UserAgent->new();
$self
->{
'ClientID'
} =
shift
;
$self
->{
'ClientSecret'
} =
shift
;
$self
->{
'PIN_code'
} =
shift
;
$self
->{
'auth_token'
} =
shift
;
if
((!
defined
$self
->{
'ClientID'
}) || (!
defined
$self
->{
'ClientSecret'
}) || (!
defined
$self
->{
'PIN_code'
}) || (!
defined
$self
->{
'auth_token'
})) {
print
"Nest->new(): ClientID, ClientSecret, PIN_code and auth_token are REQUIRED parameters.\n"
;
return
0;
}
bless
$self
,
$class
;
return
$self
;
}
sub
fetch_Auth_Token {
my
$self
=
shift
;
my
$auth_token
=
''
;
{
code
=>
$self
->{
'PIN_code'
},
grant_type
=>
'authorization_code'
,
client_id
=>
$self
->{
'ClientID'
},
client_secret
=>
$self
->{
'ClientSecret'
},
}
);
if
(
$response
->is_success) {
if
(
$response
->content =~ /\"access_token\":\"(.*?)\"/) {
print
"Found authentication code. Please use it when calling functions\n"
;
print
"Authentication code: $1\n"
;
return
1;
}
else
{
print
"No authentication token found.\n"
;
print
"Make sure your PIN is correct.\n"
;
print
"You may need to request a new PIN\n"
;
return
0;
}
}
else
{
print
"No authentication token found.\n"
;
print
"Make sure your PIN is correct.\n"
;
print
"You may need to request a new PIN\n"
;
return
0;
}
}
sub
fetch_Thermostat_Designation {
my
$self
=
shift
;
my
$response
=
$self
->{
'ua'
}->get(
$url
);
if
(
$response
->is_success) {
my
$decoded_response
= decode_json(
$response
->content);
my
$designation
= (
$decoded_response
->{
'thermostats'
});
my
@designation2
=
keys
(
%$designation
);
$self
->{
'thermostat'
} =
$designation2
[0];
return
1;
}
else
{
print
"Nest->fetch_Thermostat_Designation(): Response from server is not valid\n"
;
print
" \""
.
$response
->content.
"\"\n\n"
;
return
0;
}
}
sub
fetch_Ambient_Temperature_C {
my
$self
=
shift
;
if
(!
defined
$self
->{
'thermostat'
}) {
print
"No thermostat designation found\n"
;
return
0;
}
my
$response
=
$self
->{
'ua'
}->get(
$url
);
if
(
$response
->is_success) {
my
$decoded_response
= decode_json(
$response
->content);
return
$decoded_response
->{
'thermostats'
}->{
$self
->{
'thermostat'
}}->{
'ambient_temperature_c'
};
}
else
{
print
"Nest->fetch_Ambient_Temperature_C(): Response from server is not valid\n"
;
print
" \""
.
$response
->content.
"\"\n\n"
;
return
0;
}
}
sub
fetch_Target_Temperature_C {
my
$self
=
shift
;
if
(!
defined
$self
->{
'thermostat'
}) {
print
"No thermostat designation found\n"
;
return
0;
}
my
$response
=
$self
->{
'ua'
}->get(
$url
);
if
(
$response
->is_success) {
my
$decoded_response
= decode_json(
$response
->content);
return
$decoded_response
->{
'thermostats'
}->{
$self
->{
'thermostat'
}}->{
'target_temperature_c'
};
}
else
{
print
"Nest->fetch_Target_Temperature_C(): Response from server is not valid\n"
;
print
" \""
.
$response
->content.
"\"\n\n"
;
return
0;
}
}
sub
fetch_Target_Temperature_high_C {
my
$self
=
shift
;
if
(!
defined
$self
->{
'thermostat'
}) {
print
"No thermostat designation found\n"
;
return
0;
}
my
$response
=
$self
->{
'ua'
}->get(
$url
);
if
(
$response
->is_success) {
my
$decoded_response
= decode_json(
$response
->content);
return
$decoded_response
->{
'thermostats'
}->{
$self
->{
'thermostat'
}}->{
'target_temperature_high_c'
};
}
else
{
print
"Nest->fetch_Target_Temperature_high_C(): Response from server is not valid\n"
;
print
" \""
.
$response
->content.
"\"\n\n"
;
return
0;
}
}
sub
fetch_Target_Temperature_low_C {
my
$self
=
shift
;
if
(!
defined
$self
->{
'thermostat'
}) {
print
"No thermostat designation found\n"
;
return
0;
}
my
$response
=
$self
->{
'ua'
}->get(
$url
);
if
(
$response
->is_success) {
my
$decoded_response
= decode_json(
$response
->content);
return
$decoded_response
->{
'thermostats'
}->{
$self
->{
'thermostat'
}}->{
'target_temperature_low_c'
};
}
else
{
print
"Nest->fetch_Target_Temperature_low_C(): Response from server is not valid\n"
;
print
" \""
.
$response
->content.
"\"\n\n"
;
return
0;
}
}
sub
fetch_Away_Temperature_low_C {
my
$self
=
shift
;
if
(!
defined
$self
->{
'thermostat'
}) {
print
"No thermostat designation found\n"
;
return
0;
}
my
$response
=
$self
->{
'ua'
}->get(
$url
);
if
(
$response
->is_success) {
my
$decoded_response
= decode_json(
$response
->content);
return
$decoded_response
->{
'thermostats'
}->{
$self
->{
'thermostat'
}}->{
'away_temperature_low_c'
};
}
else
{
print
"Nest->fetch_Away_Temperature_low_C(): Response from server is not valid\n"
;
print
" \""
.
$response
->content.
"\"\n\n"
;
return
0;
}
}
sub
fetch_Away_Temperature_high_C {
my
$self
=
shift
;
if
(!
defined
$self
->{
'thermostat'
}) {
print
"No thermostat designation found\n"
;
return
0;
}
my
$response
=
$self
->{
'ua'
}->get(
$url
);
if
(
$response
->is_success) {
my
$decoded_response
= decode_json(
$response
->content);
return
$decoded_response
->{
'thermostats'
}->{
$self
->{
'thermostat'
}}->{
'away_temperature_high_c'
};
}
else
{
print
"Nest->fetch_Away_Temperature_high_C(): Response from server is not valid\n"
;
print
" \""
.
$response
->content.
"\"\n\n"
;
return
0;
}
}
sub
fetch_Ambient_Temperature_F {
my
$self
=
shift
;
if
(!
defined
$self
->{
'thermostat'
}) {
print
"No thermostat designation found\n"
;
return
0;
}
my
$response
=
$self
->{
'ua'
}->get(
$url
);
if
(
$response
->is_success) {
my
$decoded_response
= decode_json(
$response
->content);
return
$decoded_response
->{
'thermostats'
}->{
$self
->{
'thermostat'
}}->{
'ambient_temperature_f'
};
}
else
{
print
"Nest->fetch_Ambient_Temperature_F(): Response from server is not valid\n"
;
print
" \""
.
$response
->content.
"\"\n\n"
;
return
0;
}
}
sub
fetch_Away_Temperature_low_F {
my
$self
=
shift
;
if
(!
defined
$self
->{
'thermostat'
}) {
print
"No thermostat designation found\n"
;
return
0;
}
my
$response
=
$self
->{
'ua'
}->get(
$url
);
if
(
$response
->is_success) {
my
$decoded_response
= decode_json(
$response
->content);
return
$decoded_response
->{
'thermostats'
}->{
$self
->{
'thermostat'
}}->{
'away_temperature_low_f'
};
}
else
{
print
"Nest->fetch_Away_Temperature_low_F(): Response from server is not valid\n"
;
print
" \""
.
$response
->content.
"\"\n\n"
;
return
0;
}
}
sub
fetch_Away_Temperature_high_F {
my
$self
=
shift
;
if
(!
defined
$self
->{
'thermostat'
}) {
print
"No thermostat designation found\n"
;
return
0;
}
my
$response
=
$self
->{
'ua'
}->get(
$url
);
if
(
$response
->is_success) {
my
$decoded_response
= decode_json(
$response
->content);
return
$decoded_response
->{
'thermostats'
}->{
$self
->{
'thermostat'
}}->{
'away_temperature_high_f'
};
}
else
{
print
"Nest->fetch_Away_Temperature_high_F(): Response from server is not valid\n"
;
print
" \""
.
$response
->content.
"\"\n\n"
;
return
0;
}
}
sub
fetch_Target_Temperature_low_F {
my
$self
=
shift
;
if
(!
defined
$self
->{
'thermostat'
}) {
print
"No thermostat designation found\n"
;
return
0;
}
my
$response
=
$self
->{
'ua'
}->get(
$url
);
if
(
$response
->is_success) {
my
$decoded_response
= decode_json(
$response
->content);
return
$decoded_response
->{
'thermostats'
}->{
$self
->{
'thermostat'
}}->{
'target_temperature_low_f'
};
}
else
{
print
"Nest->fetch_Target_Temperature_low_F(): Response from server is not valid\n"
;
print
" \""
.
$response
->content.
"\"\n\n"
;
return
0;
}
}
sub
fetch_Target_Temperature_F {
my
$self
=
shift
;
if
(!
defined
$self
->{
'thermostat'
}) {
print
"No thermostat designation found\n"
;
return
0;
}
my
$response
=
$self
->{
'ua'
}->get(
$url
);
if
(
$response
->is_success) {
my
$decoded_response
= decode_json(
$response
->content);
return
$decoded_response
->{
'thermostats'
}->{
$self
->{
'thermostat'
}}->{
'target_temperature_f'
};
}
else
{
print
"Nest->fetch_Target_Temperature_F(): Response from server is not valid\n"
;
print
" \""
.
$response
->content.
"\"\n\n"
;
return
0;
}
}
sub
fetch_Target_Temperature_high_F {
my
$self
=
shift
;
if
(!
defined
$self
->{
'thermostat'
}) {
print
"No thermostat designation found\n"
;
return
0;
}
my
$response
=
$self
->{
'ua'
}->get(
$url
);
if
(
$response
->is_success) {
my
$decoded_response
= decode_json(
$response
->content);
return
$decoded_response
->{
'thermostats'
}->{
$self
->{
'thermostat'
}}->{
'target_temperature_high_f'
};
}
else
{
print
"Nest->fetch_Target_Temperature_high_F(): Response from server is not valid\n"
;
print
" \""
.
$response
->content.
"\"\n\n"
;
return
0;
}
}
sub
fetch_Temperature_Scale {
my
$self
=
shift
;
if
(!
defined
$self
->{
'thermostat'
}) {
print
"No thermostat designation found\n"
;
return
0;
}
my
$response
=
$self
->{
'ua'
}->get(
$url
);
if
(
$response
->is_success) {
my
$decoded_response
= decode_json(
$response
->content);
return
$decoded_response
->{
'thermostats'
}->{
$self
->{
'thermostat'
}}->{
'temperature_scale'
};
}
else
{
print
"Nest->fetch_Temperature_Scale(): Response from server is not valid\n"
;
print
" \""
.
$response
->content.
"\"\n\n"
;
return
0;
}
}
sub
fetch_Locale {
my
$self
=
shift
;
if
(!
defined
$self
->{
'thermostat'
}) {
print
"No thermostat designation found\n"
;
return
0;
}
my
$response
=
$self
->{
'ua'
}->get(
$url
);
if
(
$response
->is_success) {
my
$decoded_response
= decode_json(
$response
->content);
return
$decoded_response
->{
'thermostats'
}->{
$self
->{
'thermostat'
}}->{
'locale'
};
}
else
{
print
"Nest->fetch_Locale(): Response from server is not valid\n"
;
print
" \""
.
$response
->content.
"\"\n\n"
;
return
0;
}
}
sub
fetch_Name {
my
$self
=
shift
;
if
(!
defined
$self
->{
'thermostat'
}) {
print
"No thermostat designation found\n"
;
return
0;
}
my
$response
=
$self
->{
'ua'
}->get(
$url
);
if
(
$response
->is_success) {
my
$decoded_response
= decode_json(
$response
->content);
return
$decoded_response
->{
'thermostats'
}->{
$self
->{
'thermostat'
}}->{
'name'
};
}
else
{
print
"Nest->fetch_Name(): Response from server is not valid\n"
;
print
" \""
.
$response
->content.
"\"\n\n"
;
return
0;
}
}
sub
fetch_Long_Name {
my
$self
=
shift
;
if
(!
defined
$self
->{
'thermostat'
}) {
print
"No thermostat designation found\n"
;
return
0;
}
my
$response
=
$self
->{
'ua'
}->get(
$url
);
if
(
$response
->is_success) {
my
$decoded_response
= decode_json(
$response
->content);
return
$decoded_response
->{
'thermostats'
}->{
$self
->{
'thermostat'
}}->{
'name_long'
};
}
else
{
print
"Nest->fetch_Long_Name(): Response from server is not valid\n"
;
print
" \""
.
$response
->content.
"\"\n\n"
;
return
0;
}
}
sub
fetch_HVAC_Mode {
my
$self
=
shift
;
if
(!
defined
$self
->{
'thermostat'
}) {
print
"No thermostat designation found\n"
;
return
0;
}
my
$response
=
$self
->{
'ua'
}->get(
$url
);
if
(
$response
->is_success) {
my
$decoded_response
= decode_json(
$response
->content);
return
$decoded_response
->{
'thermostats'
}->{
$self
->{
'thermostat'
}}->{
'hvac_mode'
};
}
else
{
print
"Nest->fetch_HVAC_Mode(): Response from server is not valid\n"
;
print
" \""
.
$response
->content.
"\"\n\n"
;
return
0;
}
}
sub
fetch_SW_Version {
my
$self
=
shift
;
if
(!
defined
$self
->{
'thermostat'
}) {
print
"No thermostat designation found\n"
;
return
0;
}
my
$response
=
$self
->{
'ua'
}->get(
$url
);
if
(
$response
->is_success) {
my
$decoded_response
= decode_json(
$response
->content);
return
$decoded_response
->{
'thermostats'
}->{
$self
->{
'thermostat'
}}->{
'software_version'
};
}
else
{
print
"Nest->fetch_SW_Version(): Response from server is not valid\n"
;
print
" \""
.
$response
->content.
"\"\n\n"
;
return
0;
}
}
1;