| 1 | package Power::Outlet::HomeAssistantAPI; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base qw{Power::Outlet::Common::IP::HTTP::JSON}; | ||
| 5 | |||
| 6 | our $VERSION = '0.54'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Power::Outlet::HomeAssistantAPI - Control and query a switch via the Home Assistant API | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | my $outlet = Power::Outlet::HomeAssistantAPI->new(host=>"my_ha_hostname", token=>"my_token", entity_id=>"my.entity_id"); | ||
| 15 | print $outlet->query, "\n"; | ||
| 16 | print $outlet->on, "\n"; | ||
| 17 | print $outlet->off, "\n"; | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | Power::Outlet::HomeAssistantAPI is a package for controlling and querying a switch via the Home Assistant API. | ||
| 22 | |||
| 23 | From: L<https://developers.home-assistant.io/docs/api/rest/> | ||
| 24 | |||
| 25 | Examples: | ||
| 26 | |||
| 27 | Query switch entity | ||
| 28 | |||
| 29 | $ curl -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" http://127.0.0.1:8123/api/states/switch.my_switch | ||
| 30 | { "entity_id":"switch.my_switch", "state":"off", "attributes":{"friendly_name":"My Switch Name"}, ... } | ||
| 31 | |||
| 32 | Toggle switch entity | ||
| 33 | |||
| 34 | $ curl -X POST -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d '{"entity_id": "switch.my_switch"}' http://127.0.0.1:8123/api/services/switch/toggle | ||
| 35 | |||
| 36 | Turn ON switch entity | ||
| 37 | |||
| 38 | $ curl -X POST -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d '{"entity_id": "switch.my_switch"}' http://127.0.0.1:8123/api/services/switch/turn_on | ||
| 39 | |||
| 40 | Turn OFF switch entity | ||
| 41 | |||
| 42 | $ curl -X POST -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d '{"entity_id": "switch.my_switch"}' http://127.0.0.1:8123/api/services/switch/turn_off | ||
| 43 | |||
| 44 | =head1 USAGE | ||
| 45 | |||
| 46 | use Power::Outlet::HomeAssistantAPI; | ||
| 47 | my $outlet = Power::Outlet::HomeAssistantAPI->new(host=>"hostname", token=>$token, entity_id=>"POWER2"); | ||
| 48 | print $outlet->on, "\n"; | ||
| 49 | |||
| 50 | =head1 CONSTRUCTOR | ||
| 51 | |||
| 52 | =head2 new | ||
| 53 | |||
| 54 | my $outlet = Power::Outlet->new(type=>"HomeAssistantAPI", host=>"hostname", token=>$token, entity_id=>"POWER2"); | ||
| 55 | my $outlet = Power::Outlet::HomeAssistantAPI->new(host=>"hostname", token=>$token, entity_id=>"my.entity_id"); | ||
| 56 | |||
| 57 | =head1 PROPERTIES | ||
| 58 | |||
| 59 | =head2 token | ||
| 60 | |||
| 61 | A Home Assistant Long-lived access token is required to access the API. | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | sub token { | ||
| 66 | my $self = shift; | ||
| 67 | $self->{'token'} = shift if @_; | ||
| 68 | die('Error: token is required') unless defined $self->{'token'}; | ||
| 69 | return $self->{'token'}; | ||
| 70 | } | ||
| 71 | |||
| 72 | =head2 entity_id | ||
| 73 | |||
| 74 | Home Assistant entity_id is required and is normally formatted string such as switch.short_name. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub entity_id { | ||
| 79 | my $self = shift; | ||
| 80 | $self->{'entity_id'} = shift if @_; | ||
| 81 | die('Error: entity_id is required') unless defined $self->{'entity_id'}; | ||
| 82 | return $self->{'entity_id'}; | ||
| 83 | } | ||
| 84 | |||
| 85 | =head2 host | ||
| 86 | |||
| 87 | Sets and returns the hostname or IP address. | ||
| 88 | |||
| 89 | Default: 127.0.0.1 | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub _host_default {'127.0.0.1'}; | ||
| 94 | |||
| 95 | =head2 port | ||
| 96 | |||
| 97 | Sets and returns the port number. | ||
| 98 | |||
| 99 | Default: 8123 | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub _port_default {'8123'}; | ||
| 104 | |||
| 105 | =head1 METHODS | ||
| 106 | |||
| 107 | =head2 name | ||
| 108 | |||
| 109 | Returns the FriendlyName from the HomeAssistantAPI hardware. | ||
| 110 | |||
| 111 | Note: The FriendlyName is cached for the life of the object. | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | sub name { | ||
| 116 | my $self = shift; | ||
| 117 | unless ($self->{'name'}) { | ||
| 118 | my $attributes = $self->_get->{'attributes'} // {}; | ||
| 119 | $self->{'name'} = $attributes->{'friendly_name'} // $self->entity_id; | ||
| 120 | } | ||
| 121 | return $self->{'name'}; | ||
| 122 | } | ||
| 123 | |||
| 124 | =head2 query | ||
| 125 | |||
| 126 | Sends an HTTP message to the device to query the current state | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub query { | ||
| 131 | my $self = shift; | ||
| 132 | my $return = $self->_get; | ||
| 133 | return uc($return->{'state'}); | ||
| 134 | } | ||
| 135 | |||
| 136 | =head2 on | ||
| 137 | |||
| 138 | Sends a message to the device to Turn Power ON | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | sub on { | ||
| 143 | my $self = shift; | ||
| 144 | $self->_post('turn_on'); | ||
| 145 | return 'ON'; #$self->query | ||
| 146 | } | ||
| 147 | |||
| 148 | =head2 off | ||
| 149 | |||
| 150 | Sends a message to the device to Turn Power OFF | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | sub off { | ||
| 155 | my $self = shift; | ||
| 156 | $self->_post('turn_off'); | ||
| 157 | return 'OFF'; #$self->query | ||
| 158 | } | ||
| 159 | |||
| 160 | =head2 switch | ||
| 161 | |||
| 162 | Sends a message to the device to toggle the power | ||
| 163 | |||
| 164 | =cut | ||
| 165 | |||
| 166 | sub switch { | ||
| 167 | my $self = shift; | ||
| 168 | my $query = $self->query; | ||
| 169 | $self->_post('toggle'); #service delay | ||
| 170 | return $query eq 'ON' ? 'OFF' : 'ON'; | ||
| 171 | } | ||
| 172 | |||
| 173 | =head2 cycle | ||
| 174 | |||
| 175 | Sends messages to the device to Cycle Power (ON-OFF-ON or OFF-ON-OFF). | ||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | #see Power::Outlet::Common->cycle | ||
| 180 | |||
| 181 | sub _http_path_default {return '/'}; | ||
| 182 | |||
| 183 | sub _http_headers { | ||
| 184 | my $self = shift; | ||
| 185 | my $token = $self->token; | ||
| 186 | my $headers = {'Authorization' => "Bearer $token", 'Content-Type', 'application/json'}; | ||
| 187 | return $headers; | ||
| 188 | } | ||
| 189 | |||
| 190 | sub _post{ | ||
| 191 | my $self = shift; | ||
| 192 | my $command = shift or die; | ||
| 193 | my $path = "/api/services/switch/$command"; | ||
| 194 | my $data = {entity_id => $self->entity_id}; | ||
| 195 | my $url = $self->url; | ||
| 196 | my %options = (headers => $self->_http_headers); | ||
| 197 | $url->path($path); | ||
| 198 | my $return = $self->json_request(POST => $url, $data, \%options); | ||
| 199 | return 'ok'; | ||
| 200 | } | ||
| 201 | |||
| 202 | sub _get { | ||
| 203 | my $self = shift; | ||
| 204 | my $entity_id = $self->entity_id; | ||
| 205 | my $path = "/api/states/$entity_id"; | ||
| 206 | my $url = $self->url; | ||
| 207 | my %options = (headers=>$self->_http_headers); | ||
| 208 | $url->path($path); | ||
| 209 | my $return = $self->json_request(GET => $url, undef, \%options); | ||
| 210 | die('Error: Method _get failed to return expected JSON object') unless ref($return) eq 'HASH'; | ||
| 211 | return $return; | ||
| 212 | } | ||
| 213 | |||
| 214 | =head1 AUTHOR | ||
| 215 | |||
| 216 | Michael R. Davis | ||
| 217 | CPAN ID: MRDVT | ||
| 218 | |||
| 219 | =head1 COPYRIGHT | ||
| 220 | |||
| 221 | Copyright (c) 2026 Michael R. Davis | ||
| 222 | |||
| 223 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 224 | |||
| 225 | The full text of the license can be found in the LICENSE file included with this module. | ||
| 226 | |||
| 227 | =head1 SEE ALSO | ||
| 228 | |||
| 229 | =cut | ||
| 230 | |||
| 231 | 1; |