# @author Peter Kappelt # @author Clemens Bergmann # @author Sebastian Kessler # @version 1.18 package main; use strict; use warnings; use Data::Dumper; use JSON; use TradfriUtils; sub TradfriGroup_Initialize($) { my ($hash) = @_; $hash->{DefFn} = 'Tradfri_Define'; $hash->{UndefFn} = 'Tradfri_Undef'; $hash->{SetFn} = 'Tradfri_Set'; $hash->{GetFn} = 'Tradfri_Get'; $hash->{AttrFn} = 'Tradfri_Attr'; $hash->{ReadFn} = 'Tradfri_Read'; $hash->{ParseFn} = 'TradfriGroup_Parse'; $hash->{Match} = '(^subscribedGroupUpdate::)|(^moodList::)'; $hash->{AttrList} = "usePercentDimming:1,0 " . $readingFnAttributes; } #messages look like this: (without newlines) # subscribedGroupUpdate::group-id::{ # "createdAt":1494088484, # "mood":198884, # "groupid":173540, # "members":[ # { # "name":"Fenster Links", # "deviceid":65537 # }, # { # "deviceid":65536 # }, # { # "name":"Fenster Rechts", # "deviceid":65538 # } # ], # "name":"Wohnzimmer", # "dimvalue":200, # "onoff":0 # } sub TradfriGroup_Parse($$){ my ($io_hash, $message) = @_; my @parts = split('::', $message); if(int(@parts) < 3){ #expecting at least three parts return undef; } my $messageID = $parts[1]; #check if group with the id exists if(my $hash = $modules{'TradfriGroup'}{defptr}{$messageID}) { #parse the JSON data my $jsonData = eval{ JSON->new->utf8->decode($parts[2]) }; if($@){ return undef; #the string was probably not valid JSON } if('subscribedGroupUpdate' eq $parts[0]){ my $createdAt = FmtDateTimeRFC1123($jsonData->{'createdAt'} || ''); my $name = $jsonData->{'name'} || ''; my $members = JSON->new->pretty->encode($jsonData->{'members'}); #dimvalue is in range 0 - 254 my $dimvalue = $jsonData->{'dimvalue'} || '0'; #dimpercent is always in range 0 - 100 my $dimpercent = int($dimvalue / 2.54 + 0.5); $dimpercent = 1 if($dimvalue == 1); $dimvalue = $dimpercent if (AttrVal($hash->{name}, 'usePercentDimming', 0) == 1); my $state = 'off'; if($jsonData->{'onoff'} eq '0'){ $dimpercent = 0; }else{ $state = Tradfri_stateString($dimpercent); } my $onoff = ($jsonData->{'onoff'} || '0') ? 'on':'off'; readingsBeginUpdate($hash); readingsBulkUpdateIfChanged($hash, 'createdAt', $createdAt, 1); readingsBulkUpdateIfChanged($hash, 'name', $name, 1); readingsBulkUpdateIfChanged($hash, 'members', $members, 1); readingsBulkUpdateIfChanged($hash, 'dimvalue', $dimvalue, 1); readingsBulkUpdateIfChanged($hash, 'pct', $dimpercent, 1); readingsBulkUpdateIfChanged($hash, 'onoff', $onoff, 1) ; readingsBulkUpdateIfChanged($hash, 'state', $state, 1); readingsEndUpdate($hash, 1); }elsif('moodList' eq $parts[0]){ #update of mood list readingsSingleUpdate($hash, 'moods', JSON->new->pretty->encode($jsonData), 1); $hash->{helper}{moods} = undef; foreach (@{$jsonData}){ $hash->{helper}{moods}->{$_->{name}} = $_; } } #$attr{$hash->{NAME}}{webCmd} = 'pct:toggle:on:off'; #$attr{$hash->{NAME}}{devStateIcon} = '{(Tradfri_devStateIcon($name),"toggle")}' if( !defined( $attr{$hash->{name}}{devStateIcon} ) ); #return the appropriate group's name return $hash->{NAME}; } return undef; } 1; =pod =item device =item summary controls an IKEA Trådfri lighting group =item summary_DE steuert eine IKEA Trådfri Beleuchtungsgruppe =begin html

TradfriGroup

=end html =cut