# ############################################################################## # $Id: 98_SB_sensor.pm 0.1 $ # # FHEM Module for squeezebox radio ambient sensor # # Copyright notice # # (c) 2015 Oliv06 ( http://play.with.free.fr/ ) # # This script 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. # # This copyright notice MUST APPEAR in all copies of the script! # # ############################################################################## package main; use strict; use warnings; use Data::Dumper; sub SB_sensor_Initialize($) { my ($hash) = @_; $hash->{DefFn} = "SB_sensor_Define"; $hash->{UndefFn} = "SB_sensor_Undefine"; $hash->{GetFn} = "SB_sensor_Get"; $hash->{AttrFn} = "SB_sensor_Attr"; $hash->{AttrList} = "disable:1 "; $hash->{AttrList} .= " $readingFnAttributes"; } ##################################### sub SB_sensor_Define($$) { my ($hash, $def) = @_; my @a = split("[ \t][ \t]*", $def); return "Usage: define SB_sensor host [interval]" if(@a < 2); my $host = $a[2]; my $interval = 60; if(int(@a)>=4) { $interval = $a[3]; } if( $interval < 60 ) { $interval = 60; } $hash->{HOST} = $host; $hash->{INTERVAL} = $interval; RemoveInternalTimer($hash); InternalTimer(gettimeofday()+$hash->{INTERVAL}, "SB_sensor_GetUpdate", $hash, 0); return undef; } sub SB_sensor_Undefine($$) { my ($hash, $arg) = @_; RemoveInternalTimer($hash); return undef; } sub SB_sensor_Get($@) { my ($hash, @a) = @_; my $name = $a[0]; return "$name: get needs at least one parameter" if(@a < 2); my $cmd= $a[1]; if( $cmd eq "update" ) { $hash->{LOCAL} = 1; SB_sensor_GetUpdate( $hash ); delete $hash->{LOCAL}; return; } return "Unknown argument $cmd, choose one of update:noArg"; } sub SB_sensor_Attr($$$) { my ($cmd, $name, $attrName, $attrVal) = @_; $attrVal= "" unless defined($attrVal); my $orig = $attrVal; if( $cmd eq "set" ) { if( $orig ne $attrVal ) { $attr{$name}{$attrName} = $attrVal; return $attrName ." set to ". $attrVal; } } return; } sub SB_sensor_GetUpdate($) { my ($hash) = @_; my $name = $hash->{NAME}; if(!$hash->{LOCAL}) { RemoveInternalTimer($hash); InternalTimer(gettimeofday()+$hash->{INTERVAL}, "SB_sensor_GetUpdate", $hash, 0); return if( AttrVal($name,"disable", 0) > 0 ); } my $cmd = qx(which ssh); chomp( $cmd ); $cmd .= ' root\@'; $cmd .= "$hash->{HOST} \'cat /sys/bus/i2c/drivers/msp430/1-0010/ambient\'"; my $fh; my $brightness = undef; if( open($fh, "$cmd|" ) ) { $brightness = sprintf( "%4.2f",(11.51293 - log(<$fh> + 1))); close($fh); readingsBeginUpdate($hash); readingsBulkUpdate($hash,"brightness",$brightness); readingsBulkUpdate($hash,"state",$brightness); readingsEndUpdate($hash,defined($hash->{LOCAL} ? 0 : 1)); } } 1; =pod =begin html not to be translated

SB_sensor

=end html =cut