From 09fb060b1fcca73d91f31d99c426f1db099f9978 Mon Sep 17 00:00:00 2001 From: risiko79 <> Date: Wed, 20 May 2015 19:59:42 +0000 Subject: [PATCH] 30_pilight_dimmer: add up|down commands to change the dimlevel git-svn-id: https://svn.fhem.de/fhem/trunk@8609 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/10_pilight_ctrl.pm | 45 ++++++--- fhem/FHEM/30_pilight_dimmer.pm | 165 ++++++++++++++++++++++++++++----- fhem/FHEM/30_pilight_switch.pm | 6 +- 4 files changed, 180 insertions(+), 37 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index 42ed27337..f858ceb91 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,6 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. + - feature: 30_pilight_dimmer: up|down commands to change the dimlevel - bugfix: 76_MSGMail: Add MSGMail_Attr() - feature: 70_PIONEERAVR: new set hdmiOut <1/2>to switch hdmi outputs - new set networkStandby (on|off) Needed to turn on/off... diff --git a/fhem/FHEM/10_pilight_ctrl.pm b/fhem/FHEM/10_pilight_ctrl.pm index 8f019e7c7..81fed1d80 100644 --- a/fhem/FHEM/10_pilight_ctrl.pm +++ b/fhem/FHEM/10_pilight_ctrl.pm @@ -1,5 +1,5 @@ ############################################## -# $Id: 10_pilight_ctrl.pm 1.02 2015-05-16 Risiko $ +# $Id: 10_pilight_ctrl.pm 1.03 2015-05-20 Risiko $ # # Usage # @@ -26,6 +26,7 @@ # V 1.00 2015-05-09 - NEW: white list for defined submodules activating by ignoreProtocol * # V 1.01 2015-05-09 - NEW: add quigg_gt* protocol (e.q quigg_gt7000) # V 1.02 2015-05-16 - NEW: battery state for temperature sensors +# V 1.03 2015-05-20 - NEW: handle screen messages (up,down) ############################################## package main; @@ -44,8 +45,8 @@ sub pilight_ctrl_ClientAccepted(@); sub pilight_ctrl_Send($); my %sets = ( "reset:noArg" => ""); -my %matchList = ( "1:pilight_switch" => "^SWITCH", - "2:pilight_dimmer" => "^SWITCH|^DIMMER", +my %matchList = ( "1:pilight_switch" => "^PISWITCH", + "2:pilight_dimmer" => "^PISWITCH|^PIDIMMER|^PISCREEN", "3:pilight_temp" => "^PITEMP") ; my @idList = ("id","systemcode","gpio"); @@ -414,17 +415,25 @@ sub pilight_ctrl_addWhiteList($$) my $me = $own->{NAME}; my $devName = $dev->{NAME}; + my $id = (defined($dev->{ID})) ? $dev->{ID} : return; + my $protocol = (defined($dev->{PROTOCOL})) ? $dev->{PROTOCOL}: return; + Log3 $me, 4, "$me(addWhiteList): add $devName to white list"; my $entry = {}; - my $id = (defined($dev->{ID})) ? $dev->{ID} : ""; - my $protocol = (defined($dev->{PROTOCOL})) ? $dev->{PROTOCOL}: ""; - my %whiteHash; @whiteHash{@{$own->{helper}->{whiteList}}}=(); if (!exists $whiteHash{"$protocol:$id"}) { push @{$own->{helper}->{whiteList}}, "$protocol:$id"; } + + #spezial 2nd protocol for dimmer + if (defined($dev->{PROTOCOL2})) { + $protocol = $dev->{PROTOCOL2}; + if (!exists $whiteHash{"$protocol:$id"}) { + push @{$own->{helper}->{whiteList}}, "$protocol:$id"; + } + } } ##################################### @@ -455,8 +464,19 @@ sub pilight_ctrl_Notify($$) my $s = $dev->{CHANGED}[$i]; next if(!defined($s)); - if ( $s =~/DEFINED/ or $s =~/INITIALIZED/ or $s =~/DELETED/) { - Log3 $me, 4, "$me(Notify): create white list"; + my ($what,$who) = split(' ',$s); + + if ( $what =~ m/INITIALIZED/ ) { + Log3 $me, 4, "$me(Notify): create white list for $s"; + pilight_ctrl_createWhiteList($own); + } elsif ( $what =~ m/DEFINED/ ){ + my $hash = $defs{$who}; + next if(!$hash); + my $module = $hash->{TYPE}; + next if ($module !~ /pilight_[d|s|t].*/); + pilight_ctrl_addWhiteList($own,$hash); + } elsif ( $what =~ m/DELETED/ ){ + Log3 $me, 4, "$me(Notify): create white list for $s"; pilight_ctrl_createWhiteList($own); } } @@ -685,7 +705,7 @@ sub pilight_ctrl_Parse($$) case m/lm75/ {$protoID = 4;} case m/lm76/ {$protoID = 4;} - case m/screen/ {return;} + case m/screen/ {$protoID = 5;} case m/firmware/ {return;} else {Log3 $me, 3, "$me(Parse): unknown protocol -> $proto"; return;} } @@ -696,10 +716,10 @@ sub pilight_ctrl_Parse($$) } switch($protoID){ - case 1 { return Dispatch($hash, "SWITCH,$proto,$id,$unit,$state",undef ); } + case 1 { return Dispatch($hash, "PISWITCH,$proto,$id,$unit,$state",undef ); } case 2 { my $dimlevel = (defined($data->{$s}{dimlevel})) ? $data->{$s}{dimlevel} : ""; - my $msg = "DIMMER,$proto,$id,$unit,$state"; + my $msg = "PIDIMMER,$proto,$id,$unit,$state"; $msg.= ",$dimlevel" if ($dimlevel ne ""); return Dispatch($hash, $msg ,undef); } @@ -710,7 +730,7 @@ sub pilight_ctrl_Parse($$) $state =~ s/opened/on/g; $state =~ s/closed/off/g; Log3 $me, 5, "$me(Parse): contact as switch for $id"; - return Dispatch($hash, "SWITCH,$proto,$id,$unit,$state",undef); + return Dispatch($hash, "PISWITCH,$proto,$id,$unit,$state",undef); } return; } @@ -724,6 +744,7 @@ sub pilight_ctrl_Parse($$) my $msg = "PITEMP,$proto,$id,$temp,$humidity,$battery"; return Dispatch($hash, $msg,undef); } + case 5 { return Dispatch($hash, "PISCREEN,$proto,$id,$unit,$state",undef); } else {Log3 $me, 3, "$me(Parse): unknown protocol -> $proto"; return;} } return; diff --git a/fhem/FHEM/30_pilight_dimmer.pm b/fhem/FHEM/30_pilight_dimmer.pm index 4f1c8897f..450d82bf5 100644 --- a/fhem/FHEM/30_pilight_dimmer.pm +++ b/fhem/FHEM/30_pilight_dimmer.pm @@ -1,12 +1,15 @@ ############################################## +# $Id: 30_pilight_dimmer.pm 0.50 2015-05-20 Risiko $ # # Usage # -# define pilight_dimmer +# define pilight_dimmer [protocol] # # Changelog # # V 0.10 2015-02-26 - initial beta version +# V 0.50 2015-05-20 - NEW: handle screen messages (up,down) +# V 0.50 2015-05-20 - NEW: max dimlevel for gui and device ############################################## package main; @@ -25,10 +28,10 @@ sub pilight_dimmer_Initialize($) my ($hash) = @_; $hash->{DefFn} = "pilight_dimmer_Define"; - $hash->{Match} = "^SWITCH|^DIMMER"; + $hash->{Match} = "^PISWITCH|^PIDIMMER|^PISCREEN"; $hash->{ParseFn} = "pilight_dimmer_Parse"; $hash->{SetFn} = "pilight_dimmer_Set"; - $hash->{AttrList} = "dimlevel_max ".$readingFnAttributes; + $hash->{AttrList} = "dimlevel_max dimlevel_step dimlevel_max_device ".$readingFnAttributes; } ##################################### @@ -38,7 +41,7 @@ sub pilight_dimmer_Define($$) my @a = split("[ \t][ \t]*", $def); if(@a < 5) { - my $msg = "wrong syntax: define pilight_dimmer "; + my $msg = "wrong syntax: define pilight_dimmer [protocol]"; Log3 undef, 2, $msg; return $msg; } @@ -52,7 +55,13 @@ sub pilight_dimmer_Define($$) $hash->{PROTOCOL} = lc($protocol); $hash->{ID} = $id; $hash->{UNIT} = $unit; - + $hash->{PROTOCOL2} = lc($a[5]) if (@a == 6); + + $hash->{helper}{OWN_DIM} = 1; + $hash->{helper}{OWN_DIM} = 0 if ($hash->{PROTOCOL} =~ /screen/); + + $hash->{helper}{ISSCREEN} = 1 if ($hash->{PROTOCOL} =~ /screen/ or $hash->{PROTOCOL2} =~ /screen/); + #$attr{$me}{verbose} = 5; $modules{pilight_dimmer}{defptr}{lc($id)}{$me} = $hash; @@ -66,11 +75,12 @@ sub pilight_dimmer_Parse($$) my ($mhash, $rmsg, $rawdata) = @_; my $backend = $mhash->{NAME}; + my $dimlevel = undef; Log3 $backend, 4, "pilight_dimmer_Parse: RCV -> $rmsg"; my ($dev,$protocol,$id,$unit,$state,$dimlevel) = split(",",$rmsg); - return () if($dev ne "SWITCH" && $dev ne "DIMMER"); + return () if($dev !~ m/PISWITCH|PIDIMMER|PISCREEN/); my $chash; foreach my $n (keys %{ $modules{pilight_dimmer}{defptr}{lc($id)} }) { @@ -84,14 +94,74 @@ sub pilight_dimmer_Parse($$) return () if (!defined($chash->{NAME})); + my $max_default = 15; + $max_default = 100 if ($chash->{helper}{OWN_DIM} != 1); + + my $dimlevel_max_dev = AttrVal($chash->{NAME}, "dimlevel_max_device",$max_default); + my $dimlevel_max = AttrVal($chash->{NAME}, "dimlevel_max",$dimlevel_max_dev); + my $dimlevel_step = AttrVal($chash->{NAME}, "dimlevel_step",1); + my $dimlevel_old = ReadingsVal($chash->{NAME},"dimlevel",0); + + Log3 $chash->{NAME}, 4, "pilight_dimmer_Parse: RCV -> $rmsg"; + readingsBeginUpdate($chash); + + if ($state eq "up") { + $dimlevel = $dimlevel_old + $dimlevel_step; + $dimlevel = $dimlevel_max if ($dimlevel > $dimlevel_max); + $state="on"; + } + + if ($state eq "down") { + $dimlevel = $dimlevel_old - $dimlevel_step; + $state="on"; + if ($dimlevel < 0) { + $state="off"; + $dimlevel = 0; + } + } + readingsBulkUpdate($chash,"state",$state); - readingsBulkUpdate($chash,"dimlevel",$dimlevel) if (defined($dimlevel)); + if (defined($dimlevel)) { + $chash->{helper}{DEV_DIMLEVEL} = $dimlevel; + $dimlevel = $dimlevel / $dimlevel_max_dev * $dimlevel_max; + $dimlevel = int($dimlevel+0.5); + readingsBulkUpdate($chash,"dimlevel",$dimlevel); + } readingsEndUpdate($chash, 1); return $chash->{NAME}; } +##################################### +sub pilight_dimmer_Write($$$) +{ + my ($hash, $set, $dimlevel) = @_; + my $me = $hash->{NAME}; + + my $proto = $hash->{PROTOCOL}; + + if ($set =~ /up|down/ and $proto !~ /screen/) { + $proto = $hash->{PROTOCOL2} if (defined($hash->{PROTOCOL2})); + } + if ($set =~ /on|off/ and $proto =~ /screen/) { + $proto = $hash->{PROTOCOL2} if (defined($hash->{PROTOCOL2})); + } + + my $msg = "$me,$set"; + $msg = $msg.",".$dimlevel if (defined($dimlevel)); + + my $help = $hash->{PROTOCOL}; + $hash->{PROTOCOL} = $proto; + + Log3 $me, 4, "$me(Set): [$proto] $msg"; + + IOWrite($hash, $msg); + + $hash->{PROTOCOL} = $help; + return undef; +} + ##################################### sub pilight_dimmer_Set($$) { @@ -99,21 +169,53 @@ sub pilight_dimmer_Set($$) my $me = shift @a; return "no set value specified" if(int(@a) < 1); - my $maxlevel = $attr{$me}{dimlevel_max}; - $maxlevel = 15 if (!defined($maxlevel)); - return "Unknown argument ?, choose one of on off dimlevel:slider,0,1,$maxlevel" if($a[0] eq "?"); + my $dimlevel_max_dev = AttrVal($me, "dimlevel_max_device",15); + my $dimlevel_step = AttrVal($me, "dimlevel_step",1); + my $dimlevel_max = AttrVal($me, "dimlevel_max",$dimlevel_max_dev); - my $msg = "$me,"; + my $canSet = "on off"; + $canSet .= " up down" if ($hash->{helper}{ISSCREEN}); + + return "Unknown argument ?, choose one of $canSet dimlevel:slider,0,$dimlevel_step,$dimlevel_max" if($a[0] eq "?"); + + my $set = $a[0]; + my $dimlevel = undef; if ($a[0] eq "dimlevel") { - $msg .= "on,".$a[1]; - } else { - $msg .= $a[0]; + if ($hash->{helper}{OWN_DIM} == 1) { + $dimlevel = $a[1] / $dimlevel_max * $dimlevel_max_dev; + $dimlevel = int($dimlevel + 0.5); + $set = "on"; + } elsif ($hash->{helper}{ISSCREEN}){ + my $newlevel = $a[1]; + my $currlevel = ReadingsVal($me,"dimlevel",0); + my $cnt = int(($newlevel - $currlevel) / $dimlevel_step); + return undef if ($cnt==0); + $set = "up" if ($cnt>0); + $set = "down" if ($cnt<0); + for (my $i=0; $i < abs($cnt); $i++) { + pilight_dimmer_Write($hash,$set,undef); + } + return undef; + } else { + Log3 $me, 1, "$me(Set): error setting dimlevel"; + return undef; + } + } elsif ( ($set eq "up" or $set eq "down") and !$hash->{helper}{ISSCREEN}) { + Log3 $me, 1, "$me(Set): up|down not supported"; + return undef; + } elsif ( $set eq "off" ) { + delete $hash->{helper}{DEV_DIMLEVEL}; } - Log3 $me, 4, "$me(Set): $msg"; + if (defined($dimlevel)) { + my $dimOld = $hash->{helper}{DEV_DIMLEVEL}; + if (defined($dimOld)) { + return undef if ($dimOld == $dimlevel); + } + } - IOWrite($hash, $msg); + pilight_dimmer_Write($hash,$set,$dimlevel); return undef; } @@ -131,17 +233,23 @@ sub pilight_dimmer_Set($$) You have to define the base device pilight_ctrl first.
Further information to pilight: http://www.pilight.org/
Supported dimmers: http://wiki.pilight.org/doku.php/protocols#dimmers
-

- +
+ It is possible to add the screen feature to a dimmer. So you can change the dimlevel by set 'up' or 'down'.
+ If you push up or down on the remote control the dimlevel will be changed by dimlevel_step.
+ Further it is possible to define a simulated dimmer with a screen and switch protocol. See example three.
+ That means if you change the dimlevel a up or down command will be send n times to dim the device instead of send a dimlevel directly.
+
Define
    - define <name> pilight_dimmer protocol id unit -

    + define <name> pilight_dimmer protocol id unit [protocol] +
    The second protocol is optional. With it you can add the pilight screen feature (up|down) Example:
      define myctrl pilight_dimmer kaku_dimmer 13483668 0
      + define myctrl pilight_dimmer kaku_dimmer 13483668 0 kaku_screen - Dimmer with screen feature
      + define myctrl pilight_dimmer quigg_screen 1 0 quigg_gt7000 - Simulated dimmer with screen feature

@@ -154,6 +262,12 @@ sub pilight_dimmer_Set($$)
  • off
  • +
  • + up only if defined with screen protocol +
  • +
  • + down only if defined with screen protocol +
  • dimlevel
  • @@ -175,10 +289,15 @@ sub pilight_dimmer_Set($$) Attributes
    diff --git a/fhem/FHEM/30_pilight_switch.pm b/fhem/FHEM/30_pilight_switch.pm index 7b83d2d46..98ce8f1d1 100644 --- a/fhem/FHEM/30_pilight_switch.pm +++ b/fhem/FHEM/30_pilight_switch.pm @@ -1,4 +1,5 @@ ############################################## +# $Id: 30_pilight_switch.pm 0.12 2015-05-18 Risiko $ # # Usage # @@ -8,6 +9,7 @@ # # V 0.10 2015-02-22 - initial beta version # V 0.11 2015-03-29 - FIX: $readingFnAttributes +# V 0.12 2015-05-18 - FIX: add version information ############################################## package main; @@ -26,7 +28,7 @@ sub pilight_switch_Initialize($) my ($hash) = @_; $hash->{DefFn} = "pilight_switch_Define"; - $hash->{Match} = "^SWITCH"; + $hash->{Match} = "^PISWITCH"; $hash->{ParseFn} = "pilight_switch_Parse"; $hash->{SetFn} = "pilight_switch_Set"; $hash->{AttrList} = $readingFnAttributes; @@ -71,7 +73,7 @@ sub pilight_switch_Parse($$) Log3 $backend, 4, "pilight_switch_Parse: RCV -> $rmsg"; my ($dev,$protocol,$id,$unit,$state,@args) = split(",",$rmsg); - return () if($dev ne "SWITCH"); + return () if($dev ne "PISWITCH"); my $chash; foreach my $n (keys %{ $modules{pilight_switch}{defptr}{lc($protocol)} }) {