Device-Nest

 view release on metacpan or  search on metacpan

lib/Device/Nest.pm  view on Meta::CPAN

 
 Returns 1 on success and prints auth_token
 Returns 0 on failure
=cut
sub fetch_Auth_Token {
	my $self       = shift;
	my $auth_token = '';
	
    # Submit request for authentiaction token.
    my $response = $self->{'ua'}->post('https://api.home.nest.com/oauth2/access_token',
          { code          => $self->{'PIN_code'},
        	grant_type    => 'authorization_code', 
        	client_id     => $self->{'ClientID'},
        	client_secret => $self->{'ClientSecret'},
          }
        );
    
    $self->{'last_code'}   = $response->code;
    
    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;
    }
}


#*****************************************************************

=head2 fetch_Thermostat_Designation - fetch the designation for your thermostat

 Retrieves the code designating your thermostat and stores it in $self

   $Nest->fetch_Thermostat_Designation();

   This method accepts no parameters
 
 Returns 1 on success
 Returns 0 on failure
 
=cut
sub fetch_Thermostat_Designation {
    my $self     = shift;
	my $response = $self->{'ua'}->get($self->{'device_url'});
    
    $self->{'last_code'} = $response->code;
    
    if ($response->is_success) {
      my $decoded_response      = decode_json($response->content);
      my $designation           = ($decoded_response->{'thermostats'});
      my @designation2          = keys(%$designation);
      $self->{'thermostat'}     = $designation2[0];
      $self->{'thermostat_url'} = "https://developer-api.nest.com/devices/thermostats/".$self->{'thermostat'};
      print "Thermostat designation: ".$self->{'thermostat'}."\n" if ($self->{'debug'});

      my $response = $self->{'ua'}->get("https://developer-api.nest.com/structures?auth=".$self->{'auth_token'});

      $self->{'last_code'}   = $response->code;
    
      if ($response->is_success) {
        my $decoded_response  = decode_json($response->content);
        my @designation       = keys(%$decoded_response);        
        $self->{'structure'}  = $designation[0];
        $self->{'struct_url'} = "https://developer-api.nest.com/structures/".$self->{'structure'}."?auth=".$self->{'auth_token'};
        print "Structure Designation: ".$self->{'structure'}."\n" if ($self->{'debug'});
        return 1;
      } else {
        print "Nest->fetch_Thermostat_Designation(): Response from server for structure URL is not valid\n";
        print "  \"".$response->content."\"\n\n";
        return 0;
      }
    } else {
      print "Nest->fetch_Thermostat_Designation(): Failed with return code ".$self->get_last_code()."\n";
      return 0;
    }
}


#*****************************************************************

=head2 fetch_Ambient_Temperature_C - Fetch the ambient temperature reported by Nest in Celcius

 Retrieves the ambient temperature reported by the Nest in Celcius

   $Nest->fetch_Ambient_Temperature_C();

   This method accepts no parameters
 
 Returns the ambient temperature in Celcius
 Returns 0 on failure
 
=cut
sub fetch_Ambient_Temperature_C {
    my $self = shift;
    
    if (!defined $self->{'thermostat'}) {
      print "Nest->fetch_Ambient_Temperature_C(): No thermostat designation found\n";
      return 0;
    }

    return $self->__process_get($self->{'device_url'},'ambient_temperature_c');
}

#*****************************************************************

=head2 fetch_Target_Temperature_C - Fetch the target temperature reported by Nest in Celcius

 Retrieves the target temperature reported by the Nest in Celcius

   $Nest->fetch_Target_Temperature_C();

   This method accepts no parameters
 
 Returns the target temperature in Celcius
 Returns 0 on failure
 
=cut
sub fetch_Target_Temperature_C {
    my $self = shift;
    
    if (!defined $self->{'thermostat'}) {
      print "Nest->fetch_Target_Temperature_C(): No thermostat designation found\n";

lib/Device/Nest.pm  view on Meta::CPAN

    }
}


#*****************************************************************

=head2 fetch_Has_Fan - Fetches true or false

 Retrieves the state of the Nest indicating whether it has a fan

   $Nest->fetch_Has_Fan();

   This method accepts no parameters
 
 Returns a Boolean
 
=cut
sub fetch_Has_Fan {
    my $self = shift;
    
    if (!defined $self->{'thermostat'}) {
      print "Nest->fetch_Has_Fan(): No thermostat designation found\n";
      return 0;
    }
    
    if ($self->__process_get($self->{'device_url'},'has_fan')) {
      return 1;
    } else { 
      return 0;
    }
}


#*****************************************************************

=head2 fetch_Away_State - Fetch the away state reported by Nest

 Retrieves the away state reported by the Nest 

   $Nest->fetch_Away_State();

   This method accepts no parameters
 
 Returns the away state
 Returns 0 on failure
 
=cut
sub fetch_Away_State {
    my $self = shift;
    
    if (!defined $self->{'thermostat'}) {
      print "Nest->fetch_Away_State(): No thermostat designation found\n";
      return 0;
    }
    
	my $response = $self->{'ua'}->get($self->{'struct_url'});

    $self->{'last_code'}   = $response->code;
    
    if ($response->is_success) {
      my $decoded_response = decode_json($response->content);
      return $decoded_response->{'away'};
    } else {
      print "Nest->fetch_Away_State(): Failed with return code ".$self->get_last_code()."\n";
      return 0;
    }
}


#*****************************************************************

=head2 fetch_Country_Code - Fetch the country code reported by Nest

 Retrieves the country code reported by the Nest 

   $Nest->fetch_Country_Code();

   This method accepts no parameters
 
 Returns the away state
 Returns 0 on failure
 
=cut
sub fetch_Country_Code {
    my $self = shift;
    
    if (!defined $self->{'thermostat'}) {
      print "Nest->fetch_Country_Code(): No thermostat designation found\n";
      return 0;
    }
    
	my $response = $self->{'ua'}->get($self->{'struct_url'});
    
    $self->{'last_code'}   = $response->code;
    
    if ($response->is_success) {
      my $decoded_response = decode_json($response->content);
      return $decoded_response->{$self->{'structure'}}->{'country_code'};
    } else {
      print "Nest->fetch_Country_Code(): Failed with return code ".$self->get_last_code()."\n";
      return 0;
    }
}


#*****************************************************************

=head2 fetch_Locale - Fetch the locale reported by Nest

 Retrieves the locale reported by the Nest 

   $Nest->fetch_Locale();

   This method accepts no parameters
 
 Returns the locale
 Returns 0 on failure
 
=cut
sub fetch_Locale {
    my $self = shift;
    
    if (!defined $self->{'thermostat'}) {
      print "Nest->fetch_Locale(): No thermostat designation found\n";
      return 0;
    }
    
    return $self->__process_get($self->{'device_url'},'locale');
}

#*****************************************************************

=head2 fetch_Name - Fetch the name reported by Nest

 Retrieves the name reported by the Nest 

   $Nest->fetch_Name();

   This method accepts no parameters
 
 Returns the name of the thermostat
 Returns 0 on failure
 
=cut
sub fetch_Name {
    my $self = shift;
    
    if (!defined $self->{'thermostat'}) {
      print "Nest->fetch_Name(): No thermostat designation found\n";
      return 0;
    }
    
    return $self->__process_get($self->{'device_url'},'name');
}


#*****************************************************************

lib/Device/Nest.pm  view on Meta::CPAN

 Returns the numeric code
 
=cut

sub get_last_code {
    my $self  = shift;
    return $self->{'last_code'};
}

#*****************************************************************

=head2 get_last_reason - returns the text generated by the most recent fetch

 Returns the HTTP Header reason for the most recent fetch command

   $Nest->get_last_reason();

   This method accepts no parameters
 
 Returns the textual reason
 
=cut

sub get_last_reason {
    my $self  = shift;
    return $self->{'last_reason'};
}

#******************************************************************************
=head2 get_last_exec_time - returns the execution time for the last fetch

 Returns the number of milliseconds it took for the last fetch call

   $Nest->get_last_exec_time();

   This method accepts no parameters
 
 Returns the number of milliseconds
 
=cut

sub get_last_exec_time {
    my $self  = shift;
    return $self->{'last_exec_time'};
}

#*****************************************************************

sub __process_get {
    my $self        = shift;
    my $url         = shift;
    my $tag         = shift;
    my $time_before = gettimeofday;
	my $response    = $self->{'ua'}->get($url);
    my $time_after  = gettimeofday;
	
    $self->{'last_exec_time'} = eval{($time_after-$time_before)*1000};
    $self->{'last_code'}      = $response->code;
    
    if ($response->is_success) {
      my $decoded_response = decode_json($response->content);
#      print Dumper($decoded_response);
      return $decoded_response->{'thermostats'}->{$self->{'thermostat'}}->{$tag};
    } else {
      print "\n".(caller(1))[3]."(): Failed with return code ".$self->get_last_code()."\n";
      return 0;
    }
}


#*****************************************************************

sub __process_set {
    my $self     = shift;
    my $url      = shift;
    my $tag      = shift;
    my $value    = shift;

	my $response = $self->{'ua'}->put($url."/".$tag."?auth=".$self->{'auth_token'}, 
	         Content_Type => 'application/json',
             content      => $value );
    $self->{'last_code'}   = $response->code;
    $self->{'last_reason'} = decode_json($response->content)->{'error'};

    if ($response->is_success) {
      return $response->content;
    } else {
      print "\n".(caller(1))[3]."(): Failed with return code ".$self->get_last_code()." - ".$self->get_last_reason()."\n";
      return 0;
    }
}


#*****************************************************************

=head1 AUTHOR

Kedar Warriner, C<kedar at cpan.org>

=head1 BUGS

 Please report any bugs or feature requests to C<bug-device-Nest at rt.cpan.org>
 or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Device-Nest
 I will be notified, and then you'll automatically be notified of progress on 
 your bug as I make changes.

=head1 SUPPORT

 You can find documentation for this module with the perldoc command.

  perldoc Device::Nest

 You can also look for information at:

=over 5

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Device-Nest>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Device-Nest>



( run in 1.099 second using v1.01-cache-2.11-cpan-ba708fea25c )