Der Codeblock zum Lesen des HolidayDevice ist ein wenig umständloich. Außerdem sollte es egal sein, ob es sich um ein "Holiday-Device" mit dem vom Modulautor selbst erfundenen Datenformat handelt, oder um ein Calendar Device.
Anbei - zum Anpassen nach Wunsch - ein Codeblock aus einem in Arbeit befindlichen Modul:
#########################################################################################
#
# YAAHM_GetDayStatus
#
# Parameter hash = hash of device addressed
#
#########################################################################################
sub YAAHM_GetDayStatus($) {
my ($hash) = @_;
my $name = $hash->{NAME};
my $lang = ($attr{global}{language} ? $attr{global}{language} : "EN");
my ($ret,$line,$fline,$date);
my (@lines,@chunks,@tday,@eday,@sday,@tmor);
my ($stoday,$todaydesc,$todaytype,$stom,$tomdesc,$tomtype);
#-- workday has lowest priority
$todaytype = "workday";
$hash->{DATA}{"DD"}[0]{"isWeekend"} = 0;
$tomtype = "workday";
$hash->{DATA}{"DD"}[1]{"isWeekend"} = 0;
#-- vacation = vacdays has higher priority
my $vacdayDevs = AttrVal( $name, "vacationDevices", "" );
foreach my $vacdayDev ( split( /,/, $vacdayDevs ) ) {
#-- device of type holiday
if( IsDevice( $vacdayDev, "holiday" )){
$stoday = strftime('%2m-%2d', localtime(time));
$stom = strftime('%2m-%2d', localtime(time+86400));
my $tod = holiday_refresh( $vacdayDev, $stoday );
if ( $tod ne "none" ) {
$todaydesc = $tod;
$todaytype = "vacday";
Log3 $name, 1,"[YAAHM] found today=vacation \"$todaydesc\" in holiday $vacdayDev";
}
$tod = holiday_refresh( $vacdayDev, $stom );
if ( $tod ne "none" ) {
$tomdesc = $tod;
$tomtype = "vacday";
Log3 $name, 1,"[YAAHM] found tomorrow=vacation \"$tomdesc\" in holiday $vacdayDev";
}
#-- device of type calendar
}elsif( IsDevice($vacdayDev, "Calendar" )){
$stoday = strftime('%2d.%2m.%2y', localtime(time));
$stom = strftime('%2d.%2m.%2y', localtime(time+86400));
@tday = split('\.',$stoday);
@tmor = split('\.',$stom);
#-- more complicated to check here
$fline=Calendar_Get($defs{$vacdayDev},"get","full","mode=alarm|start|upcoming");
if($line){
#chomp($fline);
@lines = split('\n',$fline);
foreach $fline (@lines){
chomp($fline);
@chunks = split(' ',$fline);
@sday = split('\.',$chunks[2]);
@eday = split('\.',substr($chunks[3],9,10));
#-- today
my $rets = ($sday[2]-$tday[2]-2000)*365+($sday[1]-$tday[1])*31+($sday[0]-$tday[0]);
my $rete = ($eday[2]-$tday[2]-2000)*365+($eday[1]-$tday[1])*31+($eday[0]-$tday[0]);
if( ($rete>=0) && ($rets<=0) ){
$todaydesc = $chunks[5];
$todaytype = "vacation";
Log3 $name, 1,"[YAAHM] found today=vacation \"$todaydesc\" in calendar $vacdayDev";
}
$rets = ($sday[2]-$tmor[2]-2000)*365+($sday[1]-$tmor[1])*31+($sday[0]-$tmor[0]);
$rete = ($eday[2]-$tmor[2]-2000)*365+($eday[1]-$tmor[1])*31+($eday[0]-$tmor[0]);
if( ($rete>=0) && ($rets<=0) ){
$tomdesc = $chunks[5];
$tomtype = "vacation";
Log3 $name, 1,"[YAAHM] found tomorrow=vacation \"$tomdesc\" in calendar $vacdayDev";
}
}
}
}
}
#-- put into readings
if( $todaytype eq "vacation" ){
$hash->{DATA}{"DD"}[0]{"isVacation"} = 1;
$hash->{DATA}{"DD"}[0]{"vDesc"} = $todaydesc;
}else{
$hash->{DATA}{"DD"}[0]{"isVacation"} = 0;
$hash->{DATA}{"DD"}[0]{"vDesc"} = "";
}
if( $tomtype eq "vacation" ){
$hash->{DATA}{"DD"}[1]{"isVacation"} = 1;
$hash->{DATA}{"DD"}[1]{"vDesc"} = $tomdesc;
}else{
$hash->{DATA}{"DD"}[1]{"isVacation"} = 0;
$hash->{DATA}{"DD"}[1]{"vDesc"} = "";
}
#-- weekend has higher priority
if( strftime('%u', localtime(time)) > 5){
$todaytype = "weekend";
$hash->{DATA}{"DD"}[0]{"isWeekend"} = 1;
}
if( strftime('%u', localtime(time+86400)) > 5){
$tomtype = "weekend";
$hash->{DATA}{"DD"}[1]{"isWeekend"} = 1;
}
#-- holidays have the highest priority
my $holidayDevs = AttrVal( $name, "holidayDevices", "" );
foreach my $holidayDev ( split( /,/, $holidayDevs ) ) {
#-- device of type holiday
if( IsDevice( $holidayDev, "holiday" )){
$stoday = strftime('%2m-%2d', localtime(time));
$stom = strftime('%2m-%2d', localtime(time+86400));
my $tod = holiday_refresh( $holidayDev, $stoday );
if ( $tod ne "none" ) {
$todaydesc = $tod;
$todaytype = "holiday";
Log3 $name, 1,"[YAAHM] found today=holiday \"$todaydesc\" in holiday $holidayDev";
}
$tod = holiday_refresh( $holidayDev, $stom );
if ( $tod ne "none" ) {
$tomdesc = $tod;
$tomtype = "holiday";
Log3 $name, 1,"[YAAHM] found tomorrow=holiday \"$tomdesc\" in holiday $holidayDev";
}
#-- device of type calendar
}elsif( IsDevice($holidayDev, "Calendar" )){
$stoday = strftime('%2d.%2m.%2y', localtime(time));
$stom = strftime('%2d.%2m.%2y', localtime(time+86400));
$line=Calendar_Get($defs{$holidayDev},"get","text","mode=alarm|start|upcoming");
if($line){
chomp($line);
@lines = split('\n',$line);
foreach $line (@lines){
chomp($line);
$date = substr($line,0,8);
if( $date eq $stoday ){
$todaydesc = substr($line,15);
$todaytype = "holiday";
Log3 $name, 1,"[YAAHM] found today=holiday \"$todaydesc\" in calendar $holidayDev";
}
if( $date eq $stom ){
$tomdesc = substr($line,15);
$tomtype = "holiday";
Log3 $name, 1,"[YAAHM] found tomorrow=holiday \"$tomdesc\" in calendar $holidayDev";
}
}
}
}
}
#-- put into readings
if( $todaytype eq "holiday" ){
$hash->{DATA}{"DD"}[0]{"isHoliday"} = 1;
$hash->{DATA}{"DD"}[0]{"hDesc"} = $todaydesc;
}else{
$hash->{DATA}{"DD"}[0]{"isHoliday"} = 0;
$hash->{DATA}{"DD"}[0]{"hDesc"} = "";
}
if( $tomtype eq "holiday" ){
$hash->{DATA}{"DD"}[1]{"isHoliday"} = 1;
$hash->{DATA}{"DD"}[1]{"hDesc"} = $tomdesc;
}else{
$hash->{DATA}{"DD"}[1]{"isHoliday"} = 0;
$hash->{DATA}{"DD"}[1]{"hDesc"} = "";
}
#--
readingsBeginUpdate($hash);
readingsBulkUpdateIfChanged( $hash, "todayType",$todaytype );
if( $todaytype eq "workday"){
readingsBulkUpdateIfChanged( $hash, "todayDesc","--" )
}elsif( $todaytype eq "vacation"){
readingsBulkUpdateIfChanged( $hash, "todayDesc",$hash->{DATA}{"DD"}[0]{"vDesc"} )
}elsif( $todaytype eq "weekend"){
readingsBulkUpdateIfChanged( $hash, "todayDesc","--" )
}else{
readingsBulkUpdateIfChanged( $hash, "todayDesc",$hash->{DATA}{"DD"}[0]{"hDesc"} )
}
readingsBulkUpdateIfChanged( $hash, "tomorrowType",$tomtype );
if( $tomtype eq "workday"){
readingsBulkUpdateIfChanged( $hash, "tomorrowDesc","--" )
}elsif( $tomtype eq "vacation"){
readingsBulkUpdateIfChanged( $hash, "tomorrowDesc",$hash->{DATA}{"DD"}[1]{"vDesc"} )
}elsif( $tomtype eq "weekend"){
readingsBulkUpdateIfChanged( $hash, "tomorrowDesc","--" )
}else{
readingsBulkUpdateIfChanged( $hash, "tomorrowDesc",$hash->{DATA}{"DD"}[1]{"hDesc"} )
}
readingsEndUpdate($hash,1);
return undef;
}
Bei Übernahme von Teilen dieses Codeblocks: Bitte gemäß GPL meinen Namen dazuschreiben.
LG
pah