| 1 | =head1 NAME | ||
| 2 | |||
| 3 | AHA::Switch - Object representing an AHA managed switch/actor | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | # Parent object for doing the HTTP communication | ||
| 8 | my $aha = new AHA("fritz.box","s!cr!t"); | ||
| 9 | |||
| 10 | # Switch represented by the $ain which can be a name or a real AIN | ||
| 11 | my $switch = new AHA::Switch($aha,$ain) | ||
| 12 | |||
| 13 | # Obtain all switches from a list operation | ||
| 14 | for my $switch (@{$aha->list()}) { | ||
| 15 | say $switch->name(),": ",$switch->is_on(); | ||
| 16 | } | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | This module represents an actor/switch for the AVM home automation system. It | ||
| 21 | encapsulated an actor with a certain AIN and provides all methods as described | ||
| 22 | in L<"AHA"> with the difference, that not AIN is required, since this has | ||
| 23 | been already provided during the construction of this object. | ||
| 24 | |||
| 25 | =head1 METHODS | ||
| 26 | |||
| 27 | =over | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | package AHA::Switch; | ||
| 32 | use vars qw{$AUTOLOAD}; | ||
| 33 | |||
| 34 | =item $switch = new AHA::Switch($aha,$ain) | ||
| 35 | |||
| 36 | Create a new switch object. The first object must be an L<"AHA"> instance, | ||
| 37 | which is responsible for the HTTP communication. The second argument Many must | ||
| 38 | be an 8-digit AIN (actor id) or a symbolic name. This symbolic name | ||
| 39 | can be configured in the admin UI of the Fritz Box. | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | sub new { | ||
| 44 | my $class = shift; | ||
| 45 | my $aha = shift; | ||
| 46 | my $self = { | ||
| 47 | aha => $aha, | ||
| 48 | ain => $aha->_ain(shift) | ||
| 49 | }; | ||
| 50 | return bless $self,$class; | ||
| 51 | } | ||
| 52 | |||
| 53 | =item $ain = $switch->ain() | ||
| 54 | |||
| 55 | Get the AIN which this object represents. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | sub ain { | ||
| 60 | return shift->{ain}; | ||
| 61 | } | ||
| 62 | |||
| 63 | =item $switch->is_on() | ||
| 64 | |||
| 65 | =item $switch->is_present() | ||
| 66 | |||
| 67 | =item $switch->on() | ||
| 68 | |||
| 69 | =item $switch->off() | ||
| 70 | |||
| 71 | =item $switch->energy() | ||
| 72 | |||
| 73 | =item $switch->power() | ||
| 74 | |||
| 75 | =item $switch->temperature() | ||
| 76 | |||
| 77 | =item $switch->name() | ||
| 78 | |||
| 79 | Same as the corresponding method in L<"AHA"> with the exception, that no | ||
| 80 | C<$ain> argument is required since it already has been given during | ||
| 81 | construction time | ||
| 82 | |||
| 83 | =back | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | my %SUPPORTED_METHODS = | ||
| 88 | map { $_ => 1 } qw(is_on is_present on off energy power temperature name); | ||
| 89 | |||
| 90 | sub AUTOLOAD { | ||
| 91 | my $self = shift; | ||
| 92 | ( my $method = $AUTOLOAD ) =~ s{.*::}{}; | ||
| 93 | die "Unknown method $method" unless $SUPPORTED_METHODS{$method}; | ||
| 94 | return $self->{aha}->$method($self->{ain}); | ||
| 95 | } | ||
| 96 | |||
| 97 | use overload fallback => 1, | ||
| 98 | '""' => sub { "[AIN " . shift->{ain} . "]" }; | ||
| 99 | |||
| 100 | 1; |