Device-RadioThermostat

 view release on metacpan or  search on metacpan

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

}

sub remote_temp {
    my ($self) = @_;
    return $self->_ua_get('/tstat/remote_temp');
}

sub disable_remote_temp {
    my ($self) = @_;
    return $self->_ua_post( '/tstat/remote_temp', { rem_mode => 0 } );
}

sub set_remote_temp {
    my ( $self, $temp ) = @_;
    return $self->_ua_post( '/tstat/remote_temp', { rem_temp => 0 + sprintf("%d", $temp) } );
}

sub lock {
    my ($self, $mode) = @_;
    if ($mode) {
        return unless $self->_ua_post( '/tstat/lock', { lock_mode => int($mode) } );
    }
    return $self->_ua_get('/tstat/lock');
}

sub user_message {
    my ( $self, $line, $message ) = @_;
    return $self->_ua_post( '/tstat/uma', { line => int($line), message => $message } );
}

sub price_message {
    my ( $self, $line, $message ) = @_;
    return $self->_ua_post( '/tstat/pma', { line => int($line), message => $message } );
}

sub clear_user_message {
    my ($self) = @_;
    return $self->_ua_post( '/tstat/uma', { mode => 0 } );
}

sub clear_price_message {
    my ($self) = @_;
    return $self->_ua_post( '/tstat/pma', { mode => 0 } );
}

sub clear_message {
    my ($self) = @_;
    return $self->clear_price_message();
}

sub datalog {
    my ($self) = @_;
    return $self->_ua_get('/tstat/datalog');
}

sub _ua_post {
    my ( $self, $path, $data ) = @_;
    my $response
        = $self->{ua}->post( $self->{address} . $path, content => encode_json $data );
    if ( $response->is_success ) {
        my $result = decode_json $response->decoded_content();

        # return $result;
        return exists( $result->{success} ) ? 1 : 0;
    }
    else {
        my ($code, $err) = ($response->code, $response->message);
        carp $code ? "$code response: $err" : "Connection error: $err";
        return;
    }
}

sub _ua_get {
    my ( $self, $path ) = @_;
    my $response = $self->{ua}->get( $self->{address} . $path );
    if ( $response->is_success ) {
        return decode_json $response->decoded_content();
    }
    else {
        my ($code, $err) = ($response->code, $response->message);
        carp $code ? "$code response: $err" : "Connection error: $err";
        return;
    }
}

1;
__END__

=head1 NAME

Device::RadioThermostat - Access Radio Thermostat Co of America (3M-Filtrete) WiFi thermostats

=head1 SYNOPSIS

  use Device::RadioThermostat;
  my $thermostat = Device::RadioThermostat->new( address => "http://$ip");
  $thermostat->temp_cool(65);
  say "It is currently " . $thermostat->tstat()->{temp} "F inside.";

=head1 DESCRIPTION

Device::RadioThermostat is a perl module for accessing the API of thermostats
manufactured by Radio Thermostat Corporation of America.  3M-Filtrete themostats
with WiFi are OEM versions manufactured by RTCOA.

=head1 METHODS

For additional information on the arguments and values returned see the
L<RTCOA API documentation (pdf)|http://www.radiothermostat.com/documents/RTCOAWiFIAPIV1_3.pdf>.

=head2 new( address=> 'http://192.168.1.1')

Constructor takes named parameters.  Currently only C<address> which should be
the HTTP URL for the thermostat.

=head2 find_all(address1, address2)

This finds all the thermostats in the address range and returns a reference to a hash
which contains Device::RadioThermostat objects indexed by the device uuid. For example,
it might return a structure as follows:

    Device::RadioThermostat->find_all("192.168.1.1", "192.168.1.254")

returns

    {
    "5cdad4123456" => Device::RadioThermostat(address => 'http://192.168.1.76'),
    "5cdad4654321" => Device::RadioThermostat(address => 'http://192.168.1.183')
    }

=head2 tstat

Retrieve a hash of lots of info on the current thermostat state.  Possible keys
include: C<temp>, C<tmode>, C<fmode>, C<override>, C<hold>, C<t_heat>,
C<t_cool>, C<it_heat>, C<It_cool>, C<a_heat>, C<a_cool>, C<a_mode>,
C<t_type_post>, C<t_state>.  For a description of their values see the
L<RTCOA API documentation (pdf)|http://www.radiothermostat.com/documents/RTCOAWiFIAPIV1_3.pdf>.



( run in 2.984 seconds using v1.01-cache-2.11-cpan-600a1bdf6e4 )