| 1 | package Power::Outlet::Shelly; | ||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 1 | ▶ | use base qw{Power::Outlet::Common::IP::HTTP::JSON}; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 1 | our $VERSION = '0.54'; | |||||||||||||
| 7 | |||||||||||||||
| 8 | =head1 NAME | ||||||||||||||
| 9 | |||||||||||||||
| 10 | Power::Outlet::Shelly - Control and query a Shelly GIPO Relay with HTTP REST API | ||||||||||||||
| 11 | |||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||
| 13 | |||||||||||||||
| 14 | my $outlet = Power::Outlet::Shelly->new(host=>"shelly", index=>0); | ||||||||||||||
| 15 | print $outlet->query, "\n"; | ||||||||||||||
| 16 | print $outlet->on, "\n"; | ||||||||||||||
| 17 | print $outlet->off, "\n"; | ||||||||||||||
| 18 | print $outlet->switch, "\n"; | ||||||||||||||
| 19 | print $outlet->cycle, "\n"; | ||||||||||||||
| 20 | |||||||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||||||
| 22 | |||||||||||||||
| 23 | Power::Outlet::Shelly is a package for controlling and querying a relay index on Shelly hardware. | ||||||||||||||
| 24 | |||||||||||||||
| 25 | From: L<https://shelly-api-docs.shelly.cloud/> | ||||||||||||||
| 26 | |||||||||||||||
| 27 | Commands can be executed via web (HTTP) requests, for example: | ||||||||||||||
| 28 | |||||||||||||||
| 29 | http://<ip>/relay/0?turn=on | ||||||||||||||
| 30 | http://<ip>/relay/0?turn=off | ||||||||||||||
| 31 | http://<ip>/relay/0?turn=toggle | ||||||||||||||
| 32 | http://<ip>/relay/0?timer=5 | ||||||||||||||
| 33 | |||||||||||||||
| 34 | =head1 USAGE | ||||||||||||||
| 35 | |||||||||||||||
| 36 | use Power::Outlet::Shelly; | ||||||||||||||
| 37 | my $outlet = Power::Outlet::Shelly->new(host=>"sw-kitchen", style=>"relay", index=>0); | ||||||||||||||
| 38 | print $outlet->on, "\n"; | ||||||||||||||
| 39 | |||||||||||||||
| 40 | Command Line | ||||||||||||||
| 41 | |||||||||||||||
| 42 | $ power-outlet Shelly ON host sw-kitchen style relay index 0 | ||||||||||||||
| 43 | |||||||||||||||
| 44 | Command Line (from settings) | ||||||||||||||
| 45 | |||||||||||||||
| 46 | $ cat /etc/power-outlet.ini | ||||||||||||||
| 47 | |||||||||||||||
| 48 | [Kitchen] | ||||||||||||||
| 49 | type=Shelly | ||||||||||||||
| 50 | name=Kitchen | ||||||||||||||
| 51 | host=sw-kitchen | ||||||||||||||
| 52 | style=relay | ||||||||||||||
| 53 | index=0 | ||||||||||||||
| 54 | groups=Inside Lights | ||||||||||||||
| 55 | groups=Main Floor | ||||||||||||||
| 56 | |||||||||||||||
| 57 | |||||||||||||||
| 58 | $ power-outlet Config ON section Kitchen | ||||||||||||||
| 59 | $ curl http://127.0.0.1/cgi-bin/power-outlet-json.cgi?name=Kitchen;action=ON | ||||||||||||||
| 60 | |||||||||||||||
| 61 | =head1 CONSTRUCTOR | ||||||||||||||
| 62 | |||||||||||||||
| 63 | =head2 new | ||||||||||||||
| 64 | |||||||||||||||
| 65 | my $outlet = Power::Outlet->new(type=>"Shelly", host=>"shelly", index=>0); | ||||||||||||||
| 66 | my $outlet = Power::Outlet::Shelly->new(host=>"shelly", index=>0); | ||||||||||||||
| 67 | |||||||||||||||
| 68 | =head1 PROPERTIES | ||||||||||||||
| 69 | |||||||||||||||
| 70 | =head2 style | ||||||||||||||
| 71 | |||||||||||||||
| 72 | Set the style to support "relay" (1, 1L, 2.5, 4, Plug, Uni, EM, 3EM), "light" (Dimmer, Bulb, Vintage, Duo), "color" (RGB Color), or "white" (RGB White) | ||||||||||||||
| 73 | |||||||||||||||
| 74 | my $style = $outlet->style; | ||||||||||||||
| 75 | my $style = $outlet->style('light'); | ||||||||||||||
| 76 | |||||||||||||||
| 77 | default: relay | ||||||||||||||
| 78 | |||||||||||||||
| 79 | =cut | ||||||||||||||
| 80 | |||||||||||||||
| 81 | 1 | ▶ | sub style { | ||||||||||||
|
Subroutine
style: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||
| 82 | 1 | my $self = shift; | |||||||||||||
| 83 | 1 | ▶ | $self->{'style'} = shift if @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 84 | 1 | ▶ | $self->{'style'} = $self->_style_default unless defined $self->{'style'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | 1 | return $self->{'style'}; | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | 1 | ▶ | sub _style_default {'relay'}; | ||||||||||||
|
Subroutine
_style_default: called CC=1 SCAR=0.0
| |||||||||||||||
| 89 | |||||||||||||||
| 90 | =head2 index | ||||||||||||||
| 91 | |||||||||||||||
| 92 | Shelly hardware supports zero or more relay indexes starting at 0. | ||||||||||||||
| 93 | |||||||||||||||
| 94 | Default: 0 | ||||||||||||||
| 95 | |||||||||||||||
| 96 | =cut | ||||||||||||||
| 97 | |||||||||||||||
| 98 | 1 | ▶ | sub index { | ||||||||||||
|
Subroutine
index: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||
| 99 | 1 | my $self = shift; | |||||||||||||
| 100 | 1 | ▶ | $self->{'index'} = shift if @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 101 | 1 | ▶ | $self->{'index'} = $self->_index_default unless defined $self->{'index'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 102 | 1 | return $self->{'index'}; | |||||||||||||
| 103 | } | ||||||||||||||
| 104 | |||||||||||||||
| 105 | 1 | ▶ | sub _index_default {0}; | ||||||||||||
|
Subroutine
_index_default: called CC=1 SCAR=0.0
| |||||||||||||||
| 106 | |||||||||||||||
| 107 | =head2 host | ||||||||||||||
| 108 | |||||||||||||||
| 109 | Sets and returns the hostname or IP address. | ||||||||||||||
| 110 | |||||||||||||||
| 111 | Default: shelly | ||||||||||||||
| 112 | |||||||||||||||
| 113 | =cut | ||||||||||||||
| 114 | |||||||||||||||
| 115 | 0 | ▶ | sub _host_default {'shelly'}; | ||||||||||||
|
Subroutine
_host_default: not called CC=1 SCAR=6.9
| |||||||||||||||
| 116 | |||||||||||||||
| 117 | =head2 port | ||||||||||||||
| 118 | |||||||||||||||
| 119 | Sets and returns the port number. | ||||||||||||||
| 120 | |||||||||||||||
| 121 | Default: 80 | ||||||||||||||
| 122 | |||||||||||||||
| 123 | =cut | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 1 | ▶ | sub _port_default {'80'}; | ||||||||||||
|
Subroutine
_port_default: called CC=1 SCAR=0.0
| |||||||||||||||
| 126 | |||||||||||||||
| 127 | #override Power::Outlet::Common::IP->_port_default | ||||||||||||||
| 128 | |||||||||||||||
| 129 | =head1 METHODS | ||||||||||||||
| 130 | |||||||||||||||
| 131 | =head2 name | ||||||||||||||
| 132 | |||||||||||||||
| 133 | =cut | ||||||||||||||
| 134 | |||||||||||||||
| 135 | #GET /settings/relay/{index} -> $return->{'name'} | ||||||||||||||
| 136 | |||||||||||||||
| 137 | 0 | ▶ | sub _name_default { | ||||||||||||
|
Subroutine
_name_default: not called CC=1 SCAR=6.9
| |||||||||||||||
| 138 | 0 | my $self = shift; | |||||||||||||
| 139 | 0 | return $self->_call('/settings')->{'name'}; | |||||||||||||
| 140 | } | ||||||||||||||
| 141 | |||||||||||||||
| 142 | =head2 query | ||||||||||||||
| 143 | |||||||||||||||
| 144 | Sends an HTTP message to the device to query the current state | ||||||||||||||
| 145 | |||||||||||||||
| 146 | =cut | ||||||||||||||
| 147 | |||||||||||||||
| 148 | 0 | ▶ | sub query { | ||||||||||||
|
Subroutine
query: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 149 | 0 | my $self = shift; | |||||||||||||
| 150 | 0 | ▶ | return $self->_call('')->{'ison'} ? 'ON' : 'OFF'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 151 | } | ||||||||||||||
| 152 | |||||||||||||||
| 153 | =head2 on | ||||||||||||||
| 154 | |||||||||||||||
| 155 | Sends a message to the device to Turn Power ON | ||||||||||||||
| 156 | |||||||||||||||
| 157 | =cut | ||||||||||||||
| 158 | |||||||||||||||
| 159 | 0 | ▶ | sub on { | ||||||||||||
|
Subroutine
on: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 160 | 0 | my $self = shift; | |||||||||||||
| 161 | 0 | ▶ | return $self->_call('', turn => 'on')->{'ison'} ? 'ON' : 'OFF'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 162 | } | ||||||||||||||
| 163 | |||||||||||||||
| 164 | =head2 off | ||||||||||||||
| 165 | |||||||||||||||
| 166 | Sends a message to the device to Turn Power OFF | ||||||||||||||
| 167 | |||||||||||||||
| 168 | =cut | ||||||||||||||
| 169 | |||||||||||||||
| 170 | 0 | ▶ | sub off { | ||||||||||||
|
Subroutine
off: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 171 | 0 | my $self = shift; | |||||||||||||
| 172 | 0 | ▶ | return $self->_call('', turn => 'off')->{'ison'} ? 'ON' : 'OFF'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 173 | } | ||||||||||||||
| 174 | |||||||||||||||
| 175 | =head2 switch | ||||||||||||||
| 176 | |||||||||||||||
| 177 | Sends a message to the device to toggle the power | ||||||||||||||
| 178 | |||||||||||||||
| 179 | =cut | ||||||||||||||
| 180 | |||||||||||||||
| 181 | #override Power::Outlet::Common->switch | ||||||||||||||
| 182 | |||||||||||||||
| 183 | 0 | ▶ | sub switch { | ||||||||||||
|
Subroutine
switch: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 184 | 0 | my $self = shift; | |||||||||||||
| 185 | 0 | ▶ | return $self->_call('', turn => 'toggle')->{'ison'} ? 'ON' : 'OFF'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 186 | } | ||||||||||||||
| 187 | |||||||||||||||
| 188 | =head2 cycle | ||||||||||||||
| 189 | |||||||||||||||
| 190 | Sends messages to the device to Cycle Power (ON-OFF-ON or OFF-ON-OFF). | ||||||||||||||
| 191 | |||||||||||||||
| 192 | =cut | ||||||||||||||
| 193 | |||||||||||||||
| 194 | #override Power::Outlet::Common->cycle | ||||||||||||||
| 195 | |||||||||||||||
| 196 | 0 | ▶ | sub cycle { | ||||||||||||
|
Subroutine
cycle: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 197 | 0 | my $self = shift; | |||||||||||||
| 198 | 0 | my $cycle_duration = $self->cycle_duration; #from Power::Outlet::Common | |||||||||||||
| 199 | 0 | my $hash = $self->_call('', timer => $cycle_duration); | |||||||||||||
| 200 | return $hash->{'timer_started'} > 0 ? 'CYCLE' #is this the correct logic? | ||||||||||||||
| 201 | 0 | ▶ | : $hash->{'ison'} ? 'ON' | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 202 | : 'OFF'; | ||||||||||||||
| 203 | } | ||||||||||||||
| 204 | |||||||||||||||
| 205 | =head2 cycle_duration | ||||||||||||||
| 206 | |||||||||||||||
| 207 | Default; 10 seconds (floating point number) | ||||||||||||||
| 208 | |||||||||||||||
| 209 | =cut | ||||||||||||||
| 210 | |||||||||||||||
| 211 | 0 | ▶ | sub _call { | ||||||||||||
|
Subroutine
_call: not called CC=2 SCAR=17.9
| |||||||||||||||
| 212 | 0 | my $self = shift; | |||||||||||||
| 213 | 0 | my $settings = shift; | |||||||||||||
| 214 | 0 | my %param = @_; | |||||||||||||
| 215 | #http://<ip>/settings/relay/0 | ||||||||||||||
| 216 | #http://<ip>/relay/0?turn=on | ||||||||||||||
| 217 | #http://<ip>/relay/0?timer=10 | ||||||||||||||
| 218 | 0 | $self->http_path(sprintf('%s/%s/%s', $settings, $self->style, $self->index)); | |||||||||||||
| 219 | 0 | my $url = $self->url(undef); #isa URI from Power::Outlet::Common::IP::HTTP | |||||||||||||
| 220 | 0 | $url->query_form(%param); | |||||||||||||
| 221 | 0 | my $hash = $self->json_request(GET => $url); #isa HASH | |||||||||||||
| 222 | |||||||||||||||
| 223 | 0 | ▶ | die('Error: Method _call failed to return expected JSON format') unless ref($hash) eq 'HASH'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 224 | 0 | return $hash; | |||||||||||||
| 225 | } | ||||||||||||||
| 226 | |||||||||||||||
| 227 | =head1 BUGS | ||||||||||||||
| 228 | |||||||||||||||
| 229 | Please log on RT and send an email to the author. | ||||||||||||||
| 230 | |||||||||||||||
| 231 | =head1 SUPPORT | ||||||||||||||
| 232 | |||||||||||||||
| 233 | DavisNetworks.com supports all Perl applications including this package. | ||||||||||||||
| 234 | |||||||||||||||
| 235 | =head1 AUTHOR | ||||||||||||||
| 236 | |||||||||||||||
| 237 | Michael R. Davis | ||||||||||||||
| 238 | CPAN ID: MRDVT | ||||||||||||||
| 239 | DavisNetworks.com | ||||||||||||||
| 240 | |||||||||||||||
| 241 | =head1 COPYRIGHT | ||||||||||||||
| 242 | |||||||||||||||
| 243 | Copyright (c) 2020 Michael R. Davis | ||||||||||||||
| 244 | |||||||||||||||
| 245 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||
| 246 | |||||||||||||||
| 247 | The full text of the license can be found in the LICENSE file included with this module. | ||||||||||||||
| 248 | |||||||||||||||
| 249 | =head1 SEE ALSO | ||||||||||||||
| 250 | |||||||||||||||
| 251 | L<https://shelly-api-docs.shelly.cloud/> | ||||||||||||||
| 252 | |||||||||||||||
| 253 | =cut | ||||||||||||||
| 254 | |||||||||||||||
| 255 | 1 | 1; | |||||||||||||