NAME
Power::Outlet::HomeAssistantAPI - Control and query a switch via the Home Assistant API
SYNOPSIS
my $outlet = Power::Outlet::HomeAssistantAPI->new(host => "my_ha_hostname", entity_id => "my_entity_id");
print $outlet->query, "\n";
print $outlet->on, "\n";
print $outlet->off, "\n";
DESCRIPTION
Power::Outlet::HomeAssistantAPI is a package for controlling and querying a switch via the Home Assistant API.
From: https://developers.home-assistant.io/docs/api/rest/
Examples:
Query switch entity
$ curl -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" http://127.0.0.1:8123/api/states/switch.my_switch
{ "entity_id":"switch.my_switch", "state":"off", "attributes":{"friendly_name":"My Switch Name"}, ... }
Toggle switch entity
$ 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
Turn ON switch entity
$ 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
Turn OFF switch entity
$ 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
USAGE
use Power::Outlet::HomeAssistantAPI;
my $outlet = Power::Outlet::HomeAssistantAPI->new(host=>"hostname", token=>$token, entity_id=>"POWER2");
print $outlet->on, "\n";
CONSTRUCTOR
new
my $outlet = Power::Outlet->new(type=>"HomeAssistantAPI", host=>"hostname", token=>$token, entity_id=>"POWER2");
my $outlet = Power::Outlet::HomeAssistantAPI->new(host=>"hostname", token=>$token, entity_id=>"POWER2");
PROPERTIES
token
A Home Assistant Long-lived access token is required to access the API.
entity_id
Home Assistant entity_id is required and is noramlly formatted string such as switch.short_name.
host
Sets and returns the hostname or IP address.
Default: 127.0.0.1
port
Sets and returns the port number.
Default: 8123
METHODS
name
Returns the FriendlyName from the HomeAssistantAPI hardware.
Note: The FriendlyName is cached for the life of the object.
query
Sends an HTTP message to the device to query the current state
on
Sends a message to the device to Turn Power ON
off
Sends a message to the device to Turn Power OFF
switch
Sends a message to the device to toggle the power
cycle
Sends messages to the device to Cycle Power (ON-OFF-ON or OFF-ON-OFF).
AUTHOR
Michael R. Davis
CPAN ID: MRDVT
COPYRIGHT
Copyright (c) 2026 Michael R. Davis
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.