# @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 TradfriDevice_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} = 'TradfriDevice_Parse'; $hash->{Match} = '^subscribedDeviceUpdate::'; $hash->{AttrList} = "usePercentDimming:1,0 " . $readingFnAttributes; } #messages look like this: (without newlines) # subscribedDeviceUpdate::device-id::{ # "lastSeenAt":1501407261, # "createdAt":1492280964, # "reachabilityState":1, # "name":"Fenster Links", # "dimvalue":200, # "type":"TRADFRI bulb E27 opal 1000lm", # "deviceid":65537, # "version":"1.1.1.0-5.7.2.0", # "manufacturer":"IKEA of Sweden", # "onoff":0 # } sub TradfriDevice_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 device with the id exists if(my $hash = $modules{'TradfriDevice'}{defptr}{$messageID}) { # the path returned "Not Found" -> unknown resource, but this message still suits for this device if($parts[2] eq "Not Found"){ $hash->{STATE} = "NotFound"; return $hash->{NAME}; } #parse the JSON data my $jsonData = eval{ JSON->new->utf8->decode($parts[2]) }; if($@){ return undef; #the string was probably not valid JSON } my $manufacturer = $jsonData->{'manufacturer'} || ''; my $type = $jsonData->{'type'} || ''; #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'; my $name = $jsonData->{'name'} || ''; my $createdAt = FmtDateTimeRFC1123($jsonData->{'createdAt'} || ''); my $reachableState = $jsonData->{'reachabilityState'} || ''; my $lastSeen = FmtDateTimeRFC1123($jsonData->{'lastSeenAt'} || ''); my $color = $jsonData->{'color'} || ''; my $version = $jsonData->{'version'} || ''; readingsBeginUpdate($hash); readingsBulkUpdateIfChanged($hash, 'dimvalue', $dimvalue, 1); readingsBulkUpdateIfChanged($hash, 'pct', $dimpercent, 1); readingsBulkUpdateIfChanged($hash, 'state', $state, 1); readingsBulkUpdateIfChanged($hash, 'name', $name, 1); readingsBulkUpdateIfChanged($hash, 'createdAt', $createdAt, 1); readingsBulkUpdateIfChanged($hash, 'softwareVersion', $version, 1); readingsBulkUpdateIfChanged($hash, 'type', $type, 1); readingsBulkUpdateIfChanged($hash, 'manufacturer', $manufacturer, 1); readingsBulkUpdateIfChanged($hash, 'reachableState', $reachableState, 1); readingsBulkUpdateIfChanged($hash, 'color', $color, 1); readingsBulkUpdateIfChanged($hash, 'lastSeen', $lastSeen, 1); readingsBulkUpdateIfChanged($hash, 'onoff', $onoff, 1); readingsEndUpdate($hash, 1); #$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 device's name return $hash->{NAME}; } return undef; } 1; =pod =item device =item summary controls an IKEA Trådfri device (bulb, panel) =item summary_DE steuert ein IKEA Trådfri Gerät =begin html
define <name> TradfriDevice <device-address>
define trDeviceOne TradfriDevice 65538
set <name> <option> [<value>]
get <name> <option>
attr <name> <attribute> <value>