NAME

App::OpenHAP::Devices - the configured devices of the bridge

SYNOPSIS

use App::OpenHAP::Devices;
use Fugu::Config;
use Fugu::MQTT;
use App::OpenHAP::Host;

my $config = Fugu::Config->new(file => '/etc/openhapd.conf');
$config->load();

my $hap = App::OpenHAP::Host->new(...);
my $mqtt = Fugu::MQTT->new(...);

my $devices = App::OpenHAP::Devices->new();
my $count = $devices->load_devices($config, $hap, $mqtt);

print "Loaded $count devices\n";

# The blocks alone, with no bridge and no MQTT client
my @devices = App::OpenHAP::Devices->devices($config);

DESCRIPTION

This module reads the device blocks from a Fugu::Config and does a check of each block. It instantiates the device class that matches, and it registers each device with the HAP bridge.

One table describes every supported device type. Each entry names the class that builds the device, and the extra arguments that the class needs. To add a type, add one entry.

METHODS

new

my $devices = App::OpenHAP::Devices->new()

This method creates a new device collection. It keeps an internal counter that assigns the Accessory IDs (AIDs). The counter starts at 2, because AID 1 is reserved for the bridge itself.

load_devices

my $count = $devices->load_devices($config, $hap, $mqtt)

This method loads all devices from the configuration and adds them to the HAP bridge. It returns the number of devices that loaded correctly.

The method does these steps:

  • Gets the device list from the configuration

  • Does a check of each device configuration

  • Instantiates the applicable device classes

  • Subscribes the devices to MQTT topics (if MQTT is connected)

  • Registers the devices with the HAP bridge

  • Logs the progress and the errors

The method logs errors, but an error does not stop the load of the other devices.

get_devices

my @devices = $devices->get_devices()

This method returns a list of all device accessory objects that loaded correctly.

devices

my @devices = App::OpenHAP::Devices->devices($config)

This class method returns the device blocks of a Fugu::Config as plain records. Each record holds the settings of the block, plus the type, subtype and id from the block arguments.

The method builds no accessory and loads no device class. A caller that only lists the configured devices, such as hapctl devices, uses this method.

DEVICE CHECKS

Each device configuration must have these fields:

  • name - The human-readable name of the device

  • type - The device type (for example, 'tasmota')

  • subtype - The device subtype (for example, 'thermostat')

  • topic - The MQTT topic for communication

  • id - The unique identifier. The default value is the topic.

The module skips a device that does not have all the necessary fields. It logs an error for that device.

SUPPORTED DEVICES

The only device type is tasmota. These are its subtypes:

ERROR HANDLING

The device instantiation and the MQTT subscription run inside eval. Thus one misconfigured device does not stop the start of the daemon. The module logs that device and skips it.

SEE ALSO

Fugu::Config, Fugu::MQTT, App::OpenHAP::Host, App::OpenHAP::Tasmota::Thermostat

AUTHOR

Dick Olsson <hi@senzilla.io>