########################################################################################
#
# Alarm.pm
#
# FHEM module for house alarm
#
# Prof. Dr. Peter A. Henning
#
# $Id: 95_Alarm.pm 2014-08 - pahenning $
#
########################################################################################
#
# This programm is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# The GNU General Public License can be found at
# http://www.gnu.org/copyleft/gpl.html.
# A copy is found in the textfile GPL.txt and important notices to the license
# from the author is found in LICENSE.txt distributed with these scripts.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
########################################################################################
package main;
use strict;
use warnings;
use vars qw(%defs); # FHEM device/button definitions
use vars qw(%intAt); # FHEM at definitions
#########################
# Global variables
my $alarmname = "Alarms"; # link text
my $alarmhiddenroom = "AlarmRoom"; # hidden room
my $alarmpublicroom = "Alarm"; # public room
my $alarmno = 8;
my $alarmversion = "1.6";
#########################################################################################
#
# Alarm_Initialize
#
# Parameter hash = hash of device addressed
#
#########################################################################################
sub Alarm_Initialize ($) {
my ($hash) = @_;
$hash->{DefFn} = "Alarm_Define";
$hash->{SetFn} = "Alarm_Set";
$hash->{GetFn} = "Alarm_Get";
$hash->{UndefFn} = "Alarm_Undef";
#$hash->{AttrFn} = "Alarm_Attr";
my $attst = "lockstate:lock,unlock statedisplay:simple,color,table,graphics,none";
for( my $level=0;$level<$alarmno;$level++ ){
$attst .=" level".$level."start level".$level."end level".$level."msg level".$level."xec:0,1 level".$level."onact level".$level."offact";
}
$hash->{AttrList} = $attst;
$data{FWEXT}{Alarmx}{LINK} = "?room=".$alarmhiddenroom;
$data{FWEXT}{Alarmx}{NAME} = $alarmname;
return undef;
}
#########################################################################################
#
# Alarm_Define - Implements DefFn function
#
# Parameter hash = hash of device addressed, def = definition string
#
#########################################################################################
sub Alarm_Define ($$) {
my ($hash, $def) = @_;
my $now = time();
my $name = $hash->{NAME};
$hash->{VERSION} = $alarmversion;
readingsSingleUpdate( $hash, "state", "Initialized", 0 );
RemoveInternalTimer($hash);
InternalTimer ($now + 5, 'Alarm_CreateEntry', $hash, 0);
return;
}
#########################################################################################
#
# Alarm_Undef - Implements Undef function
#
# Parameter hash = hash of device addressed, def = definition string
#
#########################################################################################
sub Alarm_Undef ($$) {
my ($hash,$arg) = @_;
RemoveInternalTimer($hash);
return undef;
}
#########################################################################################
#
# Alarm_Attr - Implements Attr function
#
# Parameter hash = hash of device addressed, ???
#
#########################################################################################
sub Alarm_Attr($$$) {
my ($cmd, $name, $attrName, $attrVal) = @_;
return;
}
#########################################################################################
#
# Alarm_CreateEntry - Puts the Alarm entry into the FHEM menu
#
# Parameter hash = hash of device addressed
#
#########################################################################################
sub Alarm_CreateEntry($) {
my ($hash) = @_;
my $name = $hash->{NAME};
if (!defined $defs{$name."_weblink"}) {
FW_fC("define ".$name."_weblink weblink htmlCode {Alarm_Html(\"".$name."\")}");
Log3 $hash, 3, "[".$name. " V".$alarmversion."]"." Weblink ".$name."_weblink created";
}
FW_fC("attr ".$name."_weblink room ".$alarmhiddenroom);
foreach my $dn (sort keys %defs) {
if ($defs{$dn}{TYPE} eq "FHEMWEB" && $defs{$dn}{NAME} !~ /FHEMWEB:/) {
my $hr = AttrVal($defs{$dn}{NAME}, "hiddenroom", "");
if (index($hr,$alarmhiddenroom) == -1){
if ($hr eq "") {
FW_fC("attr ".$defs{$dn}{NAME}." hiddenroom ".$alarmhiddenroom);
}else {
FW_fC("attr ".$defs{$dn}{NAME}." hiddenroom ".$hr.",".$alarmhiddenroom);
}
Log3 $hash, 3, "[".$name. " V".$alarmversion."]"." Added hidden room '".$alarmhiddenroom."' to ".$defs{$dn}{NAME};
}
}
}
my $mga = Alarm_getstate($hash)." Keine Störung";
readingsSingleUpdate( $hash, "state", $mga, 0 );
}
#########################################################################################
#
# Alarm_Set - Implements the Set function
#
# Parameter hash = hash of device addressed
#
#########################################################################################
sub Alarm_Set($@) {
my ( $hash, $name, $cmd, @args ) = @_;
if ( ($cmd eq "cancel") || ($cmd eq "sharp") || ($cmd eq "unsharp") ) {
return "[Alarm] Invalid argument to set $cmd, must be numeric"
if ( $args[0] !~ /\d+/ );
return "[Alarm] Invalid argument to set $cmd, must be 0 < arg < $alarmno"
if ( ($args[0] >= $alarmno)||($args[0]<0) );
if( $cmd eq "cancel" ){
Alarm_Exec($name,$args[0],"web","button","off");
}elsif ( $cmd eq "sharp" ) {
Alarm_Sharp($name,$args[0],"web","button","sharp");
}else{
Alarm_Sharp($name,$args[0],"web","button","unsharp");
}
return;
} elsif ( $cmd eq "lock" ) {
readingsSingleUpdate( $hash, "lockstate", "locked", 0 );
return;
} elsif ( $cmd eq "unlock" ) {
readingsSingleUpdate( $hash, "lockstate", "unlocked", 0 );
return;
} else {
my $str = join(",",(0..($alarmno-1)));
return "[Alarm] Unknown argument " . $cmd . ", choose one of cancel:$str sharp:$str unsharp:$str lock:noArg unlock:noArg";
}
}
#########################################################################################
#
# Alarm_Set - Implements the Get function
#
# Parameter hash = hash of device addressed
#
#########################################################################################
sub Alarm_Get($@) {
my ($hash, @a) = @_;
my $res = "";
my $arg = (defined($a[1]) ? $a[1] : "");
if ($arg eq "version") {
return "Alarm.version => $alarmversion";
} else {
return "Unknown argument $arg choose one of version";
}
}
#########################################################################################
#
# Alarm_getstate - Helper function to assemble a state display
#
# Parameter hash = hash of device addressed
#
#########################################################################################
sub Alarm_getstate($) {
my ($hash) = @_;
my $res = '';
my $type = AttrVal($hash->{NAME},"statedisplay",0);
#--------------------------
if( $type eq "simple" ){
for( my $level=0;$level<$alarmno;$level++ ){
if( $hash->{READINGS}{"level".$level}{VAL} eq "off" ){
$res.='O';
}else{
$res.='X';
}
}
#--------------------------
}elsif( $type eq "color" ){
$res = '';
for( my $level=0;$level<$alarmno;$level++ ){
if( $hash->{READINGS}{"level".$level}{VAL} eq "off" ){
$res.=' '.$level;
}else{
$res.=' '.$level.'';
}
}
$res.='';
#--------------------------
}elsif( $type eq "table" ){
$res = '
set <name> lock set <name> unlock sets the lockstate of the alarm module to locked (i.e., alarm setups may not be changed)
resp. unlocked (i.e., alarm setups may be changed>)
Get
get <name> version Display the version of the module
For each of the 8 alarm levels, several attributes hold the alarm setup.
They should not be changed by hand, but through the web interface to avoid confusion:
level<level>start, level<level>end, level<level>msg, level<level>xec,
level<level>onact, level<level>offact