10_SOMFY.pm 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  1. ##############################################################################
  2. #
  3. # 10_SOMFY.pm
  4. #
  5. # This file is part of Fhem.
  6. #
  7. # Fhem is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Fhem is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Fhem. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. #
  22. # $Id: 10_SOMFY.pm 15807 2018-01-06 23:32:41Z viegener $
  23. #
  24. # SOMFY RTS / Simu Hz protocol module for FHEM
  25. # (c) Thomas Dankert <post@thomyd.de>
  26. # (c) Johannes Viegener / https://github.com/viegener/Telegram-fhem/tree/master/Somfy
  27. #
  28. # Discussed in FHEM Forum: https://forum.fhem.de/index.php/topic,53319.msg450080.html#msg450080
  29. #
  30. ##############################################################################
  31. # History:
  32. # 1.0 thomyd initial implementation
  33. # 1.1 Elektrolurch state changed to open,close,pos <x>
  34. # 1.6 viegener New state and action handling (trying to stay compatible also adding virtual receiver capabilities)
  35. # 2016-12-30 viegener - New sets / code-commands 9 / a - wind_sun_9 / wind_only_a
  36. # 2017-01-08 viegener - Handle fixed encryption A0 for switches - switchable fixed_enckey
  37. # 2017-01-21 viegener - updatestate also called in non virtual mode to sent events
  38. # 2017-02-19 viegener - Restructuring of method blocks
  39. ######################################################
  40. #
  41. # 1.9 Prep for rebuilt and signalduino
  42. # - myUtilsSOMFY_Initialize removed
  43. # - SOMFY_StartTime removed
  44. # - prepare: restructure code
  45. # - clean up history
  46. # - modify of definition to replace attr handling for enc and rolling_code
  47. # - remove attribute for enc_key and rollingcode
  48. # - cleanup namings of globals
  49. # - set default model as attribute to shutter
  50. # - support different settings for different models
  51. # - rawDevice attr
  52. # - parse function calls dispatcher for rawDevices on remotes
  53. # - changed errors in parseFn to log
  54. # - some doc cleanup
  55. # - remove dummy attribute
  56. # - special cases for pos to avoid mypos being used since stop signal is sent after automatic stop
  57. #
  58. # - changed Ys format only . no 0
  59. # - doc rework on set commands
  60. # - allow define with encryption key without starting with A
  61. # - added manual function for setting position manually without movement commands
  62. #
  63. # - remove any postfiy on position setting (like 50 pct)
  64. # - allow set command position
  65. # 2.0 Update for official version to allow further rework and Alexa handling
  66. # - change log entries and remove debug in _Parse
  67. # - added back parsestate temporarily - #msg743423
  68. #
  69. #
  70. ###############################################################################
  71. #
  72. #
  73. ###############################################################################
  74. ###############################################################################
  75. # Somfy Modul - OPEN
  76. ###############################################################################
  77. #
  78. # - Doc rework on model, set commands, rawDevice, coupling remotes
  79. # -
  80. # - test parseFn / Remotes
  81. # -
  82. # - send also wind/sun sensor codes with code E and constant rolling code --> allow complete raw send (just address is added)
  83. # -
  84. # - Check readings set
  85. # - add queuing for commands
  86. # - Autocreate
  87. # - Complete shutter / blind as different model
  88. # - Make better distinction between different IoTypes - CUL+SCC / Signalduino
  89. # - Known Issue - if timer is running and last command equals new command (only for open / close) - considered minor/but still relevant
  90. # -
  91. # - switch to standard 100 to 0 position
  92. #
  93. ###############################################################################
  94. #
  95. # Idea rebuilt handling from scratch
  96. #
  97. #
  98. #
  99. ###############################################################################
  100. package main;
  101. use strict;
  102. use warnings;
  103. #use List::Util qw(first max maxstr min minstr reduce shuffle sum);
  104. my %somfy_codes = (
  105. "10" => "go-my", # goto "my" position
  106. "11" => "stop", # stop the current movement
  107. "20" => "off", # go "up"
  108. "40" => "on", # go "down"
  109. "80" => "prog", # finish pairing
  110. "90" => "wind_sun_9", # wind and sun (sun + flag)
  111. "A0" => "wind_only_a", # wind only (flag)
  112. "100" => "on-for-timer",
  113. "101" => "off-for-timer",
  114. "XX" => "z_custom", # custom control code
  115. );
  116. my %somfy_sets = (
  117. "off" => "noArg",
  118. "on" => "noArg",
  119. "stop" => "noArg",
  120. "prog" => "noArg",
  121. "on-for-timer" => "textField",
  122. "off-for-timer" => "textField",
  123. "z_custom" => "textField",
  124. );
  125. my %somfy_sets_addition = (
  126. "go-my" => "noArg",
  127. "pos" => "100,90,80,70,60,50,40,30,20,10,0",
  128. "position" => "100,90,80,70,60,50,40,30,20,10,0",
  129. "manual" => "200,100,90,80,70,60,50,40,30,20,10,0,on,off",
  130. "wind_sun_9" => "noArg",
  131. "wind_only_a" => "noArg",
  132. );
  133. my %somfy_sendCommands = (
  134. "open" => "off",
  135. "close" => "on",
  136. );
  137. my %somfy_c2b;
  138. my $somfy_defsymbolwidth = 1240; # Default Somfy frame symbol width
  139. my $somfy_defrepetition = 6; # Default Somfy frame repeat counter
  140. my $somfy_updateFreq = 3; # Interval for State update
  141. ######################################################
  142. ######################################################
  143. ##################################################
  144. # new globals for new set
  145. #
  146. my $somfy_posAccuracy = 2;
  147. my $somfy_maxRuntime = 50;
  148. my %positions = (
  149. "moving" => "50",
  150. "go-my" => "50",
  151. "open" => "0",
  152. "off" => "0",
  153. "down" => "150",
  154. "closed" => "200",
  155. "on" => "200"
  156. );
  157. my %translations = (
  158. "0" => "open",
  159. "150" => "down",
  160. "200" => "closed"
  161. );
  162. my %translations100To0 = (
  163. "100" => "open",
  164. "10" => "down",
  165. "0" => "closed"
  166. );
  167. ##################################################
  168. # Forward declarations
  169. #
  170. sub SOMFY_CalcCurrentPos($$$$);
  171. sub SOMFY_isSwitch($);
  172. sub SOMFY_SendCommand($@);
  173. ######################################################
  174. ######################################################
  175. ##############################################################################
  176. ##############################################################################
  177. ##
  178. ## Module operation - type + instance
  179. ##
  180. ##############################################################################
  181. ##############################################################################
  182. #############################
  183. sub SOMFY_Initialize($) {
  184. my ($hash) = @_;
  185. # map commands from web interface to codes used in Somfy RTS
  186. foreach my $k ( keys %somfy_codes ) {
  187. $somfy_c2b{ $somfy_codes{$k} } = $k;
  188. }
  189. # YsKKC0RRRRAAAAAA
  190. # $hash->{Match} = "^Ys...0..........\$";
  191. $hash->{Match} = "^Ys..............\$";
  192. $hash->{SetFn} = "SOMFY_Set";
  193. #$hash->{StateFn} = "SOMFY_SetState";
  194. $hash->{DefFn} = "SOMFY_Define";
  195. $hash->{UndefFn} = "SOMFY_Undef";
  196. $hash->{ParseFn} = "SOMFY_Parse";
  197. $hash->{AttrFn} = "SOMFY_Attr";
  198. $hash->{AttrList} = " drive-down-time-to-100"
  199. . " drive-down-time-to-close"
  200. . " drive-up-time-to-100"
  201. . " drive-up-time-to-open "
  202. . " additionalPosReading "
  203. . " positionInverse:1,0 "
  204. . " IODev"
  205. . " symbol-length"
  206. . " repetition"
  207. . " switch_rfmode:1,0"
  208. . " fixed_enckey:1,0"
  209. . " do_not_notify:1,0"
  210. . " ignore:0,1"
  211. . " model:somfyblinds,somfyshutter,somfyremote,somfyswitch2,somfyswitch4"
  212. . " loglevel:0,1,2,3,4,5,6"
  213. . " rawDevice"
  214. . " $readingFnAttributes";
  215. }
  216. #############################
  217. sub SOMFY_Define($$) {
  218. my ( $hash, $def ) = @_;
  219. my @a = split( "[ \t][ \t]*", $def );
  220. my $u = "wrong syntax: define <name> SOMFY address "
  221. . "[encryption-key] [rolling-code]";
  222. # fail early and display syntax help
  223. if ( int(@a) < 3 ) {
  224. return $u;
  225. }
  226. # check address format (6 hex digits)
  227. if ( ( $a[2] !~ m/^[a-fA-F0-9]{6}$/i ) ) {
  228. return "Define $a[0]: wrong address format: specify a 6 digit hex value "
  229. }
  230. # group devices by their address
  231. my $name = $a[0];
  232. my $address = $a[2];
  233. $hash->{ADDRESS} = uc($address);
  234. # set default model if not yet set
  235. if ( ! defined( $attr{$name}{model} ) ) {
  236. $attr{$name}{model} = "somfyshutter";
  237. }
  238. # check optional arguments for device definition
  239. if ( int(@a) > 3 ) {
  240. # check encryption key (2 hex digits, first must be "A")
  241. if ( ( $a[3] !~ m/^[a-fA-F0-9]{2}$/i ) ) {
  242. return "Define $a[0]: wrong encryption key format:"
  243. . "specify a 2 digits hex value "
  244. }
  245. # reset reading time on def to 0 seconds (1970)
  246. my $tzero = FmtDateTime(0);
  247. # store it as reading, so it is saved in the statefile
  248. # only store it, if the reading does not exist yet
  249. # if(! defined( ReadingsVal($name, "enc_key", undef) )) {
  250. setReadingsVal($hash, "enc_key", uc($a[3]), $tzero);
  251. # }
  252. if ( int(@a) == 5 ) {
  253. # check rolling code (4 hex digits)
  254. if ( ( $a[4] !~ m/^[a-fA-F0-9]{4}$/i ) ) {
  255. return "Define $a[0]: wrong rolling code format:"
  256. . "specify a 4 digits hex value "
  257. }
  258. # store it, if old reading does not exist yet
  259. # if(! defined( ReadingsVal($name, "rolling_code", undef) )) {
  260. setReadingsVal($hash, "rolling_code", uc($a[4]), $tzero);
  261. # }
  262. }
  263. }
  264. my $code = uc($address);
  265. my $ncode = 1;
  266. $hash->{CODE}{ $ncode++ } = $code;
  267. $modules{SOMFY}{defptr}{$code}{$name} = $hash;
  268. $hash->{move} = 'stop';
  269. AssignIoPort($hash);
  270. }
  271. #############################
  272. sub SOMFY_Undef($$) {
  273. my ( $hash, $name ) = @_;
  274. foreach my $c ( keys %{ $hash->{CODE} } ) {
  275. $c = $hash->{CODE}{$c};
  276. # As after a rename the $name my be different from the $defptr{$c}{$n}
  277. # we look for the hash.
  278. foreach my $dname ( keys %{ $modules{SOMFY}{defptr}{$c} } ) {
  279. if ( $modules{SOMFY}{defptr}{$c}{$dname} == $hash ) {
  280. delete( $modules{SOMFY}{defptr}{$c}{$dname} );
  281. }
  282. }
  283. }
  284. return undef;
  285. }
  286. ##############################
  287. sub SOMFY_Attr(@) {
  288. my ($cmd,$name,$aName,$aVal) = @_;
  289. my $hash = $defs{$name};
  290. return "\"SOMFY Attr: \" $name does not exist" if (!defined($hash));
  291. # $cmd can be "del" or "set"
  292. # $name is device name
  293. # aName and aVal are Attribute name and value
  294. # Convert in case of change to positionINverse --> but only after init is done on restart this should not be recalculated
  295. if ( ($aName eq 'positionInverse') && ( $init_done ) ) {
  296. my $rounded;
  297. my $stateTrans;
  298. my $pos = ReadingsVal($name,'exact',undef);
  299. if ( !defined($pos) ) {
  300. $pos = ReadingsVal($name,'position',undef);
  301. }
  302. if ($cmd eq "set") {
  303. if ( ( $aVal ) && ( ! AttrVal( $name, "positionInverse", 0 ) ) ) {
  304. # set to 1 and was 0 before - convert To100To10
  305. # first exact then round to pos
  306. $pos = SOMFY_ConvertTo100To0( $pos );
  307. $rounded = SOMFY_Runden( $pos );
  308. $stateTrans = SOMFY_Translate100To0( $rounded );
  309. } elsif ( ( ! $aVal ) && ( AttrVal( $name, "positionInverse", 0 ) ) ) {
  310. # set to 0 and was 1 before - convert From100To10
  311. # first exact then round to pos
  312. $pos = SOMFY_ConvertFrom100To0( $pos );
  313. $rounded = SOMFY_Runden( $pos );
  314. $stateTrans = SOMFY_Translate( $rounded );
  315. }
  316. } elsif ($cmd eq "del") {
  317. if ( AttrVal( $name, "positionInverse", 0 ) ) {
  318. # delete and was 1 before - convert From100To10
  319. # first exact then round to pos
  320. $pos = SOMFY_ConvertFrom100To0( $pos );
  321. $rounded = SOMFY_Runden( $pos );
  322. $stateTrans = SOMFY_Translate( $rounded );
  323. }
  324. }
  325. if ( defined( $rounded ) ) {
  326. readingsBeginUpdate($hash);
  327. readingsBulkUpdate($hash,"position",$rounded);
  328. readingsBulkUpdate($hash,"exact",$pos);
  329. readingsBulkUpdate($hash,"state",$stateTrans);
  330. $hash->{STATE} = $stateTrans;
  331. readingsEndUpdate($hash,1);
  332. }
  333. } elsif ($cmd eq "set") {
  334. if($aName eq 'drive-up-time-to-100') {
  335. return "SOMFY_attr: value must be >=0 and <= 100" if($aVal < 0 || $aVal > 100);
  336. } elsif ($aName =~/drive-(down|up)-time-to.*/) {
  337. # check name and value
  338. return "SOMFY_attr: value must be >0 and <= 100" if($aVal <= 0 || $aVal > 100);
  339. }
  340. if ($aName eq 'drive-down-time-to-100') {
  341. $attr{$name}{'drive-down-time-to-100'} = $aVal;
  342. $attr{$name}{'drive-down-time-to-close'} = $aVal if(!defined($attr{$name}{'drive-down-time-to-close'}) || ($attr{$name}{'drive-down-time-to-close'} < $aVal));
  343. } elsif($aName eq 'drive-down-time-to-close') {
  344. $attr{$name}{'drive-down-time-to-close'} = $aVal;
  345. $attr{$name}{'drive-down-time-to-100'} = $aVal if(!defined($attr{$name}{'drive-down-time-to-100'}) || ($attr{$name}{'drive-down-time-to-100'} > $aVal));
  346. } elsif($aName eq 'drive-up-time-to-100') {
  347. $attr{$name}{'drive-up-time-to-100'} = $aVal;
  348. $attr{$name}{'drive-up-time-to-open'} = $aVal if(!defined($attr{$name}{'drive-up-time-to-open'}) || ($attr{$name}{'drive-up-time-to-open'} < $aVal));
  349. } elsif($aName eq 'drive-up-time-to-open') {
  350. $attr{$name}{'drive-up-time-to-open'} = $aVal;
  351. $attr{$name}{'drive-up-time-to-100'} = 0 if(!defined($attr{$name}{'drive-up-time-to-100'}) || ($attr{$name}{'drive-up-time-to-100'} > $aVal));
  352. }
  353. }
  354. return undef;
  355. }
  356. ##############################################################################
  357. ##############################################################################
  358. ##
  359. ## Parse a received command
  360. ##
  361. ##############################################################################
  362. ##############################################################################
  363. #############################
  364. sub SOMFY_DispatchRemoteCmd($$) {
  365. my ($hash, $cmd) = @_;
  366. my $name = $hash->{NAME};
  367. if ($cmd eq "10") {
  368. $cmd = "11"; # use "stop" instead of "go-my"
  369. }
  370. my $txtcmd = $somfy_codes{ $cmd };
  371. return if ( ! $txtcmd );
  372. my $rawdAttr = AttrVal($name,'rawDevice',undef);
  373. # check if rdev is defined and exists
  374. if( defined($rawdAttr) ) {
  375. # normalize address in rawdev
  376. $rawdAttr = uc( $rawdAttr );
  377. my @rawdevs = split( /\s+/, $rawdAttr );
  378. foreach my $rawdev ( @rawdevs ) {
  379. my $slist = $modules{SOMFY}{defptr}{$rawdev};
  380. if ( defined($slist)) {
  381. foreach my $n ( keys %{ $slist } ) {
  382. my $rawhash = $modules{SOMFY}{defptr}{$rawdev}{$n};
  383. Log3 $hash, 4, "SOMFY_DispatchRemoteCmd " . $name . " found dispatch SOMFY device " . $rawhash->{NAME} . " sent command :$txtcmd:";
  384. # add virtual as modifier to set command and directly call send
  385. my $ret = SOMFY_InternalSet( $rawhash, $rawhash->{NAME}, "virtual", $txtcmd );
  386. Log3 $hash, 1, "SOMFY_DispatchRemoteCmd " . $name . " Internal set :$txtcmd: to ".$rawhash->{NAME}." returned " . $ret if ( $ret );
  387. }
  388. } else {
  389. Log3 $hash, 1, "SOMFY_DispatchRemoteCmd SOMFY rawDevice $rawdev not found from $name";
  390. }
  391. }
  392. } else {
  393. Log3 $hash, 1, "SOMFY_DispatchRemoteCmd No rawDevice set in remote $name";
  394. }
  395. }
  396. #############################
  397. sub SOMFY_Parse($$) {
  398. my ($hash, $msg) = @_;
  399. my $name = $hash->{NAME};
  400. my $ret;
  401. my $ioType = $hash->{TYPE};
  402. # return "IODev unsupported" if ((my $ioType = $hash->{TYPE}) !~ m/^(CUL|SIGNALduino)$/);
  403. # preprocessing if IODev is SIGNALduino
  404. if ($ioType eq "SIGNALduino") {
  405. my $encData = substr($msg, 2);
  406. $ret = "Somfy RTS message format error (length)! :".$encData.":" if (length($encData) != 14);
  407. $ret = "Somfy RTS message format error! :".$encData.":" if ( ( ! $ret ) && ($encData !~ m/[0-9A-F]{14}/) );
  408. my ( $decData, $check );
  409. if ( ! $ret ) {
  410. $decData = SOMFY_RTS_Crypt("d", $name, $encData);
  411. $check = SOMFY_RTS_Check($name, $decData);
  412. }
  413. $ret = "Somfy RTS checksum error! :".$encData.":" if ( ( ! $ret ) && ($check ne substr($decData, 3, 1)) );
  414. if ( $ret ) {
  415. Log3 $name, 1, "$name: SOMFY_Parse : ".$ret;
  416. return undef;
  417. }
  418. Log3 $name, 4, "$name: Somfy RTS preprocessing check: $check enc: $encData dec: $decData";
  419. $msg = substr($msg, 0, 2) . $decData;
  420. }
  421. # Msg format:
  422. # Ys AB 2C 004B 010010
  423. # address needs bytes 1 and 3 swapped
  424. if (substr($msg, 0, 2) eq "Yr" || substr($msg, 0, 2) eq "Yt") {
  425. # changed time or repetition, just return the name
  426. return "";
  427. }
  428. # Check for correct length
  429. if ( length($msg) != 16 ) {
  430. Log3 $name, 1, "$name: SOMFY_Parse : SOMFY incorrect length for command (".$msg.") / length should be 16";
  431. return undef;
  432. }
  433. # get address
  434. my $address = uc(substr($msg, 14, 2).substr($msg, 12, 2).substr($msg, 10, 2));
  435. # get command and set new state
  436. my $cmd = sprintf("%X", hex(substr($msg, 4, 2)) & 0xF0);
  437. if ($cmd eq "10") {
  438. $cmd = "11"; # use "stop" instead of "go-my"
  439. }
  440. my $newstate = $somfy_codes{ $cmd };
  441. my $def = $modules{SOMFY}{defptr}{$address};
  442. if ( ($def) && (keys %{ $def }) ) { # Check also for empty hash --> issue #5
  443. my @list;
  444. foreach my $name (keys %{ $def }) {
  445. my $lh = $def->{$name};
  446. $name = $lh->{NAME}; # It may be renamed
  447. return "" if(IsIgnored($name));
  448. # update the state and log it
  449. # Debug "SOMFY Parse: $name msg: $msg --> $cmd-$newstate";
  450. Log3 $name, 4, "SOMFY Parse: $name msg: $msg --> $cmd-$newstate --> io is $ioType";
  451. readingsSingleUpdate($lh, "received", $cmd, 1);
  452. readingsSingleUpdate($lh, "parsestate", $newstate, 1);
  453. SOMFY_DispatchRemoteCmd($lh, $cmd ) if ( SOMFY_isRemote( $lh ) );
  454. push(@list, $name);
  455. }
  456. # return list of affected devices
  457. return @list;
  458. } else {
  459. # rolling code and enckey
  460. my $rolling = substr($msg, 6, 4);
  461. my $encKey = substr($msg, 2, 2);
  462. Log3 $hash, 1, "SOMFY Unknown device $address ($encKey $rolling), please define it";
  463. return "UNDEFINED SOMFY_$address SOMFY $address $encKey $rolling";
  464. }
  465. }
  466. ##############################################################################
  467. ##############################################################################
  468. ##
  469. ## Central SET routine (internal and external)
  470. ##
  471. ##############################################################################
  472. ##############################################################################
  473. ##################################################
  474. ### New set (state) method (using internalset)
  475. ###
  476. ### Reimplemented calculations for position readings and state
  477. ### Allowed sets to be done without sending actually commands to the blinds
  478. ### syntax set <name> [ <virtual|send> ] <normal set parameter>
  479. ### position and state are also updated on stop or other commands based on remaining time
  480. ### position is handled between 0 and 100 blinds down but not completely closed and 200 completely closed
  481. ### if timings for 100 and close are equal no position above 100 is used (then 100 == closed)
  482. ### position is rounded to a value of 5 and state is rounded to a value of 10
  483. #
  484. ### General assumption times are rather on the upper limit to reach desired state
  485. # Readings
  486. ## state contains rounded (to 10) position and/or textField
  487. ## position contains rounded position (limited detail)
  488. # STATE
  489. ## might contain position or textual form of the state (same as STATE reading)
  490. ###################################
  491. # call with hash, name, [virtual/send], set-args (send is default if ommitted)
  492. sub SOMFY_Set($@) {
  493. my ( $hash, $name, @args ) = @_;
  494. if ( lc($args[0]) =~m/(virtual|send)/ ) {
  495. SOMFY_InternalSet( $hash, $name, @args );
  496. } else {
  497. SOMFY_InternalSet( $hash, $name, 'send', @args );
  498. }
  499. }
  500. ###################################
  501. # call with hash, name, virtual/send, set-args
  502. sub SOMFY_InternalSet($@) {
  503. my ( $hash, $name, $mode, @args ) = @_;
  504. return undef if ( IsIgnored($name) );
  505. ### Check Args
  506. return "SOMFY_InternalSet: mode must be virtual or send: $mode " if ( $mode !~m/(virtual|send)/ );
  507. my $numberOfArgs = int(@args);
  508. return "SOMFY_set: No set value specified" if ( $numberOfArgs < 1 );
  509. my $cmd = lc($args[0]);
  510. # just a number provided, assume "pos" command
  511. if ($cmd =~ m/^\d{1,3}$/) {
  512. # overwrite args if pos (only pos and num pos)
  513. @args = ( "pos", $cmd );
  514. $cmd = "pos";
  515. $numberOfArgs = int(@args);
  516. }
  517. if( ! SOMFY_getTestSets( $hash, $cmd ) ) {
  518. my $setist = SOMFY_getTestSets( $hash );
  519. return "SOMFY_set: Unknown argument $cmd, choose one of " . $setist;
  520. }
  521. my $arg1 = "";
  522. if ( $numberOfArgs >= 2 ) {
  523. $arg1 = $args[1];
  524. }
  525. my $isSwitch = SOMFY_isSwitch( $hash );
  526. return "SOMFY_set: Bad time spec" if($cmd =~m/(on|off)-for-timer/ && $numberOfArgs == 2 && $arg1 !~ m/^\d*\.?\d+$/);
  527. return "SOMFY_set: Switch: only on/off supported" if($cmd !~ m/(on|off)/ && $isSwitch );
  528. # read timing variables
  529. my ($t1down100, $t1downclose, $t1upopen, $t1up100) = SOMFY_getTimingValues($hash);
  530. #Log3($name,5,"SOMFY_set: $name -> timings -> td1:$t1down100: tdc :$t1downclose: tuo :$t1upopen: tu1 :$t1up100: ");
  531. # get current infos
  532. my $state = $hash->{STATE};
  533. my $pos = ReadingsVal($name,'exact',undef);
  534. if ( !defined($pos) ) {
  535. $pos = ReadingsVal($name,'position',undef);
  536. }
  537. # do conversions
  538. if ( AttrVal( $name, "positionInverse", 0 ) ) {
  539. Log3($name,4,"SOMFY_set: $name Inverse before cmd:$cmd: arg1:$arg1: pos:$pos:");
  540. $arg1 = SOMFY_ConvertFrom100To0( $arg1 ) if( ($cmd eq 'pos') || ($cmd eq 'manual') ) ;
  541. $pos = SOMFY_ConvertFrom100To0( $pos );
  542. Log3($name,4,"SOMFY_set: $name Inverse after cmd:$cmd: arg1:$arg1: pos:$pos:");
  543. }
  544. if($cmd eq 'manual') {
  545. $mode = 'virtual'; # manual is virtual setting - no command to be sent
  546. return "SOMFY_set: No manual position given" if(!defined($arg1));
  547. return "SOMFY_set: $arg1 must be between 0 and 100 or on/off for manual " if($arg1 !~ /^(on|off|200|150|[1-9]?0)$/ );
  548. $arg1 = 200 if ( $arg1 eq "on" );
  549. $arg1 = 0 if ( $arg1 eq "off" );
  550. } elsif ( ($cmd eq 'pos') || ($cmd eq 'position') ) {
  551. return "SOMFY_set: No pos specification" if(!defined($arg1));
  552. return "SOMFY_set: $arg1 must be between 0 and 100 for pos" if($arg1 < 0 || $arg1 > 200);
  553. return "SOMFY_set: Please set attr drive-down-time-to-100, drive-down-time-to-close, etc"
  554. if(!defined($t1downclose) || !defined($t1down100) || !defined($t1upopen) || !defined($t1up100));
  555. $cmd = "pos";
  556. # special cases for pos to avoid mypos being used since stop signal is sent after automatic stop
  557. $cmd = "on" if ( $arg1 == 200 );
  558. $cmd = "off" if ( $arg1 == 0 );
  559. }
  560. ### initialize locals
  561. my $drivetime = 0; # timings until halt command to be sent for on/off-for-timer and pos <value> -> move by time
  562. my $updatetime = 0; # timing until update of pos to be done for any unlimited move move to endpos or go-my / stop
  563. my $move = $cmd;
  564. my $newState;
  565. my $updateState;
  566. # translate state info to numbers - closed = 200 , open = 0 (correct missing values)
  567. if ( !defined($pos) ) {
  568. if(exists($positions{$state})) {
  569. $pos = $positions{$state};
  570. } else {
  571. $pos = ($state ne "???" ? $state : 0); # fix runtime error
  572. }
  573. $pos = sprintf( "%d", $pos );
  574. }
  575. Log3($name,4,"SOMFY_set: $name -> entering with mode :$mode: cmd :$cmd: arg1 :$arg1: pos :$pos: ");
  576. # check timer running - stop timer if running and update detail pos
  577. # recognize timer running if internal updateState is still set
  578. if ( defined( $hash->{updateState} )) {
  579. # timer is running so timer needs to be stopped and pos needs update
  580. RemoveInternalTimer($hash);
  581. $pos = SOMFY_CalcCurrentPos( $hash, $hash->{move}, $pos, SOMFY_UpdateStartTime($hash) );
  582. delete $hash->{starttime};
  583. delete $hash->{updateState};
  584. delete $hash->{runningtime};
  585. delete $hash->{runningcmd};
  586. }
  587. ################ No error returns after this point to avoid stopped timer causing confusion...
  588. # calc posRounded
  589. my $posRounded = SOMFY_RoundInternal( $pos );
  590. ### handle commands
  591. if ( $isSwitch ) {
  592. if($cmd eq 'on') {
  593. $newState = 'closed';
  594. } elsif($cmd eq 'off') {
  595. $newState = 'open';
  596. } else {
  597. }
  598. } elsif(!defined($t1downclose) || !defined($t1down100) || !defined($t1upopen) || !defined($t1up100)) {
  599. #if timings not set
  600. if($cmd eq 'on') {
  601. $newState = 'closed';
  602. # $newState = 'moving';
  603. # $updatetime = $somfy_maxRuntime;
  604. # $updateState = 'closed';
  605. } elsif($cmd eq 'off') {
  606. $newState = 'open';
  607. # $newState = 'moving';
  608. # $updatetime = $somfy_maxRuntime;
  609. # $updateState = 'open';
  610. } elsif($cmd eq 'on-for-timer') {
  611. # elsif cmd == on-for-timer - time x
  612. $move = 'on';
  613. $newState = 'moving';
  614. $drivetime = $arg1;
  615. if ( $drivetime == 0 ) {
  616. $move = 'stop';
  617. } else {
  618. $updateState = 'moving';
  619. }
  620. } elsif($cmd eq 'off-for-timer') {
  621. # elsif cmd == off-for-timer - time x
  622. $move = 'off';
  623. $newState = 'moving';
  624. $drivetime = $arg1;
  625. if ( $drivetime == 0 ) {
  626. $move = 'stop';
  627. } else {
  628. $updateState = 'moving';
  629. }
  630. } elsif($cmd =~m/go-my/) {
  631. $move = 'stop';
  632. $newState = 'go-my';
  633. } elsif($cmd =~m/stop/) {
  634. $move = 'stop';
  635. $newState = $state;
  636. } elsif($cmd =~m/manual/) {
  637. $newState = $arg1;
  638. } else {
  639. $newState = $state;
  640. }
  641. ###else (here timing is set)
  642. } else {
  643. # default is roundedPos as new StatePos
  644. $newState = $posRounded;
  645. if($cmd eq 'on') {
  646. if ( $posRounded == 200 ) {
  647. # if pos == 200 - no state pos change / no timer
  648. } elsif ( $posRounded >= 100 ) {
  649. # elsif pos >= 100 - set timer for 100-to-closed --> update timer(newState 200)
  650. my $remTime = ( $t1downclose - $t1down100 ) * ( (200-$pos) / 100 );
  651. $updatetime = $remTime;
  652. $updateState = 200;
  653. } elsif ( $posRounded < 100 ) {
  654. # elseif pos < 100 - set timer for remaining time to 100+time-to-close --> update timer( newState 200)
  655. my $remTime = $t1down100 * ( (100 - $pos) / 100 );
  656. $updatetime = ( $t1downclose - $t1down100 ) + $remTime;
  657. $updateState = 200;
  658. } else {
  659. # else - unknown pos - assume pos 0 set timer for full time --> update timer( newState 200)
  660. $newState = 0;
  661. $updatetime = $t1downclose;
  662. $updateState = 200;
  663. }
  664. } elsif($cmd eq 'off') {
  665. if ( $posRounded == 0 ) {
  666. # if pos == 0 - no state pos change / no timer
  667. } elsif ( $posRounded <= 100 ) {
  668. # elsif pos <= 100 - set timer for remaining time to 0 --> update timer( newState 0 )
  669. my $remTime = ( $t1upopen - $t1up100 ) * ( $pos / 100 );
  670. $updatetime = $remTime;
  671. $updateState = 0;
  672. } elsif ( $posRounded > 100 ) {
  673. # elseif ( pos > 100 ) - set timer for remaining time to 100+time-to-open --> update timer( newState 0 )
  674. my $remTime = $t1up100 * ( ($pos - 100 ) / 100 );
  675. $updatetime = ( $t1upopen - $t1up100 ) + $remTime;
  676. $updateState = 0;
  677. } else {
  678. # else - unknown pos assume pos 200 set time for full time --> update timer( newState 0 )
  679. $newState = 200;
  680. $updatetime = $t1upopen;
  681. $updateState = 0;
  682. }
  683. } elsif($cmd eq 'pos') {
  684. if ( $pos < $arg1 ) {
  685. # if pos < x - set halt timer for remaining time to x / cmd close --> halt timer`( newState x )
  686. $move = 'on';
  687. my $remTime = $t1down100 * ( ( $arg1 - $pos ) / 100 );
  688. $drivetime = $remTime;
  689. $updateState = $arg1;
  690. } elsif ( ( $pos >= $arg1 ) && ( $posRounded <= 100 ) ) {
  691. # elsif pos <= 100 & pos > x - set halt timer for remaining time to x / cmd open --> halt timer ( newState x )
  692. $move = 'off';
  693. my $remTime = ( $t1upopen - $t1up100 ) * ( ( $pos - $arg1) / 100 );
  694. $drivetime = $remTime;
  695. if ( $drivetime == 0 ) {
  696. # $move = 'stop'; # avoid sending stop to move to my-pos
  697. $move = 'none';
  698. } else {
  699. $updateState = $arg1;
  700. }
  701. } elsif ( $pos > 100 ) {
  702. # else if pos > 100 - set timer for remaining time to 100+time for 100-x / cmd open --> halt timer ( newState x )
  703. $move = 'off';
  704. my $remTime = ( $t1upopen - $t1up100 ) * ( ( 100 - $arg1) / 100 );
  705. my $posTime = $t1up100 * ( ( $pos - 100) / 100 );
  706. $drivetime = $remTime + $posTime;
  707. $updateState = $arg1;
  708. } else {
  709. # else - send error (might be changed to first open completely then drive to pos x) / assume open
  710. $newState = 0;
  711. $move = 'on';
  712. my $remTime = $t1down100 * ( ( $arg1 - 0 ) / 100 );
  713. $drivetime = $remTime;
  714. $updateState = $arg1;
  715. ### return "SOMFY_set: Pos not currently known please open or close first";
  716. }
  717. } elsif($cmd =~m/stop|go-my/) {
  718. # update pos according to current detail pos
  719. $move = 'stop';
  720. } elsif($cmd eq 'off-for-timer') {
  721. # calcPos at new time y / cmd close --> halt timer ( newState y )
  722. $move = 'off';
  723. $drivetime = $arg1;
  724. if ( $drivetime == 0 ) {
  725. $move = 'stop';
  726. } else {
  727. $updateState = SOMFY_CalcCurrentPos( $hash, $move, $pos, $arg1 );
  728. }
  729. } elsif($cmd eq 'on-for-timer') {
  730. # calcPos at new time y / cmd open --> halt timer ( newState y )
  731. $move = 'on';
  732. $drivetime = $arg1;
  733. if ( $drivetime == 0 ) {
  734. $move = 'stop';
  735. } else {
  736. $updateState = SOMFY_CalcCurrentPos( $hash, $move, $pos, $arg1 );
  737. }
  738. } elsif($cmd =~m/manual/) {
  739. $newState = $arg1;
  740. }
  741. ## special case close is at 100 ("markisen")
  742. if( ( $t1downclose == $t1down100) && ( $t1up100 == 0 ) ) {
  743. if ( defined( $updateState )) {
  744. $updateState = minNum( 100, $updateState );
  745. }
  746. $newState = minNum( 100, $posRounded );
  747. }
  748. }
  749. ### update hash / readings
  750. Log3($name,4,"SOMFY_set: handled command $cmd --> move :$move: newState :$newState: ");
  751. if ( defined($updateState)) {
  752. Log3($name,5,"SOMFY_set: handled for drive/udpate: updateState :$updateState: drivet :$drivetime: updatet :$updatetime: ");
  753. } else {
  754. Log3($name,5,"SOMFY_set: handled for drive/udpate: updateState :: drivet :$drivetime: updatet :$updatetime: ");
  755. }
  756. # bulk update should do trigger if virtual mode
  757. # SOMFY_UpdateState( $hash, $newState, $move, $updateState, ( $mode eq 'virtual' ) );
  758. SOMFY_UpdateState( $hash, $newState, $move, $updateState, 1 );
  759. ### send command
  760. if ( $mode ne 'virtual' ) {
  761. if ( $move ne 'none' ) {
  762. $args[0] = $somfy_sendCommands{$move};
  763. $args[0] = $move if ( ! defined( $args[0] ) );
  764. SOMFY_SendCommand($hash,@args);
  765. } else {
  766. # do nothing if commmand / move is set to none
  767. }
  768. } else {
  769. # in virtual mode define drivetime as updatetime only, so no commands will be send
  770. if ( $updatetime == 0 ) {
  771. $updatetime = $drivetime;
  772. }
  773. $drivetime = 0;
  774. }
  775. ### update time stamp
  776. SOMFY_UpdateStartTime($hash);
  777. $hash->{runningtime} = 0;
  778. if($drivetime > 0) {
  779. $hash->{runningcmd} = 'stop';
  780. $hash->{runningtime} = $drivetime;
  781. } elsif($updatetime > 0) {
  782. $hash->{runningtime} = $updatetime;
  783. }
  784. ### start timer
  785. if($hash->{runningtime} > 0) {
  786. # timer fuer stop starten
  787. if ( defined( $hash->{runningcmd} )) {
  788. Log3($name,4,"SOMFY_set: $name -> stopping in $hash->{runningtime} sec");
  789. } else {
  790. Log3($name,4,"SOMFY_set: $name -> update state in $hash->{runningtime} sec");
  791. }
  792. my $utime = $hash->{runningtime} ;
  793. if($utime > $somfy_updateFreq) {
  794. $utime = $somfy_updateFreq;
  795. }
  796. InternalTimer(gettimeofday()+$utime,"SOMFY_TimedUpdate",$hash,0);
  797. } else {
  798. delete $hash->{runningtime};
  799. delete $hash->{starttime};
  800. }
  801. return undef;
  802. } # end sub SOMFY_setFN
  803. ###############################
  804. ##############################################################################
  805. ##############################################################################
  806. ##
  807. ## Internal helper - not position related
  808. ##
  809. ##############################################################################
  810. ##############################################################################
  811. #############################
  812. # 0 blinds / 2 or 4 for switches
  813. sub SOMFY_isSwitch($) {
  814. my ($hash) = @_;
  815. my $name = $hash->{NAME};
  816. my $model = AttrVal( $name, "model", "" );
  817. if ( $model =~ /switch(\d)$/ ) {
  818. $model = $1;
  819. } else {
  820. $model = 0;
  821. }
  822. return $model;
  823. }
  824. #############################
  825. sub SOMFY_isShutter($) {
  826. my ($hash) = @_;
  827. my $name = $hash->{NAME};
  828. my $model = AttrVal( $name, "model", "shutter" );
  829. return ( $model =~ /shutter$/ )?$model:0;
  830. }
  831. #############################
  832. sub SOMFY_isRemote($) {
  833. my ($hash) = @_;
  834. my $name = $hash->{NAME};
  835. my $model = AttrVal( $name, "model", "" );
  836. return ( $model =~ /remote$/ )?$model:0;
  837. }
  838. #####################################
  839. sub SOMFY_updateDef($;$$)
  840. {
  841. my ($hash, $ec, $rc) = @_;
  842. my $name = $hash->{NAME};
  843. $ec = ReadingsVal($name, "enc_key", "A0") if ( ! defined( $ec ) );
  844. $rc = ReadingsVal($name, "rolling_code", "0000") if ( ! defined( $rc ) );
  845. $hash->{DEF} = $hash->{ADDRESS}." ".uc($ec)." ".uc($rc);
  846. }
  847. ######################################################
  848. ######################################################
  849. ###
  850. ### Helper for set routine
  851. ###
  852. ######################################################
  853. ######################################################
  854. ###################################
  855. sub SOMFY_getTestSets($;$) {
  856. my ($hash, $cmd) = @_;
  857. my $name = $hash->{NAME};
  858. if ( $cmd ) {
  859. # no sets for remotes
  860. return 0 if ( SOMFY_isRemote( $hash ) );
  861. # default sets
  862. return 1 if ( exists($somfy_sets{$cmd}) );
  863. # addtl cmds for shutters and blinds (not switches)
  864. return 1 if ( ( ! SOMFY_isSwitch( $hash ) ) && ( exists($somfy_sets_addition{$cmd}) ) );
  865. return 0;
  866. }
  867. # no sets for remotes
  868. return "" if ( SOMFY_isRemote( $hash ) );
  869. my @cList;
  870. foreach my $k (sort keys %somfy_sets) {
  871. my $opts = $somfy_sets{$k};
  872. push(@cList,$k.(defined($opts)?':' . $opts:""));
  873. } # end foreach
  874. if ( ! SOMFY_isSwitch( $hash ) ) {
  875. foreach my $k (sort keys %somfy_sets_addition) {
  876. my $opts = $somfy_sets_addition{$k};
  877. push(@cList,$k.(defined($opts)?':' . $opts:""));
  878. } # end foreach
  879. }
  880. return join(" ", @cList);
  881. }
  882. ###################################
  883. sub SOMFY_Runden($) {
  884. my ($v) = @_;
  885. if ( ( $v > 105 ) && ( $v < 195 ) ) {
  886. $v = 150;
  887. } else {
  888. $v = int(($v + 5) /10) * 10;
  889. }
  890. return sprintf("%d", $v );
  891. } # end sub SOMFY_Runden
  892. ###################################
  893. sub SOMFY_Translate($) {
  894. my ($v) = @_;
  895. if(exists($translations{$v})) {
  896. $v = $translations{$v}
  897. }
  898. return $v
  899. }
  900. ###################################
  901. sub SOMFY_Translate100To0($) {
  902. my ($v) = @_;
  903. if(exists($translations100To0{$v})) {
  904. $v = $translations100To0{$v}
  905. }
  906. return $v
  907. }
  908. #############################
  909. sub SOMFY_ConvertFrom100To0($) {
  910. my ($v) = @_;
  911. return $v if ( ! defined($v) );
  912. return $v if ( length($v) == 0 );
  913. return $v if ( $v =~ /^(on|off)$/);
  914. $v = minNum( 100, maxNum( 0, $v ) );
  915. return (( $v < 10 ) ? ( 200-($v*10.0) ) : ( (100-$v)*10.0/9 ));
  916. }
  917. #############################
  918. sub SOMFY_ConvertTo100To0($) {
  919. my ($v) = @_;
  920. return $v if ( ! defined($v) );
  921. return $v if ( length($v) == 0 );
  922. $v = minNum( 200, maxNum( 0, $v ) );
  923. return ( $v > 100 ) ? ( (200-$v)/10.0 ) : ( 100-(9*$v/10.0) );
  924. }
  925. #############################
  926. sub SOMFY_RoundInternal($) {
  927. my ($v) = @_;
  928. return sprintf("%d", ($v + ($somfy_posAccuracy/2)) / $somfy_posAccuracy) * $somfy_posAccuracy;
  929. } # end sub SOMFY_RoundInternal
  930. ###################################
  931. # call with hash, translated state
  932. sub SOMFY_CalcCurrentPos($$$$) {
  933. my ($hash, $move, $pos, $dt) = @_;
  934. my $name = $hash->{NAME};
  935. my $newPos = $pos;
  936. # Attributes for calculation
  937. my ($t1down100, $t1downclose, $t1upopen, $t1up100) = SOMFY_getTimingValues($hash);
  938. if(defined($t1down100) && defined($t1downclose) && defined($t1up100) && defined($t1upopen)) {
  939. if( ( $t1downclose == $t1down100) && ( $t1up100 == 0 ) ) {
  940. $pos = minNum( 100, $pos );
  941. if($move eq 'on') {
  942. $newPos = minNum( 100, $pos );
  943. if ( $pos < 100 ) {
  944. # calc remaining time to 100%
  945. my $remTime = ( 100 - $pos ) * $t1down100 / 100;
  946. if ( $remTime > $dt ) {
  947. $newPos = $pos + ( $dt * 100 / $t1down100 );
  948. }
  949. }
  950. } elsif($move eq 'off') {
  951. $newPos = maxNum( 0, $pos );
  952. if ( $pos > 0 ) {
  953. $newPos = $dt * 100 / ( $t1upopen );
  954. $newPos = maxNum( 0, ($pos - $newPos) );
  955. }
  956. } else {
  957. Log3($name,1,"SOMFY_CalcCurrentPos: $name move wrong $move");
  958. }
  959. } else {
  960. if($move eq 'on') {
  961. if ( $pos >= 100 ) {
  962. $newPos = $dt * 100 / ( $t1downclose - $t1down100 );
  963. $newPos = minNum( 200, $pos + $newPos );
  964. } else {
  965. # calc remaining time to 100%
  966. my $remTime = ( 100 - $pos ) * $t1down100 / 100;
  967. if ( $remTime > $dt ) {
  968. $newPos = $pos + ( $dt * 100 / $t1down100 );
  969. } else {
  970. $dt = $dt - $remTime;
  971. $newPos = 100 + ( $dt * 100 / ( $t1downclose - $t1down100 ) );
  972. }
  973. }
  974. } elsif($move eq 'off') {
  975. if ( $pos <= 100 ) {
  976. $newPos = $dt * 100 / ( $t1upopen - $t1up100 );
  977. $newPos = maxNum( 0, $pos - $newPos );
  978. } else {
  979. # calc remaining time to 100%
  980. my $remTime = ( $pos - 100 ) * $t1up100 / 100;
  981. if ( $remTime > $dt ) {
  982. $newPos = $pos - ( $dt * 100 / $t1up100 );
  983. } else {
  984. $dt = $dt - $remTime;
  985. $newPos = 100 - ( $dt * 100 / ( $t1upopen - $t1up100 ) );
  986. }
  987. }
  988. } else {
  989. Log3($name,1,"SOMFY_CalcCurrentPos: $name move wrong $move");
  990. }
  991. }
  992. } else {
  993. ### no timings set so just assume it is always moving
  994. $newPos = $positions{'moving'};
  995. }
  996. return $newPos;
  997. }
  998. ######################################################
  999. ######################################################
  1000. ###
  1001. ### Helper for TIMING
  1002. ###
  1003. ######################################################
  1004. ######################################################
  1005. #############################
  1006. sub SOMFY_UpdateStartTime($) {
  1007. my ($d) = @_;
  1008. my ($s, $ms) = gettimeofday();
  1009. my $t = $s + ($ms / 1000000); # 10 msec
  1010. my $t1 = 0;
  1011. $t1 = $d->{starttime} if(exists($d->{starttime} ));
  1012. $d->{starttime} = $t;
  1013. my $dt = sprintf("%.2f", $t - $t1);
  1014. return $dt;
  1015. } # end sub SOMFY_UpdateStartTime
  1016. ###################################
  1017. sub SOMFY_TimedUpdate($) {
  1018. my ($hash) = @_;
  1019. Log3($hash->{NAME},4,"SOMFY_TimedUpdate");
  1020. # get current infos
  1021. my $pos = ReadingsVal($hash->{NAME},'exact',undef);
  1022. if ( AttrVal( $hash->{NAME}, "positionInverse", 0 ) ) {
  1023. Log3($hash->{NAME},5,"SOMFY_TimedUpdate : pos before convert so far : $pos");
  1024. $pos = SOMFY_ConvertFrom100To0( $pos );
  1025. }
  1026. Log3($hash->{NAME},5,"SOMFY_TimedUpdate : pos so far : $pos");
  1027. my $dt = SOMFY_UpdateStartTime($hash);
  1028. my $nowt = gettimeofday();
  1029. $pos = SOMFY_CalcCurrentPos( $hash, $hash->{move}, $pos, $dt );
  1030. # my $posRounded = SOMFY_RoundInternal( $pos );
  1031. Log3($hash->{NAME},5,"SOMFY_TimedUpdate : delta time : $dt new rounde pos (rounded): $pos ");
  1032. $hash->{runningtime} = $hash->{runningtime} - $dt;
  1033. if ( $hash->{runningtime} <= 0.1) {
  1034. if ( defined( $hash->{runningcmd} ) ) {
  1035. SOMFY_SendCommand($hash, $hash->{runningcmd});
  1036. }
  1037. # trigger update from timer
  1038. SOMFY_UpdateState( $hash, $hash->{updateState}, 'stop', undef, 1 );
  1039. delete $hash->{starttime};
  1040. delete $hash->{runningtime};
  1041. delete $hash->{runningcmd};
  1042. } else {
  1043. my $utime = $hash->{runningtime} ;
  1044. if($utime > $somfy_updateFreq) {
  1045. $utime = $somfy_updateFreq;
  1046. }
  1047. SOMFY_UpdateState( $hash, $pos, $hash->{move}, $hash->{updateState}, 1 );
  1048. if ( defined( $hash->{runningcmd} )) {
  1049. Log3($hash->{NAME},4,"SOMFY_TimedUpdate: $hash->{NAME} -> stopping in $hash->{runningtime} sec");
  1050. } else {
  1051. Log3($hash->{NAME},4,"SOMFY_TimedUpdate: $hash->{NAME} -> update state in $hash->{runningtime} sec");
  1052. }
  1053. my $nstt = maxNum($nowt+$utime-0.01, gettimeofday()+.1 );
  1054. Log3($hash->{NAME},5,"SOMFY_TimedUpdate: $hash->{NAME} -> next time to stop: $nstt");
  1055. InternalTimer($nstt,"SOMFY_TimedUpdate",$hash,0);
  1056. }
  1057. Log3($hash->{NAME},5,"SOMFY_TimedUpdate DONE");
  1058. } # end sub SOMFY_TimedUpdate
  1059. ###################################
  1060. # SOMFY_UpdateState( $hash, $newState, $move, $updateState );
  1061. sub SOMFY_UpdateState($$$$$) {
  1062. my ($hash, $newState, $move, $updateState, $doTrigger) = @_;
  1063. my $name = $hash->{NAME};
  1064. my $addtlPosReading = AttrVal($hash->{NAME},'additionalPosReading',undef);
  1065. if ( defined($addtlPosReading )) {
  1066. $addtlPosReading = undef if ( ( $addtlPosReading eq "" ) or ( $addtlPosReading eq "state" ) or ( $addtlPosReading eq "position" ) or ( $addtlPosReading eq "exact" ) );
  1067. }
  1068. my $newExact = $newState;
  1069. readingsBeginUpdate($hash);
  1070. if(exists($positions{$newState})) {
  1071. readingsBulkUpdate($hash,"state",$newState);
  1072. $hash->{STATE} = $newState;
  1073. $newExact = $positions{$newState};
  1074. readingsBulkUpdate($hash,"position",$newExact);
  1075. readingsBulkUpdate($hash,$addtlPosReading,$newExact) if ( defined($addtlPosReading) );
  1076. } else {
  1077. my $rounded;
  1078. my $stateTrans;
  1079. Log3($name,4,"SOMFY_UpdateState: $name enter with newState:$newState: updatestate:".(defined( $updateState )?$updateState:"<undef>").
  1080. ": move:$move:");
  1081. # do conversions
  1082. if ( AttrVal( $name, "positionInverse", 0 ) ) {
  1083. $newState = SOMFY_ConvertTo100To0( $newState );
  1084. $newExact = $newState;
  1085. $rounded = SOMFY_Runden( $newState );
  1086. $stateTrans = SOMFY_Translate100To0( $rounded );
  1087. } else {
  1088. $rounded = SOMFY_Runden( $newState );
  1089. $stateTrans = SOMFY_Translate( $rounded );
  1090. }
  1091. Log3($name,4,"SOMFY_UpdateState: $name after conversions newState:$newState: rounded:$rounded: stateTrans:$stateTrans:");
  1092. readingsBulkUpdate($hash,"state",$stateTrans);
  1093. $hash->{STATE} = $stateTrans;
  1094. readingsBulkUpdate($hash,"position",$rounded);
  1095. readingsBulkUpdate($hash,$addtlPosReading,$rounded) if ( defined($addtlPosReading) );
  1096. }
  1097. readingsBulkUpdate($hash,"exact",$newExact);
  1098. if ( defined( $updateState ) ) {
  1099. $hash->{updateState} = $updateState;
  1100. } else {
  1101. delete $hash->{updateState};
  1102. }
  1103. $hash->{move} = $move;
  1104. readingsEndUpdate($hash,$doTrigger);
  1105. } # end sub SOMFY_UpdateState
  1106. ###################################
  1107. # Return timingvalues from attr and after correction
  1108. sub SOMFY_getTimingValues($) {
  1109. my ($hash) = @_;
  1110. my $name = $hash->{NAME};
  1111. my $t1down100 = AttrVal($name,'drive-down-time-to-100',undef);
  1112. my $t1downclose = AttrVal($name,'drive-down-time-to-close',undef);
  1113. my $t1upopen = AttrVal($name,'drive-up-time-to-open',undef);
  1114. my $t1up100 = AttrVal($name,'drive-up-time-to-100',undef);
  1115. return (undef, undef, undef, undef) if(!defined($t1downclose) || !defined($t1down100) || !defined($t1upopen) || !defined($t1up100));
  1116. if ( ( $t1downclose < 0 ) || ( $t1down100 < 0 ) || ( $t1upopen < 0 ) || ( $t1up100 < 0 ) ) {
  1117. Log3($name,1,"SOMFY_getTimingValues: $name time values need to be positive values");
  1118. return (undef, undef, undef, undef);
  1119. }
  1120. if ( $t1downclose < $t1down100 ) {
  1121. Log3($name,1,"SOMFY_getTimingValues: $name close time needs to be higher or equal than time to pos100");
  1122. return (undef, undef, undef, undef);
  1123. } elsif ( $t1downclose == $t1down100 ) {
  1124. $t1up100 = 0;
  1125. }
  1126. if ( $t1upopen <= $t1up100 ) {
  1127. Log3($name,1,"SOMFY_getTimingValues: $name open time needs to be higher or equal than time to pos100");
  1128. return (undef, undef, undef, undef);
  1129. }
  1130. if ( $t1upopen < 1 ) {
  1131. Log3($name,1,"SOMFY_getTimingValues: $name time to open needs to be at least 1 second");
  1132. return (undef, undef, undef, undef);
  1133. }
  1134. if ( $t1downclose < 1 ) {
  1135. Log3($name,1,"SOMFY_getTimingValues: $name time to close needs to be at least 1 second");
  1136. return (undef, undef, undef, undef);
  1137. }
  1138. return ($t1down100, $t1downclose, $t1upopen, $t1up100);
  1139. }
  1140. ##############################################################################
  1141. ##############################################################################
  1142. ##
  1143. ## Helper for command parse and send
  1144. ##
  1145. ##############################################################################
  1146. ##############################################################################
  1147. #############################
  1148. sub SOMFY_RTS_Crypt($$$)
  1149. {
  1150. my ($operation, $name, $data) = @_;
  1151. my $res = substr($data, 0, 2);
  1152. my $ref = ($operation eq "e" ? \$res : \$data);
  1153. for (my $idx=1; $idx < 7; $idx++)
  1154. {
  1155. my $high = hex(substr($data, $idx * 2, 2));
  1156. my $low = hex(substr(${$ref}, ($idx - 1) * 2, 2));
  1157. my $val = $high ^ $low;
  1158. $res .= sprintf("%02X", $val);
  1159. }
  1160. return $res;
  1161. }
  1162. #############################
  1163. sub SOMFY_RTS_Check($$)
  1164. {
  1165. my ($name, $data) = @_;
  1166. my $checkSum = 0;
  1167. for (my $idx=0; $idx < 7; $idx++)
  1168. {
  1169. my $val = hex(substr($data, $idx * 2, 2));
  1170. $val &= 0xF0 if ($idx == 1);
  1171. $checkSum = $checkSum ^ $val ^ ($val >> 4);
  1172. ##Log3 $name, 4, "$name: Somfy RTS check: " . sprintf("%02X, %02X", $val, $checkSum);
  1173. }
  1174. $checkSum &= hex("0x0F");
  1175. return sprintf("%X", $checkSum);
  1176. }
  1177. ##############################################################################
  1178. ##############################################################################
  1179. ##
  1180. ## Central Command send routine
  1181. ##
  1182. ##############################################################################
  1183. ##############################################################################
  1184. #####################################
  1185. sub SOMFY_SendCommand($@)
  1186. {
  1187. my ($hash, @args) = @_;
  1188. my $ret = undef;
  1189. my $cmd = $args[0];
  1190. my $message;
  1191. my $name = $hash->{NAME};
  1192. my $numberOfArgs = int(@args);
  1193. my $io = $hash->{IODev};
  1194. my $ioType = $io->{TYPE};
  1195. Log3($name,4,"SOMFY_sendCommand: $name -> cmd :$cmd: ");
  1196. # custom control needs 2 digit hex code
  1197. return "Bad custom control code, use 2 digit hex codes only" if($args[0] eq "z_custom"
  1198. && ($numberOfArgs == 1
  1199. || ($numberOfArgs == 2 && $args[1] !~ m/^[a-fA-F0-9]{2}$/)));
  1200. my $command = $somfy_c2b{ $cmd };
  1201. # eigentlich überflüssig, da oben schon auf Existenz geprüft wird
  1202. if ( !defined($command) ) {
  1203. return "Unknown argument $cmd, choose one of "
  1204. . join( " ", sort keys %somfy_c2b );
  1205. }
  1206. # CUL specifics
  1207. if ($ioType ne "SIGNALduino") {
  1208. ## Do we need to change RFMode to SlowRF?
  1209. if ( defined( $attr{ $name } )
  1210. && defined( $attr{ $name }{"switch_rfmode"} ) )
  1211. {
  1212. if ( $attr{ $name }{"switch_rfmode"} eq "1" )
  1213. { # do we need to change RFMode of IODev
  1214. my $ret =
  1215. CallFn( $io->{NAME}, "AttrFn", "set",
  1216. ( $io->{NAME}, "rfmode", "SlowRF" ) );
  1217. }
  1218. }
  1219. ## Do we need to change symbol length?
  1220. if ( defined( $attr{ $name } )
  1221. && defined( $attr{ $name }{"symbol-length"} ) )
  1222. {
  1223. $message = "t" . $attr{ $name }{"symbol-length"};
  1224. IOWrite( $hash, "Y", $message );
  1225. Log GetLogLevel( $name, 4 ),
  1226. "SOMFY set symbol-length: $message for $io->{NAME}";
  1227. }
  1228. ## Do we need to change frame repetition?
  1229. if ( defined( $attr{ $name } )
  1230. && defined( $attr{ $name }{"repetition"} ) )
  1231. {
  1232. $message = "r" . $attr{ $name }{"repetition"};
  1233. IOWrite( $hash, "Y", $message );
  1234. Log GetLogLevel( $name, 4 ),
  1235. "SOMFY set repetition: $message for $io->{NAME}";
  1236. }
  1237. }
  1238. # convert old attribute values to READINGs
  1239. my $timestamp = TimeNow();
  1240. # message looks like this
  1241. # Ys_key_ctrl_cks_rollcode_a0_a1_a2
  1242. # Ys ad 20 0ae3 a2 98 42
  1243. my $enckey = uc(ReadingsVal($name, "enc_key", "A0"));
  1244. my $rollingcode = uc(ReadingsVal($name, "rolling_code", "0000"));
  1245. if($command eq "XX") {
  1246. # use user-supplied custom command
  1247. $command = $args[1];
  1248. }
  1249. # increment encryption key and rolling code
  1250. my $new_enc_key = $enckey;
  1251. if ( (! AttrVal( $name, "fixed_enckey", 0 ) ) && ( ! SOMFY_isSwitch($hash) ) ) {
  1252. my $enc_key_increment = hex( $enckey );
  1253. $new_enc_key = sprintf( "%02X", ( ++$enc_key_increment & hex("0xAF") ) );
  1254. }
  1255. my $rolling_code_increment = hex( $rollingcode );
  1256. my $new_rolling_code = sprintf( "%04X", ( ++$rolling_code_increment ) );
  1257. $message = "s"
  1258. . $new_enc_key
  1259. . $command
  1260. . $new_rolling_code
  1261. . uc( $hash->{ADDRESS} );
  1262. ## Log that we are going to switch Somfy
  1263. Log GetLogLevel( $name, 4 ), "SOMFY set $name " . join(" ", @args) . ": $message";
  1264. ## Send Message to IODev using IOWrite
  1265. if ($ioType eq "SIGNALduino") {
  1266. my $SignalRepeats = AttrVal($name,'repetition', '6');
  1267. # swap address, remove leading s
  1268. my $decData = substr($message, 1, 8) . substr($message, 13, 2) . substr($message, 11, 2) . substr($message, 9, 2);
  1269. my $check = SOMFY_RTS_Check($name, $decData);
  1270. my $encData = SOMFY_RTS_Crypt("e", $name, substr($decData, 0, 3) . $check . substr($decData, 4));
  1271. $message = 'P43#' . $encData . '#R' . $SignalRepeats;
  1272. #Log3 $hash, 4, "$hash->{IODev}->{NAME} SOMFY_sendCommand: $name -> message :$message: ";
  1273. IOWrite($hash, 'sendMsg', $message);
  1274. } else {
  1275. Log3($name,5,"SOMFY_sendCommand: $name -> message :$message: ");
  1276. IOWrite( $hash, "Y", $message );
  1277. }
  1278. # update the readings, but do not generate an event
  1279. setReadingsVal($hash, "enc_key", $new_enc_key, $timestamp);
  1280. setReadingsVal($hash, "rolling_code", $new_rolling_code, $timestamp);
  1281. # modify definition of device with actual enc/rc
  1282. SOMFY_updateDef( $hash, $new_enc_key, $new_rolling_code );
  1283. # CUL specifics
  1284. if ($ioType ne "SIGNALduino") {
  1285. ## Do we need to change symbol length back?
  1286. if ( defined( $attr{ $name } )
  1287. && defined( $attr{ $name }{"symbol-length"} ) )
  1288. {
  1289. $message = "t" . $somfy_defsymbolwidth;
  1290. IOWrite( $hash, "Y", $message );
  1291. Log GetLogLevel( $name, 4 ),
  1292. "SOMFY set symbol-length back: $message for $io->{NAME}";
  1293. }
  1294. ## Do we need to change repetition back?
  1295. if ( defined( $attr{ $name } )
  1296. && defined( $attr{ $name }{"repetition"} ) )
  1297. {
  1298. $message = "r" . $somfy_defrepetition;
  1299. IOWrite( $hash, "Y", $message );
  1300. Log GetLogLevel( $name, 4 ),
  1301. "SOMFY set repetition back: $message for $io->{NAME}";
  1302. }
  1303. ## Do we need to change RFMode back to HomeMatic??
  1304. if ( defined( $attr{ $name } )
  1305. && defined( $attr{ $name }{"switch_rfmode"} ) )
  1306. {
  1307. if ( $attr{ $name }{"switch_rfmode"} eq "1" )
  1308. { # do we need to change RFMode of IODev?
  1309. my $ret =
  1310. CallFn( $io->{NAME}, "AttrFn", "set",
  1311. ( $io->{NAME}, "rfmode", "HomeMatic" ) );
  1312. }
  1313. }
  1314. }
  1315. ##########################
  1316. # Look for all devices with the same address, and set state, enc-key, rolling-code and timestamp
  1317. my $code = "$hash->{ADDRESS}";
  1318. my $tn = TimeNow();
  1319. foreach my $n ( keys %{ $modules{SOMFY}{defptr}{$code} } ) {
  1320. my $lh = $modules{SOMFY}{defptr}{$code}{$n};
  1321. $lh->{READINGS}{enc_key}{TIME} = $tn;
  1322. $lh->{READINGS}{enc_key}{VAL} = $new_enc_key;
  1323. $lh->{READINGS}{rolling_code}{TIME} = $tn;
  1324. $lh->{READINGS}{rolling_code}{VAL} = $new_rolling_code;
  1325. SOMFY_updateDef( $lh, $new_enc_key, $new_rolling_code );
  1326. }
  1327. return $ret;
  1328. } # end sub SOMFY_SendCommand
  1329. ######################################################
  1330. ######################################################
  1331. ######################################################
  1332. 1;
  1333. =pod
  1334. =item summary supporting devices using the SOMFY RTS protocol - window shades
  1335. =item summary_DE für Geräte, die das SOMFY RTS protocol unterstützen - Rolläden
  1336. =begin html
  1337. <a name="SOMFY"></a>
  1338. <h3>SOMFY - Somfy RTS / Simu Hz protocol</h3>
  1339. <ul>
  1340. The Somfy RTS (identical to Simu Hz) protocol is used by a wide range of devices,
  1341. which are either senders or receivers/actuators.
  1342. Right now only SENDING of Somfy commands is implemented in the CULFW, so this module currently only
  1343. supports devices like blinds, dimmers, etc. through a <a href="#CUL">CUL</a> device (which must be defined first).
  1344. Reception of Somfy remotes is only supported indirectly through the usage of an FHEMduino
  1345. <a href="http://www.fhemwiki.de/wiki/FHEMduino">http://www.fhemwiki.de/wiki/FHEMduino</a>
  1346. which can then be used to connect to the SOMFY device.
  1347. <br><br>
  1348. <a name="SOMFYdefine"></a>
  1349. <b>Define</b>
  1350. <ul>
  1351. <code>define &lt;name&gt; SOMFY &lt;address&gt; [&lt;encryption-key&gt;] [&lt;rolling-code&gt;] </code>
  1352. <br><br>
  1353. The address is a 6-digit hex code, that uniquely identifies a single remote control channel.
  1354. It is used to pair the remote to the blind or dimmer it should control.
  1355. <br>
  1356. Pairing is done by setting the blind in programming mode, either by disconnecting/reconnecting the power,
  1357. or by pressing the program button on an already associated remote.
  1358. <br>
  1359. Once the blind is in programming mode, send the "prog" command from within FHEM to complete the pairing.
  1360. The blind will move up and down shortly to indicate completion.
  1361. <br>
  1362. You are now able to control this blind from FHEM, the receiver thinks it is just another remote control.
  1363. <ul>
  1364. <li><code>&lt;address&gt;</code> is a 6 digit hex number that uniquely identifies FHEM as a new remote control channel.
  1365. <br>You should use a different one for each device definition, and group them using a structure.
  1366. </li>
  1367. <li>The optional <code>&lt;encryption-key&gt;</code> is a 2 digit hex number (first letter should always be A)
  1368. that can be set to clone an existing remote control channel.</li>
  1369. <li>The optional <code>&lt;rolling-code&gt;</code> is a 4 digit hex number that can be set
  1370. to clone an existing remote control channel.<br>
  1371. If you set one of them, you need to pick the same address as an existing remote.
  1372. Be aware that the receiver might not accept commands from the remote any longer,<br>
  1373. if you used FHEM to clone an existing remote.
  1374. <br>
  1375. This is because the code is original remote's codes are out of sync.</li>
  1376. <br>
  1377. Rolling code and encryption key in the device definition will be always updated on commands sent and can be also changed manually by modifying the original definition (e.g in FHEMWeb - modify).
  1378. </ul>
  1379. <br>
  1380. Examples:
  1381. <ul>
  1382. <code>define rollo_1 SOMFY 000001</code><br>
  1383. <code>define rollo_2 SOMFY 000002</code><br>
  1384. <code>define rollo_3_original SOMFY 42ABCD A5 0A1C</code><br>
  1385. </ul>
  1386. </ul>
  1387. <br>
  1388. <a name="SOMFYset"></a>
  1389. <b>Set </b>
  1390. <ul>
  1391. <code>set &lt;name&gt; &lt;value&gt; [&lt;time&gt]</code>
  1392. <br><br>
  1393. where <code>value</code> is one of:<br>
  1394. <pre>
  1395. on
  1396. off
  1397. go-my
  1398. stop
  1399. pos value (0..100) # see note
  1400. prog # Special, see note
  1401. wind_sun_9
  1402. wind_only_a
  1403. on-for-timer
  1404. off-for-timer
  1405. manual 0,...,100,200,on,off
  1406. </pre>
  1407. Examples:
  1408. <ul>
  1409. <code>set rollo_1 on</code><br>
  1410. <code>set rollo_1,rollo_2,rollo_3 on</code><br>
  1411. <code>set rollo_1-rollo_3 on</code><br>
  1412. <code>set rollo_1 off</code><br>
  1413. <code>set rollo_1 pos 50</code><br>
  1414. </ul>
  1415. <br>
  1416. Notes:
  1417. <ul>
  1418. <li>prog is a special command used to pair the receiver to FHEM:
  1419. Set the receiver in programming mode (eg. by pressing the program-button on the original remote)
  1420. and send the "prog" command from FHEM to finish pairing.<br>
  1421. The blind will move up and down shortly to indicate success.
  1422. </li>
  1423. <li>on-for-timer and off-for-timer send a stop command after the specified time,
  1424. instead of reversing the blind.<br>
  1425. This can be used to go to a specific position by measuring the time it takes to close the blind completely.
  1426. </li>
  1427. <li>pos value<br>
  1428. The position is variying between 0 completely open and 100 for covering the full window.
  1429. The position must be between 0 and 100 and the appropriate
  1430. attributes drive-down-time-to-100, drive-down-time-to-close,
  1431. drive-up-time-to-100 and drive-up-time-to-open must be set. See also positionInverse attribute.<br>
  1432. </li>
  1433. <li>wind_sun_9 and wind_only_a send special commands to the Somfy device that to represent the codes sent from wind and sun detector (with the respective code contained in the set command name)
  1434. </li>
  1435. <li>manual will only set the position without sending any commands to the somfy device - can be used to correct the position manually
  1436. </li>
  1437. </ul>
  1438. The position reading distinuishes between multiple cases
  1439. <ul>
  1440. <li>Without timing values (see attributes) set only generic values are used for status and position: <pre>open, closed, moving</pre> are used
  1441. </li>
  1442. <li>With timing values set but drive-down-time-to-close equal to drive-down-time-to-100 and drive-up-time-to-100 equal 0
  1443. the device is considered to only vary between 0 and 100 (100 being completely closed)
  1444. </li>
  1445. <li>With full timing values set the device is considerd a window shutter (Rolladen) with a difference between
  1446. covering the full window (position 100) and being completely closed (position 200)
  1447. </li>
  1448. </ul>
  1449. </ul>
  1450. <br>
  1451. <b>Get</b> <ul>N/A</ul><br>
  1452. <a name="SOMFYattr"></a>
  1453. <b>Attributes</b>
  1454. <ul>
  1455. <li>IODev<br>
  1456. Set the IO or physical device which should be used for sending signals
  1457. for this "logical" device. An example for the physical device is a CUL.<br>
  1458. Note: The IODev has to be set, otherwise no commands will be sent!<br>
  1459. If you have both a CUL868 and CUL433, use the CUL433 as IODev for increased range.
  1460. </li><br>
  1461. <li>positionInverse<br>
  1462. Inverse operation for positions instead of 0 to 100-200 the positions are ranging from 100 to 10 (down) and then to 0 (closed). The pos set command will point in this case to the reversed pos values. This does NOT reverse the operation of the on/off command, meaning that on always will move the shade down and off will move it up towards the initial position.
  1463. </li><br>
  1464. <li>additionalPosReading<br>
  1465. Position of the shutter will be stored in the reading <code>pos</code> as numeric value.
  1466. Additionally this attribute might specify a name for an additional reading to be updated with the same value than the pos.
  1467. </li><br>
  1468. <li>fixed_enckey 1|0<br>
  1469. If set to 1 the enc-key is not changed after a command sent to the device. Default is value 0 meaning enc-key is changed normally for the RTS protocol.
  1470. </li><br>
  1471. <li>eventMap<br>
  1472. Replace event names and set arguments. The value of this attribute
  1473. consists of a list of space separated values, each value is a colon
  1474. separated pair. The first part specifies the "old" value, the second
  1475. the new/desired value. If the first character is slash(/) or comma(,)
  1476. then split not by space but by this character, enabling to embed spaces.
  1477. Examples:<ul><code>
  1478. attr store eventMap on:open off:closed<br>
  1479. attr store eventMap /on-for-timer 10:open/off:closed/<br>
  1480. set store open
  1481. </code></ul>
  1482. </li><br>
  1483. <li><a href="#do_not_notify">do_not_notify</a></li><br>
  1484. <li><a href="#loglevel">loglevel</a></li><br>
  1485. <li><a href="#showtime">showtime</a></li><br>
  1486. <li>model<br>
  1487. The model attribute denotes the model type of the device.
  1488. The attributes will (currently) not be used by the fhem.pl directly.
  1489. It can be used by e.g. external programs or web interfaces to
  1490. distinguish classes of devices and send the appropriate commands
  1491. (e.g. "on" or "off" to a switch, "dim..%" to dimmers etc.).<br>
  1492. The spelling of the model names are as quoted on the printed
  1493. documentation which comes which each device. This name is used
  1494. without blanks in all lower-case letters. Valid characters should be
  1495. <code>a-z 0-9</code> and <code>-</code> (dash),
  1496. other characters should be ommited.<br>
  1497. Here is a list of "official" devices:<br>
  1498. <b>Receiver/Actor</b>: somfyblinds<br>
  1499. </li><br>
  1500. <li>ignore<br>
  1501. Ignore this device, e.g. if it belongs to your neighbour. The device
  1502. won't trigger any FileLogs/notifys, issued commands will silently
  1503. ignored (no RF signal will be sent out, just like for the <a
  1504. href="#attrdummy">dummy</a> attribute). The device won't appear in the
  1505. list command (only if it is explicitely asked for it), nor will it
  1506. appear in commands which use some wildcard/attribute as name specifiers
  1507. (see <a href="#devspec">devspec</a>). You still get them with the
  1508. "ignored=1" special devspec.
  1509. </li><br>
  1510. <li>drive-down-time-to-100<br>
  1511. The time the blind needs to drive down from "open" (pos 0) to pos 100.<br>
  1512. In this position, the lower edge touches the window frame, but it is not completely shut.<br>
  1513. For a mid-size window this time is about 12 to 15 seconds.
  1514. </li><br>
  1515. <li>drive-down-time-to-close<br>
  1516. The time the blind needs to drive down from "open" (pos 0) to "close", the end position of the blind.<br>
  1517. Note: If set, this value always needs to be higher than drive-down-time-to-100
  1518. This is about 3 to 5 seonds more than the "drive-down-time-to-100" value.
  1519. </li><br>
  1520. <li>drive-up-time-to-100<br>
  1521. The time the blind needs to drive up from "close" (endposition) to "pos 100".<br>
  1522. This usually takes about 3 to 5 seconds.
  1523. </li><br>
  1524. <li>drive-up-time-to-open<br>
  1525. The time the blind needs drive up from "close" (endposition) to "open" (upper endposition).<br>
  1526. Note: If set, this value always needs to be higher than drive-down-time-to-100
  1527. This value is usually a bit higher than "drive-down-time-to-close", due to the blind's weight.
  1528. </li><br>
  1529. </ul>
  1530. </ul>
  1531. =end html
  1532. =cut