From dff76274ec963b15cc8c41ba672f06a390a17ca2 Mon Sep 17 00:00:00 2001 From: teevau <> Date: Fri, 16 Aug 2013 14:37:07 +0000 Subject: [PATCH] Changed several commands and readings according to DevelopmentGuidelinesAV git-svn-id: https://svn.fhem.de/fhem/trunk@3717 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 2 + fhem/FHEM/70_VIERA.pm | 250 ++++++++++++++++++++++++++++-------------- 2 files changed, 167 insertions(+), 85 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index 3ef19deb0..9278a024b 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,7 @@ # Add changes at the top of the list. Keep it in ASCII - SVN + - change: VIERA: changed several readings/commands according to + DevelopmentGuidelinesAV. See FHEM Wiki and commandref for more details. - change: YAMAHA_AVR: changed several readings/commands and introduced new volume handling. please see the commandref for more details - change: weblink fileplot/dbplot converted to SVG device (+FHEMWEB cleanup) diff --git a/fhem/FHEM/70_VIERA.pm b/fhem/FHEM/70_VIERA.pm index 38aa00ede..e90a897e7 100644 --- a/fhem/FHEM/70_VIERA.pm +++ b/fhem/FHEM/70_VIERA.pm @@ -8,9 +8,12 @@ # written 2013 by Tobias Vaupel # # -# Version = 1.11 +# Version = 1.20 # # Version History: +# - 1.20 - 2013-08-16 +# -- added support according to http://www.fhemwiki.de/wiki/DevelopmentGuidelinesAV +# # - 1.11 - 2013-08-13 # -- added "noArg" at get/set-command # -- changed format of return() in VIERA_Get() for get-command dropdown menu in FHEMWEB @@ -24,24 +27,13 @@ # -- First release # ############################################################################## -# -# define VIERA -# -# set -# -# where is one of mute, volume, remoteControl or off -# examples: -# set mute on < This will switch mute on -# set volume 20 < This will set volume level to 20, mute will be set to off if enabled -# set remoteControl mute < This is equal to push the mute button at remote control. State of muting will be toggeled! -# set remoteControl ? < Print an overview of remotecontrol buttons -# -############################################################################## package main; use strict; use warnings; use IO::Socket::INET; +use feature qw/say switch/; +use Time::HiRes qw(gettimeofday sleep); ######################### # Forward declaration for remotecontrol module @@ -151,72 +143,127 @@ sub VIERA_Set($@){ my $name = $hash->{NAME}; my $host = $hash->{helper}{HOST}; my $count = @a; - my $ret = ""; my $key = ""; my $tab = ""; - my $usage = "choose one of off:noArg mute:on,off volume:slider,0,1,100 remoteControl:" . join(",", sort keys %VIERA_remoteControl_args); + my $usage = "choose one of off:noArg mute:on,off " . + "remoteControl:" . join(",", sort keys %VIERA_remoteControl_args) . " " . + "volume:slider,0,1,100 volumeUp:noArg volumeDown:noArg ". + "channel channelUp:noArg channelDown:noArg"; $usage =~ s/(NRC_|-ONOFF)//g; - + my $what = lc($a[1]); + return "VIERA: Device is not present or reachable, power on or check ethernet connection" if(ReadingsVal($name,"presence","absent")ne "present" && $what ne "?"); return "VIERA: No argument given, $usage" if(!defined($a[1])); - my $what = $a[1]; + my $state = lc($a[2]) if(defined($a[2])); - return "VIERA: No state given, $usage" if(!defined($a[2]) && $what ne "off"); - my $state = $a[2]; - - - if($what eq "mute"){ - Log GetLogLevel($name, 3), "VIERA: Set mute $state"; + given($what) { + when("mute"){ + Log GetLogLevel($name, 3), "VIERA: Set mute $state"; + if ($state eq "on") {$state = 1;} else {$state = 0;} + VIERA_connection(VIERA_BuildXML_RendCtrl($hash, "Set", "Mute", $state), $host); + break; + } - if ($state eq "on") {$state = 1;} else {$state = 0;} - $ret = connection(VIERA_BuildXML_RendCtrl($hash, "Set", "Mute", $state), $host); - } - elsif($what eq "volume"){ - if($state < 0 || $state > 100){ - return "Range is too high! Use Value 0 till 100 for volume."; + when("volume"){ + return "VIERA: Volume range is too high! Use Value 0 till 100 for volume." if($state < 0 || $state > 100); + Log GetLogLevel($name, 3), "VIERA: Set volume $state"; + VIERA_connection(VIERA_BuildXML_RendCtrl($hash, "Set", "Volume", $state), $host); + break; } - Log GetLogLevel($name, 3), "VIERA: Set volume $state"; - $ret = connection(VIERA_BuildXML_RendCtrl($hash, "Set", "Volume", $state), $host); - } - elsif($what eq "remoteControl"){ - if($state eq "?"){ - $usage = "choose one of the states:\n"; - foreach $key (sort keys %VIERA_remoteControl_args){ - if(length($key) < 17){ $tab = "\t\t"; }else{ $tab = "\t"; } - $usage .= "$key $tab=> $VIERA_remoteControl_args{$key}\n"; + + when("volumeup"){ + return "VIERA: Volume range is too high!" if(ReadingsVal($name, "volume", "0") > 100); + Log GetLogLevel($name, 3), "VIERA: Set volumeUp"; + VIERA_connection(VIERA_BuildXML_NetCtrl($hash,"VOLUP"), $host); + break; + } + + when("volumedown"){ + return "VIERA: Volume range is too low!" if(ReadingsVal($name, "volume", "0") < 1); + Log GetLogLevel($name, 3), "VIERA: Set volumeDown"; + VIERA_connection(VIERA_BuildXML_NetCtrl($hash,"VOLDOWN"), $host); + break; + } + + when("channel"){ + return "VIERA: Channel is too high or low!" if($state < 1 || $state > 9999); + Log GetLogLevel($name, 3), "VIERA: Set channel $state"; + for(my $i = 0; $i <= length($state)-1; $i++) { + VIERA_connection(VIERA_BuildXML_NetCtrl($hash,"D" . substr($state, $i, 1)), $host); + sleep 0.1; } - $usage =~ s/(NRC_|-ONOFF)//g; - return $usage; + VIERA_connection(VIERA_BuildXML_NetCtrl($hash,"ENTER"), $host); + break; + } + + when("channelup"){ + Log GetLogLevel($name, 3), "VIERA: Set channelUp"; + VIERA_connection(VIERA_BuildXML_NetCtrl($hash,"CH_UP"), $host); + break; } - else{ - $state = uc($state); - Log GetLogLevel($name, 3), "VIERA: Set remoteControl $state"; - $ret = connection(VIERA_BuildXML_NetCtrl($hash,$state), $host); + + when("channeldown"){ + Log GetLogLevel($name, 3), "VIERA: Set channelDown"; + VIERA_connection(VIERA_BuildXML_NetCtrl($hash,"CH_DOWN"), $host); + break; } + + when("remoteControl"){ + if($state eq "?"){ + $usage = "choose one of the states:\n"; + foreach $key (sort keys %VIERA_remoteControl_args){ + if(length($key) < 17){ $tab = "\t\t"; }else{ $tab = "\t"; } + $usage .= "$key $tab=> $VIERA_remoteControl_args{$key}\n"; + } + $usage =~ s/(NRC_|-ONOFF)//g; + return $usage; + } + else{ + $state = uc($state); + Log GetLogLevel($name, 3), "VIERA: Set remoteControl $state"; + VIERA_connection(VIERA_BuildXML_NetCtrl($hash,$state), $host); + } + break; + } + + when("off"){ + Log GetLogLevel($name, 3), "VIERA: Set off"; + VIERA_connection(VIERA_BuildXML_NetCtrl($hash,"POWER"), $host); + break; + } + + when("statusrequest"){ + Log GetLogLevel($name, 3), "VIERA: Set statusRequest"; + VIERA_GetStatus($hash); + break; + } + + when("?"){ + return "$usage"; + break; + } + + default{ + Log GetLogLevel($name, 3), "VIERA: Unknown argument $what, $usage"; + return "Unknown argument $what, $usage"; + }; } - elsif($what eq "off"){ - Log GetLogLevel($name, 3), "VIERA: Set off"; - $ret = connection(VIERA_BuildXML_NetCtrl($hash,"POWER"), $host); - } - else{ - Log GetLogLevel($name, 3), "VIERA: $usage"; - return "Unknown argument $what, $usage"; - } - return; + return undef; } sub VIERA_Get($@){ my ($hash, @a) = @_; my $what; - my $usage = "choose one of mute:noArg volume:noArg"; + my $usage = "choose one of mute:noArg volume:noArg power:noArg presence:noArg"; + my $name = $hash->{NAME}; return "VIERA: No argument given, $usage" if(int(@a) != 2); - $what = $a[1]; + $what = lc($a[1]); - if($what =~ /^(volume|mute)$/) { + if($what =~ /^(volume|mute|power|presence)$/) { if (defined($hash->{READINGS}{$what})) { - return $hash->{READINGS}{$what}{VAL}; + ReadingsVal($name, $what, "undefined"); } else{ return "no such reading: $what"; @@ -244,45 +291,58 @@ sub VIERA_GetStatus($;$){ return "" if(!defined($hash->{helper}{HOST}) or !defined($hash->{helper}{INTERVAL})); - my $returnVol = connection(VIERA_BuildXML_RendCtrl($hash, "Get", "Volume", ""), $host); + my $returnVol = VIERA_connection(VIERA_BuildXML_RendCtrl($hash, "Get", "Volume", ""), $host); Log GetLogLevel($name, 5), "VIERA: GetStatusVol-Request returned: $returnVol" if(defined($returnVol)); if(not defined($returnVol) or $returnVol eq "") { Log GetLogLevel($name, 4), "VIERA: GetStatusVol-Request NO SOCKET!"; - #readingsSingleUpdate($hash,"state","off",1); - if( $hash->{STATE} ne "off") {readingsSingleUpdate($hash,"state","off",1);} + if( ReadingsVal($name,"state","absent") ne "absent") { + readingsBeginUpdate($hash); + readingsBulkUpdate($hash, "state", "absent"); + readingsBulkUpdate($hash, "power", "off"); + readingsBulkUpdate($hash, "presence", "absent"); + readingsEndUpdate($hash, 1); + } return; } - my $returnMute = connection(VIERA_BuildXML_RendCtrl($hash, "Get", "Mute", ""), $host); + my $returnMute = VIERA_connection(VIERA_BuildXML_RendCtrl($hash, "Get", "Mute", ""), $host); Log GetLogLevel($name, 5), "VIERA: GetStatusMute-Request returned: $returnMute" if(defined($returnMute)); if(not defined($returnMute) or $returnMute eq "") { Log GetLogLevel($name, 4), "VIERA: GetStatusMute-Request NO SOCKET!"; - #readingsSingleUpdate($hash,"state","off",1); - if( $hash->{STATE} ne "off") {readingsSingleUpdate($hash,"state","off",1);} + if( ReadingsVal($name,"state","absent") ne "absent") { + readingsBeginUpdate($hash); + readingsBulkUpdate($hash, "state", "absent"); + readingsBulkUpdate($hash, "power", "off"); + readingsBulkUpdate($hash, "presence", "absent"); + readingsEndUpdate($hash, 1); + } return; } readingsBeginUpdate($hash); if($returnVol =~ /(.+)<\/CurrentVolume>/){ Log GetLogLevel($name, 4), "VIERA: GetStatus-Set reading volume to $1"; - if( $1 != $hash->{READINGS}{volume}{VAL} ) {readingsBulkUpdate($hash, "volume", $1);} + if( $1 != ReadingsVal($name, "volume", "0") ) {readingsBulkUpdate($hash, "volume", $1);} } if($returnMute =~ /(.+)<\/CurrentMute>/){ my $myMute = $1; if ($myMute == 0) { $myMute = "off"; } else { $myMute = "on";} - Log GetLogLevel($name, 4), "VIERA: GetStatus-Set reading volume to $myMute"; - if( $myMute ne $hash->{READINGS}{mute}{VAL} ) {readingsBulkUpdate($hash, "mute", $myMute);} + Log GetLogLevel($name, 4), "VIERA: GetStatus-Set reading mute to $myMute"; + if( $myMute ne ReadingsVal($name, "mute", "0") ) {readingsBulkUpdate($hash, "mute", $myMute);} + } + if( ReadingsVal($name,"state","absent") ne "on") { + readingsBulkUpdate($hash, "state", "on"); + readingsBulkUpdate($hash, "power", "on"); + readingsBulkUpdate($hash, "presence", "present"); } - #readingsBulkUpdate($hash, "state", "on"); - if( $hash->{STATE} ne "on") {readingsBulkUpdate($hash, "state", "on");} readingsEndUpdate($hash, 1); - Log GetLogLevel($name,4), "VIERA $name: $hash->{STATE}"; + #Log GetLogLevel($name,4), "VIERA $name: $hash->{STATE}"; return $hash->{STATE}; } -sub connection($$){ +sub VIERA_connection($$){ my $tmp = shift ; my $TV = shift; my $buffer = ""; @@ -461,12 +521,20 @@ sub VIERA_BuildXML_RendCtrl($$$$){ set <name> <command> [<value>]

Currently, the following commands are defined. -
    - off
    - mute [on|off]
    - volume <value>
    - remoteControl <command>
    -
+
    + + off
    + mute [on|off]
    + volume [0-100]
    + volumeUp
    + volumeDown
    + channel [1-9999]
    + channelUp
    + channelDown
    + statusRequest
    + remoteControl <command>
    +
    +

    @@ -551,6 +619,8 @@ sub VIERA_BuildXML_RendCtrl($$$$){
      mute
      volume
      + power
      + presence
@@ -563,10 +633,11 @@ sub VIERA_BuildXML_RendCtrl($$$$){ Generated events:
    -
  • on
  • -
  • off
  • volume
  • mute
  • +
  • presence
  • +
  • power
  • +
  • state
@@ -609,8 +680,14 @@ sub VIERA_BuildXML_RendCtrl($$$$){
    off
    mute [on|off]
    - volume <Wert>
    - remoteControl <Befehl>
    + volume [0-100]
    + volumeUp
    + volumeDown
    + channel [1-9999]
    + channelUp
    + channelDown
    + statusRequest
    + remoteControl <command>
    @@ -694,8 +771,10 @@ sub VIERA_BuildXML_RendCtrl($$$$){

    Die folgenden Befehle sind definiert und geben den entsprechenden Wert zurück, der vom Fernseher zurückgegeben wurde.
      - mute
      - volume
      + mute
      + volume
      + power
      + presence
@@ -708,10 +787,11 @@ sub VIERA_BuildXML_RendCtrl($$$$){ Generierte events:
    -
  • on
  • -
  • off
  • volume
  • mute
  • +
  • presence
  • +
  • power
  • +
  • state