File Coverage

blib/lib/OAuth/Cmdline/Smartthings.pm
Criterion Covered Total %
statement 12 19 63.1
branch 0 2 0.0
condition n/a
subroutine 4 6 66.6
pod 0 2 0.0
total 16 29 55.1


line stmt bran cond sub pod time code
1             ###########################################
2             package OAuth::Cmdline::Smartthings;
3             ###########################################
4 1     1   88552 use strict;
  1         11  
  1         33  
5 1     1   4 use warnings;
  1         1  
  1         22  
6 1     1   347 use MIME::Base64;
  1         485  
  1         58  
7 1     1   381 use Moo;
  1         8711  
  1         4  
8              
9             extends "OAuth::Cmdline";
10              
11             our $VERSION = '0.07'; # VERSION
12             # ABSTRACT: Smartthings-specific OAuth oddities
13              
14             sub BUILD {
15 0     0 0   my( $self ) = @_;
16              
17 0 0         if( !defined $self->base_uri ) {
18             # default to US
19 0           $self->base_uri( "https://graph-na02-useast1.api.smartthings.com" );
20             }
21              
22 0           $self->login_uri( $self->base_uri . "/oauth/authorize" );
23 0           $self->token_uri( $self->base_uri . "/oauth/token" );
24              
25 0           $self->client_init_conf_check( $self->base_uri );
26             }
27              
28             ###########################################
29             sub site {
30             ###########################################
31 0     0 0   return "smartthings";
32             }
33              
34             1;
35              
36             __END__
37              
38             =pod
39              
40             =encoding UTF-8
41              
42             =head1 NAME
43              
44             OAuth::Cmdline::Smartthings - Smartthings-specific OAuth oddities
45              
46             =head1 VERSION
47              
48             version 0.07
49              
50             =head1 SYNOPSIS
51              
52             my $oauth = OAuth::Cmdline::Smartthings->new( );
53             my $json =
54             $oauth->http_get( $oauth->base_uri . "/api/smartapps/endpoints" );
55              
56             =head1 DESCRIPTION
57              
58             This class provides the necessary glue to interact with Smartthings
59             Web API. A short tutorial on how to use the API can be found here:
60              
61             http://docs.smartthings.com/en/latest/smartapp-web-services-developers-guide/tutorial-part1.html
62              
63             =head1 REGISTRATION
64              
65             To register with Smartthings, go to
66              
67             https://graph-na02-useast1.api.smartthings.com (US)
68             https://graph-eu01-euwest1.api.smartthings.com (UK)
69              
70             with your browser and create a new app, enable the "OAuth" section, then
71             cut-and-paste the client ID and client secret into
72             your C<~/.smartthings.yml> file:
73              
74             client_id: xxx
75             client_secret: yyy
76              
77             Also use
78              
79             http://localhost:8082/callback
80              
81             as a "redirect URI" (not optionial as the label would suggest) and then
82             run C<eg/smartthings-token-init> (in this distribution) and point your
83             browser to
84              
85             http://localhost:8082
86              
87             Then click the login link and follow the flow.
88              
89             =head1 Web Client Use
90              
91             After registration and initialization (see above), the local
92             C<~/.smartthings.yml> file should contain an access token. With
93             this in place, all that is required to obtain the endpoint of
94             the specific Smartthings app installation and fetch the status of
95             all switches to which the user has granted access to is the following code:
96              
97             use OAuth::Cmdline::Smartthings;
98             use JSON qw( from_json );
99              
100             my $oauth = OAuth::Cmdline::Smartthings->new;
101              
102             my $json = $oauth->http_get(
103             $oauth->base_uri . "/api/smartapps/endpoints" );
104              
105             if( !defined $json ) {
106             die "Can't get endpoints";
107             }
108              
109             my $uri = from_json( $json )->[ 0 ]->{ uri } . "/switches";
110             my $data = $oauth->http_get( $uri );
111             print "$data\n";
112              
113             will print something like
114              
115             [{"name":"Outlet","value":"on"}]
116              
117             =head1 AUTHOR
118              
119             Mike Schilli <cpan@perlmeister.com>
120              
121             =head1 COPYRIGHT AND LICENSE
122              
123             This software is copyright (c) 2022 by Mike Schilli.
124              
125             This is free software; you can redistribute it and/or modify it under
126             the same terms as the Perl 5 programming language system itself.
127              
128             =cut