| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532 |
- ##############################################################################
- # $Id: 38_netatmo.pm 17485 2018-10-07 19:05:13Z moises $
- #
- # 38_netatmo.pm
- #
- # 2018 Markus Moises < vorname at nachname . de >
- #
- # Based on original code by justme1968
- #
- # https://forum.fhem.de/index.php/topic,53500.0.html
- #
- #
- ##############################################################################
- # Release 21 / 2018-10-06
- package main;
- use strict;
- use warnings;
- use Encode qw(encode_utf8 decode_utf8);
- use JSON;
- use Math::Trig;
- use HttpUtils;
- use Data::Dumper; #debugging
- use MIME::Base64;
- use vars qw($FW_ME);
- use vars qw($FW_CSRF);
- my %health_index = ( 0 => "healthy",
- 1 => "fine",
- 2 => "fair",
- 3 => "poor",
- 4 => "unhealthy",
- 5 => "unknown", );
- sub
- netatmo_Initialize($)
- {
- my ($hash) = @_;
- $hash->{DefFn} = "netatmo_Define";
- $hash->{NotifyFn} = "netatmo_Notify";
- $hash->{UndefFn} = "netatmo_Undefine";
- $hash->{SetFn} = "netatmo_Set";
- $hash->{GetFn} = "netatmo_Get";
- $hash->{DbLog_splitFn} = "netatmo_DbLog_splitFn";
- $hash->{AttrFn} = "netatmo_Attr";
- $hash->{AttrList} = "IODev ".
- "disable:0,1 ".
- "interval ".
- "videoquality:poor,low,medium,high ".
- "ignored_device_ids ".
- "setpoint_duration ".
- "webhookURL webhookPoll:0,1 ".
- "addresslimit ".
- "serverAPI ";
- $hash->{AttrList} .= $readingFnAttributes;
- }
- #####################################
- sub
- netatmo_Define($$)
- {
- my ($hash, $def) = @_;
- my @a = split("[ \t][ \t]*", $def);
- my $name = $a[0];
- $hash->{status} = "initialized";
- $hash->{helper}{last_status_store} = 0;
- my $subtype;
- if($a[2] eq "WEBHOOK") {
- $subtype = "WEBHOOK";
- $hash->{model} = "WEBHOOK";
- my $d = $modules{$hash->{TYPE}}{defptr}{"WEBHOOK"};
- return "Netatmo webkook already defined as $d->{NAME}" if( defined($d) && $d->{NAME} ne $name );
- $modules{$hash->{TYPE}}{defptr}{"WEBHOOK"} = $hash;
- readingsBeginUpdate($hash);
- readingsBulkUpdate( $hash, "webhook", "initialized" );
- readingsEndUpdate( $hash, 1 );
- my $account = $modules{$hash->{TYPE}}{defptr}{"account"};
- $hash->{IODev} = $account;
- $attr{$name}{IODev} = $account->{NAME} if( !defined($attr{$name}{IODev}) && $account);
- }
- elsif( @a == 3 ) {
- $subtype = "DEVICE";
- my $device = $a[2];
- $hash->{Device} = $device;
- $hash->{openRequests} = 0;
- $hash->{helper}{INTERVAL} = 60*15 if( !$hash->{helper}{INTERVAL} );
- my $d = $modules{$hash->{TYPE}}{defptr}{"D$device"};
- return "device $device already defined as $d->{NAME}" if( defined($d) && $d->{NAME} ne $name );
- $modules{$hash->{TYPE}}{defptr}{"D$device"} = $hash;
- }
- elsif( ($a[2] eq "PUBLIC" && @a > 3 ) )
- {
- $hash->{openRequests} = 0;
- Log3 $name, 5, "$name: pub ".Dumper(@a);
- if( $a[3] && $a[3] =~ m/[\da-f]{2}(:[\da-f]{2}){5}/ )
- {
- $hash->{model} = "PUBLIC";
- my $device = $a[3];
- $hash->{Device} = $device;
- if( $a[4] && $a[4] =~ m/[\da-f]{2}(:[\da-f]{2}){5}/ )
- {
- $subtype = "MODULE";
- my $module = "";
- my $readings = "";
- my @a = splice( @a, 4 );
- while( @a ) {
- $module .= " " if( $module );
- $module .= shift( @a );
- $readings .= " " if( $readings );
- $readings .= shift( @a );
- }
- $hash->{Module} = $module;
- $hash->{dataTypes} = $readings if($readings);
- $hash->{dataTypes} = "Temperature,CO2,Humidity,Noise,Pressure,Rain,WindStrength,WindAngle,GustStrength,GustAngle,Sp_Temperature,BoilerOn,BoilerOff" if( !$readings );
- my $d = $modules{$hash->{TYPE}}{defptr}{"M$module"};
- return "module $module already defined as $d->{NAME}" if( defined($d) && $d->{NAME} ne $name );
- $modules{$hash->{TYPE}}{defptr}{"M$module"} = $hash;
- my $state_format;
- if( $readings =~ m/temperature/ ) {
- $state_format .= " " if( $state_format );
- $state_format .= "T: temperature";
- }
- if( $readings =~ m/humidity/ ) {
- $state_format .= " " if( $state_format );
- $state_format .= "H: humidity";
- }
- $attr{$name}{stateFormat} = $state_format if( !defined($attr{$name}{stateFormat}) && defined($state_format) && defined($name) );
- $attr{$name}{room} = "netatmo" if( !defined($attr{$name}{room}) && defined($name));
- $attr{$name}{devStateIcon} = ".*:no-icon" if( !defined($attr{$name}{devStateIcon}) && defined($name));
- }
- $subtype = "DEVICE";
- my $d = $modules{$hash->{TYPE}}{defptr}{"D$device"};
- return "device $device already defined as $d->{NAME}" if( defined($d) && $d->{NAME} ne $name );
- $modules{$hash->{TYPE}}{defptr}{"D$device"} = $hash;
- delete( $hash->{LAST_POLL} );
- }
- else
- {
- my ($lat, $lon, $rad);
- if($a[3] =~ m/,/){
- Log3 $name, 5, "$name: latlng 2 ".$a[3];
- my @latlon = split( ',', $a[3] );
- $lat = $latlon[0];
- $lon = $latlon[1];
- $rad = $a[4];
- }
- else {
- $lat = $a[3];
- $lon = $a[4];
- $rad = $a[5];
- }
- $rad = 0.02 if( !$rad );
- $hash->{Lat} = $lat;
- $hash->{Lon} = $lon;
- $hash->{Rad} = $rad;
- $subtype = "PUBLIC";
- $hash->{model} = "WEATHERMAP";
- $modules{$hash->{TYPE}}{defptr}{$hash->{Lat}.$hash->{Lon}.$hash->{Rad}} = $hash;
- my $account = $modules{$hash->{TYPE}}{defptr}{"account"};
- $hash->{IODev} = $account;
- $attr{$name}{IODev} = $account->{NAME} if( !defined($attr{$name}{IODev}) && $account);
- }
- $hash->{helper}{INTERVAL} = 60*30 if( !$hash->{helper}{INTERVAL} );
- $attr{$name}{room} = "netatmo" if( !defined($attr{$name}{room}) && defined($name));
- $attr{$name}{devStateIcon} = ".*:no-icon" if( !defined($attr{$name}{devStateIcon}) && defined($name));
- } elsif( ($a[2] eq "MODULE" && @a == 5 ) ) {
- $subtype = "MODULE";
- my $device = $a[@a-2];
- my $module = $a[@a-1];
- $hash->{Device} = $device;
- $hash->{Module} = $module;
- $hash->{openRequests} = 0;
- $hash->{helper}{INTERVAL} = 60*15 if( !$hash->{helper}{INTERVAL} );
- my $d = $modules{$hash->{TYPE}}{defptr}{"M$module"};
- return "module $module already defined as $d->{NAME}" if( defined($d) && $d->{NAME} ne $name );
- $modules{$hash->{TYPE}}{defptr}{"M$module"} = $hash;
- } elsif( ($a[2] eq "FORECAST" && @a == 4 ) ) {
- $subtype = "FORECAST";
- $hash->{model} = "FORECAST";
- my $device = $a[3];
- $hash->{Station} = $device;
- $hash->{openRequests} = 0;
- $hash->{helper}{INTERVAL} = 60*60 if( !$hash->{helper}{INTERVAL} );
- $attr{$name}{room} = "netatmo" if( !defined($attr{$name}{room}) && defined($name));
- $attr{$name}{devStateIcon} = ".*:no-icon" if( !defined($attr{$name}{devStateIcon}) && defined($name));
- $attr{$name}{'event-on-change-reading'} = ".*" if( !defined($attr{$name}{'event-on-change-reading'}) && defined($name));
- my $d = $modules{$hash->{TYPE}}{defptr}{"F$device"};
- return "forecast $device already defined as $d->{NAME}" if( defined($d) && $d->{NAME} ne $name );
- $modules{$hash->{TYPE}}{defptr}{"F$device"} = $hash;
- my $account = $modules{$hash->{TYPE}}{defptr}{"account"};
- $hash->{IODev} = $account;
- $attr{$name}{IODev} = $account->{NAME} if( !defined($attr{$name}{IODev}) && $account);
- } elsif( ($a[2] eq "RELAY" && @a == 4 ) ) {
- $subtype = "RELAY";
- my $device = $a[3];
- $hash->{Relay} = $device;
- $hash->{openRequests} = 0;
- $hash->{helper}{INTERVAL} = 60*30 if( !$hash->{helper}{INTERVAL} );
- my $d = $modules{$hash->{TYPE}}{defptr}{"R$device"};
- return "relay $device already defined as $d->{NAME}" if( defined($d) && $d->{NAME} ne $name );
- $modules{$hash->{TYPE}}{defptr}{"R$device"} = $hash;
- } elsif( ($a[2] eq "THERMOSTAT" && @a == 5 ) ) {
- $subtype = "THERMOSTAT";
- my $device = $a[@a-2];
- my $module = $a[@a-1];
- $hash->{Relay} = $device;
- $hash->{Thermostat} = $module;
- $hash->{openRequests} = 0;
- $hash->{dataTypes} = "Temperature,Sp_Temperature,BoilerOn,BoilerOff";
- $hash->{helper}{INTERVAL} = 60*30 if( !$hash->{helper}{INTERVAL} );
- my $d = $modules{$hash->{TYPE}}{defptr}{"T$module"};
- return "thermostat $module already defined as $d->{NAME}" if( defined($d) && $d->{NAME} ne $name );
- $modules{$hash->{TYPE}}{defptr}{"T$module"} = $hash;
- } elsif( ($a[2] eq "HEATINGHOME" && @a == 4 ) ) {
- $subtype = "HEATINGHOME";
- $hash->{model} = "HEATINGHOME";
- my $home = $a[@a-1];
- $hash->{Home} = $home;
- $hash->{openRequests} = 0;
- #$hash->{dataTypes} = "Temperature,Sp_Temperature,BoilerOn,BoilerOff";
- $hash->{helper}{INTERVAL} = 60*30 if( !$hash->{helper}{INTERVAL} );
- my $d = $modules{$hash->{TYPE}}{defptr}{"E$home"};
- return "heating home $home already defined as $d->{NAME}" if( defined($d) && $d->{NAME} ne $name );
- $modules{$hash->{TYPE}}{defptr}{"E$home"} = $hash;
- } elsif( ($a[2] eq "HEATINGROOM" && @a == 5 ) ) {
- $subtype = "HEATINGROOM";
- $hash->{model} = "HEATINGROOM";
- my $room = $a[@a-1];
- my $home = $a[@a-2];
- $hash->{Home} = $home;
- $hash->{Room} = $room;
- $hash->{openRequests} = 0;
- $hash->{dataTypes} = "Temperature,Sp_Temperature,heating_power_request,BoilerOn,BoilerOff";
- $hash->{helper}{INTERVAL} = 60*30 if( !$hash->{helper}{INTERVAL} );
- my $d = $modules{$hash->{TYPE}}{defptr}{"O$room"};
- return "heating room $room already defined as $d->{NAME}" if( defined($d) && $d->{NAME} ne $name );
- $modules{$hash->{TYPE}}{defptr}{"O$room"} = $hash;
- } elsif( ($a[2] eq "HOME" && @a == 4 ) ) {
- $subtype = "HOME";
- $hash->{model} = "HOME";
- my $home = $a[@a-1];
- $hash->{Home} = $home;
- $hash->{helper}{INTERVAL} = 60*15 if( !$hash->{helper}{INTERVAL} );
- $attr{$name}{videoquality} = "medium" if( !defined($attr{$name}{videoquality}) && defined($name));
- my $d = $modules{$hash->{TYPE}}{defptr}{"H$home"};
- return "home $home already defined as $d->{NAME}" if( defined($d) && $d->{NAME} ne $name );
- $modules{$hash->{TYPE}}{defptr}{"H$home"} = $hash;
- } elsif( ($a[2] eq "PERSON" && @a == 5 ) ) {
- $subtype = "PERSON";
- $hash->{model} = "PERSON";
- my $home = $a[@a-2];
- my $person = $a[@a-1];
- $hash->{Home} = $home;
- $hash->{Person} = $person;
- $hash->{helper}{INTERVAL} = 60*15 if( !$hash->{helper}{INTERVAL} );
- my $d = $modules{$hash->{TYPE}}{defptr}{"P$person"};
- return "person $person already defined as $d->{NAME}" if( defined($d) && $d->{NAME} ne $name );
- $modules{$hash->{TYPE}}{defptr}{"P$person"} = $hash;
- } elsif( ($a[2] eq "CAMERA" && @a == 5 ) ) {
- $subtype = "CAMERA";
- my $home = $a[@a-2];
- my $camera = $a[@a-1];
- $hash->{Home} = $home;
- $hash->{Camera} = $camera;
- $hash->{helper}{INTERVAL} = 60*15 if( !$hash->{helper}{INTERVAL} );
- my $d = $modules{$hash->{TYPE}}{defptr}{"C$camera"};
- return "camera $camera already defined as $d->{NAME}" if( defined($d) && $d->{NAME} ne $name );
- $modules{$hash->{TYPE}}{defptr}{"C$camera"} = $hash;
- } elsif( ($a[2] eq "TAG" && @a == 5 ) ) {
- $subtype = "TAG";
- my $camera = $a[@a-2];
- my $tag = $a[@a-1];
- $hash->{Tag} = $tag;
- $hash->{Camera} = $camera;
- #$hash->{helper}{INTERVAL} = 60*15 if( !$hash->{helper}{INTERVAL} );
- my $d = $modules{$hash->{TYPE}}{defptr}{"G$tag"};
- return "tag $tag already defined as $d->{NAME}" if( defined($d) && $d->{NAME} ne $name );
- $modules{$hash->{TYPE}}{defptr}{"G$tag"} = $hash;
- } elsif( @a == 6 || ($a[2] eq "ACCOUNT" && @a == 7 ) ) {
- $subtype = "ACCOUNT";
- $hash->{model} = "ACCOUNT";
- $hash->{network} = "ok";
- delete($hash->{access_token});
- delete($hash->{access_token_app});
- delete($hash->{refresh_token});
- delete($hash->{refresh_token_app});
- delete($hash->{expires_at});
- delete($hash->{expires_at_app});
- delete($hash->{csrf_token});
- my $user = $a[@a-4];
- my $pass = $a[@a-3];
- my $username = netatmo_encrypt($user);
- my $password = netatmo_encrypt($pass);
- Log3 $name, 2, "$name: encrypt $user/$pass to $username/$password" if($user ne $username || $pass ne $password);
- my $client_id = $a[@a-2];
- my $client_secret = $a[@a-1];
- #$hash->{DEF} =~ s/$user/$username/g;
- #$hash->{DEF} =~ s/$pass/$password/g;
- $hash->{DEF} = "ACCOUNT $username $password $client_id $client_secret";
- $hash->{Clients} = ":netatmo:";
- $hash->{helper}{username} = $username;
- $hash->{helper}{password} = $password;
- $hash->{helper}{client_id} = $client_id;
- $hash->{helper}{client_secret} = $client_secret;
- $hash->{helper}{INTERVAL} = 60*60 if( !$hash->{helper}{INTERVAL} );
- $attr{$name}{room} = "netatmo" if( !defined($attr{$name}{room}) && defined($name));
- $modules{$hash->{TYPE}}{defptr}{"account"} = $hash;
- $hash->{helper}{apiserver} = AttrVal($name, "serverAPI", "api.netatmo.com");
- } else {
- return "Usage: define <name> netatmo device\
- define <name> netatmo userid publickey\
- define <name> netatmo PUBLIC latitude longitude [radius]\
- define <name> netatmo [ACCOUNT] username password" if(@a < 3 || @a > 5);
- }
- $hash->{NAME} = $name;
- $hash->{SUBTYPE} = $subtype;
- $hash->{STATE} = "Initialized" if( $hash->{SUBTYPE} eq "ACCOUNT" );
- $hash->{NOTIFYDEV} = "global";
- if(IsDisabled($name) || !defined($name)) {
- RemoveInternalTimer($hash);
- $hash->{STATE} = "Disabled";
- return undef;
- }
- if( $init_done ) {
- netatmo_connect($hash) if( $hash->{SUBTYPE} eq "ACCOUNT" );
- netatmo_initDevice($hash) if( $hash->{SUBTYPE} eq "DEVICE" );
- netatmo_initDevice($hash) if( $hash->{SUBTYPE} eq "MODULE" );
- netatmo_poll($hash) if( $hash->{SUBTYPE} eq "PUBLIC" );
- netatmo_poll($hash) if( $hash->{SUBTYPE} eq "FORECAST" );
- netatmo_initHome($hash) if( $hash->{SUBTYPE} eq "HOME" );
- netatmo_pingCamera($hash) if( $hash->{SUBTYPE} eq "CAMERA" );
- netatmo_poll($hash) if( $hash->{SUBTYPE} eq "RELAY" );
- netatmo_poll($hash) if( $hash->{SUBTYPE} eq "THERMOSTAT" );
- netatmo_initHeatingHome($hash) if( $hash->{SUBTYPE} eq "HEATINGHOME" );
- #netatmo_initHeatingRoom($hash) if( $hash->{SUBTYPE} eq "HEATINGROOM" );
- netatmo_addExtension($hash) if( $hash->{SUBTYPE} eq "WEBHOOK" );
- }
- else
- {
- InternalTimer(gettimeofday()+120, "netatmo_InitWait", $hash);
- }
- return undef;
- }
- sub netatmo_InitWait($) {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- Log3 "netatmo", 5, "netatmo: initwait ".$init_done;
- RemoveInternalTimer($hash);
- if( $init_done ) {
- netatmo_connect($hash) if( $hash->{SUBTYPE} eq "ACCOUNT" );
- netatmo_initDevice($hash) if( $hash->{SUBTYPE} eq "DEVICE" );
- netatmo_initDevice($hash) if( $hash->{SUBTYPE} eq "MODULE" );
- netatmo_poll($hash) if( $hash->{SUBTYPE} eq "PUBLIC" );
- netatmo_poll($hash) if( $hash->{SUBTYPE} eq "FORECAST" );
- netatmo_initHome($hash) if( $hash->{SUBTYPE} eq "HOME" );
- netatmo_pingCamera($hash) if( $hash->{SUBTYPE} eq "CAMERA" );
- netatmo_poll($hash) if( $hash->{SUBTYPE} eq "RELAY" );
- netatmo_poll($hash) if( $hash->{SUBTYPE} eq "THERMOSTAT" );
- netatmo_initHeatingHome($hash) if( $hash->{SUBTYPE} eq "HEATINGHOME" );
- #netatmo_initHeatingRoom($hash) if( $hash->{SUBTYPE} eq "HEATINGROOM" );
- netatmo_addExtension($hash) if( $hash->{SUBTYPE} eq "WEBHOOK" );
- }
- else
- {
- InternalTimer(gettimeofday()+120, "netatmo_InitWait", $hash);
- }
- return undef;
- }
- sub
- netatmo_Notify($$)
- {
- my ($hash,$dev) = @_;
- my $name = $hash->{NAME};
- return if($dev->{NAME} ne "global");
- return if(!grep(m/^INITIALIZED|REREADCFG$/, @{$dev->{CHANGED}}));
- RemoveInternalTimer($hash);
- if(IsDisabled($name) || !defined($name)) {
- RemoveInternalTimer($hash);
- $hash->{STATE} = "Disabled";
- return undef;
- }
- netatmo_connect($hash) if( $hash->{SUBTYPE} eq "ACCOUNT" );
- netatmo_initDevice($hash) if( $hash->{SUBTYPE} eq "DEVICE" );
- netatmo_initDevice($hash) if( $hash->{SUBTYPE} eq "MODULE" );
- netatmo_poll($hash) if( $hash->{SUBTYPE} eq "PUBLIC" );
- netatmo_poll($hash) if( $hash->{SUBTYPE} eq "FORECAST" );
- netatmo_initHome($hash) if( $hash->{SUBTYPE} eq "HOME" );
- netatmo_pingCamera($hash) if( $hash->{SUBTYPE} eq "CAMERA" );
- netatmo_poll($hash) if( $hash->{SUBTYPE} eq "RELAY" );
- netatmo_poll($hash) if( $hash->{SUBTYPE} eq "THERMOSTAT" );
- netatmo_initHeatingHome($hash) if( $hash->{SUBTYPE} eq "HEATINGHOME" );
- #netatmo_initHeatingRoom($hash) if( $hash->{SUBTYPE} eq "HEATINGROOM" );
- netatmo_addExtension($hash) if( $hash->{SUBTYPE} eq "WEBHOOK" );
- return undef;
- }
- sub
- netatmo_Undefine($$)
- {
- my ($hash, $arg) = @_;
- RemoveInternalTimer($hash);
- delete( $modules{$hash->{TYPE}}{defptr}{"D$hash->{Device}"} ) if( $hash->{SUBTYPE} eq "DEVICE" );
- delete( $modules{$hash->{TYPE}}{defptr}{"M$hash->{Module}"} ) if( $hash->{SUBTYPE} eq "MODULE" );
- delete( $modules{$hash->{TYPE}}{defptr}{$hash->{Lat}.$hash->{Lon}.$hash->{Rad}} ) if( $hash->{SUBTYPE} eq "PUBLIC" );
- delete( $modules{$hash->{TYPE}}{defptr}{"F$hash->{Station}"} ) if( $hash->{SUBTYPE} eq "FORECAST" );
- delete( $modules{$hash->{TYPE}}{defptr}{"H$hash->{Home}"} ) if( $hash->{SUBTYPE} eq "HOME" );
- delete( $modules{$hash->{TYPE}}{defptr}{"C$hash->{Camera}"} ) if( $hash->{SUBTYPE} eq "CAMERA" );
- delete( $modules{$hash->{TYPE}}{defptr}{"P$hash->{Person}"} ) if( $hash->{SUBTYPE} eq "PERSON" );
- delete( $modules{$hash->{TYPE}}{defptr}{"R$hash->{Relay}"} ) if( $hash->{SUBTYPE} eq "RELAY" );
- delete( $modules{$hash->{TYPE}}{defptr}{"T$hash->{Thermostat}"} ) if( $hash->{SUBTYPE} eq "THERMOSTAT" );
- delete( $modules{$hash->{TYPE}}{defptr}{"E$hash->{Home}"} ) if( $hash->{SUBTYPE} eq "HEATINGHOME" );
- delete( $modules{$hash->{TYPE}}{defptr}{"O$hash->{Room}"} ) if( $hash->{SUBTYPE} eq "HEATINGROOM" );
- netatmo_removeExtension($hash) if( $hash->{SUBTYPE} eq "WEBHOOK" );
- return undef;
- }
- sub
- netatmo_Set($$@)
- {
- my ($hash, $name, $cmd, @parameters) = @_;
- $hash->{SUBTYPE} = "unknown" if(!defined($hash->{SUBTYPE}));
- my $list = "";
- $list = "autocreate:noArg autocreate_homes:noArg autocreate_thermostats:noArg autocreate_homecoachs:noArg" if( $hash->{SUBTYPE} eq "ACCOUNT" );
- #$list .= " unban:noArg" if( $hash->{SUBTYPE} eq "ACCOUNT" );
- $list = "home:noArg away:noArg" if ($hash->{SUBTYPE} eq "PERSON");
- $list = "empty:noArg notify_movements:never,empty,always notify_unknowns:empty,always notify_animals:true,false record_animals:true,false record_movements:never,empty,always record_alarms:never,empty,always presence_record_humans:ignore,record,record_and_notify presence_record_vehicles:ignore,record,record_and_notify presence_record_animals:ignore,record,record_and_notify presence_record_movements:ignore,record,record_and_notify presence_record_alarms:ignore,record,record_and_notify gone_after presence_enable_notify_from_to:empty,always presence_notify_from presence_notify_to smart_notifs:on,off" if ($hash->{SUBTYPE} eq "HOME");
- $list = "enable disable irmode:auto,always,never led_on_live:on,off mirror:off,on audio:on,off" if ($hash->{SUBTYPE} eq "CAMERA");
- $list = "enable disable light_mode:auto,on,off floodlight intensity:slider,0,1,100 night_always:true,false night_person:true,false night_vehicle:true,false night_animal:true,false night_movement:true,false" if ($hash->{SUBTYPE} eq "CAMERA" && defined($hash->{model}) && $hash->{model} eq "NOC");
- $list = "calibrate:noArg" if ($hash->{SUBTYPE} eq "TAG");
- if ($hash->{SUBTYPE} eq "THERMOSTAT" || $hash->{SUBTYPE} eq "HEATINGROOM")
- {
- $list = "setpoint_mode:off,hg,away,program,manual,max setpoint_temp:5.0,5.5,6.0,6.5,7.0,7.5,8.0,8.5,9.0,9.5,10.0,10.5,11.0,11.5,12.0,12.5,13.0,13.5,14.0,14.5,15.0,15.5,16.0,16.5,17.0,17.5,18.0,18.5,19.0,19.5,20.0,20.5,21.0,21.5,22.0,22.5,23.0,23.5,24.0,24.5,25.0,25.5,26.0,26.5,27.0,27.5,28.0,28.5,29.0,29.5,30.0";
- $list = "setpoint_mode:off,hg,away,program,manual,max program:".$hash->{schedulenames}." setpoint_temp:5.0,5.5,6.0,6.5,7.0,7.5,8.0,8.5,9.0,9.5,10.0,10.5,11.0,11.5,12.0,12.5,13.0,13.5,14.0,14.5,15.0,15.5,16.0,16.5,17.0,17.5,18.0,18.5,19.0,19.5,20.0,20.5,21.0,21.5,22.0,22.5,23.0,23.5,24.0,24.5,25.0,25.5,26.0,26.5,27.0,27.5,28.0,28.5,29.0,29.5,30.0" if(defined($hash->{schedulenames}));
- }
- $list = "clear:noArg webhook:add,drop" if ($hash->{SUBTYPE} eq "WEBHOOK");
- return undef if( $list eq "" );
- if( $cmd eq "autocreate" ) {
- return netatmo_autocreate($hash, 1 );
- return undef;
- }
- elsif( $cmd eq "autocreate_homes" ) {
- return netatmo_autocreatehome($hash, 1 );
- return undef;
- }
- elsif( $cmd eq "autocreate_thermostats" ) {
- return netatmo_autocreatethermostat($hash, 1 );
- return undef;
- }
- elsif( $cmd eq "autocreate_homecoachs" ) {
- return netatmo_autocreatehomecoach($hash, 1 );
- return undef;
- }
- elsif( $cmd eq "home" ) {
- return netatmo_setPresence($hash, "home");
- return undef;
- }
- elsif( $cmd eq "away" ) {
- return netatmo_setPresence($hash, "away");
- return undef;
- }
- elsif( $cmd eq "empty" ) {
- return netatmo_setPresence($hash, "empty");
- return undef;
- }
- elsif( $cmd =~ /^notify_/ || $cmd =~ /^record_/ || $cmd =~ /^presence_/ || $cmd eq "gone_after" || $cmd eq "smart_notifs" ) {
- return netatmo_setNotifications($hash, $cmd, $parameters[0]);
- return undef;
- }
- elsif( $cmd eq "enable" ) {
- my $pin = $parameters[0];
- $pin = "0000" if(!defined($pin) || length($pin) != 4);
- return netatmo_setCamera($hash, "on", $pin);
- return undef;
- }
- elsif( $cmd eq "disable" ) {
- my $pin = $parameters[0];
- $pin = "0000" if(!defined($pin) || length($pin) != 4);
- $hash->{pin} = $pin;
- return netatmo_setCamera($hash, "off", $pin);
- return undef;
- }
- elsif( $cmd eq "irmode" || $cmd eq "led_on_live" || $cmd eq "mirror" || $cmd eq "audio" ) {
- my $setting = $parameters[0];
- return "You have to define a value" if(!defined($setting) || $setting eq "");
- readingsSingleUpdate($hash, $cmd, $setting, 1);
- return netatmo_setCameraSetting($hash, $cmd, $setting);
- return undef;
- }
- elsif( $cmd eq "light_mode" ) {
- my $setting = $parameters[0];
- return "You have to define a value" if(!defined($setting) || $setting eq "");
- return netatmo_setFloodlight($hash, $setting);
- return undef;
- }
- elsif( $cmd eq "floodlight" ) {
- my $setting = $parameters[0];
- $setting = 100 if(!defined($setting) || $setting eq "");
- $setting = int($setting);
- return netatmo_setIntensity($hash, $setting);
- return undef;
- }
- elsif( $cmd eq "intensity" || $cmd eq "night_always" || $cmd eq "night_person" || $cmd eq "night_vehicle" || $cmd eq "night_animal" || $cmd eq "night_movement" ) {
- my $setting = $parameters[0];
- return "You have to define a value" if(!defined($setting) || $setting eq "");
- readingsSingleUpdate($hash, $cmd, $setting, 1);
- return netatmo_setPresenceConfig($hash, $setting);
- return undef;
- }
- elsif( $cmd eq "calibrate" ) {
- return netatmo_setTagCalibration($hash, $cmd);
- return undef;
- }
- elsif( $cmd eq "setpoint_mode" ) {
- my $setting = $parameters[0];
- my $duration = $parameters[1];
- return "You have to define a mode" if(!defined($setting) || $setting eq "");
- return netatmo_setRoomMode($hash,$setting,$duration) if($hash->{SUBTYPE} eq "HEATINGROOM");
- return netatmo_setThermostatMode($hash,$setting,$duration);
- return undef;
- }
- elsif( $cmd eq "setpoint_temp" ) {
- my $setting = $parameters[0];
- my $duration = $parameters[1];
- return "You have to define a temperature" if(!defined($setting) || $setting eq "");
- return netatmo_setRoomTemp($hash,$setting,$duration) if($hash->{SUBTYPE} eq "HEATINGROOM");
- return netatmo_setThermostatTemp($hash,$setting,$duration);
- return undef;
- }
- elsif( $cmd eq "program" ) {
- my $setting = $parameters[0];
- return "You have to define a program" if(!defined($setting) || $setting eq "");
- return netatmo_setThermostatProgram($hash,$setting);
- return undef;
- }
- elsif( $cmd eq "clear" ) {
- delete $hash->{READINGS};
- return undef;
- }
- elsif( $cmd eq "webhook" ) {
- if($parameters[0] eq "drop")
- {
- netatmo_dropWebhook($hash);
- } else {
- netatmo_registerWebhook($hash);
- }
- return undef;
- }
- if( $cmd eq 'unban' )# unban:noArg
- {
- return netatmo_Unban($hash);
- }
- return "Unknown argument $cmd, choose one of $list";
- }
- sub
- netatmo_getToken($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- return Log3 $name, 1, "$name: No client id was found! (getToken)" if(!defined($hash->{helper}{client_id}));
- return Log3 $name, 1, "$name: No client secret was found! (getToken)" if(!defined($hash->{helper}{client_secret}));
- return Log3 $name, 1, "$name: No username was found! (getToken)" if(!defined($hash->{helper}{username}));
- return Log3 $name, 1, "$name: No password was found! (getToken)" if(!defined($hash->{helper}{password}));
- my($err,$data) = HttpUtils_BlockingGet({
- url => "https://".$hash->{helper}{apiserver}."/oauth2/token",
- timeout => 5,
- noshutdown => 1,
- data => {grant_type => 'password', client_id => $hash->{helper}{client_id}, client_secret=> $hash->{helper}{client_secret}, username => netatmo_decrypt($hash->{helper}{username}), password => netatmo_decrypt($hash->{helper}{password}), scope => 'read_station read_thermostat write_thermostat read_camera write_camera access_camera read_presence write_presence access_presence read_homecoach'},
- });
- netatmo_dispatch( {hash=>$hash,type=>'token'},$err,$data );
- }
- sub
- netatmo_getAppToken($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- return Log3 $name, 1, "$name: No username was found! (getAppToken)" if(!defined($hash->{helper}{username}));
- return Log3 $name, 1, "$name: No password was found! (getAppToken)" if(!defined($hash->{helper}{password}));
- #my $auth = "QXV0aG9yaXphdGlvbjogQmFzaWMgYm1GZlkyeHBaVzUwWDJsdmMxOTNaV3hqYjIxbE9qaGhZalU0TkdRMk1tTmhNbUUzTjJVek4yTmpZelppTW1NM1pUUm1Namxs";
- my $auth = "QXV0aG9yaXphdGlvbjogQmFzaWMgYm1GZlkyeHBaVzUwWDJsdmN6bzFObU5qTmpSaU56azBOak5oT1RrMU9HSTNOREF4TkRjeVpEbGxNREUxT0E9PQ==";
- $auth = decode_base64($auth);
- my($err,$data) = HttpUtils_BlockingGet({
- url => "https://app.netatmo.net/oauth2/token",
- method => "POST",
- timeout => 5,
- noshutdown => 1,
- header => "$auth",
- data => {app_identifier=>'com.netatmo.camera', grant_type => 'password', password => netatmo_decrypt($hash->{helper}{password}), scope => 'write_camera read_camera access_camera read_presence write_presence access_presence read_station', username => netatmo_decrypt($hash->{helper}{username})},
- });
- netatmo_dispatch( {hash=>$hash,type=>'apptoken'},$err,$data );
- }
- sub
- netatmo_refreshToken($;$)
- {
- my ($hash,$nonblocking) = @_;
- my $name = $hash->{NAME};
- if( defined($hash->{access_token}) && defined($hash->{expires_at}) ) {
- my ($seconds) = gettimeofday();
- return undef if( $seconds < $hash->{expires_at} - 300 );
- }
- Log3 $name, 3, "$name: refreshing token";
-
- my $resolve = inet_aton($hash->{helper}{apiserver});
- if(!defined($resolve))
- {
- $hash->{STATE} = "DNS error";
- $hash->{network} = "dns" if($hash->{SUBTYPE} eq "ACCOUNT");
- delete($hash->{access_token});
- delete($hash->{access_token_app});
- InternalTimer( gettimeofday() + 1800, "netatmo_refreshTokenTimer", $hash);
- Log3 $name, 1, "$name: DNS error, cannot resolve ".$hash->{helper}{apiserver};
- return undef;
- } else {
- $hash->{network} = "ok";
- }
- if( !$hash->{refresh_token} ) {
- netatmo_getToken($hash);
- return undef;
- }
- if( $nonblocking ) {
- HttpUtils_NonblockingGet({
- url => "https://".$hash->{helper}{apiserver}."/oauth2/token",
- timeout => 30,
- noshutdown => 1,
- data => {grant_type => 'refresh_token', client_id => $hash->{helper}{client_id}, client_secret=> $hash->{helper}{client_secret}, refresh_token => $hash->{refresh_token}},
- hash => $hash,
- type => 'token',
- callback => \&netatmo_dispatch,
- });
- } else {
- my($err,$data) = HttpUtils_BlockingGet({
- url => "https://".$hash->{helper}{apiserver}."/oauth2/token",
- timeout => 5,
- noshutdown => 1,
- data => {grant_type => 'refresh_token', client_id => $hash->{helper}{client_id}, client_secret=> $hash->{helper}{client_secret}, refresh_token => $hash->{refresh_token}},
- });
- netatmo_dispatch( {hash=>$hash,type=>'token'},$err,$data );
- }
- }
- sub
- netatmo_refreshAppToken($;$)
- {
- my ($hash,$nonblocking) = @_;
- my $name = $hash->{NAME};
- if($hash->{network} eq "dns")
- {
- Log3 $name, 2, "$name: app token dns error, update postponed!";
- InternalTimer( gettimeofday() + 600, "netatmo_refreshAppTokenTimer", $hash);
- return undef;
- }
- if( defined($hash->{access_token_app}) && defined($hash->{expires_at_app}) ) {
- my ($seconds) = gettimeofday();
- return undef if( $seconds < $hash->{expires_at_app} - 300 );
- } elsif( !defined($hash->{refresh_token_app}) ) {
- Log3 $name, 2, "$name: missing app refresh token!";
- netatmo_getAppToken($hash);
- return undef;
- }
- delete($hash->{csrf_token});
- Log3 $name, 3, "$name: refreshing app token";
- my $auth = "QXV0aG9yaXphdGlvbjogQmFzaWMgYm1GZlkyeHBaVzUwWDJsdmN6bzFObU5qTmpSaU56azBOak5oT1RrMU9HSTNOREF4TkRjeVpEbGxNREUxT0E9PQ==";
- $auth = decode_base64($auth);
- if( $nonblocking ) {
- HttpUtils_NonblockingGet({
- url => "https://app.netatmo.net/oauth2/token",
- timeout => 30,
- noshutdown => 1,
- header => "$auth",
- data => {grant_type => 'refresh_token', refresh_token => $hash->{refresh_token_app}},
- hash => $hash,
- type => 'apptoken',
- callback => \&netatmo_dispatch,
- });
- } else {
- my($err,$data) = HttpUtils_BlockingGet({
- url => "https://app.netatmo.net/oauth2/token",
- timeout => 5,
- noshutdown => 1,
- header => "$auth",
- data => {grant_type => 'refresh_token', refresh_token => $hash->{refresh_token_app}},
- });
- netatmo_dispatch( {hash=>$hash,type=>'apptoken'},$err,$data );
- }
- }
- sub
- netatmo_refreshTokenTimer($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- if(!defined($name))
- {
- Log3 "netatmo", 1, "error ".Dumper($hash);
- return undef;
- }
- Log3 $name, 5, "$name: refreshing token (timer)";
- netatmo_refreshToken($hash, 1);
- }
- sub
- netatmo_refreshAppTokenTimer($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 5, "$name: refreshing app token (timer)";
- netatmo_refreshAppToken($hash, 1);
- }
- sub
- netatmo_checkConnection($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- return undef if($hash->{network} eq "ok");
- return undef if(!defined($hash->{access_token}));
- Log3 $name, 3, "$name: refreshing connection information";
- my $json = '{"limit":2,"divider":3,"zoom":18,"lat_ne":0.1,"lon_ne":-0.1,"lat_sw":0.1,"lon_sw":-0.1,"date_end":"last","quality":1}';
- HttpUtils_NonblockingGet({
- url => "https://".$hash->{helper}{apiserver}."/api/getpublicmeasures",
- method => "POST",
- timeout => 30,
- header => "Content-Type: application/json\r\nAuthorization: Bearer ".$hash->{access_token},
- hash => $hash,
- data => $json,
- callback => \&netatmo_parseConnection,
- });
- return undef;
- }
- sub
- netatmo_parseConnection($$$)
- {
- my ($param,$err,$data) = @_;
- my $hash = $param->{hash};
- my $name = $hash->{NAME};
- if( $err ) {
- Log3 $name, 1, "$name: connection check failed: $err";
- if($err =~ /refused/ ){
- RemoveInternalTimer($hash);
- $hash->{status} = "banned";
- $hash->{network} = "banned";
- }
- elsif($err =~ /Bad hostname/ || $err =~ /gethostbyname/){
- $hash->{status} = "timeout";
- $hash->{network} = "dns";
- }
- elsif($err =~ /timed out/){
- $hash->{status} = "timeout";
- $hash->{network} = "timeout";
- }
- elsif($err =~ /Can't connect/){
- $hash->{status} = "timeout";
- $hash->{network} = "disconnected";
- }
-
- return undef;
- } elsif( $data ) {
- $data =~ s/\n//g;
- if( $data !~ m/^{.*}$/ ) {
- Log3 $name, 2, "$name: invalid json on connection check";
- return undef;
- }
- my $json = eval { JSON->new->utf8(0)->decode($data) };
- if($@)
- {
- Log3 $name, 2, "$name: invalid json evaluation on connection check ".$@;
- return undef;
- }
- if(!defined($json->{status})) {
- Log3 $name, 2, "$name: invalid json data on connection check: \n".$data;
- } else {
- Log3 $name, 4, "$name: connection check: \n".$data;
- $hash->{network} = "ok" if($json->{status} eq "ok");
- }
- }
- return undef;
- }
- sub
- netatmo_connect($)
- {
- my ($hash) = @_;
- netatmo_getToken($hash);
- #netatmo_getAppToken($hash);
- InternalTimer(gettimeofday()+90, "netatmo_poll", $hash);
- }
- sub
- netatmo_Unban($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- HttpUtils_NonblockingGet({
- url => "https://dev.netatmo.com/",
- timeout => 20,
- noshutdown => 1,
- hash => $hash,
- type => 'unban',
- callback => \&netatmo_parseUnban,
- });
- return undef;
- }
- sub
- netatmo_parseUnban($$$)
- {
- my ($param,$err,$data) = @_;
- my $hash = $param->{hash};
- my $name = $hash->{NAME};
-
- #Log3 $name, 1, "$name unban\n".Dumper($param->{httpheader});
- $data =~ /csrf_value: "(.*)"/;
- my $csrf_token = $1;
- # https://auth.netatmo.com/en-US/access/login?next_url=https://dev.netatmo.com/dev/myaccount
- Log3 $name, 1, "$name unban ".$csrf_token;
- HttpUtils_NonblockingGet({
- url => "https://auth.netatmo.com/en-US/access/login?next_url=https://dev.netatmo.com/dev/myaccount",
- timeout => 30,
- hash => $hash,
- ignoreredirects => 1,
- type => 'unban',
- header => "Cookie: netatmocomci_csrf_cookie_na=".$csrf_token."; netatmocomlocale=en-US",
- data => {ci_csrf_netatmo => $csrf_token, mail => netatmo_decrypt($hash->{helper}{username}), pass => netatmo_decrypt($hash->{helper}{password}), log_submit => 'Log+in', stay_logged => 'accept'},
- callback => \&netatmo_parseUnban2,
- });
-
- return undef;
- }
- sub
- netatmo_parseUnban2($$$)
- {
- my ($param,$err,$data) = @_;
- my $hash = $param->{hash};
- my $name = $hash->{NAME};
-
- Log3 $name, 1, "$name header\n".Dumper($param->{httpheader});
- my $header1 = $param->{httpheader};
- my $header2 = $param->{httpheader};
- my $header3 = $param->{httpheader};
-
- $header1 =~ s/=deleted/x=deleted/g;
- $header2 =~ s/=deleted/x=deleted/g;
- $header3 =~ s/=deleted/x=deleted/g;
-
- $header1 =~ /Set-Cookie: netatmocomci_csrf_cookie_na=(.*); expires/;
- my $csrf_token = $1;
- $hash->{helper}{csrf_token} = $csrf_token;
- $header2 =~ /Set-Cookie: netatmocomaccess_token=(.*); path/;
- my $accesstoken = $1;
- $accesstoken =~ s/%7C/|/g;
- $hash->{helper}{access_token} = $accesstoken;
- $header3 =~ /Set-Cookie: netatmocomrefresh_token=(.*); expires/;
- my $refreshtoken = $1;
- $hash->{helper}{refresh_token} = $refreshtoken;
- Log3 $name, 1, "$name csrftoken ".$csrf_token;
- Log3 $name, 1, "$name accesstoken ".$accesstoken;
- Log3 $name, 1, "$name refreshtoken ".$refreshtoken;
- my $json = '{"application_id":"'.$hash->{helper}{client_id}.'"}';
- HttpUtils_NonblockingGet({
- url => "https://dev.netatmo.com/api/unbanapp",
- timeout => 30,
- hash => $hash,
- type => 'unban',
- header => "Referer: https://dev.netatmo.com/dev/myaccount\r\nAuthorization: Bearer ".$accesstoken."\r\nContent-Type: application/json;charset=utf-8\r\nCookie: netatmocomci_csrf_cookie_na=".$csrf_token."; netatmocomlocale=en-US; netatmocomacces_token=".$accesstoken,
- data => $json,
- callback => \&netatmo_parseUnban3,
- });
-
- return undef;
- }
- sub
- netatmo_parseUnban3($$$)
- {
- my ($param,$err,$data) = @_;
- my $hash = $param->{hash};
- my $name = $hash->{NAME};
-
- Log3 $name, 1, "$name header\n".Dumper($param->{httpheader});
- Log3 $name, 1, "$name data\n".Dumper($data);
- Log3 $name, 1, "$name err\n".Dumper($err);
- return undef;
- }
- sub
- netatmo_initDevice($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- AssignIoPort($hash);
- if(defined($hash->{IODev}->{NAME})) {
- Log3 $name, 3, "$name: I/O device is " . $hash->{IODev}->{NAME};
- } else {
- Log3 $name, 1, "$name: no I/O device";
- }
- if(IsDisabled($name) || !defined($name)) {
- RemoveInternalTimer($hash);
- #$hash->{STATE} = "Disabled";
- readingsSingleUpdate($hash, "active", "disabled", 1);
- return undef;
- }
- my $device;
- if( $hash->{Module} ) {
- $device = netatmo_getDeviceDetail( $hash, $hash->{Module} );
- } else {
- $device = netatmo_getDeviceDetail( $hash, $hash->{Device} );
- }
- $hash->{stationName} = encode_utf8($device->{station_name}) if( $device->{station_name} );
- $hash->{moduleName} = encode_utf8($device->{module_name}) if( $device->{module_name} );
- $hash->{name} = encode_utf8($device->{name}) if( $device->{name} );
- $hash->{model} = $device->{type} if(defined($device->{type}));
- $hash->{firmware} = $device->{firmware} if(defined($device->{firmware}));
- $hash->{co2_calibrating} = $device->{co2_calibrating} if(defined($device->{co2_calibrating}));
- $hash->{last_upgrade} = FmtDateTime($device->{last_upgrade}) if(defined($device->{last_upgrade}));
- $hash->{date_setup} = FmtDateTime($device->{date_setup}) if(defined($device->{date_setup}));
- $hash->{last_setup} = FmtDateTime($device->{last_setup}) if(defined($device->{last_setup}));
- $hash->{last_status_store} = FmtDateTime($device->{last_status_store}) if(defined($device->{last_status_store}));
- $hash->{helper}{last_status_store} = $device->{last_status_store} if(defined($device->{last_status_store}) && $device->{last_status_store} > $hash->{helper}{last_status_store});
- $hash->{last_message} = FmtDateTime($device->{last_message}) if(defined($device->{last_message}));
- $hash->{last_seen} = FmtDateTime($device->{last_seen}) if(defined($device->{last_seen}));
- $hash->{wifi_status} = $device->{wifi_status} if(defined($device->{wifi_status}));
- $hash->{rf_status} = $device->{rf_status} if(defined($device->{rf_status}));
- #$hash->{battery_percent} = $device->{battery_percent} if(defined($device->{battery_percent}));
- #$hash->{battery_vp} = $device->{battery_vp} if(defined($device->{battery_vp}));
- if( $device->{place} ) {
- $hash->{country} = $device->{place}{country};
- $hash->{bssid} = $device->{place}{bssid} if(defined($device->{place}{bssid}));
- $hash->{altitude} = $device->{place}{altitude} if(defined($device->{place}{altitude}));
- $hash->{city} = encode_utf8($device->{place}{geoip_city}) if(defined($device->{place}{geoip_city}));
- $hash->{city} = encode_utf8($device->{place}{city}) if(defined($device->{place}{city}));;
- $hash->{location} = $device->{place}{location}[1] .",". $device->{place}{location}[0];
- }
- readingsSingleUpdate($hash, "batteryState", ($device->{battery_percent} > 20) ? "ok" : "low", 1) if(defined($device->{battery_percent}));
- readingsSingleUpdate($hash, "batteryPercent", $device->{battery_percent}, 1) if(defined($device->{battery_percent}));
- readingsSingleUpdate($hash, "batteryVoltage", $device->{battery_vp}/1000, 1) if(defined($device->{battery_vp}));
- my $state_format;
- if( $device->{data_type} ) {
- my $newdatatypes = "";
- my @reading_names = ();
- foreach my $type (@{$device->{data_type}}) {
- $newdatatypes = "" if ( !defined($newdatatypes) );
- $newdatatypes .= "," if ( $newdatatypes );
- $type = "WindStrength,WindAngle,GustStrength,GustAngle" if($type eq "Wind");
- $newdatatypes .= $type;
- push @reading_names, lc($type);
- if( $type eq "Temperature" ) {
- $state_format .= " " if( $state_format );
- $state_format .= "T: temperature";
- } elsif( $type eq "Humidity" ) {
- $state_format .= " " if( $state_format );
- $state_format .= "H: humidity";
- }
- }
- if($newdatatypes ne "")
- {
- delete($hash->{dataTypes});
- $hash->{dataTypes} = $newdatatypes;
- }
- $hash->{helper}{readingNames} = \@reading_names;
- }
- $attr{$name}{stateFormat} = $state_format if( !defined($attr{$name}{stateFormat}) && defined($state_format) && defined($name) );
- if(IsDisabled($name) || !defined($name)) {
- RemoveInternalTimer($hash);
- #$hash->{STATE} = "Disabled";
- readingsSingleUpdate($hash, "active", "disabled", 1);
- return undef;
- }
- InternalTimer(gettimeofday()+90, "netatmo_poll", $hash);
- }
- sub
- netatmo_getDevices($;$)
- {
- my ($hash,$blocking) = @_;
- my $name = $hash->{NAME};
- netatmo_refreshToken($hash, defined($hash->{access_token}));
- Log3 $name, 3, "$name getDevices (devicelist)";
- return Log3 $name, 1, "$name: No access token was found! (getDevices)" if(!defined($hash->{access_token}));
-
- if( $blocking ) {
- my($err,$data) = HttpUtils_BlockingGet({
- url => "https://".$hash->{helper}{apiserver}."/api/getstationsdata",
- timeout => 5,
- noshutdown => 1,
- data => { access_token => $hash->{access_token}, },
- });
- netatmo_dispatch( {hash=>$hash,type=>'devicelist'},$err,$data );
- return $hash->{helper}{devices};
- } else {
- HttpUtils_NonblockingGet({
- url => "https://".$hash->{helper}{apiserver}."/api/getstationsdata",
- timeout => 30,
- noshutdown => 1,
- data => { access_token => $hash->{access_token}, },
- hash => $hash,
- type => 'devicelist',
- callback => \&netatmo_dispatch,
- });
- }
- }
- sub
- netatmo_getHomes($;$)
- {
- my ($hash,$blocking) = @_;
- my $name = $hash->{NAME};
- netatmo_refreshToken($hash, defined($hash->{access_token}));
- Log3 $name, 3, "$name getHomes (homelist)";
- return Log3 $name, 1, "$name: No access token was found! (getHomes)" if(!defined($hash->{access_token}));
- if( $blocking ) {
- my($err,$data) = HttpUtils_BlockingGet({
- url => "https://".$hash->{helper}{apiserver}."/api/gethomedata",
- timeout => 5,
- noshutdown => 1,
- data => { access_token => $hash->{access_token}, },
- });
- netatmo_dispatch( {hash=>$hash,type=>'homelist'},$err,$data );
- return $hash->{helper}{homes};
- } else {
- HttpUtils_NonblockingGet({
- url => "https://".$hash->{helper}{apiserver}."/api/gethomedata",
- timeout => 30,
- noshutdown => 1,
- data => { access_token => $hash->{access_token}, },
- hash => $hash,
- type => 'homelist',
- callback => \&netatmo_dispatch,
- });
- }
- }
- sub
- netatmo_getThermostats($;$)
- {
- my ($hash,$blocking) = @_;
- my $name = $hash->{NAME};
- netatmo_refreshToken($hash, defined($hash->{access_token}));
- Log3 $name, 3, "$name getThermostats (thermostatlist)";
- return Log3 $name, 1, "$name: No access token was found! (getThermostats)" if(!defined($hash->{access_token}));
- # url => "https://".$hash->{helper}{apiserver}."/api/getthermostatsdata",
- if( $blocking ) {
- my($err,$data) = HttpUtils_BlockingGet({
- url => "https://".$hash->{helper}{apiserver}."/api/gethomesdata",
- timeout => 5,
- noshutdown => 1,
- data => { access_token => $hash->{access_token}, },
- });
- netatmo_dispatch( {hash=>$hash,type=>'thermostatlist'},$err,$data );
- return $hash->{helper}{thermostats};
- } else {
- HttpUtils_NonblockingGet({
- url => "https://".$hash->{helper}{apiserver}."/api/gethomesdata",
- timeout => 30,
- noshutdown => 1,
- data => { access_token => $hash->{access_token}, },
- hash => $hash,
- type => 'thermostatlist',
- callback => \&netatmo_dispatch,
- });
- }
- }
- sub
- netatmo_getHomecoachs($;$)
- {
- my ($hash,$blocking) = @_;
- my $name = $hash->{NAME};
- netatmo_refreshToken($hash, defined($hash->{access_token}));
- Log3 $name, 3, "$name getHomecoachs (homecoachlist)";
- return Log3 $name, 1, "$name: No access token was found! (getHomecoachs)" if(!defined($hash->{access_token}));
- if( $blocking ) {
- my($err,$data) = HttpUtils_BlockingGet({
- url => "https://".$hash->{helper}{apiserver}."/api/gethomecoachsdata",
- timeout => 5,
- noshutdown => 1,
- data => { access_token => $hash->{access_token}, },
- });
- netatmo_dispatch( {hash=>$hash,type=>'homecoachlist'},$err,$data );
- return $hash->{helper}{homecoachs};
- } else {
- HttpUtils_NonblockingGet({
- url => "https://".$hash->{helper}{apiserver}."/api/gethomecoachsdata",
- timeout => 30,
- noshutdown => 1,
- data => { access_token => $hash->{access_token}, },
- hash => $hash,
- type => 'homecoachlist',
- callback => \&netatmo_dispatch,
- });
- }
- }
- sub
- netatmo_pingCamera($;$)
- {
- my ($hash,$blocking) = @_;
- my $name = $hash->{NAME};
- my $iohash = $hash->{IODev};
- netatmo_refreshToken($iohash, defined($iohash->{access_token}));
- return Log3 $name, 1, "$name: No access token was found! (pingCamera)" if(!defined($iohash->{access_token}));
- my $pingurl = ReadingsVal( $name, "vpn_url", undef );
- return undef if(!defined($pingurl));
- Log3 $name, 3, "$name pingCamera (cameraping)";
- $pingurl .= "/command/ping";
- Log3 $name, 5, "$name pingCamera ".$pingurl;
- if( $blocking ) {
- my($err,$data) = HttpUtils_BlockingGet({
- url => $pingurl,
- timeout => 10,
- sslargs => { SSL_hostname => '', },
- data => { access_token => $iohash->{access_token}, },
- });
- netatmo_dispatch( {hash=>$hash,type=>'cameraping'},$err,$data );
- return undef;
- } else {
- HttpUtils_NonblockingGet({
- url => $pingurl,
- timeout => 30,
- sslargs => { SSL_hostname => '', },
- data => { access_token => $iohash->{access_token}, },
- hash => $hash,
- type => 'cameraping',
- callback => \&netatmo_dispatch,
- });
- }
- }
- sub
- netatmo_getCameraVideo($$;$)
- {
- my ($hash,$videoid,$local) = @_;
- my $name = $hash->{NAME};
- $local = ($local eq "video_local" ? "_local" : "");
- #my $iohash = $hash->{IODev};
- #netatmo_refreshToken($iohash, defined($iohash->{access_token}));
- my $commandurl = ReadingsVal( $name, "local_url", undef);
- if(!defined($commandurl)) {
- ReadingsVal( $name, "vpn_url", undef );
- } else {
- $local = "";
- }
- return undef if(!defined($commandurl));
- my $quality = AttrVal($name,"videoquality","medium");
- $commandurl .= "/vod/".$videoid."/files/".$quality."/index".$local.".m3u8";
- Log3 $name, 3, "$name getCameraVideo ".$commandurl;
- # HttpUtils_BlockingGet({
- # url => $cmdurl,
- # noshutdown => 1,
- # data => { access_token => $iohash->{access_token}, },
- # hash => $hash,
- # type => 'cameravideo',
- # callback => \&netatmo_dispatch,
- # });
- return $commandurl;
- }
- sub
- netatmo_getCameraLive($;$)
- {
- my ($hash,$local) = @_;
- my $name = $hash->{NAME};
- $local = ($local eq "live_local" ? "_local" : "");
- #my $iohash = $hash->{IODev};
- #netatmo_refreshToken($iohash, defined($iohash->{access_token}));
- my $commandurl = ReadingsVal( $name, "local_url", undef);
- if(!defined($commandurl)) {
- ReadingsVal( $name, "vpn_url", undef );
- } else {
- $local = "";
- }
- return undef if(!defined($commandurl));
- my $quality = AttrVal($name,"videoquality","medium");
- $commandurl .= "/live/files/".$quality."/index".$local.".m3u8";
- Log3 $name, 3, "$name getCameraLive ".$commandurl;
- # HttpUtils_BlockingGet({
- # url => $cmdurl,
- # noshutdown => 1,
- # data => { access_token => $iohash->{access_token}, },
- # hash => $hash,
- # type => 'cameravideo',
- # callback => \&netatmo_dispatch,
- # });
- return $commandurl;
- }
- sub
- netatmo_getCameraTimelapse($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- #my $iohash = $hash->{IODev};
- #netatmo_refreshToken($iohash, defined($iohash->{access_token}));
- my $cmdurl = ReadingsVal( $name, "local_url", undef );
- return undef if(!defined($cmdurl));
- $cmdurl .= "/command/dl/timelapse";
- Log3 $name, 3, "$name getCameraTimelapse ".$cmdurl;
- # HttpUtils_BlockingGet({
- # url => $cmdurl,
- # noshutdown => 1,
- # data => { access_token => $iohash->{access_token}, },
- # hash => $hash,
- # type => 'cameravideo',
- # callback => \&netatmo_dispatch,
- # });
- return $cmdurl;
- }
- sub
- netatmo_getCameraSnapshot($;$)
- {
- my ($hash,$local) = @_;
- my $name = $hash->{NAME};
- #my $iohash = $hash->{IODev};
- #netatmo_refreshToken($iohash, defined($iohash->{access_token}));
- my $commandurl = ReadingsVal( $name, "local_url", ReadingsVal( $name, "vpn_url", undef ) );
- return undef if(!defined($commandurl));
- $commandurl .= "/live/snapshot_720.jpg";
- Log3 $name, 3, "$name getCameraSnapshot ".$commandurl;
- # HttpUtils_BlockingGet({
- # url => $cmdurl,
- # noshutdown => 1,
- # data => { access_token => $iohash->{access_token}, },
- # hash => $hash,
- # type => 'cameravideo',
- # callback => \&netatmo_dispatch,
- # });
- return $commandurl;
- }
- sub
- netatmo_getEvents($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- my $iohash = $hash->{IODev};
- netatmo_refreshToken($iohash, defined($iohash->{access_token}));
- Log3 $name, 3, "$name getEvents (homeevents)";
- return Log3 $name, 1, "$name: No access token was found! (getEvents)" if(!defined($iohash->{access_token}));
- HttpUtils_NonblockingGet({
- url => "https://".$iohash->{helper}{apiserver}."/api/getnextevents",
- timeout => 60,
- noshutdown => 1,
- data => { access_token => $iohash->{access_token}, home_id => $hash->{Home}, event_id => $hash->{lastevent}, },
- hash => $hash,
- type => 'homeevents',
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_getPublicDevices($$;$$$$)
- {
- my ($hash,$blocking,$lat1,$lon1,$lat2,$lon2) = @_;
- my $name = $hash->{NAME};
- my $iohash = $hash->{IODev};
- $iohash = $hash if( !defined($iohash) );
- #Log3 $name, 5, "$name getPublicDevices $lat1,$lon1,$lat2,$lon2";
- if( !defined($lon1) ) {
- my $s = $lat1;
- $s = 0.025 if ( !defined($s) );
- my $lat = AttrVal("global","latitude", 50.112);
- my $lon = AttrVal("global","longitude", 8.686);
- $lat1 = $lat + $s;
- $lon1 = $lon + $s;
- $lat2 = $lat - $s;
- $lon2 = $lon - $s;
- } elsif( !defined($lon2) ) {
- my $lat = $lat1;
- my $lon = $lon1;
- my $s = $lat2;
- $s = 0.025 if ( !defined($s) );
- $lat1 = $lat + $s;
- $lon1 = $lon + $s;
- $lat2 = $lat - $s;
- $lon2 = $lon - $s;
- }
- my $lat_ne = ($lat1 > $lat2) ? $lat1 : $lat2;
- my $lon_ne = ($lon1 > $lon2) ? $lon1 : $lon2;
- my $lat_sw = ($lat1 > $lat2) ? $lat2 : $lat1;
- my $lon_sw = ($lon1 > $lon2) ? $lon2 : $lon1;
- Log3 $name, 3, "$name getPublicDevices ($lat_ne,$lon_ne / $lat_sw,$lon_sw)";
- netatmo_refreshToken($iohash, defined($iohash->{access_token}));
- return Log3 $name, 1, "$name: No access token was found! (getPublicDevices)" if(!defined($iohash->{access_token}));
- if( $blocking ) {
- my($err,$data) = HttpUtils_BlockingGet({
- url => "https://".$iohash->{helper}{apiserver}."/api/getpublicmeasures",
- timeout => 10,
- noshutdown => 1,
- data => { access_token => $iohash->{access_token}, lat_ne => $lat_ne, lon_ne => $lon_ne, lat_sw => $lat_sw, lon_sw => $lon_sw, quality => 1, divider => 8, limit => 4, zoom => 10, date_end => 'last' },
- });
- return netatmo_dispatch( {hash=>$hash,type=>'publicdata'},$err,$data );
- } else {
- HttpUtils_NonblockingGet({
- url => "https://".$iohash->{helper}{apiserver}."/api/getpublicmeasures",
- timeout => 60,
- noshutdown => 1,
- data => { access_token => $iohash->{access_token}, lat_ne => $lat_ne, lon_ne => $lon_ne, lat_sw => $lat_sw, lon_sw => $lon_sw, quality => 1, divider => 8, limit => 4, zoom => 10, date_end => 'last' },
- hash => $hash,
- type => 'publicdata',
- callback => \&netatmo_dispatch,
- });
- }
- }
- sub
- netatmo_getAddress($$$$)
- {
- my ($hash,$blocking,$lat,$lon) = @_;
- my $name = $hash->{NAME};
- my $iohash = $hash->{IODev};
- $iohash = $hash if( !defined($iohash) );
- Log3 $name, 5, "$name getAddress ($lat,$lon)";
- if( $blocking ) {
- my($err,$data) = HttpUtils_BlockingGet({
- url => "https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$lon",
- noshutdown => 1,
- });
- return netatmo_dispatch( {hash=>$hash,type=>'address'},$err,$data );
- } else {
- HttpUtils_NonblockingGet({
- url => "https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$lon",
- noshutdown => 1,
- hash => $hash,
- type => 'address',
- callback => \&netatmo_dispatch,
- });
- }
- }
- sub
- netatmo_getLatLong($$$)
- {
- my ($hash,$blocking,$addr) = @_;
- my $name = $hash->{NAME};
- my $iohash = $hash->{IODev};
- $iohash = $hash if( !defined($iohash) );
- Log3 $name, 5, "$name getLatLong ($addr)";
- if( $blocking ) {
- my($err,$data) = HttpUtils_BlockingGet({
- url => "https://maps.googleapis.com/maps/api/geocode/json?address=germany+$addr",
- noshutdown => 1,
- });
- return netatmo_dispatch( {hash=>$hash,type=>'latlng'},$err,$data );
- } else {
- HttpUtils_NonblockingGet({
- url => "https://maps.googleapis.com/maps/api/geocode/json?address=germany+$addr",
- noshutdown => 1,
- hash => $hash,
- type => 'latlng',
- callback => \&netatmo_dispatch,
- });
- }
- }
- sub
- netatmo_getDeviceDetail($$)
- {
- my ($hash,$id) = @_;
- my $name = $hash->{NAME};
- $hash = $hash->{IODev} if( defined($hash->{IODev}) );
- Log3 $name, 5, "$name getDeviceDetail ($id)";
- netatmo_getDevices($hash,1) if( !$hash->{helper}{devices} );
- netatmo_getHomecoachs($hash,1) if( !$hash->{helper}{homecoachs} );
- foreach my $device (@{$hash->{helper}{devices}}) {
- return $device if( $device->{_id} eq $id );
- }
- foreach my $device (@{$hash->{helper}{homecoachs}}) {
- return $device if( $device->{_id} eq $id );
- }
- return undef;
- }
- sub
- netatmo_requestDeviceReadings($@)
- {
- my ($hash,$id,$type,$module) = @_;
- my $name = $hash->{NAME};
- return undef if( !defined($hash->{IODev}) );
- Log3 $name, 5, "$name: requestDeviceReadings ($id ".(!$type?"-":$type)." ".(!$module?"-":$module).")";
- my $iohash = $hash->{IODev};
- $type = $hash->{dataTypes} if( !$type );
- $type = "Temperature,CO2,Humidity,Noise,Pressure,health_idx" if( !$type && $hash->{SUBTYPE} eq "DEVICE" );
- $type = "Temperature,CO2,Humidity,Noise,Pressure,Rain,WindStrength,WindAngle,GustStrength,GustAngle,Sp_Temperature,BoilerOn,BoilerOff,health_idx" if( !$type );
- $type = "WindAngle,WindStrength,GustStrength,GustAngle" if ($type eq "Wind");
- netatmo_refreshToken( $iohash, defined($iohash->{access_token}) );
- return Log3 $name, 1, "$name: No access token was found! (requestDeviceReadings)" if(!defined($iohash->{access_token}));
- my %data = (access_token => $iohash->{access_token}, device_id => $id, scale => "max", type => $type);
- $data{"module_id"} = $module if( $module );
- my $lastupdate = ReadingsVal( $name, ".lastupdate", undef );
- $data{"date_begin"} = $lastupdate if( defined($lastupdate) );
- Log3 $name, 3, "$name: requestDeviceReadings ($type)";
- HttpUtils_NonblockingGet({
- url => "https://".$iohash->{helper}{apiserver}."/api/getmeasure",
- timeout => 60,
- noshutdown => 1,
- data => \%data,
- hash => $hash,
- type => 'getmeasure',
- requested => $type,
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_initHome($@)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- return undef if( !defined($hash->{IODev}) );
- my $iohash = $hash->{IODev};
- netatmo_refreshToken( $iohash, defined($iohash->{access_token}) );
- return Log3 $name, 1, "$name: No access token was found! (initHome)" if(!defined($iohash->{access_token}));
- my %data = (access_token => $iohash->{access_token}, home_id => $hash->{Home});
- my $lastupdate = ReadingsVal( $name, ".lastupdate", undef );
- Log3 $name, 3, "$name initHome (gethomedata)";
- # url => "https://".$iohash->{helper}{apiserver}."/api/gethomedata",
- # data => \%data,
- HttpUtils_NonblockingGet({
- url => "https://app.netatmo.net/api/gethomesdata",
- timeout => 30,
- noshutdown => 1,
- header => "Content-Type: application/json\r\nAuthorization: Bearer ".$iohash->{access_token_app},
- hash => $hash,
- type => 'gethomedata',
- callback => \&netatmo_dispatch,
- });
- InternalTimer(gettimeofday()+$hash->{helper}{INTERVAL}, "netatmo_poll", $hash);
- $hash->{helper}{NEXT_POLL} = int(gettimeofday())+$hash->{helper}{INTERVAL};
- }
- sub
- netatmo_requestHomeReadings($@)
- {
- my ($hash,$id) = @_;
- my $name = $hash->{NAME};
- return undef if( !defined($hash->{IODev}) );
- my $iohash = $hash->{IODev};
- netatmo_refreshToken( $iohash, defined($iohash->{access_token}) );
- return undef if(!defined($iohash->{access_token}));
- netatmo_refreshAppToken( $iohash, defined($iohash->{access_token_app}) );
- return undef if(!defined($iohash->{access_token_app}));
- my %data = (access_token => $iohash->{access_token}, home_id => $id, size => 50);
- my $lastupdate = ReadingsVal( $name, ".lastupdate", undef );
- #$data{"size"} = 1;#$lastupdate if( defined($lastupdate) );
- Log3 $name, 3, "$name requestHomeReadings (gethomedata)";
- # url => "https://".$iohash->{helper}{apiserver}."/api/gethomedata",
- # data => \%data,
- HttpUtils_NonblockingGet({
- url => "https://app.netatmo.net/api/gethomesdata",
- timeout => 60,
- noshutdown => 1,
- header => "Content-Type: application/json\r\nAuthorization: Bearer ".$iohash->{access_token_app},
- hash => $hash,
- type => 'gethomedata',
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_requestThermostatReadings($@)
- {
- my ($hash,$id) = @_;
- my $name = $hash->{NAME};
- return undef if( !defined($hash->{IODev}) );
- Log3 $name, 3, "$name: requestThermostatReadings ($id)";
- my $iohash = $hash->{IODev};
- netatmo_refreshToken( $iohash, defined($iohash->{access_token}) );
- return Log3 $name, 1, "$name: No access token was found! (requestThermostatReadings)" if(!defined($iohash->{access_token}));
- my %data = (access_token => $iohash->{access_token}, device_id => $id);
- my $lastupdate = ReadingsVal( $name, ".lastupdate", undef );
- #$data{"size"} = 1;#$lastupdate if( defined($lastupdate) );
- HttpUtils_NonblockingGet({
- url => "https://".$iohash->{helper}{apiserver}."/api/getthermostatsdata",
- timeout => 60,
- noshutdown => 1,
- data => \%data,
- hash => $hash,
- type => 'getthermostatsdata',
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_initHeatingHome($@)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- return undef if( !defined($hash->{IODev}) );
- my $iohash = $hash->{IODev};
- netatmo_refreshToken( $iohash, defined($iohash->{access_token}) );
- netatmo_refreshAppToken( $iohash, defined($iohash->{access_token_app}) );
- return Log3 $name, 1, "$name: No access token was found! (initHeatingHome)" if(!defined($iohash->{access_token}));
- return Log3 $name, 1, "$name: No app access token was found! (initHeatingHome)" if(!defined($iohash->{access_token_app}));
- my %data = (app_type => 'app_thermostat', home_id => $hash->{Home});
- my $lastupdate = ReadingsVal( $name, ".lastupdate", undef );
- #$data{"size"} = 1;#$lastupdate if( defined($lastupdate) );
- Log3 $name, 3, "$name initHeatingHome (gethomedata)";
- # url => "https://".$iohash->{helper}{apiserver}."/api/gethomedata",
- # data => \%data,
- HttpUtils_NonblockingGet({
- url => "https://app.netatmo.net/api/gethomesdata",
- timeout => 30,
- noshutdown => 1,
- header => "Content-Type: application/json\r\nAuthorization: Bearer ".$iohash->{access_token_app},
- data => \%data,
- hash => $hash,
- type => 'gethomedata',
- callback => \&netatmo_dispatch,
- });
- InternalTimer(gettimeofday()+$hash->{helper}{INTERVAL}, "netatmo_poll", $hash);
- $hash->{helper}{NEXT_POLL} = int(gettimeofday())+$hash->{helper}{INTERVAL};
- }
- sub
- netatmo_pollHeatingHome($@)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- return undef if( !defined($hash->{IODev}) );
- my $iohash = $hash->{IODev};
- netatmo_refreshToken( $iohash, defined($iohash->{access_token}) );
- return Log3 $name, 1, "$name: No app access token was found! (pollHeatingHome)" if(!defined($iohash->{access_token_app}));
- my %data = (home_id => $hash->{Home});
- my $lastupdate = ReadingsVal( $name, ".lastupdate", undef );
- #$data{"size"} = 1;#$lastupdate if( defined($lastupdate) );
- my $json = encode_json( { home_id => $hash->{Home} } );
- $json =~s/\"true\"/true/g;
- $json =~s/\"false\"/false/g;
- Log3 $name, 3, "$name pollHeatingHome (getheatinghomedata)";
- # url => "https://".$iohash->{helper}{apiserver}."/api/gethomedata",
- # data => \%data,
- HttpUtils_NonblockingGet({
- url => "https://my.netatmo.com/syncapi/v1/gethomestatus",
- timeout => 60,
- noshutdown => 1,
- header => "Content-Type: application/json;charset=utf-8\r\nAuthorization: Bearer ".$iohash->{access_token},
- data => $json,
- hash => $hash,
- type => 'getheatinghomedata',
- callback => \&netatmo_dispatch,
- });
- InternalTimer(gettimeofday()+$hash->{helper}{INTERVAL}, "netatmo_poll", $hash);
- $hash->{helper}{NEXT_POLL} = int(gettimeofday())+$hash->{helper}{INTERVAL};
- return undef;
- }
- sub
- netatmo_pollHeatingRoom($@)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- return undef if( !defined($hash->{IODev}) );
- my $iohash = $hash->{IODev};
- netatmo_refreshAppToken( $iohash, defined($iohash->{access_token_app}) );
- return Log3 $name, 1, "$name: No app access token was found! (pollHeatingRoom)" if(!defined($iohash->{access_token_app}));
- $hash->{openRequests} = 0 if ( !defined( $hash->{openRequests}) );
- Log3 $name, 4, "$name: pollHeatingRoom types [".$hash->{dataTypes} . "] for room [".$hash->{Room}."]" if(defined($hash->{dataTypes}));
- my $lastupdate = ReadingsVal( $name, ".lastupdate", undef );
- $lastupdate = (time-7*24*60*60) if(!$lastupdate);
- $lastupdate += 10;
- $hash->{openRequests} += 1;
- my $now = int(time);
- my $json = encode_json( { home_id => $hash->{Home},
- room_id => $hash->{Room},
- scale => "max",
- type => $hash->{dataTypes},
- date_begin => "$lastupdate",
- date_end => "$now",
- real_time => "true" } );
- $json =~s/\"true\"/true/g;
- $json =~s/\"false\"/false/g;
- Log3 $name, 3, "$name pollHeatingRoom (getheatinghomedata)";
- # url => "https://".$iohash->{helper}{apiserver}."/api/gethomedata",
- # data => \%data,
- HttpUtils_NonblockingGet({
- url => "https://app.netatmo.net/api/getroommeasure",
- timeout => 60,
- noshutdown => 1,
- header => "Content-Type: application/json\r\nAuthorization: Bearer ".$iohash->{access_token_app},
- data => $json,
- hash => $hash,
- requested => $hash->{dataTypes},
- type => 'getmeasure',
- callback => \&netatmo_dispatch,
- });
- InternalTimer(gettimeofday()+$hash->{helper}{INTERVAL}, "netatmo_poll", $hash);
- $hash->{helper}{NEXT_POLL} = int(gettimeofday())+$hash->{helper}{INTERVAL};
- return undef;
- }
- sub
- netatmo_setRoomMode($$;$)
- {
- my ($hash,$set,$duration) = @_;
- my $name = $hash->{NAME};
- return undef if( !defined($hash->{IODev}) );
- my $iohash = $hash->{IODev};
- netatmo_refreshToken( $iohash, defined($iohash->{access_token}) );
- return Log3 $name, 1, "$name: No access token was found! (setRoomMode)" if(!defined($iohash->{access_token}));
- my $json = encode_json( { home_id => $hash->{Home},
- room_id => $hash->{Room},
- mode => "$set" } );
- if(defined($duration) || $set eq "max")
- {
- $duration = AttrVal($name,"setpoint_duration",60) if(!defined($duration));
- my $endpoint = time + (60 * $duration);
- my $json = encode_json( { home_id => $hash->{Home},
- room_id => $hash->{Room},
- mode => "$set",
- endtime => $endpoint } );
- }
- $json =~s/\"true\"/true/g;
- $json =~s/\"false\"/false/g;
- Log3 $name, 3, "$name: setRoomMode ($set)";
- HttpUtils_NonblockingGet({
- url => "https://".$iohash->{helper}{apiserver}."/syncapi/v1/setthermpoint",
- timeout => 30,
- noshutdown => 1,
- header => "Content-Type: application/json\r\nAuthorization: Bearer ".$iohash->{access_token},
- data => $json,
- hash => $hash,
- type => 'setroom',
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_setRoomTemp($$;$)
- {
- my ($hash,$set,$duration) = @_;
- my $name = $hash->{NAME};
- return undef if( !defined($hash->{IODev}) );
- my $iohash = $hash->{IODev};
- netatmo_refreshToken( $iohash, defined($iohash->{access_token}) );
- return Log3 $name, 1, "$name: No access token was found! (setRoomTemp)" if(!defined($iohash->{access_token}));
- $duration = AttrVal($name,"setpoint_duration",60) if(!defined($duration));
- my $endpoint = time + (60 * $duration);
- my $json = encode_json( { home_id => $hash->{Home},
- room_id => $hash->{Room},
- mode => "manual",
- endtime => $endpoint,
- temp => $set } );
- Log3 $name, 3, "$name: setRoomTemp ($set)";
- HttpUtils_NonblockingGet({
- url => "https://".$iohash->{helper}{apiserver}."/syncapi/v1/setthermpoint",
- timeout => 30,
- noshutdown => 1,
- header => "Content-Type: application/json\r\nAuthorization: Bearer ".$iohash->{access_token},
- data => $json,
- hash => $hash,
- type => 'setroom',
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_requestPersonReadings($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- return undef if( !defined($hash->{IODev}) );
- return undef if( !defined($hash->{Home}) );
- my $iohash = $hash->{IODev};
- netatmo_refreshToken( $iohash, defined($iohash->{access_token}) );
- return Log3 $name, 1, "$name: No access token was found! (requestPersonReadings)" if(!defined($iohash->{access_token}));
- Log3 $name, 3, "$name: requestPersonReadings (getpersondata)";
-
- my %data = (access_token => $iohash->{access_token}, home_id => $hash->{Home}, person_id => $hash->{Person}, offset => '20');
- my $lastupdate = ReadingsVal( $name, ".lastupdate", undef );
- HttpUtils_NonblockingGet({
- url => "https://".$iohash->{helper}{apiserver}."/api/getlasteventof",
- timeout => 60,
- noshutdown => 1,
- data => \%data,
- hash => $hash,
- type => 'getpersondata',
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_setPresence($$)
- {
- my ($hash,$status) = @_;
- my $name = $hash->{NAME};
- return undef if( !defined($hash->{IODev}) );
- my $iohash = $hash->{IODev};
- netatmo_refreshAppToken($iohash, defined($iohash->{access_token_app}));
- return Log3 $name, 1, "$name: No access token was found! (setPresence)" if(!defined($iohash->{access_token_app}));
- my $personid = $hash->{Person};
- my $urlstatus = $status;
- my $json;
- if($status eq "home")
- {
- $json = '{"home_id":"'.$hash->{Home}.'","person_ids":["'.$hash->{Person}.'"]}';
- }
- elsif($status eq "away")
- {
- $json = '{"home_id":"'.$hash->{Home}.'","person_id":"'.$hash->{Person}.'"}';
- }
- elsif($status eq "empty")
- {
- $json = '{"home_id":"'.$hash->{Home}.'"}';
- $urlstatus = "away";
- }
- Log3 $name, 5, "$name: setPresence ($status)";
- HttpUtils_NonblockingGet({
- url => "https://app.netatmo.net/api/setpersons".$urlstatus,
- timeout => 30,
- noshutdown => 1,
- method => "POST",
- header => "Content-Type: application/json\r\nAuthorization: Bearer ".$iohash->{access_token_app},
- data => $json,
- hash => $hash,
- type => 'setpersonsstatus_'.$status,
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_setNotifications($$$)
- {
- my ($hash,$setting,$value) = @_;
- my $name = $hash->{NAME};
- return undef if( !defined($hash->{IODev}) );
- my $iohash = $hash->{IODev};
- netatmo_refreshAppToken($iohash, defined($iohash->{access_token_app}));
- return Log3 $name, 1, "$name: No access token was found! (setNotifications)" if(!defined($iohash->{access_token_app}));
- if( !defined($iohash->{csrf_token}) )
- {
- my($err0,$data0) = HttpUtils_BlockingGet({
- url => "https://auth.netatmo.com/access/checklogin",
- timeout => 10,
- noshutdown => 1,
- });
- if($err0 || !defined($data0))
- {
- Log3 $name, 1, "$name: csrf call failed! ".$err0;
- return undef;
- }
- $data0 =~ /ci_csrf_netatmo" value="(.*)"/;
- my $tmptoken = $1;
- $iohash->{csrf_token} = $tmptoken;
- if(!defined($iohash->{csrf_token})) {
- Log3 $name, 1, "$name: CSRF ERROR ";
- return undef;
- }
- Log3 $name, 4, "$name: csrf_token ".$iohash->{csrf_token};
- }
-
- my $homeid = $hash->{Home};
- my %data;
-
- if($setting eq "presence_notify_from" || $setting eq "presence_notify_to" || $setting eq "gone_after")
- {
- my @timevalue = split(":",$value);
- if(defined($timevalue[1]))
- {
- $value = int($timevalue[0])*3600 + int($timevalue[1])*60;
- }
- else
- {
- $value *= 60;
- }
- $value = 0 if($value < 0);
- $value = 86400 if($value > 86400 && $setting ne "gone_after");
- }
- elsif($setting eq "smart_notifs")
- {
- $value = (($value eq "on") ? "true" : "false");
- }
- if($setting eq "presence_enable_notify_from_to" || $setting =~ /^presence_record_/ || $setting =~ /^presence_notify_/ )
- {
- %data = (home_id => $homeid, 'presence_settings['.$setting.']' => $value, ci_csrf_netatmo => $iohash->{csrf_token});
- }
- else
- {
- %data = (home_id => $homeid, $setting => $value, ci_csrf_netatmo => $iohash->{csrf_token});
- }
- Log3 $name, 5, "$name: setNotifications ($setting $value)";
- HttpUtils_NonblockingGet({
- url => "https://app.netatmo.net/api/updatehome",
- timeout => 30,
- noshutdown => 1,
- method => "POST",
- header => "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\nAuthorization: Bearer ".$iohash->{access_token_app},
- data => \%data,
- hash => $hash,
- type => 'sethomesettings',
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_setCamera($$$)
- {
- my ($hash,$status,$pin) = @_;
- my $name = $hash->{NAME};
- my $commandurl = ReadingsVal( $name, "local_url", ReadingsVal( $name, "vpn_url", undef ) );
- return undef if(!defined($commandurl));
- $commandurl .= "/command/changestatus?status=$status&pin=$pin";
- Log3 $name, 3, "$name: setCamera ".$commandurl;
- HttpUtils_NonblockingGet({
- url => $commandurl,
- timeout => 30,
- noshutdown => 1,
- verify_hostname => 0,
- hash => $hash,
- type => 'camerastatus',
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_setCameraSetting($$$)
- {
- my ($hash,$setting,$newvalue) = @_;
- my $name = $hash->{NAME};
- #netatmo_pingCamera( $hash );
- my $commandurl = ReadingsVal( $name, "vpn_url", undef );
- return undef if(!defined($commandurl));
- $commandurl .= "/command/changesetting?$setting=$newvalue";
- Log3 $name, 3, "$name: setCameraSetting ".$commandurl;
- HttpUtils_NonblockingGet({
- url => $commandurl,
- timeout => 30,
- noshutdown => 1,
- verify_hostname => 0,
- hash => $hash,
- type => 'camerastatus',
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_setFloodlight($$)
- {
- my ($hash,$setting) = @_;
- my $name = $hash->{NAME};
- #netatmo_pingCamera( $hash );
- my $commandurl = ReadingsVal( $name, "local_url", ReadingsVal( $name, "vpn_url", undef ) );
- return undef if(!defined($commandurl));
- $commandurl .= "/command/floodlight_set_config?config=%7B%22mode%22:%22$setting%22%7D";
- Log3 $name, 3, "$name: setFloodlight ".$commandurl;
- HttpUtils_NonblockingGet({
- url => $commandurl,
- timeout => 30,
- noshutdown => 1,
- verify_hostname => 0,
- hash => $hash,
- type => 'camerastatus',
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_setIntensity($$)
- {
- my ($hash,$setting) = @_;
- my $name = $hash->{NAME};
- #netatmo_pingCamera( $hash );
- my $commandurl = ReadingsVal( $name, "local_url", ReadingsVal( $name, "vpn_url", undef ) );
- return undef if(!defined($commandurl));
- $commandurl .= "/command/floodlight_interactive_config?intensity=$setting";
- Log3 $name, 3, "$name: setIntensity ".$commandurl;
- HttpUtils_NonblockingGet({
- url => $commandurl,
- timeout => 30,
- noshutdown => 1,
- verify_hostname => 0,
- hash => $hash,
- type => 'camerastatus',
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_setPresenceConfig($$)
- {
- my ($hash,$setting) = @_;
- my $name = $hash->{NAME};
- #netatmo_pingCamera( $hash );
- my $commandurl = ReadingsVal( $name, "local_url", ReadingsVal( $name, "vpn_url", undef ) );
- return undef if(!defined($commandurl));
- $commandurl .= "/command/floodlight_set_config?config=%7B%22intensity%22:".ReadingsVal( $name, "intensity", 50 ).",%22night%22:%7B%22always%22:".ReadingsVal( $name, "night_always", "false" ).",%22animal%22:".ReadingsVal( $name, "night_animal", "false" ).",%22movement%22:".ReadingsVal( $name, "night_movement", "false" ).",%22person%22:".ReadingsVal( $name, "night_person", "true" ).",%22vehicle%22:".ReadingsVal( $name, "night_vehicle", "false" )."%7D%7D";
- Log3 $name, 3, "$name: setPresenceConfig ".$commandurl;
- HttpUtils_NonblockingGet({
- url => $commandurl,
- timeout => 30,
- noshutdown => 1,
- verify_hostname => 0,
- hash => $hash,
- type => 'camerastatus',
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_getPresenceConfig($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- #netatmo_pingCamera( $hash );
- my $commandurl = ReadingsVal( $name, "local_url", ReadingsVal( $name, "vpn_url", undef ) );
- return undef if(!defined($commandurl));
- $commandurl .= "/command/floodlight_get_config";
- Log3 $name, 3, "$name: getPresenceConfig ".$commandurl;
- HttpUtils_NonblockingGet({
- url => $commandurl,
- timeout => 30,
- noshutdown => 1,
- verify_hostname => 0,
- hash => $hash,
- type => 'cameraconfig',
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_setTagCalibration($$)
- {
- my ($hash,$setting) = @_;
- my $name = $hash->{NAME};
- return undef if( !defined($hash->{Camera}) );
- my $camerahash = $modules{$hash->{TYPE}}{defptr}{"C$hash->{Camera}"};
- return undef if( !defined($camerahash));
- #netatmo_pingCamera( $hash );
- my $commandurl = ReadingsVal( $camerahash->{NAME}, "local_url", ReadingsVal( $camerahash->{NAME}, "vpn_url", undef ) );
- return undef if(!defined($commandurl));
- $commandurl .= "/command/dtg_cal?id=".$hash->{Tag};
- Log3 $name, 3, "$name: setTagCalibration ".$commandurl;
- HttpUtils_NonblockingGet({
- url => $commandurl,
- timeout => 30,
- noshutdown => 1,
- verify_hostname => 0,
- hash => $hash,
- type => 'tagstatus',
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_setThermostatMode($$;$)
- {
- my ($hash,$set,$duration) = @_;
- my $name = $hash->{NAME};
- return undef if( !defined($hash->{IODev}) );
- my $iohash = $hash->{IODev};
- netatmo_refreshToken( $iohash, defined($iohash->{access_token}) );
- return Log3 $name, 1, "$name: No access token was found! (setThermostatMode)" if(!defined($iohash->{access_token}));
- my %data;
- %data = (access_token => $iohash->{access_token}, device_id => $hash->{Relay}, module_id => $hash->{Thermostat}, setpoint_mode => $set);
- if(defined($duration) || $set eq "max")
- {
- $duration = AttrVal($name,"setpoint_duration",60) if(!defined($duration));
- my $endpoint = time + (60 * $duration);
- %data = (access_token => $iohash->{access_token}, device_id => $hash->{Relay}, module_id => $hash->{Thermostat}, setpoint_mode => $set, setpoint_endtime => $endpoint);
- }
- Log3 $name, 3, "$name: setThermostatMode ($set)";
- HttpUtils_NonblockingGet({
- url => "https://".$iohash->{helper}{apiserver}."/api/setthermpoint",
- timeout => 30,
- noshutdown => 1,
- data => \%data,
- hash => $hash,
- type => 'setthermostat',
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_setThermostatTemp($$;$$)
- {
- my ($hash,$set,$duration) = @_;
- my $name = $hash->{NAME};
- return undef if( !defined($hash->{IODev}) );
- my $iohash = $hash->{IODev};
- netatmo_refreshToken( $iohash, defined($iohash->{access_token}) );
- return Log3 $name, 1, "$name: No access token was found! (setThermostatTemp)" if(!defined($iohash->{access_token}));
- $duration = AttrVal($name,"setpoint_duration",60) if(!defined($duration));
- my $endpoint = time + (60 * $duration);
- my %data = (access_token => $iohash->{access_token}, device_id => $hash->{Relay}, module_id => $hash->{Thermostat}, setpoint_mode => 'manual', setpoint_temp => $set, setpoint_endtime => $endpoint);
- Log3 $name, 3, "$name: setThermostatTemp ($set)";
- HttpUtils_NonblockingGet({
- url => "https://".$iohash->{helper}{apiserver}."/api/setthermpoint",
- timeout => 30,
- noshutdown => 1,
- data => \%data,
- hash => $hash,
- type => 'setthermostat',
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_setThermostatProgram($$)
- {
- my ($hash,$set) = @_;
- my $name = $hash->{NAME};
- return undef if( !defined($hash->{IODev}) );
- my $iohash = $hash->{IODev};
- netatmo_refreshToken( $iohash, defined($iohash->{access_token}) );
- return Log3 $name, 1, "$name: No access token was found! (setThermostatProgram)" if(!defined($iohash->{access_token}));
- my $schedule_id = 0;
- foreach my $scheduledata ( @{$hash->{schedules}})
- {
- $schedule_id = @{$scheduledata}[1] if($set eq @{$scheduledata}[0]);
- }
- my %data = (access_token => $iohash->{access_token}, device_id => $hash->{Relay}, module_id => $hash->{Thermostat}, schedule_id => $schedule_id);
- Log3 $name, 3, "$name: setThermostatProgram ($set / $schedule_id)";
- HttpUtils_NonblockingGet({
- url => "https://".$iohash->{helper}{apiserver}."/api/switchschedule",
- timeout => 30,
- noshutdown => 1,
- data => \%data,
- hash => $hash,
- type => 'setthermostat',
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_poll($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- if(IsDisabled($name) || !defined($name)) {
- RemoveInternalTimer($hash);
- #$hash->{STATE} = "Disabled";
- readingsSingleUpdate($hash, "active", "disabled", 1);
- return undef;
- }
- # my $resolve = inet_aton($hash->{helper}{apiserver});
- # if(!defined($resolve))
- # {
- # Log3 $name, 1, "$name: DNS error on poll";
- # InternalTimer( gettimeofday() + 1800, "netatmo_poll", $hash);
- # return undef;
- # }
- $hash->{helper}{INTERVAL} = 3600 if(!defined($hash->{helper}{INTERVAL}));
-
-
- if(defined($hash->{status}) && ($hash->{status} =~ /usage/ || $hash->{status} =~ /too_many_connections/)) {
- RemoveInternalTimer($hash);
- InternalTimer(gettimeofday()+$hash->{helper}{INTERVAL}+1800, "netatmo_poll", $hash);
- Log3 $name, 1, "$name: API usage limit reached";
- $hash->{status} = "postponed update";
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if($hash->{status} ne "no data");
- return undef;
- }
- $hash->{status} = "ok";
- if( $hash->{SUBTYPE} eq "ACCOUNT" && defined($hash->{network}) && $hash->{network} eq "timeout" ) {
- RemoveInternalTimer($hash);
- InternalTimer(gettimeofday()+300, "netatmo_poll", $hash);
- $hash->{status} = "recovering timeout";
- netatmo_checkConnection($hash);
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if($hash->{status} ne "no data");
- return undef;
- } elsif( $hash->{SUBTYPE} eq "ACCOUNT" && defined($hash->{network}) && $hash->{network} ne "ok" ) {
- RemoveInternalTimer($hash);
- InternalTimer(gettimeofday()+600, "netatmo_poll", $hash);
- $hash->{status} = "recovering network";
- netatmo_checkConnection($hash);
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if($hash->{status} ne "no data");
- Log3 $name, 5, "$name: ACCOUNT network error: ".$hash->{network};
- return undef;
- } elsif( $hash->{SUBTYPE} ne "ACCOUNT" && defined($hash->{IODev}->{network}) && $hash->{IODev}->{network} ne "ok" ) {
- RemoveInternalTimer($hash);
- InternalTimer(gettimeofday()+150, "netatmo_poll", $hash);
- $hash->{status} = "delayed update";
- #netatmo_checkConnection($hash->{IODev});
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if($hash->{status} ne "no data");
- Log3 $name, 5, "$name: DEVICE network error: ".$hash->{IODev}->{network};
- return undef;
- }
- Log3 $name, 3, "$name: poll ($hash->{SUBTYPE})";
- if( $hash->{SUBTYPE} eq "ACCOUNT" ) {
- netatmo_pollGlobal($hash);
- netatmo_pollGlobalHealth($hash);
- } elsif( $hash->{SUBTYPE} eq "DEVICE" ) {
- netatmo_pollDevice($hash);
- } elsif( $hash->{SUBTYPE} eq "MODULE" ) {
- netatmo_pollDevice($hash);
- } elsif( $hash->{SUBTYPE} eq "PUBLIC" ) {
- netatmo_pollDevice($hash);
- } elsif( $hash->{SUBTYPE} eq "FORECAST" ) {
- netatmo_pollForecast($hash);
- } elsif( $hash->{SUBTYPE} eq "HOME" ) {
- netatmo_pollHome($hash);
- } elsif( $hash->{SUBTYPE} eq "CAMERA" ) {
- netatmo_pingCamera($hash);
- } elsif( $hash->{SUBTYPE} eq "RELAY" ) {
- netatmo_pollRelay($hash);
- } elsif( $hash->{SUBTYPE} eq "THERMOSTAT" ) {
- netatmo_pollThermostat($hash);
- } elsif( $hash->{SUBTYPE} eq "HEATINGHOME" ) {
- netatmo_pollHeatingHome($hash);
- } elsif( $hash->{SUBTYPE} eq "HEATINGROOM" ) {
- netatmo_pollHeatingRoom($hash);
- } elsif( $hash->{SUBTYPE} eq "PERSON" ) {
- netatmo_pollPerson($hash);
- } else {
- Log3 $name, 1, "$name: unknown netatmo type $hash->{SUBTYPE} on poll";
- return undef;
- }
- if( defined($hash->{helper}{update_count}) && $hash->{helper}{update_count} > 1024 ) {
- InternalTimer(gettimeofday()+30, "netatmo_poll", $hash);
- } else {
- $hash->{helper}{NEXT_POLL} = int(gettimeofday())+$hash->{helper}{INTERVAL};
- InternalTimer(gettimeofday()+$hash->{helper}{INTERVAL}, "netatmo_poll", $hash);
- }
- }
- sub
- netatmo_dispatch($$$)
- {
- my ($param, $err, $data) = @_;
- my $hash = $param->{hash};
- my $name = $hash->{NAME};
- if(!defined($param->{hash})){
- Log3 "netatmo", 2, "netatmo: ".$param->{type}."dispatch fail (hash missing)";
- return undef;
- }
- if(!defined($hash->{NAME})){
- Log3 "netatmo", 2, "netatmo: ".$param->{type}."dispatch fail (name missing)";
- return undef;
- }
- Log3 $name, 4, "$name: dispatch ($param->{type})";
- $hash->{openRequests} -= 1 if( $param->{type} eq 'getmeasure' );
- if( $err ) {
- Log3 $name, 2, "$name: ".$param->{type}." request failed: $err";
- if($err =~ /refused/ ){
- RemoveInternalTimer($hash);
- InternalTimer(gettimeofday()+3600, "netatmo_poll", $hash);
- Log3 $name, 1, "$name: Possible IP Ban by Netatmo servers, try to change your IP and increase your request interval";
- $hash->{status} = "banned";
- $hash->{network} = "banned" if($hash->{SUBTYPE} eq "ACCOUNT");
- }
- elsif($err =~ /Invalid access token/){
- RemoveInternalTimer($hash);
- InternalTimer(gettimeofday()+300, "netatmo_poll", $hash);
- $hash->{status} = "token";
- $hash->{expires_at} = int(gettimeofday()) if($hash->{SUBTYPE} eq "ACCOUNT");
- $hash->{IODev}->{expires_at} = int(gettimeofday()) if($hash->{SUBTYPE} ne "ACCOUNT");
- }
- elsif($err =~ /Bad hostname/ || $err =~ /gethostbyname/){
- RemoveInternalTimer($hash);
- InternalTimer(gettimeofday()+600, "netatmo_poll", $hash);
- $hash->{status} = "timeout";
- $hash->{network} = "dns" if($hash->{SUBTYPE} eq "ACCOUNT");
- }
- elsif($err =~ /timed out/){
- RemoveInternalTimer($hash);
- InternalTimer(gettimeofday()+300, "netatmo_poll", $hash);
- $hash->{status} = "timeout";
- $hash->{network} = "timeout" if($hash->{SUBTYPE} eq "ACCOUNT");
- }
- elsif($err =~ /Can't connect/){
- RemoveInternalTimer($hash);
- InternalTimer(gettimeofday()+300, "netatmo_poll", $hash);
- $hash->{status} = "timeout";
- $hash->{network} = "disconnected" if($hash->{SUBTYPE} eq "ACCOUNT");
- #CommandDeleteReading( undef, "$hash->{NAME} vpn_url" ) if($hash->{SUBTYPE} eq "CAMERA");
- }
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if($hash->{status} ne "no data");
- return undef;
- } elsif( $data ) {
- $data =~ s/\n//g;
- if( $data !~ m/^{.*}$/ ) {
- RemoveInternalTimer($hash);
- InternalTimer(gettimeofday()+300, "netatmo_poll", $hash);
- Log3 $name, 2, "$name: invalid json detected";
- Log3 $name, 5, "$name: $data";
- $hash->{status} = "error";
- $hash->{network} = "ok" if($hash->{SUBTYPE} eq "ACCOUNT");
- $hash->{IODev}->{network} = "ok" if($hash->{SUBTYPE} ne "ACCOUNT");
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if($hash->{status} ne "no data");
- return undef;
- }
- $hash->{network} = "ok" if($hash->{SUBTYPE} eq "ACCOUNT");
- $hash->{IODev}->{network} = "ok" if($hash->{SUBTYPE} ne "ACCOUNT");
- my $json = eval { JSON->new->utf8(0)->decode($data) };
- if($@)
- {
- Log3 $name, 2, "$name: invalid json evaluation on dispatch type ".$param->{type}." ".$@;
- return undef;
- }
- Log3 "unknown", 2, "unknown (no name) ".Dumper($hash) if(!defined($name));
- Log3 $name, 4, "$name: dispatch return: ".$param->{type};
- Log3 $name, 5, Dumper($json);
- if( $json->{error} ) {
- if(ref($json->{error}) ne "HASH") {
- $hash->{STATE} = "LOGIN FAILED" if($hash->{SUBTYPE} eq "ACCOUNT");
- $hash->{status} = $json->{error};
- Log3 $name, 2, "$name: json message error: ".$json->{error};
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if($hash->{status} ne "no data");
- return undef;
- }
- $hash->{status} = $json->{error}{message} if(defined($json->{error}{message}));
- InternalTimer(gettimeofday()+1800, "netatmo_poll", $hash, 0) if($hash->{status} =~ /usage/ || $hash->{status} =~ /too_many_connections/);
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if($hash->{status} ne "no data");
- return undef if($hash->{status} =~ /usage/ || $hash->{status} =~ /too_many_connections/);
- }
-
- if( $param->{type} eq 'token' ) {
- netatmo_parseToken($hash,$json);
- } elsif( $param->{type} eq 'apptoken' ) {
- netatmo_parseAppToken($hash,$json);
- } elsif( $param->{type} eq 'devicelist' ) {
- netatmo_parseDeviceList($hash,$json);
- } elsif( $param->{type} eq 'stationsdata' ) {
- netatmo_parseGlobal($hash,$json);
- } elsif( $param->{type} eq 'forecastdata' ) {
- netatmo_parseForecast($hash,$json);
- } elsif( $param->{type} eq 'getmeasure' ) {
- netatmo_parseReadings($hash,$json,$param->{requested});
- } elsif( $param->{type} eq 'homelist' ) {
- netatmo_parseHomeList($hash,$json);
- } elsif( $param->{type} eq 'gethomedata' ) {
- netatmo_parseHomeReadings($hash,$json);
- } elsif( $param->{type} eq 'cameraping' ) {
- netatmo_parseCameraPing($hash,$json);
- } elsif( $param->{type} eq 'camerastatus' ) {
- netatmo_parseCameraStatus($hash,$json);
- } elsif( $param->{type} eq 'cameraconfig' ) {
- netatmo_parseCameraConfig($hash,$json);
- } elsif( $param->{type} eq 'tagstatus' ) {
- netatmo_parseTagStatus($hash,$json);
- } elsif( $param->{type} eq 'cameravideo' ) {
- netatmo_parseCameraVideo($hash,$json);
- } elsif( $param->{type} =~ /setpersonsstatus_/ ) {
- netatmo_parsePersonsStatus($hash,$json,$param->{type});
- } elsif( $param->{type} eq 'homecoachlist' ) {
- netatmo_parseHomecoachList($hash,$json);
- } elsif( $param->{type} eq 'thermostatlist' ) {
- netatmo_parseThermostatList($hash,$json);
- } elsif( $param->{type} eq 'getthermostatsdata' ) {
- netatmo_parseThermostatReadings($hash,$json);
- } elsif( $param->{type} eq 'setthermostat' ) {
- netatmo_parseThermostatStatus($hash,$json);
- } elsif( $param->{type} eq 'getheatinghomedata' ) {
- netatmo_parseHeatingHomeStatus($hash,$json);
- } elsif( $param->{type} eq 'getpersondata' ) {
- netatmo_parsePersonReadings($hash,$json);
- } elsif( $param->{type} eq 'publicdata' ) {
- return netatmo_parsePublic($hash,$json);
- } elsif( $param->{type} eq 'address' ) {
- return netatmo_parseAddress($hash,$json);
- } elsif( $param->{type} eq 'latlng' ) {
- return netatmo_parseLatLng($hash,$json);
- } elsif( $param->{type} eq 'addwebhook' ) {
- return netatmo_webhookStatus($hash,$json,"added");
- } elsif( $param->{type} eq 'dropwebhook' ) {
- return netatmo_webhookStatus($hash,$json,"dropped");
- } elsif( $param->{type} eq 'sethomesettings' ) {
- return netatmo_refreshHomeSettings($hash);
- } else {
- Log3 $name, 1, "$name: unknown '$param->{type}' ".Dumper($json);
- }
- }
- }
- sub
- netatmo_parsePersonsStatus($$$)
- {
- my ($hash, $json, $param) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 5, "$name: parsePersonsStatus ($param)\n".Dumper($json);
- return if(!defined($json->{status}) || $json->{status} ne "ok");
- if($hash->{SUBTYPE} eq "PERSON")
- {
- if($param =~ /away/)
- {
- readingsSingleUpdate( $hash, "status", "away", 1 );
- }
- else{
- readingsSingleUpdate( $hash, "status", "home", 1 );
- }
- }
- elsif($hash->{SUBTYPE} eq "HOME")
- {
- readingsSingleUpdate( $hash, "event", "Everyone left", 1 );
- }
- }
- sub
- netatmo_autocreate($;$)
- {
- my($hash,$force) = @_;
- my $name = $hash->{NAME};
- if( !$hash->{helper}{devices} ) {
- netatmo_getDevices($hash,1);
- return undef if( !$force );
- }
- if( !$force ) {
- foreach my $d (keys %defs) {
- next if(!defined($defs{$d}));
- next if($defs{$d}{TYPE} ne "autocreate");
- return undef if(IsDisabled($defs{$d}{NAME}));
- }
- }
- my $autocreated = 0;
- my $devices = $hash->{helper}{devices};
- foreach my $device (@{$devices}) {
- if( defined($modules{$hash->{TYPE}}{defptr}{"D$device->{_id}"}) ) {
- Log3 $name, 4, "$name: device '$device->{_id}' already defined";
- next;
- }
- if( defined($modules{$hash->{TYPE}}{defptr}{"M$device->{_id}"}) ) {
- Log3 $name, 4, "$name: module '$device->{_id}' already defined";
- next;
- }
- if(AttrVal($name,"ignored_device_ids","") =~ /$device->{_id}/) {
- Log3 $name, 4, "$name: '$device->{_id}' ignored for autocreate";
- next;
- }
- my $id = $device->{_id};
- my $devname = "netatmo_D". $id;
- $devname =~ s/:/_/g;
- my $define= "$devname netatmo $id";
- if( $device->{main_device} ) {
- $devname = "netatmo_M". $id;
- $devname =~ s/:/_/g;
- $define= "$devname netatmo MODULE $device->{main_device} $id";
- }
- Log3 $name, 3, "$name: create new device '$devname' for device '$id'";
- my $cmdret= CommandDefine(undef,$define);
- if($cmdret) {
- Log3 $name, 1, "$name: Autocreate: An error occurred while creating device for id '$id': $cmdret";
- } else {
- $cmdret= CommandAttr(undef,"$devname alias ".encode_utf8($device->{module_name})) if( defined($device->{module_name}) );
- $cmdret= CommandAttr(undef,"$devname room netatmo");
- $cmdret= CommandAttr(undef,"$devname IODev $name");
- $cmdret= CommandAttr(undef,"$devname devStateIcon .*:no-icon");
- $autocreated++;
- }
- }
- CommandSave(undef,undef) if( $autocreated && AttrVal( "autocreate", "autosave", 1 ) );
- return "created $autocreated devices";
- }
- sub
- netatmo_autocreatehome($;$)
- {
- my($hash,$force) = @_;
- my $name = $hash->{NAME};
- if( !$hash->{helper}{homes} ) {
- return undef if( !$force );
- netatmo_getHomes($hash,1);
- }
- if( !$force ) {
- foreach my $d (keys %defs) {
- next if(!defined($defs{$d}));
- next if($defs{$d}{TYPE} ne "autocreate");
- return undef if(IsDisabled($defs{$d}{NAME}));
- }
- }
- my $autocreated = 0;
- my $homes = $hash->{helper}{homes};
- foreach my $home (@{$homes}) {
- if( defined($modules{$hash->{TYPE}}{defptr}{"H$home->{id}"}) ) {
- Log3 $name, 4, "$name: home '$home->{id}' already defined";
- next;
- }
- if( defined($modules{$hash->{TYPE}}{defptr}{"P$home->{id}"}) ) {
- Log3 $name, 4, "$name: person '$home->{id}' already defined";
- next;
- }
- foreach my $module (@{$home->{modules}}) {
- if( defined($modules{$hash->{TYPE}}{defptr}{"G$module->{id}"}) ) {
- Log3 $name, 4, "$name: tag '$module->{id}' already defined";
- next;
- }
- my $tagid = $module->{id};
- my $tagdevname = "netatmo_G". $tagid;
- $tagdevname =~ s/:/_/g;
- my $tagdefine= "$tagdevname netatmo TAG $home->{id} $tagid";
- Log3 $name, 3, "$name: create new tag '$tagdevname' for camera '$home->{id}'";
- my $tagcmdret= CommandDefine(undef,$tagdefine);
- if($tagcmdret) {
- Log3 $name, 1, "$name: Autocreate: An error occurred while creating tag for id '$tagid': $tagcmdret";
- } else {
- $tagcmdret= CommandAttr(undef,"$tagdevname alias ".encode_utf8($module->{name})) if( defined($module->{name}) );
- $tagcmdret= CommandAttr(undef,"$tagdevname devStateIcon .*:no-icon");
- $tagcmdret= CommandAttr(undef,"$tagdevname room netatmo");
- $tagcmdret= CommandAttr(undef,"$tagdevname stateFormat status");
- $tagcmdret= CommandAttr(undef,"$tagdevname IODev $name");
- $autocreated++;
- }
-
-
- }
- if( defined($modules{$hash->{TYPE}}{defptr}{"C$home->{id}"}) ) {
- Log3 $name, 4, "$name: camera '$home->{id}' already defined";
- next;
- }
- if(AttrVal($name,"ignored_device_ids","") =~ /$home->{id}/) {
- Log3 $name, 4, "$name: '$home->{id}' ignored for autocreate";
- next;
- }
- my $id = $home->{id};
- my $devname = "netatmo_H". $id;
- $devname =~ s/-/_/g;
- my $define= "$devname netatmo HOME $id";
- if( $home->{sd_status} ) {
- $devname = "netatmo_C". $id;
- $devname =~ s/:/_/g;
- $devname =~ s/-/_/g;
- $define= "$devname netatmo CAMERA $home->{home} $id";
- }
- elsif( $home->{face} ) {
- next if(!defined($home->{pseudo})); #ignore unassigned faces
- Log3 $name, 5, "$name: create new home/person '$devname' for home '$home->{home}'".Dumper($home);
- $devname = "netatmo_P". $id;
- $devname =~ s/-/_/g;
- $define= "$devname netatmo PERSON $home->{home} $id";
- }
- $home->{home} = "?" if(!defined($home->{home}));
- Log3 $name, 3, "$name: create new home/person '$devname' for home '$home->{home}'";
- my $cmdret= CommandDefine(undef,$define);
- if($cmdret) {
- Log3 $name, 1, "$name: Autocreate: An error occurred while creating home for id '$id': $cmdret";
- } else {
- $cmdret= CommandAttr(undef,"$devname alias Unknown") if( defined($home->{face}) );
- $cmdret= CommandAttr(undef,"$devname alias ".encode_utf8($home->{pseudo})) if( defined($home->{pseudo}) );
- $cmdret= CommandAttr(undef,"$devname alias ".encode_utf8($home->{name})) if( defined($home->{name}) );
- $cmdret= CommandAttr(undef,"$devname devStateIcon .*:no-icon");
- $cmdret= CommandAttr(undef,"$devname room netatmo");
- $cmdret= CommandAttr(undef,"$devname stateFormat status");
- $cmdret= CommandAttr(undef,"$devname IODev $name");
- $autocreated++;
- }
- }
- CommandSave(undef,undef) if( $autocreated && AttrVal( "autocreate", "autosave", 1 ) );
- return "created $autocreated devices";
- }
- sub
- netatmo_autocreatethermostat($;$)
- {
- my($hash,$force) = @_;
- my $name = $hash->{NAME};
- if( !$hash->{helper}{thermostats} ) {
- netatmo_getThermostats($hash,1);
- return undef if( !$force );
- }
- if( !$force ) {
- foreach my $d (keys %defs) {
- next if(!defined($defs{$d}));
- next if($defs{$d}{TYPE} ne "autocreate");
- return undef if(IsDisabled($defs{$d}{NAME}));
- }
- }
- my $autocreated = 0;
- my $devices = $hash->{helper}{thermostats};
-
- #Log3 $name, 1, "$name: autocreating ".Dumper($devices);
- foreach my $device (@{$devices}) {
- if( defined($modules{$hash->{TYPE}}{defptr}{"R$device->{id}"}) ) {
- Log3 $name, 4, "$name: relay '$device->{id}' already defined";
- next;
- }
- if( defined($modules{$hash->{TYPE}}{defptr}{"T$device->{id}"}) ) {
- Log3 $name, 4, "$name: thermostat '$device->{id}' already defined";
- next;
- }
- if( defined($modules{$hash->{TYPE}}{defptr}{"E$device->{id}"}) ) {
- Log3 $name, 4, "$name: heating home '$device->{id}' already defined";
- next;
- }
- if( defined($modules{$hash->{TYPE}}{defptr}{"O$device->{id}"}) ) {
- Log3 $name, 4, "$name: heating room '$device->{id}' already defined";
- next;
- }
- if(AttrVal($name,"ignored_device_ids","") =~ /$device->{id}/) {
- Log3 $name, 4, "$name: '$device->{id}' ignored for autocreate";
- next;
- }
- my $id = $device->{id};
- my $devname = "netatmo_R". $id;
- $devname =~ s/:/_/g;
- my $define= "$devname netatmo RELAY $id";
- if( $device->{type} eq "Home" ) {
- next;
- $devname = "netatmo_E". $id;
- $devname =~ s/:/_/g;
- $define= "$devname netatmo HEATINGHOME $id";
- }
- elsif( $device->{type} eq "Room" ) {
- next;
- $devname = "netatmo_O". $id;
- $devname =~ s/:/_/g;
- $define= "$devname netatmo HEATINGROOM $device->{Home} $id";
- }
- elsif( $device->{main_device} ) {
- $devname = "netatmo_T". $id;
- $devname =~ s/:/_/g;
- $define= "$devname netatmo THERMOSTAT $device->{main_device} $id";
- }
- Log3 $name, 3, "$name: create new device '$devname' for device '$id'";
- my $cmdret= CommandDefine(undef,$define);
- if($cmdret) {
- Log3 $name, 1, "$name: Autocreate: An error occurred while creating device for id '$id': $cmdret";
- } else {
- $cmdret= CommandAttr(undef,"$devname alias ".encode_utf8($device->{name})) if( defined($device->{name}) );
- #$cmdret= CommandAttr(undef,"$devname alias ".encode_utf8($device->{module_name})) if( defined($device->{module_name}) );
- $cmdret= CommandAttr(undef,"$devname room netatmo");
- $cmdret= CommandAttr(undef,"$devname IODev $name");
- $cmdret= CommandAttr(undef,"$devname devStateIcon .*:no-icon");
- $cmdret= CommandAttr(undef,"$devname stateFormat setpoint|temperature") if( $device->{main_device} );
- $cmdret= CommandAttr(undef,"$devname stateFormat active") if( !$device->{main_device} );
- $autocreated++;
- }
- }
- CommandSave(undef,undef) if( $autocreated && AttrVal( "autocreate", "autosave", 1 ) );
- return "created $autocreated devices";
- }
- sub
- netatmo_autocreatehomecoach($;$)
- {
- my($hash,$force) = @_;
- my $name = $hash->{NAME};
- if( !$hash->{helper}{homecoachs} ) {
- netatmo_getHomecoachs($hash,1);
- return undef if( !$force );
- }
- if( !$force ) {
- foreach my $d (keys %defs) {
- next if(!defined($defs{$d}));
- next if($defs{$d}{TYPE} ne "autocreate");
- return undef if(IsDisabled($defs{$d}{NAME}));
- }
- }
- my $autocreated = 0;
- my $devices = $hash->{helper}{homecoachs};
- foreach my $device (@{$devices}) {
- if( defined($modules{$hash->{TYPE}}{defptr}{"D$device->{_id}"}) ) {
- Log3 $name, 4, "$name: homecoach '$device->{_id}' already defined";
- next;
- }
- if(AttrVal($name,"ignored_device_ids","") =~ /$device->{_id}/) {
- Log3 $name, 4, "$name: '$device->{_id}' ignored for autocreate";
- next;
- }
- my $id = $device->{_id};
- my $devname = "netatmo_D". $id;
- $devname =~ s/:/_/g;
- my $define= "$devname netatmo $id";
- Log3 $name, 3, "$name: create new device '$devname' for device '$id'";
- my $cmdret= CommandDefine(undef,$define);
- if($cmdret) {
- Log3 $name, 1, "$name: Autocreate: An error occurred while creating device for id '$id': $cmdret";
- } else {
- $cmdret= CommandAttr(undef,"$devname alias ".encode_utf8($device->{station_name})) if( defined($device->{station_name}) );
- $cmdret= CommandAttr(undef,"$devname room netatmo");
- $cmdret= CommandAttr(undef,"$devname IODev $name");
- $cmdret= CommandAttr(undef,"$devname devStateIcon .*:no-icon");
- $cmdret= CommandAttr(undef,"$devname stateFormat health_idx");
- $autocreated++;
- }
- }
- CommandSave(undef,undef) if( $autocreated && AttrVal( "autocreate", "autosave", 1 ) );
- return "created $autocreated devices";
- }
- sub
- netatmo_parseToken($$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- my $had_token = $hash->{access_token};
- $hash->{access_token} = $json->{access_token};
- $hash->{refresh_token} = $json->{refresh_token};
- if( $hash->{access_token} ) {
- $hash->{STATE} = "Connected";
- $hash->{network} = "ok";
- $hash->{expires_at} = int(gettimeofday());
- $hash->{expires_at} += int($json->{expires_in}*0.8);
- netatmo_getDevices($hash) if( !$had_token );
- InternalTimer($hash->{expires_at}, "netatmo_refreshTokenTimer", $hash);
- } else {
- $hash->{expires_at} = int(gettimeofday());
- $hash->{STATE} = "Error" if( !$hash->{access_token} );
- Log3 $name, 1, "$name: token error ".Dumper($json);
- InternalTimer(gettimeofday()+600, "netatmo_refreshTokenTimer", $hash);
- }
- }
- sub
- netatmo_parseAppToken($$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- $hash->{access_token_app} = $json->{access_token};
- $hash->{refresh_token_app} = $json->{refresh_token};
- if( $hash->{access_token_app} ) {
- $hash->{expires_at_app} = int(gettimeofday());
- $hash->{expires_at_app} += int($json->{expires_in}*0.8);
- InternalTimer($hash->{expires_at_app}, "netatmo_refreshAppTokenTimer", $hash);
- } else {
- $hash->{expires_at_app} = int(gettimeofday());
- $hash->{STATE} = "Error" if( !$hash->{access_token_app} );
- Log3 $name, 1, "$name: app token error ".Dumper($json);
- InternalTimer(gettimeofday()+600, "netatmo_refreshAppTokenTimer", $hash);
- }
- }
- sub
- netatmo_parseDeviceList($$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 4, "$name: parsedevicelist ";
- #my $do_autocreate = 1;
- #$do_autocreate = 0 if( !defined($hash->{helper}{devices}) ); #autocreate
- my @devices = ();
- foreach my $device (@{$json->{body}{devices}}) {
- push( @devices, $device );
- foreach my $module (@{$device->{modules}}) {
- $module->{main_device} = $device->{_id};
- push( @devices, $module );
- }
- }
- $hash->{helper}{devices} = \@devices;
- #netatmo_autocreate($hash) if( $do_autocreate );
- }
- sub
- netatmo_parseHomeList($$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- #my $do_autocreate = 1;
- #$do_autocreate = 0 if( !defined($hash->{helper}{homes}) ); #autocreate
- Log3 $name, 5, "$name: parsehomelist";
- my @homes = ();
- foreach my $home (@{$json->{body}{homes}}) {
- push( @homes, $home ) if(defined($home->{cameras}) && @{$home->{cameras}});
- foreach my $camera (@{$home->{cameras}}) {
- $camera->{home} = $home->{id};
- push( @homes, $camera ) if(defined($camera->{status}));
- }
- foreach my $person (@{$home->{persons}}) {
- $person->{home} = $home->{id};
- push( @homes, $person ) if(defined($person->{face}));
- }
- }
- $hash->{helper}{homes} = \@homes;
- #netatmo_autocreatehome($hash) if( $do_autocreate );
- }
- sub
- netatmo_parseThermostatList($$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 4, "$name: parsethermostatlist ";
- #my $do_autocreate = 1;
- #$do_autocreate = 0 if( !defined($hash->{helper}{devices}) ); #autocreate
- my @devices = ();
- foreach my $home (@{$json->{body}{homes}}) {
-
- next if(!defined($home->{devices}) || !@{$home->{devices}});
- $home->{type} = "Home";
- $home->{firmware} = "-";
- push( @devices, $home );
-
- foreach my $room (@{$home->{rooms}}) {
- next if(!defined($room->{modules}) || !@{$room->{modules}});
- $room->{Home} = $home->{id};
- $room->{type} = "Room";
- $room->{firmware} = "-";
- push( @devices, $room );
- }
- foreach my $device (@{$home->{devices}}) {
- push( @devices, $device );
- foreach my $module (@{$device->{modules}}) {
- $module->{main_device} = $device->{id};
- push( @devices, $module );
- }
- }
- }
- $hash->{helper}{thermostats} = \@devices;
- #netatmo_autocreate($hash) if( $do_autocreate );
- }
- sub
- netatmo_parseHomecoachList($$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 4, "$name: parsehomecoachlist ";
- #my $do_autocreate = 1;
- #$do_autocreate = 0 if( !defined($hash->{helper}{devices}) ); #autocreate
- my @devices = ();
- foreach my $device (@{$json->{body}{devices}}) {
- push( @devices, $device );
- foreach my $module (@{$device->{modules}}) {
- $module->{main_device} = $device->{_id};
- push( @devices, $module );
- }
- }
- $hash->{helper}{homecoachs} = \@devices;
- #netatmo_autocreate($hash) if( $do_autocreate );
- }
- sub
- netatmo_updateReadings($$)
- {
- my($hash, $readings) = @_;
- my $name = $hash->{NAME};
- my ($seconds) = gettimeofday();
- my $latest = 0;
- if( $readings && @{$readings} ) {
- my $i = 0;
- foreach my $reading (sort { $a->[0] <=> $b->[0] } @{$readings}) {
- if(!defined($reading->[0]) || !defined($reading->[1]) || !defined($reading->[2]))
- {
- Log3 $name, 1, "$name: invalid readings set: ".Dumper($reading);
- next;
- }
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($reading->[0]);
- readingsBulkUpdate( $hash, $reading->[1], $reading->[2] );
- $hash->{CHANGETIME}[0] = FmtDateTime($reading->[0]);
- readingsEndUpdate($hash,1);
- $latest = $reading->[0] if( $reading->[0] > $latest );
- }
- readingsSingleUpdate( $hash, ".lastupdate", $seconds, 0 );
- Log3 $name, 4, "$name: updatereadings";
- }
- return ($seconds,$latest);
- }
- sub
- netatmo_parseReadings($$;$)
- {
- my($hash, $json, $requested) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 4, "$name: parsereadings ".$requested;
- my $reading_names = $hash->{helper}{readingNames};
- if( $requested ) {
- my @readings = split( ',', $requested );
- $reading_names = \@readings;
- }
- if( $json ) {
- $hash->{status} = $json->{status} if($json->{status});
- $hash->{status} = $json->{error}{message} if( $json->{error} );
- my $lastupdate = ReadingsVal( $name, ".lastupdate", 0 );
- my @r = ();
- my $readings = \@r;
- $readings = $hash->{readings} if( defined($hash->{readings}) );
- my ($time,$step_time,$last_time) = 0;
- if( $hash->{status} eq "ok" )
- {
- if(scalar(@{$json->{body}}) == 0)
- {
- $hash->{status} = "no data";
- readingsSingleUpdate( $hash, "active", "dead", 1 ) if($hash->{helper}{last_status_store} > 0 && $hash->{helper}{last_status_store} < (int(time) - 7200) );
- }
- foreach my $values ( @{$json->{body}}) {
- $time = $values->{beg_time};
- $step_time = $values->{step_time};
- foreach my $value (@{$values->{value}}) {
- my $i = 0;
- foreach my $reading (@{$value}) {
- #my $rname = $hash->{helper}{readingNames}[$i++];
- my $rname = lc($reading_names->[$i++]);
- if( !defined($reading) )
- {
- $reading = "undefined";#next;
- }
- if(lc($requested) =~ /wind/ && ($rname eq "temperature" || $rname eq "humidity"))
- {
- Log3 $name, 3, "$name netatmo - wind sensor $rname reading: $reading ($time)";
- next;# if($reading == 0);
- }
- if($reading ne "undefined" && (($rname eq "noise" && int($reading) > 150) || ($rname eq "temperature" && int($reading) > 60) || ($rname eq "humidity" && int($reading) > 100) || ($rname eq "pressure" && int($reading) < 500)))
- {
- Log3 $name, 1, "$name netatmo - invalid reading: $rname: ".Dumper($reading)." \n ".Dumper($reading_names);
- next;
- }
- if($reading ne "undefined" && $rname eq "health_idx"){
- $reading = $health_index{$reading};
- }
- if($reading ne "undefined" && $rname =~ /temperature/){
- $reading = sprintf( "%.1f", $reading);
- }
- # if($reading ne "undefined" && $rname eq "rain" && $reading > 0)
- # {
- # my $rain_sum = ReadingsVal($name,"rain_sum",0);
- # $rain_sum += $reading;
- # readingsSingleUpdate($hash,"rain_sum",$rain_sum,1);
- # Log3 $name, 2, $name.": summed rain ".$reading." (to ".$rain_sum.")";
- # }
- push(@{$readings}, [$time, $rname, $reading]) if($reading ne "undefined");
-
- }
- $last_time = $time if(defined($time));
- $time += $step_time if( $step_time );
- }
- $hash->{helper}{last_status_store} = $last_time if($last_time > $hash->{helper}{last_status_store});
- }
- if( $hash->{openRequests} > 1 ) {
- $hash->{readings} = $readings;
- } else {
- my ($seconds,undef) = netatmo_updateReadings( $hash, $readings );
- $hash->{LAST_POLL} = FmtDateTime( $seconds );
- delete $hash->{readings};
- # readingsSingleUpdate($hash, ".lastupdate", $last_time, 0);
- }
-
-
- if(defined(AttrVal($name, "interval", undef))){
- $hash->{helper}{NEXT_POLL} = int(gettimeofday())+$hash->{helper}{INTERVAL};
- RemoveInternalTimer($hash, "netatmo_poll");
- InternalTimer($hash->{helper}{NEXT_POLL}, "netatmo_poll", $hash);
- Log3 $name, 3, "$name: next fixed interval update for device ($requested) at ".FmtDateTime($hash->{helper}{NEXT_POLL});
- } elsif(defined($last_time) && int($last_time) > 0 && defined($step_time)) {
- my $nextdata = $last_time + 2*$step_time + 10 + int(rand(20));
-
- if($hash->{SUBTYPE} eq "MODULE")
- {
- my $devicehash = $modules{$hash->{TYPE}}{defptr}{"D$hash->{Device}"};
- if(defined($devicehash) && defined($devicehash->{helper}{NEXT_POLL}))
- {
- $nextdata = ($devicehash->{helper}{NEXT_POLL} + 10 + int(rand(20)) ) if($devicehash->{helper}{NEXT_POLL} >= gettimeofday()+150);
- if($nextdata >= (gettimeofday()+155))
- {
- RemoveInternalTimer($hash, "netatmo_poll");
- InternalTimer($nextdata, "netatmo_poll", $hash);
- $hash->{helper}{NEXT_POLL} = $nextdata;
- Log3 $name, 3, "$name: next dynamic update from device ($requested) at ".FmtDateTime($nextdata);
- } else {
- $nextdata += $step_time;
- if($nextdata >= (gettimeofday()+155))
- {
- RemoveInternalTimer($hash, "netatmo_poll");
- InternalTimer($nextdata, "netatmo_poll", $hash);
- $hash->{helper}{NEXT_POLL} = $nextdata;
- Log3 $name, 3, "$name: next extended dynamic update from device ($requested) at ".FmtDateTime($nextdata);
- } else {
- Log3 $name, 3, "$name: invalid time for dynamic update from device ($requested): ".FmtDateTime($nextdata);
- }
- }
- }
- }
- elsif($nextdata >= (gettimeofday()+280))
- {
- $nextdata = $nextdata + 10 + int(rand(20));
- RemoveInternalTimer($hash, "netatmo_poll");
- InternalTimer($nextdata, "netatmo_poll", $hash);
- $hash->{helper}{NEXT_POLL} = $nextdata;
- Log3 $name, 3, "$name: next dynamic update ($requested) at ".FmtDateTime($nextdata);
- } else {
- $nextdata += $step_time;
- if($nextdata >= (gettimeofday()+280))
- {
- RemoveInternalTimer($hash, "netatmo_poll");
- InternalTimer($nextdata, "netatmo_poll", $hash);
- $hash->{helper}{NEXT_POLL} = $nextdata;
- Log3 $name, 3, "$name: next extended dynamic update ($requested) at ".FmtDateTime($nextdata);
- } else {
- Log3 $name, 2, "$name: invalid time for dynamic update ($requested): ".FmtDateTime($nextdata);
- }
- }
- } elsif(defined($last_time) && int($last_time) > 0) {
- my $nextdata = int($last_time)+(12*60);
- $nextdata = int(gettimeofday()+280) if($nextdata <= (gettimeofday()+280));
- RemoveInternalTimer($hash, "netatmo_poll");
- InternalTimer($nextdata, "netatmo_poll", $hash);
- $hash->{helper}{NEXT_POLL} = $nextdata;
- Log3 $name, 3, "$name: next predictive update for device ($requested) at ".FmtDateTime($nextdata);
- } else {
- $hash->{helper}{NEXT_POLL} = int(gettimeofday())+(12*60);
- RemoveInternalTimer($hash, "netatmo_poll");
- InternalTimer($hash->{helper}{NEXT_POLL}, "netatmo_poll", $hash);
- Log3 $name, 3, "$name: next fixed update for device ($requested) at ".FmtDateTime($hash->{helper}{NEXT_POLL});
- }
- }
- }
- else
- {
- $hash->{status} = "error";
- }
- if($hash->{helper}{last_status_store} > 0 && $hash->{helper}{last_status_store} < (int(time) - $hash->{helper}{INTERVAL} - 7200) ) {
- readingsSingleUpdate( $hash, "active", "dead", 1 );
- } else {
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if($hash->{status} ne "no data");
- }
- }
- sub
- netatmo_parseGlobal($$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 4, "$name: parseGlobal";
- if( $json )
- {
- Log3 $name, 5, "$name: ".Dumper($json);
- $hash->{status} = $json->{status};
- $hash->{status} = $json->{error}{message} if( $json->{error} );
- my $lastupdate = ReadingsVal( $name, ".lastupdate", 0 );
- my @r = ();
- my $readings = \@r;
- $readings = $hash->{readings} if( defined($hash->{readings}) );
- if( $hash->{status} eq "ok" )
- {
- $hash->{STATE} = "Connected";
- foreach my $devicedata ( @{$json->{body}{devices}})
- {
- #Log3 $name, 5, "$name: device " . "D$devicedata->{_id} " .Dumper($devicedata);
- my $device = $modules{$hash->{TYPE}}{defptr}{"D$devicedata->{_id}"};
- next if (!defined($device));
- #Log3 $name, 4, "$name: device " . "D$devicedata->{_id} found";
- if(defined($devicedata->{dashboard_data}{AbsolutePressure}) && $devicedata->{dashboard_data}{AbsolutePressure} ne $devicedata->{dashboard_data}{Pressure})
- {
- readingsBeginUpdate($device);
- $device->{".updateTimestamp"} = FmtDateTime($devicedata->{dashboard_data}{time_utc});
- readingsBulkUpdate( $device, "pressure_abs", $devicedata->{dashboard_data}{AbsolutePressure}, 1 );
- $device->{CHANGETIME}[0] = FmtDateTime($devicedata->{dashboard_data}{time_utc});
- readingsEndUpdate($device,1);
- }
- if(defined($devicedata->{dashboard_data}{pressure_trend}))
- {
- readingsBeginUpdate($device);
- $device->{".updateTimestamp"} = FmtDateTime($devicedata->{dashboard_data}{time_utc});
- readingsBulkUpdate( $device, "pressure_trend", $devicedata->{dashboard_data}{pressure_trend}, 1 );
- $device->{CHANGETIME}[0] = FmtDateTime($devicedata->{dashboard_data}{time_utc});
- readingsEndUpdate($device,1);
- }
- if(defined($devicedata->{dashboard_data}{temp_trend}))
- {
- readingsBeginUpdate($device);
- $device->{".updateTimestamp"} = FmtDateTime($devicedata->{dashboard_data}{time_utc});
- readingsBulkUpdate( $device, "temp_trend", $devicedata->{dashboard_data}{temp_trend}, 1 );
- $device->{CHANGETIME}[0] = FmtDateTime($devicedata->{dashboard_data}{time_utc});
- readingsEndUpdate($device,1);
- }
- if(defined($devicedata->{dashboard_data}{max_temp}) && $devicedata->{type} ne "NAModule2")
- {
- readingsBeginUpdate($device);
- $device->{".updateTimestamp"} = FmtDateTime($devicedata->{dashboard_data}{date_max_temp});
- readingsBulkUpdate( $device, "temp_max", $devicedata->{dashboard_data}{max_temp}, 1 );
- $device->{CHANGETIME}[0] = FmtDateTime($devicedata->{dashboard_data}{date_max_temp});
- readingsEndUpdate($device,1);
- }
- if(defined($devicedata->{dashboard_data}{min_temp}) && $devicedata->{type} ne "NAModule2")
- {
- readingsBeginUpdate($device);
- $device->{".updateTimestamp"} = FmtDateTime($devicedata->{dashboard_data}{date_min_temp});
- readingsBulkUpdate( $device, "temp_min", $devicedata->{dashboard_data}{min_temp}, 1 );
- $device->{CHANGETIME}[0] = FmtDateTime($devicedata->{dashboard_data}{date_min_temp});
- readingsEndUpdate($device,1);
- }
- if(defined($devicedata->{dashboard_data}{sum_rain_1}))
- {
- readingsBeginUpdate($device);
- $device->{".updateTimestamp"} = FmtDateTime($devicedata->{dashboard_data}{time_utc});
- readingsBulkUpdate( $device, "rain_hour", $devicedata->{dashboard_data}{sum_rain_1}, 1 );
- $device->{CHANGETIME}[0] = FmtDateTime($devicedata->{dashboard_data}{time_utc});
- readingsEndUpdate($device,1);
- }
- if(defined($devicedata->{dashboard_data}{sum_rain_24}))
- {
- #my $rain_day = ReadingsVal($device->{NAME},"rain_day",0);
- # if($devicedata->{dashboard_data}{sum_rain_24} < $rain_day)
- # {
- # my $rain_total = ReadingsVal($device->{NAME},"rain_total",0);
- # $rain_total += $rain_day;
- # readingsSingleUpdate($device,"rain_total",$rain_total,1);
- # Log3 $name, 1, $device->{NAME}.": added rain ".$rain_day." (to ".$rain_total.")";
- # }
- readingsBeginUpdate($device);
- $device->{".updateTimestamp"} = FmtDateTime($devicedata->{dashboard_data}{time_utc});
- readingsBulkUpdate( $device, "rain_day", $devicedata->{dashboard_data}{sum_rain_24}, 1 );
- $device->{CHANGETIME}[0] = FmtDateTime($devicedata->{dashboard_data}{time_utc});
- readingsEndUpdate($device,1);
- }
- if(defined($devicedata->{dashboard_data}{max_wind_str}))
- {
- readingsBeginUpdate($device);
- $device->{".updateTimestamp"} = FmtDateTime($devicedata->{dashboard_data}{date_max_wind_str});
- readingsBulkUpdate( $device, "windstrength_max", $devicedata->{dashboard_data}{max_wind_str}, 1 );
- $device->{CHANGETIME}[0] = FmtDateTime($devicedata->{dashboard_data}{date_max_wind_str});
- readingsEndUpdate($device,1);
- }
- if(defined($devicedata->{dashboard_data}{max_wind_angle}))
- {
- readingsBeginUpdate($device);
- $device->{".updateTimestamp"} = FmtDateTime($devicedata->{dashboard_data}{date_max_wind_str});
- readingsBulkUpdate( $device, "windangle_max", $devicedata->{dashboard_data}{max_wind_angle}, 1 );
- $device->{CHANGETIME}[0] = FmtDateTime($devicedata->{dashboard_data}{date_max_wind_str});
- readingsEndUpdate($device,1);
- }
- if(defined($devicedata->{dashboard_data}{health_idx}) && $devicedata->{type} ne "NHC")
- {
- readingsBeginUpdate($device);
- $device->{".updateTimestamp"} = FmtDateTime($devicedata->{dashboard_data}{health_idx});
- readingsBulkUpdate( $device, "health_idx", $devicedata->{dashboard_data}{health_idx}, 1 );
- $device->{CHANGETIME}[0] = FmtDateTime($devicedata->{dashboard_data}{health_idx});
- readingsEndUpdate($device,1);
- }
- $device->{co2_calibrating} = $devicedata->{co2_calibrating} if(defined($devicedata->{co2_calibrating}));
- $device->{last_status_store} = FmtDateTime($devicedata->{last_status_store}) if(defined($devicedata->{last_status_store}));
- $device->{helper}{last_status_store} = $devicedata->{last_status_store} if(defined($devicedata->{last_status_store}) && $devicedata->{last_status_store} > $device->{helper}{last_status_store});
- $device->{last_message} = FmtDateTime($devicedata->{last_message}) if(defined($devicedata->{last_message}));
- $device->{last_seen} = FmtDateTime($devicedata->{last_seen}) if(defined($devicedata->{last_seen}));
- $device->{wifi_status} = $devicedata->{wifi_status} if(defined($devicedata->{wifi_status}));
- $device->{rf_status} = $devicedata->{rf_status} if(defined($devicedata->{rf_status}));
- #$device->{battery_percent} = $devicedata->{battery_percent} if(defined($devicedata->{battery_percent}));
- #$device->{battery_vp} = $devicedata->{battery_vp} if(defined($devicedata->{battery_vp}));
- readingsSingleUpdate($device, "batteryState", ($devicedata->{battery_percent} > 20) ? "ok" : "low", 1) if(defined($devicedata->{battery_percent}));
- readingsSingleUpdate($device, "batteryPercent", $devicedata->{battery_percent}, 1) if(defined($devicedata->{battery_percent}));
- readingsSingleUpdate($device, "batteryVoltage", $devicedata->{battery_vp}/1000, 1) if(defined($devicedata->{battery_vp}));
- if(defined($devicedata->{modules}))
- {
- foreach my $moduledata ( @{$devicedata->{modules}})
- {
- #Log3 $name, 5, "$name: module "."M$moduledata->{_id} ".Dumper($moduledata);
- my $module = $modules{$hash->{TYPE}}{defptr}{"M$moduledata->{_id}"};
- next if (!defined($module));
- if(defined($moduledata->{dashboard_data}{AbsolutePressure}))
- {
- readingsBeginUpdate($module);
- $module->{".updateTimestamp"} = FmtDateTime($moduledata->{dashboard_data}{time_utc});
- readingsBulkUpdate( $module, "pressure_abs", $moduledata->{dashboard_data}{AbsolutePressure}, 1 );
- $module->{CHANGETIME}[0] = FmtDateTime($moduledata->{dashboard_data}{time_utc});
- readingsEndUpdate($module,1);
- }
- if(defined($moduledata->{dashboard_data}{pressure_trend}))
- {
- readingsBeginUpdate($module);
- $module->{".updateTimestamp"} = FmtDateTime($moduledata->{dashboard_data}{time_utc});
- readingsBulkUpdate( $module, "pressure_trend", $moduledata->{dashboard_data}{pressure_trend}, 1 );
- $module->{CHANGETIME}[0] = FmtDateTime($moduledata->{dashboard_data}{time_utc});
- readingsEndUpdate($module,1);
- }
- if(defined($moduledata->{dashboard_data}{temp_trend}))
- {
- readingsBeginUpdate($module);
- $module->{".updateTimestamp"} = FmtDateTime($moduledata->{dashboard_data}{time_utc});
- readingsBulkUpdate( $module, "temp_trend", $moduledata->{dashboard_data}{temp_trend}, 1 );
- $module->{CHANGETIME}[0] = FmtDateTime($moduledata->{dashboard_data}{time_utc});
- readingsEndUpdate($module,1);
- }
- if(defined($moduledata->{dashboard_data}{max_temp}) && $moduledata->{type} ne "NAModule2")
- {
- readingsBeginUpdate($module);
- $module->{".updateTimestamp"} = FmtDateTime($moduledata->{dashboard_data}{date_max_temp});
- readingsBulkUpdate( $module, "temp_max", $moduledata->{dashboard_data}{max_temp}, 1 );
- $module->{CHANGETIME}[0] = FmtDateTime($moduledata->{dashboard_data}{date_max_temp});
- readingsEndUpdate($module,1);
- }
- if(defined($moduledata->{dashboard_data}{min_temp}) && $moduledata->{type} ne "NAModule2")
- {
- readingsBeginUpdate($module);
- $module->{".updateTimestamp"} = FmtDateTime($moduledata->{dashboard_data}{date_min_temp});
- readingsBulkUpdate( $module, "temp_min", $moduledata->{dashboard_data}{min_temp}, 1 );
- $module->{CHANGETIME}[0] = FmtDateTime($moduledata->{dashboard_data}{date_min_temp});
- readingsEndUpdate($module,1);
- }
- if(defined($moduledata->{dashboard_data}{sum_rain_1}))
- {
- readingsBeginUpdate($module);
- $module->{".updateTimestamp"} = FmtDateTime($moduledata->{dashboard_data}{time_utc});
- readingsBulkUpdate( $module, "rain_hour", $moduledata->{dashboard_data}{sum_rain_1}, 1 );
- $module->{CHANGETIME}[0] = FmtDateTime($moduledata->{dashboard_data}{time_utc});
- readingsEndUpdate($module,1);
- }
- if(defined($moduledata->{dashboard_data}{sum_rain_24}))
- {
- # my $rain_day = ReadingsVal($module->{NAME},"rain_day",0);
- # if($moduledata->{dashboard_data}{sum_rain_24} < $rain_day)
- # {
- # my $rain_total = ReadingsVal($module->{NAME},"rain_total",0);
- # $rain_total += $rain_day;
- # readingsSingleUpdate($module,"rain_total",$rain_total,1);
- # Log3 $name, 1, $module->{NAME}.":_added rain ".$rain_day." (to ".$rain_total.")";
- # }
- readingsBeginUpdate($module);
- $module->{".updateTimestamp"} = FmtDateTime($moduledata->{dashboard_data}{time_utc});
- readingsBulkUpdate( $module, "rain_day", $moduledata->{dashboard_data}{sum_rain_24}, 1 );
- $module->{CHANGETIME}[0] = FmtDateTime($moduledata->{dashboard_data}{time_utc});
- readingsEndUpdate($module,1);
- }
- if(defined($moduledata->{dashboard_data}{max_wind_str}))
- {
- readingsBeginUpdate($module);
- $module->{".updateTimestamp"} = FmtDateTime($moduledata->{dashboard_data}{date_max_wind_str});
- readingsBulkUpdate( $module, "windstrength_max", $moduledata->{dashboard_data}{max_wind_str}, 1 );
- $module->{CHANGETIME}[0] = FmtDateTime($moduledata->{dashboard_data}{date_max_wind_str});
- readingsEndUpdate($module,1);
- }
- if(defined($moduledata->{dashboard_data}{max_wind_angle}))
- {
- readingsBeginUpdate($module);
- $module->{".updateTimestamp"} = FmtDateTime($moduledata->{dashboard_data}{date_max_wind_str});
- readingsBulkUpdate( $module, "windangle_max", $moduledata->{dashboard_data}{max_wind_angle}, 1 );
- $module->{CHANGETIME}[0] = FmtDateTime($moduledata->{dashboard_data}{date_max_wind_str});
- readingsEndUpdate($module,1);
- }
- $module->{co2_calibrating} = $moduledata->{co2_calibrating} if(defined($moduledata->{co2_calibrating}));
- $module->{last_status_store} = FmtDateTime($moduledata->{last_status_store}) if(defined($moduledata->{last_status_store}));
- $module->{helper}{last_status_store} = $moduledata->{last_status_store} if(defined($moduledata->{last_status_store}) && $moduledata->{last_status_store} > $module->{helper}{last_status_store});
- $module->{last_message} = FmtDateTime($moduledata->{last_message}) if(defined($moduledata->{last_message}));
- $module->{last_seen} = FmtDateTime($moduledata->{last_seen}) if(defined($moduledata->{last_seen}));
- $module->{wifi_status} = $moduledata->{wifi_status} if(defined($moduledata->{wifi_status}));
- $module->{rf_status} = $moduledata->{rf_status} if(defined($moduledata->{rf_status}));
- #$module->{battery_percent} = $moduledata->{battery_percent} if(defined($moduledata->{battery_percent}));
- #$module->{battery_vp} = $moduledata->{battery_vp} if(defined($moduledata->{battery_vp}));
- readingsSingleUpdate($module, "batteryState", ($moduledata->{battery_percent} > 20) ? "ok" : "low", 1) if(defined($moduledata->{battery_percent}));
- readingsSingleUpdate($module, "batteryPercent", $moduledata->{battery_percent}, 1) if(defined($moduledata->{battery_percent}));
- readingsSingleUpdate($module, "batteryVoltage", $moduledata->{battery_vp}/1000, 1) if(defined($moduledata->{battery_vp}));
- }#foreach module
- }#defined modules
- }#foreach devices
- }#ok
- }#json
- else
- {
- $hash->{status} = "error";
- }
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if($hash->{status} ne "no data");
- return undef;
- }
- sub
- netatmo_parseForecast($$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 4, "$name: parseForecast";
- if( $json )
- {
- Log3 $name, 5, "$name: ".Dumper($json);
- $hash->{status} = $json->{status};
- $hash->{status} = $json->{error}{message} if( $json->{error} );
- my $lastupdate = ReadingsVal( $name, ".lastupdate", 0 );
- if( $hash->{status} eq "ok" )
- {
- #$hash->{STATE} = "Connected";
-
- my $datatime = time;
- my $forecasttime = time;
-
- $hash->{stationname} = encode_utf8($json->{body}{stationname}) if(defined($json->{body}{stationname}));
- $hash->{city} = encode_utf8($json->{body}{cityname}) if(defined($json->{body}{cityname}));
-
- if(defined($json->{body}{current_temp_time}))
- {
- $hash->{time_data} = FmtDateTime($json->{body}{current_temp_time});
- $datatime = $json->{body}{current_temp_time};
- }
- if(defined($json->{body}{time_current_symbol}))
- {
- $hash->{time_forecast} = FmtDateTime($json->{body}{time_current_symbol});
- $forecasttime = $json->{body}{time_current_symbol};
- }
- return undef if($datatime <= $lastupdate);
- readingsSingleUpdate($hash, ".lastupdate", $datatime, 0);
- if($json->{body}{airqdata})
- {
- if(defined($json->{body}{airqdata}{data}))
- {
- #CommandDeleteReading( undef, "$hash->{NAME} air_.*" );
- foreach my $airdata ( @{$json->{body}{airqdata}{data}})
- {
- my $timestamp = $airdata->{beg_time};
- foreach my $airvalue ( @{$airdata->{value}})
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($timestamp);
- readingsBulkUpdate( $hash, "air_".@{$airvalue}[1], @{$airvalue}[0], 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($timestamp);
- readingsEndUpdate($hash,1);
- next if(!defined(@{$airvalue}[2]));
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($timestamp);
- readingsBulkUpdate( $hash, "air_".@{$airvalue}[1]."_message", @{$airvalue}[2], 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($timestamp);
- readingsEndUpdate($hash,1);
- }
- }
- }
- }#airqdata
-
- if(defined($json->{body}{current_windgust}))
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($datatime);
- readingsBulkUpdate( $hash, "windgust", $json->{body}{current_windgust}, 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($datatime);
- readingsEndUpdate($hash,1);
- }
- if(defined($json->{body}{current_windstrength}))
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($datatime);
- readingsBulkUpdate( $hash, "windstrength", $json->{body}{current_windstrength}, 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($datatime);
- readingsEndUpdate($hash,1);
- }
- if(defined($json->{body}{current_temp}))
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($datatime);
- readingsBulkUpdate( $hash, "temperature", $json->{body}{current_temp}, 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($datatime);
- readingsEndUpdate($hash,1);
- }
- if(defined($json->{body}{current_symbol}))
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($forecasttime);
- readingsBulkUpdate( $hash, "symbol", $json->{body}{current_symbol}, 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($forecasttime);
- readingsEndUpdate($hash,1);
- }
-
- if(defined($json->{body}{forecastDays}))
- {
- my $i = 0;
- foreach my $forecastdata ( @{$json->{body}{forecastDays}})
- {
- next if(ref($forecastdata) ne "HASH");
- if(defined($forecastdata->{rain}))
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($forecasttime);
- readingsBulkUpdate( $hash, "fc".$i."_rain", $forecastdata->{rain}, 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($forecasttime);
- readingsEndUpdate($hash,1);
- }
- if(defined($forecastdata->{max_temp}))
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($forecasttime);
- readingsBulkUpdate( $hash, "fc".$i."_temp_max", $forecastdata->{max_temp}, 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($forecasttime);
- readingsEndUpdate($hash,1);
- }
- if(defined($forecastdata->{min_temp}))
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($forecasttime);
- readingsBulkUpdate( $hash, "fc".$i."_temp_min", $forecastdata->{min_temp}, 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($forecasttime);
- readingsEndUpdate($hash,1);
- }
- if(defined($forecastdata->{windangle}))
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($forecasttime);
- readingsBulkUpdate( $hash, "fc".$i."_windangle", $forecastdata->{windangle}, 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($forecasttime);
- readingsEndUpdate($hash,1);
- }
- if(defined($forecastdata->{wind_direction}))
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($forecasttime);
- readingsBulkUpdate( $hash, "fc".$i."_wind_direction", $forecastdata->{wind_direction}, 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($forecasttime);
- readingsEndUpdate($hash,1);
- }
- if(defined($forecastdata->{windgust}))
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($forecasttime);
- readingsBulkUpdate( $hash, "fc".$i."_windgust", $forecastdata->{windgust}, 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($forecasttime);
- readingsEndUpdate($hash,1);
- }
- if(defined($forecastdata->{sun}))
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($forecasttime);
- readingsBulkUpdate( $hash, "fc".$i."_sun", $forecastdata->{sun}, 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($forecasttime);
- readingsEndUpdate($hash,1);
- }
- if(defined($forecastdata->{uv}))
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($forecasttime);
- readingsBulkUpdate( $hash, "fc".$i."_uv", $forecastdata->{uv}, 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($forecasttime);
- readingsEndUpdate($hash,1);
- }
- if(defined($forecastdata->{sunset}))
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($forecasttime);
- readingsBulkUpdate( $hash, "fc".$i."_sunset", FmtDateTime($forecastdata->{sunset}), 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($forecasttime);
- readingsEndUpdate($hash,1);
- }
- if(defined($forecastdata->{sunrise}))
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($forecasttime);
- readingsBulkUpdate( $hash, "fc".$i."_sunrise", FmtDateTime($forecastdata->{sunrise}), 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($forecasttime);
- readingsEndUpdate($hash,1);
- }
- if(defined($forecastdata->{day_locale}))
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($forecasttime);
- readingsBulkUpdate( $hash, "fc".$i."_day", encode_utf8($forecastdata->{day_locale}), 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($forecasttime);
- readingsEndUpdate($hash,1);
- }
- if(defined($forecastdata->{weather_symbol_day}))
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($forecasttime);
- readingsBulkUpdate( $hash, "fc".$i."_symbol_day", $forecastdata->{weather_symbol_day}, 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($forecasttime);
- readingsEndUpdate($hash,1);
- }
- if(defined($forecastdata->{weather_symbol_night}))
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($forecasttime);
- readingsBulkUpdate( $hash, "fc".$i."_symbol_night", $forecastdata->{weather_symbol_night}, 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($forecasttime);
- readingsEndUpdate($hash,1);
- }
-
- $i++;
- }#foreach forecast
- }#defined forecastdays
- }#ok
- }#json
- else
- {
- $hash->{status} = "error";
- }
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if($hash->{status} ne "no data");
- return undef;
- }
- sub
- netatmo_parseHomeReadings($$;$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 4, "$name: parseHomeReadings";
- if( $json ) {
- Log3 $name, 5, "$name: ".Dumper($json);
- $hash->{status} = "ok";
- $hash->{status} = $json->{error}{message} if( $json->{error} );
- my $lastupdate = ReadingsVal( $name, ".lastupdate", 0 );
- my @r = ();
- my $readings = \@r;
- $readings = $hash->{readings} if( defined($hash->{readings}) );
- if( $hash->{status} eq "ok" )
- {
- #$hash->{STATE} = "Connected";
- return undef if(!defined($json->{body}{homes}));
- foreach my $homedata ( @{$json->{body}{homes}})
- {
- next if($homedata->{id} ne $hash->{Home});
- readingsSingleUpdate($hash, "name", encode_utf8($homedata->{name}), 1) if(defined($homedata->{name}));
- readingsSingleUpdate($hash, "presence_record_humans", $homedata->{presence_record_humans}, 1) if(defined($homedata->{presence_record_humans}));
- readingsSingleUpdate($hash, "presence_record_vehicles", $homedata->{presence_record_vehicles}, 1) if(defined($homedata->{presence_record_vehicles}));
- readingsSingleUpdate($hash, "presence_record_animals", $homedata->{presence_record_animals}, 1) if(defined($homedata->{presence_record_animals}));
- readingsSingleUpdate($hash, "presence_record_movements", $homedata->{presence_record_movements}, 1) if(defined($homedata->{presence_record_movements}));
- readingsSingleUpdate($hash, "presence_record_alarms", $homedata->{presence_record_alarms}, 1) if(defined($homedata->{presence_record_alarms}));
- readingsSingleUpdate($hash, "gone_after", sprintf("%02d",(int($homedata->{gone_after}/60)/60)).":".sprintf("%02d",(int($homedata->{gone_after}/60)%60)), 1) if(defined($homedata->{gone_after}));
- readingsSingleUpdate($hash, "smart_notifs", ($homedata->{smart_notifs} eq "1")?"on":"off", 1) if(defined($homedata->{smart_notifs}));
- readingsSingleUpdate($hash, "presence_enable_notify_from_to", $homedata->{presence_enable_notify_from_to}, 1) if(defined($homedata->{presence_enable_notify_from_to}));
- readingsSingleUpdate($hash, "presence_notify_from", sprintf("%02d",(int($homedata->{presence_notify_from}/60)/60)).":".sprintf("%02d",(int($homedata->{presence_notify_from}/60)%60)), 1) if(defined($homedata->{presence_notify_from}));
- readingsSingleUpdate($hash, "presence_notify_to", sprintf("%02d",(int($homedata->{presence_notify_to}/60)/60)).":".sprintf("%02d",(int($homedata->{presence_notify_to}/60)%60)), 1) if(defined($homedata->{presence_notify_to}));
- readingsSingleUpdate($hash, "notify_unknowns", $homedata->{notify_unknowns}, 1) if(defined($homedata->{notify_unknowns}));
- readingsSingleUpdate($hash, "notify_movements", $homedata->{notify_movements}, 1) if(defined($homedata->{notify_movements}));
- readingsSingleUpdate($hash, "notify_animals", ($homedata->{notify_animals} eq "1")?"true":"false", 1) if(defined($homedata->{notify_animals}));
- readingsSingleUpdate($hash, "record_animals", ($homedata->{record_animals} eq "1")?"true":"false", 1) if(defined($homedata->{record_animals}));
- readingsSingleUpdate($hash, "record_alarms", $homedata->{record_alarms}, 1) if(defined($homedata->{record_alarms}));
- readingsSingleUpdate($hash, "record_movements", $homedata->{record_movements}, 1) if(defined($homedata->{record_movements}));
- if( $homedata->{place} ) {
- $hash->{country} = encode_utf8($homedata->{place}{country}) if(defined($homedata->{place}{country}));
- $hash->{bssid} = $homedata->{place}{bssid} if(defined($homedata->{place}{bssid}));
- $hash->{altitude} = $homedata->{place}{altitude} if(defined($homedata->{place}{altitude}));
- $hash->{city} = encode_utf8($homedata->{place}{geoip_city}) if(defined($homedata->{place}{geoip_city}));
- $hash->{city} = encode_utf8($homedata->{place}{city}) if(defined($homedata->{place}{city}));;
- $hash->{location} = $homedata->{place}{location}[1] .",". $homedata->{place}{location}[0] if(defined($homedata->{place}{location}));
- $hash->{timezone} = encode_utf8($homedata->{place}{timezone}) if(defined($homedata->{place}{timezone}));
- }
- if(defined($homedata->{persons}))
- {
- foreach my $persondata ( @{$homedata->{persons}})
- {
- my $person = $modules{$hash->{TYPE}}{defptr}{"P$persondata->{id}"};
- next if (!defined($person));
- readingsSingleUpdate($person, "pseudo", encode_utf8($persondata->{pseudo}), 1) if(defined($persondata->{pseudo}));
- readingsSingleUpdate($person, "last_seen", FmtDateTime($persondata->{last_seen}), 1) if(defined($persondata->{last_seen}));
- readingsSingleUpdate($person, "out_of_sight", $persondata->{out_of_sight}, 1) if(defined($persondata->{out_of_sight}));
- readingsSingleUpdate($person, "status", (($persondata->{out_of_sight} eq "0") ? "home" : "away"), 1) if(defined($persondata->{out_of_sight}));
- #$person->{STATE} = ($persondata->{out_of_sight} eq "0") ? "home" : "away";
- readingsSingleUpdate($person, "face_id", $persondata->{face}{id}, 0) if(defined($persondata->{face}{id}));
- readingsSingleUpdate($person, "face_key", $persondata->{face}{key}, 0) if(defined($persondata->{face}{key}));
- readingsSingleUpdate($person, "face_version", $persondata->{face}{version}, 1) if(defined($persondata->{face}{version}));
- }
- }
- if(defined($homedata->{cameras}))
- {
- foreach my $cameradata ( @{$homedata->{cameras}})
- {
- my $camera = $modules{$hash->{TYPE}}{defptr}{"C$cameradata->{id}"};
- next if (!defined($camera));
- readingsSingleUpdate($camera, "name", encode_utf8($cameradata->{name}), 1) if(defined($cameradata->{name}));
- readingsSingleUpdate($camera, "status", $cameradata->{status}, 1) if(defined($cameradata->{status}));
- #$camera->{STATE} = ($cameradata->{status} eq "on") ? "online" : "offline";
- readingsSingleUpdate($camera, "sd_status", $cameradata->{sd_status}, 0) if(defined($cameradata->{sd_status}));
- readingsSingleUpdate($camera, "alim_status", $cameradata->{alim_status}, 0) if(defined($cameradata->{alim_status}));
- readingsSingleUpdate($camera, "is_local", $cameradata->{is_local}, 1) if(defined($cameradata->{is_local}));
- readingsSingleUpdate($camera, "vpn_url", $cameradata->{vpn_url}, 1) if(defined($cameradata->{vpn_url}));
- CommandDeleteReading( undef, "$camera->{NAME} vpn_url" ) if(!defined($cameradata->{vpn_url}));
- CommandDeleteReading( undef, "$camera->{NAME} local_url" ) if(!defined($cameradata->{vpn_url}));
- readingsSingleUpdate($camera, "light_mode", $cameradata->{light_mode_status}, 1) if(defined($cameradata->{light_mode_status}));
- readingsSingleUpdate($camera, "timelapse_available", $cameradata->{timelapse_available}, 0) if(defined($cameradata->{timelapse_available}));
- delete($camera->{pin}) if($cameradata->{status} eq "on");
-
- $camera->{model} = $cameradata->{type} if(defined($cameradata->{type}));
- $camera->{firmware} = $cameradata->{firmware} if(defined($cameradata->{firmware}));
-
- foreach my $tagdata ( @{$cameradata->{modules}})
- {
- my $tag = $modules{$hash->{TYPE}}{defptr}{"G$tagdata->{id}"};
- next if (!defined($tag));
- readingsSingleUpdate($tag, "name", encode_utf8($tagdata->{name}), 1) if(defined($tagdata->{name}));
- readingsSingleUpdate($tag, "status", $tagdata->{status}, 1) if(defined($tagdata->{status}));
- readingsSingleUpdate($tag, "category", $tagdata->{category}, 1) if(defined($tagdata->{category}));
- $tag->{model} = $tagdata->{type};
- $tag->{last_activity} = FmtDateTime($tagdata->{last_activity}) if(defined($tagdata->{last_activity}));
- $tag->{last_seen} = FmtDateTime($tagdata->{last_seen}) if(defined($tagdata->{last_seen}));
- $tag->{rf} = $tagdata->{rf};
- $tag->{notify_rule} = $tagdata->{notify_rule};
- $tag->{notify_rule} = $tagdata->{notify_rule};
- readingsSingleUpdate($tag, "batteryState", ($tagdata->{battery_percent} > 20) ? "ok" : "low", 1) if(defined($tagdata->{battery_percent}));
- readingsSingleUpdate($tag, "batteryPercent", $tagdata->{battery_percent}, 1) if(defined($tagdata->{battery_percent}));
- readingsSingleUpdate($tag, "batteryVoltage", $tagdata->{battery_vp}/1000, 1) if(defined($tagdata->{battery_vp}));
- }
-
- }
- }
- if(defined($homedata->{events}))
- {
- my @eventslist = @{$homedata->{events}};
- my $eventdata;
- while ($eventdata = pop( @eventslist ))
- {
- $eventdata->{time} = time() if(!defined($eventdata->{time}));
- next if($eventdata->{time} <= $lastupdate);
- readingsSingleUpdate($hash, ".lastupdate", $eventdata->{time}, 0);
- Log3 $name, 4, "$name: new event: ".FmtDateTime($eventdata->{time});
- if(defined($eventdata->{event_list}))
- {
- my @singleeventslist = @{$eventdata->{event_list}};
- my $singleeventdata;
- while ($singleeventdata = pop( @singleeventslist ))
- {
- if(defined($singleeventdata->{message}))
- {
- my $eventmessage = $singleeventdata->{message};
- $eventmessage = "-" if(!defined($singleeventdata->{message}));
- $eventmessage =~ s/<\/b>//g;
- $eventmessage =~ s/<b>//g;
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($singleeventdata->{time});
- readingsBulkUpdate( $hash, "event", encode_utf8($eventmessage), 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($singleeventdata->{time});
- readingsEndUpdate($hash,1);
- }
- if(defined($singleeventdata->{snapshot}{key}))
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($singleeventdata->{time});
- readingsBulkUpdate( $hash, "last_snapshot", "https://api.netatmo.com/api/getcamerapicture?image_id=".$singleeventdata->{snapshot}{id}."&key=".$singleeventdata->{snapshot}{key}, 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($singleeventdata->{time});
- readingsEndUpdate($hash,1);
- }
- }
- }
- else
- {
- my $eventmessage = $eventdata->{message};
- $eventmessage = "-" if(!defined($eventdata->{message}));
- $eventmessage =~ s/<\/b>//g;
- $eventmessage =~ s/<b>//g;
- if(defined($eventdata->{message}))
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $hash, "event", encode_utf8($eventmessage), 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($hash,1);
- }
- if(defined($eventdata->{snapshot}))
- {
- readingsBeginUpdate($hash);
- $hash->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $hash, "last_snapshot", "https://api.netatmo.com/api/getcamerapicture?image_id=".$eventdata->{snapshot}{id}."&key=".$eventdata->{snapshot}{key}, 1 );
- $hash->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($hash,1);
- }
- }
- my $camera = $modules{$hash->{TYPE}}{defptr}{"C$eventdata->{camera_id}"};
- my $tag = $modules{$hash->{TYPE}}{defptr}{"G$eventdata->{module_id}"} if(defined($eventdata->{module_id}));
- my $person = $modules{$hash->{TYPE}}{defptr}{"P$eventdata->{person_id}"} if(defined($eventdata->{person_id}));
- if (defined($camera))
- {
- my $lastupdate = ReadingsVal( $camera->{NAME}, ".lastupdate", 0 );
- next if($eventdata->{time} <= $lastupdate);
- readingsSingleUpdate($camera, ".lastupdate", $eventdata->{time}, 0);
- if(defined($eventdata->{event_list}))
- {
- my @singleeventslist = @{$eventdata->{event_list}};
- my $singleeventdata;
- while ($singleeventdata = pop( @singleeventslist ))
- {
- if(defined($singleeventdata->{message}))
- {
- my $cameraname = ReadingsVal( $camera->{NAME}, "name", "Welcome" );
- my $eventmessage = $singleeventdata->{message};
- $eventmessage =~ s/<b>//g;
- $eventmessage =~ s/<\/b>//g;
- $eventmessage =~ s/$cameraname: //g;
- $eventmessage =~ s/$cameraname /Camera /g;
- readingsBeginUpdate($camera);
- $camera->{".updateTimestamp"} = FmtDateTime($singleeventdata->{time});
- readingsBulkUpdate( $camera, "event", encode_utf8($eventmessage), 1 );
- $camera->{CHANGETIME}[0] = FmtDateTime($singleeventdata->{time});
- readingsEndUpdate($camera,1);
- }
- if(defined($singleeventdata->{time}))
- {
- readingsBeginUpdate($camera);
- $camera->{".updateTimestamp"} = FmtDateTime($singleeventdata->{time});
- readingsBulkUpdate( $camera, "event_time", FmtDateTime($singleeventdata->{time}), 1 );
- $camera->{CHANGETIME}[0] = FmtDateTime($singleeventdata->{time});
- readingsEndUpdate($camera,1);
- }
- if(defined($singleeventdata->{type}))
- {
- readingsBeginUpdate($camera);
- $camera->{".updateTimestamp"} = FmtDateTime($singleeventdata->{time});
- readingsBulkUpdate( $camera, "event_type", $singleeventdata->{type}, 1 );
- $camera->{CHANGETIME}[0] = FmtDateTime($singleeventdata->{time});
- readingsEndUpdate($camera,1);
- }
- if(defined($singleeventdata->{id}))
- {
- readingsBeginUpdate($camera);
- $camera->{".updateTimestamp"} = FmtDateTime($singleeventdata->{time});
- readingsBulkUpdate( $camera, "event_id", $singleeventdata->{id}, 1 );
- $camera->{CHANGETIME}[0] = FmtDateTime($singleeventdata->{time});
- readingsEndUpdate($camera,1);
- }
- if(defined($singleeventdata->{snapshot}{filename}))
- {
- readingsBeginUpdate($camera);
- $camera->{".updateTimestamp"} = FmtDateTime($singleeventdata->{time});
- readingsBulkUpdate( $camera, "filename", $singleeventdata->{snapshot}{filename}, 1 );
- $camera->{CHANGETIME}[0] = FmtDateTime($singleeventdata->{time});
- readingsEndUpdate($camera,1);
- }
-
- if(defined($singleeventdata->{snapshot}{key}))
- {
- readingsBeginUpdate($camera);
- $camera->{".updateTimestamp"} = FmtDateTime($singleeventdata->{time});
- readingsBulkUpdate( $camera, "snapshot", $singleeventdata->{snapshot}{id}."|".$singleeventdata->{snapshot}{key}, 1 );
- $camera->{CHANGETIME}[0] = FmtDateTime($singleeventdata->{time});
- readingsEndUpdate($camera,1);
- }
- if(defined($singleeventdata->{snapshot}{key}))
- {
- readingsBeginUpdate($camera);
- $camera->{".updateTimestamp"} = FmtDateTime($singleeventdata->{time});
- readingsBulkUpdate( $camera, "last_snapshot", "https://api.netatmo.com/api/getcamerapicture?image_id=".$singleeventdata->{snapshot}{id}."&key=".$singleeventdata->{snapshot}{key}, 1 );
- $camera->{CHANGETIME}[0] = FmtDateTime($singleeventdata->{time});
- readingsEndUpdate($camera,1);
- }
- }
- }
- else
- {
- if(defined($eventdata->{message}))
- {
- my $cameraname = ReadingsVal( $camera->{NAME}, "name", "Welcome" );
- my $eventmessage = $eventdata->{message};
- $eventmessage =~ s/<b>//g;
- $eventmessage =~ s/<\/b>//g;
- $eventmessage =~ s/$cameraname: //g;
- $eventmessage =~ s/$cameraname /Camera /g;
- readingsBeginUpdate($camera);
- $camera->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $camera, "event", encode_utf8($eventmessage), 1 );
- $camera->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($camera,1);
- }
- if(defined($eventdata->{time}))
- {
- readingsBeginUpdate($camera);
- $camera->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $camera, "event_time", FmtDateTime($eventdata->{time}), 1 );
- $camera->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($camera,1);
- }
- if(defined($eventdata->{type}))
- {
- readingsBeginUpdate($camera);
- $camera->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $camera, "event_type", $eventdata->{type}, 1 );
- $camera->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($camera,1);
- }
- if(defined($eventdata->{id}))
- {
- readingsBeginUpdate($camera);
- $camera->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $camera, "event_id", $eventdata->{id}, 1 );
- $camera->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($camera,1);
- }
- if(defined($person))
- {
- readingsBeginUpdate($camera);
- $camera->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $camera, "person_seen", ReadingsVal($person->{NAME},"pseudo","Unknown"), 1 );
- $camera->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($camera,1);
- }
- if(defined($eventdata->{snapshot}))
- {
- readingsBeginUpdate($camera);
- $camera->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $camera, "snapshot", $eventdata->{snapshot}{id}."|".$eventdata->{snapshot}{key}, 1 );
- $camera->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($camera,1);
- }
- if(defined($eventdata->{snapshot}))
- {
- readingsBeginUpdate($camera);
- $camera->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $camera, "last_snapshot", "https://api.netatmo.com/api/getcamerapicture?image_id=".$eventdata->{snapshot}{id}."&key=".$eventdata->{snapshot}{key}, 1 );
- $camera->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($camera,1);
- }
- }
-
- if(defined($eventdata->{video_status}))
- {
- readingsBeginUpdate($camera);
- $camera->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $camera, "video_status", $eventdata->{video_status}, 1 );
- $camera->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($camera,1);
- }
- if(defined($eventdata->{video_id}))
- {
- readingsBeginUpdate($camera);
- $camera->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $camera, "video_id", $eventdata->{video_id}, 1 );
- $camera->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($camera,1);
- }
- }
- if (defined($tag))
- {
- my $lastupdate = ReadingsVal( $tag->{NAME}, ".lastupdate", 0 );
- next if($eventdata->{time} <= $lastupdate);
- readingsSingleUpdate($tag, ".lastupdate", $eventdata->{time}, 0);
- if(defined($eventdata->{message}))
- {
- my $tagname = ReadingsVal( $tag->{NAME}, "name", "Tag" );
- my $eventmessage = $eventdata->{message};
- $eventmessage =~ s/<b>//g;
- $eventmessage =~ s/<\/b>//g;
- $eventmessage =~ s/ by $tagname//g;
- $eventmessage =~ s/$tagname /Tag /g;
- readingsBeginUpdate($tag);
- $tag->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $tag, "event", encode_utf8($eventmessage), 1 );
- $tag->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($tag,1);
- }
- if(defined($eventdata->{time}))
- {
- readingsBeginUpdate($tag);
- $tag->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $tag, "event_time", FmtDateTime($eventdata->{time}), 1 );
- $tag->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($tag,1);
- }
- if(defined($eventdata->{type}))
- {
- readingsBeginUpdate($tag);
- $tag->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $tag, "event_type", $eventdata->{type}, 1 );
- $tag->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($tag,1);
- }
- if(defined($eventdata->{id}))
- {
- readingsBeginUpdate($tag);
- $tag->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $tag, "event_id", $eventdata->{id}, 1 );
- $tag->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($tag,1);
- }
- if(defined($eventdata->{snapshot}))
- {
- readingsBeginUpdate($tag);
- $tag->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $tag, "snapshot", $eventdata->{snapshot}{id}."|".$eventdata->{snapshot}{key}, 1 );
- $tag->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($tag,1);
- }
- if(defined($eventdata->{video_status}))
- {
- readingsBeginUpdate($tag);
- $tag->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $tag, "video_status", $eventdata->{video_status}, 1 );
- $tag->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($tag,1);
- }
- if(defined($eventdata->{video_id}))
- {
- readingsBeginUpdate($tag);
- $tag->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $tag, "video_id", $eventdata->{video_id}, 1 );
- $tag->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($tag,1);
- }
- if(defined($eventdata->{snapshot}))
- {
- readingsBeginUpdate($tag);
- $tag->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $tag, "last_snapshot", "https://api.netatmo.com/api/getcamerapicture?image_id=".$eventdata->{snapshot}{id}."&key=".$eventdata->{snapshot}{key}, 1 );
- $tag->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($tag,1);
- }
- }
- if (defined($person))
- {
- my $lastupdate = ReadingsVal( $person->{NAME}, ".lastupdate", 0 );
- next if($eventdata->{time} <= $lastupdate);
- readingsSingleUpdate($person, ".lastupdate", $eventdata->{time}, 0);
- readingsSingleUpdate($person, "last_seen", FmtDateTime($eventdata->{time}), 1) if(defined($eventdata->{time}));
- readingsSingleUpdate($person, "last_arrival", FmtDateTime($eventdata->{time}), 1) if(defined($eventdata->{time}) && defined($eventdata->{is_arrival}) && $eventdata->{is_arrival} eq "1");
- if(defined($camera))
- {
- readingsBeginUpdate($person);
- $person->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $person, "camera", ReadingsVal($camera->{NAME},"name","Unknown"), 1 );
- $person->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($person,1);
- }
- if(defined($eventdata->{id}))
- {
- readingsBeginUpdate($person);
- $person->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $person, "event_id", $eventdata->{id}, 1 );
- $person->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($person,1);
- }
- if(defined($eventdata->{video_status}))
- {
- readingsBeginUpdate($person);
- $person->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $person, "video_status", $eventdata->{video_status}, 1 );
- $person->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($person,1);
- }
- if(defined($eventdata->{video_id}))
- {
- readingsBeginUpdate($person);
- $person->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $person, "video_id", $eventdata->{video_id}, 1 );
- $person->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($person,1);
- }
- if(defined($eventdata->{snapshot}))
- {
- readingsBeginUpdate($person);
- $person->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $person, "snapshot", $eventdata->{snapshot}{id}."|".$eventdata->{snapshot}{key}, 1 );
- $person->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($person,1);
- }
- if(defined($eventdata->{snapshot}))
- {
- readingsBeginUpdate($person);
- $person->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $person, "last_snapshot", "https://api.netatmo.com/api/getcamerapicture?image_id=".$eventdata->{snapshot}{id}."&key=".$eventdata->{snapshot}{key}, 1 );
- $person->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($person,1);
- }
- }
- }
- }
- my $time = $homedata->{time_server};
- }
- }
- }
- else
- {
- $hash->{status} = "error";
- }
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if($hash->{status} ne "no data");
- }
- sub
- netatmo_refreshHomeSettings($)
- {
- my($hash) = @_;
- my $name = $hash->{NAME};
-
- InternalTimer(gettimeofday()+5, "netatmo_poll", $hash);
-
- return undef;
- }
- sub
- netatmo_parseCameraPing($$;$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 4, "$name: parseCameraPing";
- if( $json ) {
- Log3 $name, 5, "$name: ".Dumper($json);
- $hash->{status} = $json->{status};
- $hash->{status} = $json->{error}{message} if( $json->{error} );
- my $lastupdate = ReadingsVal( $name, ".lastupdate", 0 );
- readingsSingleUpdate($hash, "local_url", $json->{local_url}, 1) if(defined($json->{local_url}));
- CommandDeleteReading( undef, "$hash->{NAME} local_url" ) if(!defined($json->{local_url}));
- }
- else
- {
- $hash->{status} = "error";
- if(ReadingsVal( $name, "status", "ok" ) eq "disconnected"){
- $hash->{status} = "disconnected";
- RemoveInternalTimer($hash);
- }
- }
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if(defined($hash->{status}) && $hash->{status} ne "no data");
- }
- sub
- netatmo_parseCameraStatus($$;$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 4, "$name: parseCameraStatus";
- my $home = $modules{$hash->{TYPE}}{defptr}{"H$hash->{Home}"};
- if( $json ) {
- Log3 $name, 5, "$name: ".Dumper($json);
- $hash->{status} = "ok";
- $hash->{status} = $json->{error}{message} if( $json->{error} );
- InternalTimer( gettimeofday() + 10, "netatmo_pollHome", $home) if($hash->{status} eq "ok" );
- }
- else{
- netatmo_pollHome($home) if($home->{status} !~ /usage/ && $home->{status} !~ /too_many_connections/ && $home->{status} !~ /postponed/);
- }
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if($hash->{status} ne "no data");
- }
- sub
- netatmo_parseCameraConfig($$;$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 4, "$name: parseCameraConfig";
- my $home = $modules{$hash->{TYPE}}{defptr}{"H$hash->{Home}"};
- if( $json ) {
- Log3 $name, 5, "$name: ".Dumper($json);
- $hash->{status} = "ok";
- $hash->{status} = $json->{error}{message} if( $json->{error} );
- return undef if($hash->{status} ne "ok");
-
- readingsBeginUpdate($hash);
- readingsBulkUpdate( $hash, "intensity", $json->{intensity}, 1 ) if( $json->{intensity} );
- readingsBulkUpdate( $hash, "light_mode", $json->{mode}, 1 ) if( $json->{mode} );
- readingsBulkUpdate( $hash, "night_always", ($json->{night}{always}?"true":"false"), 1 ) if( $json->{night} );
- readingsBulkUpdate( $hash, "night_person", ($json->{night}{person}?"true":"false"), 1 ) if( $json->{night} );
- readingsBulkUpdate( $hash, "night_vehicle", ($json->{night}{vehicle}?"true":"false"), 1 ) if( $json->{night} );
- readingsBulkUpdate( $hash, "night_animal", ($json->{night}{animal}?"true":"false"), 1 ) if( $json->{night} );
- readingsBulkUpdate( $hash, "night_movement", ($json->{night}{movement}?"true":"false"), 1 ) if( $json->{night} );
- readingsEndUpdate( $hash, 1);
- InternalTimer( gettimeofday() + 10, "netatmo_pollHome", $home) if($hash->{status} eq "ok" );
- }
- else{
- netatmo_pollHome($home) if($home->{status} !~ /usage/ && $home->{status} !~ /too_many_connections/ && $home->{status} !~ /postponed/);
- }
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if($hash->{status} ne "no data");
- }
- sub
- netatmo_parseTagStatus($$;$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 4, "$name: parseTagStatus";
- if( $json ) {
- Log3 $name, 5, "$name: ".Dumper($json);
- $hash->{status} = $json->{status};
- $hash->{status} = $json->{error}{message} if( $json->{error} );
- readingsSingleUpdate($hash, "status", "calibrating", 1) if($hash->{status} eq "ok");
- }
- else
- {
- $hash->{status} = "error";
- }
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if($hash->{status} ne "no data");
- }
- sub
- netatmo_parseCameraVideo($$;$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 4, "$name: parseCameraVideo";
- if( $json ) {
- Log3 $name, 5, "$name: ".Dumper($json);
- $hash->{status} = $json->{status};
- $hash->{status} = $json->{error}{message} if( $json->{error} );
- return undef if($hash->{status} ne "ok");
- return undef if($hash->{status} ne "ok");
-
- my $lastupdate = ReadingsVal( $name, ".lastupdate", 0 );
- readingsSingleUpdate($hash, "local_url", $json->{local_url}, 1) if(defined($json->{local_url}));
- }
- else
- {
- $hash->{status} = "error";
- }
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if($hash->{status} ne "no data");
- }
- sub
- netatmo_parsePersonReadings($$;$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 4, "$name: parsePersonReadings";
- if( $json ) {
- Log3 $name, 5, "$name: ".Dumper($json);
- $hash->{status} = $json->{status};
- $hash->{status} = $json->{error}{message} if( $json->{error} );
-
- my $lastupdate = ReadingsVal( $name, ".lastupdate", 0 );
- if( $hash->{status} eq "ok" )
- {
- #$hash->{STATE} = "Connected";
- if(defined($json->{body}{events_list}))
- {
- my @eventslist = @{$json->{body}{events_list}};
- my $eventdata;
- while ($eventdata = pop( @eventslist ))
- {
- next if(!defined($eventdata->{person_id}));
- next if($eventdata->{time} <= $lastupdate);
- next if($eventdata->{person_id} ne $hash->{Person});
- $eventdata->{time} = time() if(!defined($eventdata->{time}));
- readingsSingleUpdate($hash, ".lastupdate", $eventdata->{time}, 0);
- Log3 $name, 4, "$name: new event: ".FmtDateTime($eventdata->{time});
- my $camera = $modules{$hash->{TYPE}}{defptr}{"C$eventdata->{camera_id}"};
- my $person = $modules{$hash->{TYPE}}{defptr}{"P$eventdata->{person_id}"} if(defined($eventdata->{person_id}));
- if (defined($person))
- {
- readingsSingleUpdate($person, "last_seen", FmtDateTime($eventdata->{time}), 1) if(defined($eventdata->{time}));
- readingsSingleUpdate($person, "last_arrival", FmtDateTime($eventdata->{time}), 1) if(defined($eventdata->{time}) && defined($eventdata->{is_arrival}) && $eventdata->{is_arrival} eq "1");
- if(defined($camera))
- {
- readingsBeginUpdate($person);
- $person->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $person, "camera", ReadingsVal($camera->{NAME},"name","Unknown"), 1 );
- $person->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($person,1);
- }
- if(defined($eventdata->{id}))
- {
- readingsBeginUpdate($person);
- $person->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $person, "event_id", $eventdata->{id}, 1 );
- $person->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($person,1);
- }
- if(defined($eventdata->{video_status}))
- {
- readingsBeginUpdate($person);
- $person->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $person, "video_status", $eventdata->{video_status}, 1 );
- $person->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($person,1);
- }
- if(defined($eventdata->{video_id}))
- {
- readingsBeginUpdate($person);
- $person->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $person, "video_id", $eventdata->{video_id}, 1 );
- $person->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($person,1);
- }
- if(defined($eventdata->{snapshot}))
- {
- readingsBeginUpdate($person);
- $person->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $person, "snapshot", $eventdata->{snapshot}{id}."|".$eventdata->{snapshot}{key}, 1 );
- $person->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($person,1);
- }
- if(defined($eventdata->{snapshot}))
- {
- readingsBeginUpdate($person);
- $person->{".updateTimestamp"} = FmtDateTime($eventdata->{time});
- readingsBulkUpdate( $person, "last_snapshot", "https://api.netatmo.com/api/getcamerapicture?image_id=".$eventdata->{snapshot}{id}."&key=".$eventdata->{snapshot}{key}, 1 );
- $person->{CHANGETIME}[0] = FmtDateTime($eventdata->{time});
- readingsEndUpdate($person,1);
- }
- }
- }
- }
- }
- }
- else
- {
- $hash->{status} = "error";
- }
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if($hash->{status} ne "no data");
- }
- sub
- netatmo_parseThermostatReadings($$;$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 4, "$name: parseThermostatReadings";
- if( $json ) {
- Log3 $name, 5, "$name: ".Dumper($json);
- $hash->{status} = $json->{status};
- $hash->{status} = $json->{error}{message} if( $json->{error} );
-
- my $lastupdate = ReadingsVal( $name, ".lastupdate", 0 );
- my @r = ();
- my $readings = \@r;
- $readings = $hash->{readings} if( defined($hash->{readings}) );
- if( $hash->{status} eq "ok" )
- {
- foreach my $devicedata ( @{$json->{body}{devices}})
- {
- my $hash = $modules{$hash->{TYPE}}{defptr}{"R$devicedata->{_id}"};
- next if (!defined($hash));
- next if($devicedata->{_id} ne $hash->{Relay});
- #$hash->{STATE} = "Connected";
- readingsSingleUpdate($hash, "name", encode_utf8($devicedata->{station_name}), 1) if(defined($devicedata->{station_name}));
- $hash->{stationName} = encode_utf8($devicedata->{station_name}) if( $devicedata->{station_name} );
- $hash->{moduleName} = encode_utf8($devicedata->{module_name}) if( $devicedata->{module_name} );
- $hash->{model} = $devicedata->{type} if(defined($devicedata->{type}));
- $hash->{firmware} = $devicedata->{firmware} if(defined($devicedata->{firmware}));
- $hash->{last_upgrade} = FmtDateTime($devicedata->{last_upgrade}) if(defined($devicedata->{last_upgrade}));
- $hash->{date_setup} = FmtDateTime($devicedata->{date_setup}) if(defined($devicedata->{date_setup}));
- $hash->{last_setup} = FmtDateTime($devicedata->{last_setup}) if(defined($devicedata->{last_setup}));
- $hash->{last_status_store} = FmtDateTime($devicedata->{last_status_store}) if(defined($devicedata->{last_status_store}));
- $hash->{helper}{last_status_store} = $devicedata->{last_status_store} if(defined($devicedata->{last_status_store}) && $devicedata->{last_status_store} > $hash->{helper}{last_status_store});
- $hash->{last_message} = FmtDateTime($devicedata->{last_message}) if(defined($devicedata->{last_message}));
- $hash->{last_seen} = FmtDateTime($devicedata->{last_seen}) if(defined($devicedata->{last_seen}));
- $hash->{last_plug_seen} = FmtDateTime($devicedata->{last_plug_seen}) if(defined($devicedata->{last_plug_seen}));
- $hash->{last_therm_seen} = FmtDateTime($devicedata->{last_therm_seen}) if(defined($devicedata->{last_therm_seen}));
- $hash->{wifi_status} = $devicedata->{wifi_status} if(defined($devicedata->{wifi_status}));
- $hash->{rf_status} = $devicedata->{rf_status} if(defined($devicedata->{rf_status}));
- #$hash->{battery_percent} = $devicedata->{battery_percent} if(defined($devicedata->{battery_percent}));
- #$hash->{battery_vp} = $devicedata->{battery_vp} if(defined($devicedata->{battery_vp}));
- $hash->{therm_orientation} = $devicedata->{therm_orientation} if(defined($devicedata->{therm_orientation}));
- $hash->{therm_relay_cmd} = $devicedata->{therm_relay_cmd} if(defined($devicedata->{therm_relay_cmd}));
- $hash->{udp_conn} = $devicedata->{udp_conn} if(defined($devicedata->{udp_conn}));
- $hash->{plug_connected_boiler} = $devicedata->{plug_connected_boiler} if(defined($devicedata->{plug_connected_boiler}));
- $hash->{syncing} = $devicedata->{syncing} if(defined($devicedata->{syncing}));
- $hash->{room} = $devicedata->{room} if(defined($devicedata->{room}));
- if( $devicedata->{place} ) {
- $hash->{country} = encode_utf8($devicedata->{place}{country});
- $hash->{bssid} = $devicedata->{place}{bssid} if(defined($devicedata->{place}{bssid}));
- $hash->{altitude} = $devicedata->{place}{altitude} if(defined($devicedata->{place}{altitude}));
- $hash->{city} = encode_utf8($devicedata->{place}{geoip_city}) if(defined($devicedata->{place}{geoip_city}));
- $hash->{city} = encode_utf8($devicedata->{place}{city}) if(defined($devicedata->{place}{city}));;
- $hash->{location} = $devicedata->{place}{location}[1] .",". $devicedata->{place}{location}[0];
- $hash->{timezone} = encode_utf8($devicedata->{place}{timezone});
- }
- readingsSingleUpdate($hash, "batteryState", ($devicedata->{battery_percent} > 20) ? "ok" : "low", 1) if(defined($devicedata->{battery_percent}));
- readingsSingleUpdate($hash, "batteryPercent", $devicedata->{battery_percent}, 1) if(defined($devicedata->{battery_percent}));
- readingsSingleUpdate($hash, "batteryVoltage", $devicedata->{battery_vp}/1000, 1) if(defined($devicedata->{battery_vp}));
- if(defined($devicedata->{modules}))
- {
- foreach my $moduledata ( @{$devicedata->{modules}})
- {
- my $module = $modules{$hash->{TYPE}}{defptr}{"T$moduledata->{_id}"};
- next if (!defined($module));
- $module->{stationName} = encode_utf8($moduledata->{station_name}) if( $moduledata->{station_name} );
- $module->{moduleName} = encode_utf8($moduledata->{module_name}) if( $moduledata->{module_name} );
- $module->{model} = $moduledata->{type} if(defined($moduledata->{type}));
- $module->{firmware} = $moduledata->{firmware} if(defined($moduledata->{firmware}));
- $module->{last_upgrade} = FmtDateTime($moduledata->{last_upgrade}) if(defined($moduledata->{last_upgrade}));
- $module->{date_setup} = FmtDateTime($moduledata->{date_setup}) if(defined($moduledata->{date_setup}));
- $module->{last_setup} = FmtDateTime($moduledata->{last_setup}) if(defined($moduledata->{last_setup}));
- $module->{last_status_store} = FmtDateTime($moduledata->{last_status_store}) if(defined($moduledata->{last_status_store}));
- $module->{helper}{last_status_store} = $moduledata->{last_status_store} if(defined($moduledata->{last_status_store}) && $moduledata->{last_status_store} > $module->{helper}{last_status_store});
- $module->{last_message} = FmtDateTime($moduledata->{last_message}) if(defined($moduledata->{last_message}));
- $module->{last_seen} = FmtDateTime($moduledata->{last_seen}) if(defined($moduledata->{last_seen}));
- $module->{last_plug_seen} = FmtDateTime($moduledata->{last_plug_seen}) if(defined($moduledata->{last_plug_seen}));
- $module->{last_therm_seen} = FmtDateTime($moduledata->{last_therm_seen}) if(defined($moduledata->{last_therm_seen}));
- $module->{wifi_status} = $moduledata->{wifi_status} if(defined($moduledata->{wifi_status}));
- $module->{rf_status} = $moduledata->{rf_status} if(defined($moduledata->{rf_status}));
- #$module->{battery_percent} = $moduledata->{battery_percent} if(defined($moduledata->{battery_percent}));
- #$module->{battery_vp} = $moduledata->{battery_vp} if(defined($moduledata->{battery_vp}));
- $module->{therm_orientation} = $moduledata->{therm_orientation} if(defined($moduledata->{therm_orientation}));
- #$module->{therm_relay_cmd} = $moduledata->{therm_relay_cmd} if(defined($moduledata->{therm_relay_cmd}));
- $module->{udp_conn} = $moduledata->{udp_conn} if(defined($moduledata->{udp_conn}));
- $module->{plug_connected_boiler} = $moduledata->{plug_connected_boiler} if(defined($moduledata->{plug_connected_boiler}));
- $module->{syncing} = $moduledata->{syncing} if(defined($moduledata->{syncing}));
- $module->{room} = $moduledata->{room} if(defined($moduledata->{room}));
- if( $moduledata->{place} ) {
- $module->{country} = $moduledata->{place}{country};
- $module->{bssid} = $moduledata->{place}{bssid} if(defined($moduledata->{place}{bssid}));
- $module->{altitude} = $moduledata->{place}{altitude} if(defined($moduledata->{place}{altitude}));
- $module->{city} = encode_utf8($moduledata->{place}{geoip_city}) if(defined($moduledata->{place}{geoip_city}));
- $module->{city} = encode_utf8($moduledata->{place}{city}) if(defined($moduledata->{place}{city}));;
- $module->{location} = $moduledata->{place}{location}[1] .",". $moduledata->{place}{location}[0];
- $module->{timezone} = encode_utf8($moduledata->{place}{timezone});
- }
- readingsSingleUpdate($module, "batteryState", ($moduledata->{battery_percent} > 20) ? "ok" : "low", 1) if(defined($moduledata->{battery_percent}));
- readingsSingleUpdate($module, "batteryPercent", $moduledata->{battery_percent}, 1) if(defined($moduledata->{battery_percent}));
- readingsSingleUpdate($module, "batteryVoltage", $moduledata->{battery_vp}/1000, 1) if(defined($moduledata->{battery_vp}));
- #readingsSingleUpdate($module, "name", encode_utf8($moduledata->{module_name}), 1) if(defined($moduledata->{module_name}));
- my $setmode = "manual";
- if(defined($moduledata->{setpoint}))
- {
- readingsSingleUpdate($module, "setpoint_mode", $moduledata->{setpoint}{setpoint_mode}, 1) if(defined($moduledata->{setpoint}{setpoint_mode}));
- readingsSingleUpdate($module, "setpoint_endtime", FmtDateTime($moduledata->{setpoint}{setpoint_endtime}), 1) if(defined($moduledata->{setpoint}{setpoint_endtime}));
- CommandDeleteReading( undef, "$module->{NAME} setpoint_endtime" ) if(!defined($moduledata->{setpoint}{setpoint_endtime}));
- $setmode = $moduledata->{setpoint}{setpoint_mode} if(defined($moduledata->{setpoint}{setpoint_mode}));
- }
- readingsSingleUpdate($module, "therm_relay_cmd", $moduledata->{therm_relay_cmd}, 1) if(defined($moduledata->{therm_relay_cmd}));
- if(defined($moduledata->{measured}{setpoint_temp}))
- {
- readingsBeginUpdate($module);
- $module->{".updateTimestamp"} = FmtDateTime($moduledata->{measured}{time});
- readingsBulkUpdate( $module, "setpoint_temp", sprintf( "%.1f", $moduledata->{measured}{setpoint_temp}), 1 );
- $module->{CHANGETIME}[0] = FmtDateTime($moduledata->{measured}{time});
- readingsEndUpdate($module,1);
- $setmode = sprintf( "%.1f", $moduledata->{measured}{setpoint_temp}) if($setmode ne "max" && $setmode ne "off");
- }
- readingsSingleUpdate($module, "setpoint", $setmode, 1);
- my @s = ();
- my $schedules = \@s;
- my @schedulelist;
- foreach my $scheduledata ( @{$moduledata->{therm_program_list}})
- {
- my $program = encode_utf8($scheduledata->{name});
- $program =~ s/ /_/g;
- push(@{$schedules}, [$program, $scheduledata->{program_id}]);
- push(@schedulelist, $program);
- if(defined($scheduledata->{selected}))
- {
- readingsSingleUpdate($module, "program", $program, 1);
- }
- }
- $module->{schedules} = $schedules;
- $module->{schedulenames} = join(',', @schedulelist);
- }
- }
- #my $time = $devicedata->{time_server};
- }
- }
- }
- else
- {
- $hash->{status} = "error";
- }
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if($hash->{status} ne "no data");
- }
- sub
- netatmo_parseThermostatStatus($$;$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 4, "$name: parseThermostatStatus";
- my $thermostat = $modules{$hash->{TYPE}}{defptr}{"T$hash->{Thermostat}"};
- if( $json ) {
- Log3 $name, 5, "$name: ".Dumper($json);
- $hash->{status} = $json->{status};
- $hash->{status} = $json->{error}{message} if( $json->{error} );
- InternalTimer( gettimeofday() + 10, "netatmo_pollRelay", $thermostat) if($hash->{status} eq "ok");
- } else {
- netatmo_pollRelay($thermostat) if($thermostat->{status} !~ /usage/ && $thermostat->{status} !~ /too_many_connections/ && $thermostat->{status} !~ /postponed/);;
- }
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if($hash->{status} ne "no data");
- }
- sub
- netatmo_parseHeatingHomeStatus($$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 4, "$name: parseHeatingHomeStatus";
- my $thermostat = $modules{$hash->{TYPE}}{defptr}{"E$hash->{Home}"};
- Log3 $name, 5, "$name: parseHeatingHomeStatus ".Dumper($json);
- return undef;
- }
- sub
- netatmo_parsePublic($$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 5, "$name: parsepublic ".Dumper($json);
- if( $json ) {
- $hash->{status} = $json->{status};
- $hash->{status} = $json->{error}{message} if( $json->{error} );
- if( $hash->{status} eq "ok" ) {
- if( $hash->{Lat} && $hash->{Lon} )
- {
- my $found = 0;
- my @readings = ();
- my @readings_temperature = ();
- my @readings_humidity = ();
- my @readings_pressure = ();
- my @readings_rain = ();
- my @readings_rain_1 = ();
- my @readings_rain_24 = ();
- my @readings_wind_angle = ();
- my @readings_wind_strength = ();
- my @readings_gust_angle = ();
- my @readings_gust_strength = ();
- my @timestamps_temperature = ();
- my @timestamps_pressure = ();
- my @timestamps_rain = ();
- my @timestamps_wind = ();
- my @readings_altitude = ();
- my @readings_latitude = ();
- my @readings_longitude = ();
- my $devices = $json->{body};
- if( ref($devices) eq "ARRAY" ) {
- foreach my $device (@{$devices}) {
- $found++;
- #next if( $device->{_id} ne $hash->{Device} );
- next if( ref($device->{measures}) ne "HASH" );
- if(defined($device->{place}))
- {
- push(@readings_altitude, $device->{place}{altitude}) if(defined($device->{place}{altitude}));
- push(@readings_latitude, $device->{place}{location}[1]) if(defined($device->{place}{location}));
- push(@readings_longitude, $device->{place}{location}[0]) if(defined($device->{place}{location}));
- }
- foreach my $module ( keys %{$device->{measures}}) {
- #next if( ref($device->{measures}->{$module}->{res}) ne "HASH" );
- if(defined($device->{measures}->{$module}->{rain_live}))
- {
- push(@readings_rain, $device->{measures}->{$module}->{rain_live});
- push(@readings_rain_1, $device->{measures}->{$module}->{rain_60min});
- push(@readings_rain_24, $device->{measures}->{$module}->{rain_24h});
- push(@timestamps_rain, $device->{measures}->{$module}->{rain_timeutc});
- next;
- }
- if(defined($device->{measures}->{$module}->{wind_strength}))
- {
- push(@readings_wind_angle, $device->{measures}->{$module}->{wind_angle});
- push(@readings_wind_strength, $device->{measures}->{$module}->{wind_strength});
- push(@readings_gust_angle, $device->{measures}->{$module}->{gust_angle});
- push(@readings_gust_strength, $device->{measures}->{$module}->{gust_strength});
- push(@timestamps_wind, $device->{measures}->{$module}->{wind_timeutc});
- next;
- }
- foreach my $timestamp ( keys %{$device->{measures}->{$module}->{res}} ) {
- #next if( $hash->{LAST_POLL} && $timestamp <= $hash->{LAST_POLL} );
- my $i = 0;
- foreach my $value ( @{$device->{measures}->{$module}->{res}->{$timestamp}} ) {
- my $type = $device->{measures}->{$module}->{type}[$i];
- ++$i;
- if(lc($type) eq "pressure")
- {
- push(@timestamps_pressure, $timestamp);
- push(@readings_pressure, $value);
- next;
- }
- else
- {
- push(@timestamps_temperature, $timestamp) if(lc($type) eq "temperature");
- push(@readings_temperature, $value) if(lc($type) eq "temperature");
- push(@readings_humidity, $value) if(lc($type) eq "humidity");
- next;
- }
- }
- }
- }
- #$found = 1;
- #last;
- }
- }
- @readings_temperature = sort {$a <=> $b} @readings_temperature;
- @readings_humidity = sort {$a <=> $b} @readings_humidity;
- @readings_pressure = sort {$a <=> $b} @readings_pressure;
- @readings_rain = sort {$a <=> $b} @readings_rain;
- @readings_rain_1 = sort {$a <=> $b} @readings_rain_1;
- @readings_rain_24 = sort {$a <=> $b} @readings_rain_24;
- @readings_wind_angle = sort {$a <=> $b} @readings_wind_angle;
- @readings_wind_strength = sort {$a <=> $b} @readings_wind_strength;
- @readings_gust_angle = sort {$a <=> $b} @readings_gust_angle;
- @readings_gust_strength = sort {$a <=> $b} @readings_gust_strength;
- @timestamps_temperature = sort {$a <=> $b} @timestamps_temperature;
- @timestamps_pressure = sort {$a <=> $b} @timestamps_pressure;
- @timestamps_rain = sort {$a <=> $b} @timestamps_rain;
- @timestamps_wind = sort {$a <=> $b} @timestamps_wind;
- @readings_altitude = sort {$a <=> $b} @readings_altitude;
- @readings_latitude = sort {$a <=> $b} @readings_latitude;
- @readings_longitude = sort {$a <=> $b} @readings_longitude;
- if(scalar(@readings_temperature) > 4)
- {
- for (my $i=0;$i<scalar(@readings_temperature)/10;$i++)
- {
- pop @readings_temperature;
- pop @readings_humidity;
- pop @timestamps_temperature;
- shift @readings_temperature;
- shift @readings_humidity;
- shift @timestamps_temperature;
- }
- }
- if(scalar(@readings_pressure) > 4)
- {
- for (my $i=0;$i<scalar(@readings_pressure)/10;$i++)
- {
- pop @readings_pressure;
- pop @timestamps_pressure;
- shift @readings_pressure;
- shift @timestamps_pressure;
- }
- }
- if(scalar(@readings_rain) > 4)
- {
- for (my $i=0;$i<scalar(@readings_rain)/20;$i++)
- {
- pop @readings_rain;
- pop @readings_rain_1;
- pop @readings_rain_24;
- pop @timestamps_rain;
- shift @readings_rain;
- shift @readings_rain_1;
- shift @readings_rain_24;
- shift @timestamps_rain;
- }
- }
- if(scalar(@readings_wind_strength) > 4)
- {
- for (my $i=0;$i<scalar(@readings_wind_strength)/25;$i++)
- {
- pop @readings_wind_strength;
- pop @readings_gust_strength;
- pop @timestamps_wind;
- shift @readings_wind_strength;
- shift @readings_gust_strength;
- shift @timestamps_wind;
- }
- }
- if(scalar(@readings_pressure) > 4)
- {
- for (my $i=0;$i<scalar(@readings_pressure)/20;$i++)
- {
- pop @readings_altitude;
- pop @readings_latitude;
- pop @readings_longitude;
- shift @readings_altitude;
- shift @readings_latitude;
- shift @readings_longitude;
- }
- }
- my $avg_temperature = 0;
- my $min_temperature = 100;
- my $max_temperature = -100;
- foreach my $val (@readings_temperature)
- {
- $avg_temperature += $val / scalar(@readings_temperature);
- $min_temperature = $val if($val < $min_temperature);
- $max_temperature = $val if($val > $max_temperature);
- }
- my $avg_humidity = 0;
- my $min_humidity = 100;
- my $max_humidity = -100;
- foreach my $val (@readings_humidity)
- {
- $avg_humidity += $val / scalar(@readings_humidity);
- $min_humidity = $val if($val < $min_humidity);
- $max_humidity = $val if($val > $max_humidity);
- }
- my $avgtime_temperature = 0;
- foreach my $val (@timestamps_temperature)
- {
- $avgtime_temperature += $val / scalar(@timestamps_temperature);
- }
- my $avg_pressure = 0;
- my $min_pressure = 2000;
- my $max_pressure = -2000;
- foreach my $val (@readings_pressure)
- {
- $avg_pressure += $val / scalar(@readings_pressure);
- $min_pressure = $val if($val < $min_pressure);
- $max_pressure = $val if($val > $max_pressure);
- }
- my $avgtime_pressure = 0;
- foreach my $val (@timestamps_pressure)
- {
- $avgtime_pressure += $val / scalar(@timestamps_pressure);
- }
- my $avg_rain = 0;
- my $min_rain = 1000;
- my $max_rain = -1000;
- foreach my $val (@readings_rain)
- {
- $avg_rain += $val / scalar(@readings_rain);
- $min_rain = $val if($val < $min_rain);
- $max_rain = $val if($val > $max_rain);
- }
- my $avg_rain_1 = 0;
- my $min_rain_1 = 1000;
- my $max_rain_1 = -1000;
- foreach my $val (@readings_rain_1)
- {
- $avg_rain_1 += $val / scalar(@readings_rain_1);
- $min_rain_1 = $val if($val < $min_rain_1);
- $max_rain_1 = $val if($val > $max_rain_1);
- }
- my $avg_rain_24 = 0;
- my $min_rain_24 = 1000;
- my $max_rain_24 = -1000;
- foreach my $val (@readings_rain_24)
- {
- $avg_rain_24 += $val / scalar(@readings_rain_24);
- $min_rain_24 = $val if($val < $min_rain_24);
- $max_rain_24 = $val if($val > $max_rain_24);
- }
- my $avgtime_rain = 0;
- foreach my $val (@timestamps_rain)
- {
- $avgtime_rain += $val / scalar(@timestamps_rain);
- }
- my $avg_wind = 0;
- my $min_wind = 100;
- my $max_wind = -100;
- foreach my $val (@readings_wind_strength)
- {
- $avg_wind += $val / scalar(@readings_wind_strength);
- $min_wind = $val if($val < $min_wind);
- $max_wind = $val if($val > $max_wind);
- }
- my $avg_gust = 0;
- my $min_gust = 100;
- my $max_gust = -100;
- foreach my $val (@readings_gust_strength)
- {
- $avg_gust += $val / scalar(@readings_gust_strength);
- $min_gust = $val if($val < $min_gust);
- $max_gust = $val if($val > $max_gust);
- }
- my $angle_wind_x = 0;
- my $angle_wind_y = 0;
- foreach my $val (@readings_wind_angle)
- {
- next if($val == -1);
- Log3 $name, 5, "$name: wind angle ".$val;
- $angle_wind_x += cos($val);
- $angle_wind_y += sin($val);
- }
- my $angle_wind = atan2($angle_wind_x,$angle_wind_y);
- $angle_wind = ($angle_wind >= 0 ? $angle_wind : (2* pi + $angle_wind)) * 180/ pi;
- Log3 $name, 4, "$name: wind angle avg ".$angle_wind;
- my $angle_gust_x = 0;
- my $angle_gust_y = 0;
- foreach my $val (@readings_gust_angle)
- {
- next if($val == -1);
- Log3 $name, 5, "$name: gust angle ".$val;
- $angle_gust_x += cos($val);
- $angle_gust_y += sin($val);
- }
- my $angle_gust = atan2($angle_gust_x,$angle_gust_y);
- $angle_gust = ($angle_gust >= 0 ? $angle_gust : (2* pi + $angle_gust)) * 180/ pi;
- Log3 $name, 4, "$name: gust angle avg ".$angle_gust;
- my $avgtime_wind = 0;
- foreach my $val (@timestamps_wind)
- {
- $avgtime_wind += $val / scalar(@timestamps_wind);
- }
- my $avg_altitude = 0;
- my $min_altitude = 10000;
- my $max_altitude = -10000;
- foreach my $val (@readings_altitude)
- {
- $avg_altitude += $val / scalar(@readings_altitude);
- $min_altitude = $val if($val < $min_altitude);
- $max_altitude = $val if($val > $max_altitude);
- }
- my $avg_latitude = 0;
- foreach my $val (@readings_latitude)
- {
- $avg_latitude += $val / scalar(@readings_latitude);
- }
- my $avg_longitude = 0;
- foreach my $val (@readings_longitude)
- {
- $avg_longitude += $val / scalar(@readings_longitude);
- }
- $avg_temperature = sprintf( "%.2f", $avg_temperature );
- $avg_humidity = sprintf( "%.2f", $avg_humidity );
- $avg_pressure = sprintf( "%.2f", $avg_pressure );
- $avg_rain = sprintf( "%.2f", $avg_rain );
- $avg_rain_1 = sprintf( "%.2f", $avg_rain_1 );
- $avg_rain_24 = sprintf( "%.2f", $avg_rain_24 );
- $avg_wind = sprintf( "%.1f", $avg_wind );
- $avg_gust = sprintf( "%.1f", $avg_gust );
- $angle_wind = sprintf( "%i", $angle_wind );
- $angle_gust = sprintf( "%i", $angle_gust );
- $avgtime_temperature = sprintf( "%i", $avgtime_temperature );
- $avgtime_pressure = sprintf( "%i", $avgtime_pressure );
- $avgtime_rain = sprintf( "%i", $avgtime_rain );
- $avgtime_wind = sprintf( "%i", $avgtime_wind );
- $avg_altitude = sprintf( "%.2f", $avg_altitude );
- $avg_latitude = sprintf( "%.8f", $avg_latitude );
- $avg_longitude = sprintf( "%.8f", $avg_longitude );
- if(scalar(@readings_temperature) > 0)
- {
- push(@readings, [$avgtime_temperature, 'temperature', $avg_temperature]);
- push(@readings, [$avgtime_temperature, 'temperature_min', $min_temperature]);
- push(@readings, [$avgtime_temperature, 'temperature_max', $max_temperature]);
- push(@readings, [$avgtime_temperature, 'humidity', $avg_humidity]);
- push(@readings, [$avgtime_temperature, 'humidity_min', $min_humidity]);
- push(@readings, [$avgtime_temperature, 'humidity_max', $max_humidity]);
- }
- if(scalar(@readings_pressure) > 0)
- {
- push(@readings, [$avgtime_pressure, 'pressure', $avg_pressure]);
- push(@readings, [$avgtime_pressure, 'pressure_min', $min_pressure]);
- push(@readings, [$avgtime_pressure, 'pressure_max', $max_pressure]);
- }
- if(scalar(@readings_rain) > 0)
- {
- push(@readings, [$avgtime_rain, 'rain', $avg_rain]);
- push(@readings, [$avgtime_rain, 'rain_min', $min_rain]);
- push(@readings, [$avgtime_rain, 'rain_max', $max_rain]);
- push(@readings, [$avgtime_rain, 'rain_hour', $avg_rain_1]);
- push(@readings, [$avgtime_rain, 'rain_hour_min', $min_rain_1]);
- push(@readings, [$avgtime_rain, 'rain_hour_max', $max_rain_1]);
- push(@readings, [$avgtime_rain, 'rain_day', $avg_rain_24]);
- push(@readings, [$avgtime_rain, 'rain_day_min', $min_rain_24]);
- push(@readings, [$avgtime_rain, 'rain_day_max', $max_rain_24]);
- }
- if(scalar(@readings_wind_strength) > 0)
- {
- push(@readings, [$avgtime_wind, 'wind', $avg_wind]);
- push(@readings, [$avgtime_wind, 'wind_min', $min_wind]);
- push(@readings, [$avgtime_wind, 'wind_max', $max_wind]);
- push(@readings, [$avgtime_wind, 'gust', $avg_gust]);
- push(@readings, [$avgtime_wind, 'gust_min', $min_gust]);
- push(@readings, [$avgtime_wind, 'gust_max', $max_gust]);
- push(@readings, [$avgtime_wind, 'wind_angle', $angle_wind]);
- push(@readings, [$avgtime_wind, 'gust_angle', $angle_gust]);
- }
- if(scalar(@readings_altitude) > 0)
- {
- $hash->{altitude} = $avg_altitude;
- $hash->{location} = $avg_latitude.",".$avg_longitude;
- }
- $hash->{stations_indoor} = scalar(@readings_pressure);
- $hash->{stations_outdoor} = scalar(@readings_temperature);
- $hash->{stations_rain} = scalar(@readings_rain);
- $hash->{stations_wind} = scalar(@readings_wind_strength);
- my (undef,$latest) = netatmo_updateReadings( $hash, \@readings );
- $hash->{LAST_POLL} = FmtDateTime( $latest ) if( @readings );
- #$hash->{STATE} = "Error: device not found" if( !$found );
- } else {
- return $json->{body};
- }
- } #else {
- #return $hash->{status};
- #}
- }
- else
- {
- $hash->{status} = "error";
- }
- readingsSingleUpdate( $hash, "active", $hash->{status}, 1 ) if($hash->{status} ne "no data");
- }
- sub
- netatmo_parseAddress($$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 4, "$name: parseAddress";
-
- if( $json ) {
- Log3 $name, 5, "$name: ".Dumper($json);
- $hash->{status} = $json->{status};
- $hash->{status} = $json->{error}{message} if( $json->{error} );
- if( $hash->{status} eq "OK" ) {
- if( $json->{results} ) {
- return $json->{results}->[0]->{formatted_address};
- }
- } else {
- return $hash->{status};
- }
- }
- }
- sub
- netatmo_parseLatLng($$)
- {
- my($hash, $json) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 4, "$name: parseLatLng";
-
- if( $json ) {
- Log3 $name, 5, "$name: ".Dumper($json);
- $hash->{status} = $json->{status};
- $hash->{status} = $json->{error}{message} if( $json->{error} );
- if( $hash->{status} eq "OK" ) {
- if( $json->{results} ) {
- return $json->{results}->[0]->{geometry}->{bounds};
- }
- } else {
- return $hash->{status};
- }
- }
- }
- sub
- netatmo_pollDevice($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- $hash->{openRequests} = 0 if ( !defined( $hash->{openRequests}) );
- if( $hash->{Module} )
- {
- my @types = split( ' ', $hash->{dataTypes} ) if(defined($hash->{dataTypes}));
- Log3 $name, 4, "$name: pollDevice types [".$hash->{dataTypes} . "] for modules [".$hash->{Module}."]" if(defined($hash->{dataTypes}));
- my $lastupdate = ReadingsVal( $hash->{NAME}, ".lastupdate", undef );
- $lastupdate = (time-7*24*60*60) if(!$lastupdate and !$hash->{model});
- $hash->{openRequests} += int(@types);
- $hash->{openRequests} += 1 if(int(@types)==0);
-
- readingsSingleUpdate($hash, ".lastupdate", $lastupdate, 0) if(int(@types)>0);
-
- foreach my $module (split( ' ', $hash->{Module} ) ) {
- my $type = shift(@types) if( $module and @types);
- netatmo_requestDeviceReadings( $hash, $hash->{Device}, $type, ($module ne $hash->{Device})?$module:undef );# if($type);
- }
- }
- elsif( defined($hash->{Lat}) )
- {
- #$hash->{openRequests} += 1;
- netatmo_getPublicDevices($hash, 0, $hash->{Lat}, $hash->{Lon}, $hash->{Rad} );
- } elsif( $hash->{Device} ) {
- $hash->{openRequests} += 1;
- netatmo_requestDeviceReadings( $hash, $hash->{Device} );
- }
- }
- sub
- netatmo_pollThermostat($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- $hash->{openRequests} = 0 if ( !defined( $hash->{openRequests}) );
- if( $hash->{Thermostat} )
- {
- Log3 $name, 4, "$name: pollThermostat types [".$hash->{dataTypes} . "] for thermostat [".$hash->{Thermostat}."]" if(defined($hash->{dataTypes}));
- my $lastupdate = ReadingsVal( $hash->{NAME}, ".lastupdate", undef );
- $lastupdate = (time-7*24*60*60) if(!$lastupdate and !$hash->{model});
- $hash->{openRequests} += 1;
- my $type = $hash->{dataTypes};
- readingsSingleUpdate($hash, ".lastupdate", $lastupdate, 0) if($type);
- netatmo_requestDeviceReadings( $hash, $hash->{Relay}, $type, $hash->{Thermostat} );# if($type);
- }
- elsif( $hash->{Relay} )
- {
- $hash->{openRequests} += 1;
- netatmo_requestDeviceReadings( $hash, $hash->{Relay} );
- }
- }
- sub
- netatmo_pollGlobal($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- netatmo_refreshToken($hash, defined($hash->{access_token}));
- return undef if(!defined($hash->{access_token}));
- Log3 $name, 4, "$name: pollGlobal";
- HttpUtils_NonblockingGet({
- url => "https://".$hash->{helper}{apiserver}."/api/getstationsdata",
- timeout => 60,
- noshutdown => 1,
- data => { access_token => $hash->{access_token}, },
- hash => $hash,
- type => 'stationsdata',
- callback => \&netatmo_dispatch,
- });
- return undef;
- }
- sub
- netatmo_pollGlobalHealth($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- netatmo_refreshToken($hash, defined($hash->{access_token}));
- return undef if(!defined($hash->{access_token}));
- Log3 $name, 4, "$name: pollGlobalHealth";
- HttpUtils_NonblockingGet({
- url => "https://".$hash->{helper}{apiserver}."/api/gethomecoachsdata",
- timeout => 60,
- noshutdown => 1,
- data => { access_token => $hash->{access_token}, },
- hash => $hash,
- type => 'stationsdata',
- callback => \&netatmo_dispatch,
- });
- return undef;
- }
- sub
- netatmo_pollForecast($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- if(!defined($hash->{Station}))
- {
- Log3 $name, 1, "$name: device missing the definition! please redefine it.";
-
- return undef;
- }
- return undef if( !defined($hash->{IODev}) );
- my $iohash = $hash->{IODev};
- netatmo_refreshAppToken($iohash, defined($iohash->{access_token_app}));
- return undef if(!defined($iohash->{access_token_app}));
-
- if(!defined($iohash->{access_token_app}))
- {
- Log3 $name, 1, "$name: pollForecast - missing app token!";
- return undef;
- }
- Log3 $name, 4, "$name: pollForecast (forecastdata)";
- HttpUtils_NonblockingGet({
- url => "https://app.netatmo.net/api/simplifiedfuturemeasure",
- timeout => 60,
- noshutdown => 1,
- data => { device_id => $hash->{Station}, },
- header => "Authorization: Bearer ".$iohash->{access_token_app},
- hash => $hash,
- type => 'forecastdata',
- callback => \&netatmo_dispatch,
- });
- return undef;
- }
- sub
- netatmo_pollHome($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 3, "$name: pollHome (".$hash->{Home}.")";
- if( $hash->{Home} ) {
-
- return undef if($hash->{status} =~ /usage/ || $hash->{status} =~ /too_many_connections/ || $hash->{status} =~ /postponed/);
-
- my $lastupdate = ReadingsVal( $hash->{NAME}, ".lastupdate", undef );
- $lastupdate = (time-7*24*60*60) if(!$lastupdate);
-
- readingsSingleUpdate($hash, ".lastupdate", $lastupdate, 0);
- netatmo_requestHomeReadings( $hash, $hash->{Home} );
- }
- }
- sub
- netatmo_pollRelay($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 5, "$name: pollRelay (".$hash->{Relay}.")";
- if( $hash->{Relay} ) {
- return undef if(defined($hash->{status}) && ($hash->{status} =~ /usage/ || $hash->{status} =~ /too_many_connections/ || $hash->{status} =~ /postponed/));
- my $lastupdate = ReadingsVal( $hash->{NAME}, ".lastupdate", undef );
- $lastupdate = (time-7*24*60*60) if(!$lastupdate);
- readingsSingleUpdate($hash, ".lastupdate", $lastupdate, 0);
- netatmo_requestThermostatReadings( $hash, $hash->{Relay} );
- }
- }
- sub
- netatmo_pollPerson($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 5, "$name: pollPerson";
- return undef if(defined($hash->{status}) && ($hash->{status} =~ /usage/ || $hash->{status} =~ /too_many_connections/ || $hash->{status} =~ /postponed/));
- if( $hash->{Home} ) {
- my $lastupdate = ReadingsVal( $hash->{NAME}, ".lastupdate", undef );
- $lastupdate = (time-7*24*60*60) if(!$lastupdate);
- readingsSingleUpdate($hash, ".lastupdate", $lastupdate, 0);
- netatmo_requestPersonReadings( $hash );
- }
- }
- sub
- netatmo_Get($$@)
- {
- my ($hash, $name, $cmd, @args) = @_;
- my $list = "";
- if( $hash->{SUBTYPE} eq "DEVICE"
- || $hash->{SUBTYPE} eq "MODULE"
- || $hash->{SUBTYPE} eq "PUBLIC"
- || $hash->{SUBTYPE} eq "FORECAST"
- || $hash->{SUBTYPE} eq "HOME"
- || $hash->{SUBTYPE} eq "CAMERA"
- || $hash->{SUBTYPE} eq "PERSON"
- || $hash->{SUBTYPE} eq "RELAY"
- || $hash->{SUBTYPE} eq "THERMOSTAT"
- || $hash->{SUBTYPE} eq "HEATINGHOME"
- || $hash->{SUBTYPE} eq "HEATINGROOM" ) {
- $list = "update:noArg";
- $list = " ping:noArg video video_local live live_local snapshot" if($hash->{SUBTYPE} eq "CAMERA");
- $list .= " config:noArg timelapse:noArg" if($hash->{SUBTYPE} eq "CAMERA" && defined($hash->{model}) && $hash->{model} eq "NOC");
- #$list .= " weathericon" if($hash->{SUBTYPE} eq "FORECAST");
- if( $cmd eq "weathericon" ) {
- return "no weather code was passed" if($args[0] eq "");
- return netatmo_weatherIcon();
- }
- if( $cmd eq "ping" ) {
- netatmo_pingCamera($hash);
- return undef;
- }
- if( $cmd eq "video" || $cmd eq "video_local" ) {
- return "no video_id was passed" if(!defined($args[0]) || $args[0] eq "");
- return netatmo_getCameraVideo($hash,$args[0],$cmd);
- }
- elsif( $cmd eq "live" || $cmd eq "live_local" ) {
- return netatmo_getCameraLive($hash,$cmd);
- }
- elsif($cmd eq "timelapse") {
- return netatmo_getCameraTimelapse($hash);
- }
- elsif( $cmd eq "snapshot" ) {
- return netatmo_getCameraSnapshot($hash);
- }
- elsif( $cmd eq "config" ) {
- return netatmo_getPresenceConfig($hash);
- }
- if( $cmd eq "updateAll" ) {
- $cmd = "update";
- CommandDeleteReading( undef, "$name .*" );
- }
- if( $cmd eq "update" ) {
- netatmo_poll($hash);
- return undef;
- }
- } elsif( $hash->{SUBTYPE} eq "ACCOUNT" ) {
- $list = "update:noArg devices:noArg homes:noArg thermostats:noArg homecoachs:noArg public showAccount:noArg";
- if( $cmd eq "update" ) {
- netatmo_poll($hash);
- return undef;
- }
- if( $cmd eq 'showAccount' )
- {
- my $username = $hash->{helper}{username};
- my $password = $hash->{helper}{password};
- return 'no username set' if( !$username );
- return 'no password set' if( !$password );
- $username = netatmo_decrypt( $username );
- $password = netatmo_decrypt( $password );
- return "username: $username\npassword: $password";
- }
- if( $cmd eq "devices" ) {
- my $devices = netatmo_getDevices($hash,1);
- my $ret;
- foreach my $device (@{$devices}) {
- $ret .= "\n" if( $ret );
- $ret .= "$device->{_id}\t$device->{firmware}\t$device->{type}\t".($device->{type} !~ /Module/ ? "\t" : "")."$device->{module_name}";
- }
- $ret = "id\t\t\tfw\ttype\t\tname\n" . $ret if( $ret );
- $ret = "no devices found" if( !$ret );
- return $ret;
- } elsif( $cmd eq "homes" ) {
- my $homes = netatmo_getHomes($hash,1);
- Log3 $name, 5, "$name: homes ".Dumper($homes);
- my $ret;
- foreach my $home (@{$homes}) {
- $ret .= "\n" if( $ret );
- $ret .= "$home->{id} \t\tHome\t".encode_utf8($home->{name}) if(defined($home->{cameras}) && @{$home->{cameras}});;
- if(defined($home->{status}))
- {
- $ret .= "$home->{id} \t\t\tCamera\t".encode_utf8($home->{name});
- foreach my $tag (@{$home->{modules}}) {
- $ret .= "\n$tag->{id} \t\t\tTag\t".encode_utf8($tag->{name}) if(defined($tag->{name}));
- }
- }
- $ret .= "$home->{id} \tPerson\t".encode_utf8($home->{pseudo}) if(defined($home->{pseudo}));
- $ret .= "$home->{id} \tPerson\t(Unknown)" if(defined($home->{face}) && !defined($home->{pseudo}));
- }
- $ret = "id\t\t\t\t\ttype\tname\n" . $ret if( $ret );
- $ret = "no homes found" if( !$ret );
- return $ret;
- } elsif( $cmd eq "thermostats" ) {
- my $thermostats = netatmo_getThermostats($hash,1);
- Log3 $name, 5, "$name: thermostats ".Dumper($thermostats);
- my $ret;
- foreach my $thermostat (@{$thermostats}) {
- $ret .= "\n" if( $ret );
- $ret .= "$thermostat->{id}\t$thermostat->{firmware}\t$thermostat->{type}\t ".$thermostat->{name};
- }
- $ret = "id\t\t\tfw\ttype\t name\n" . $ret if( $ret );
- $ret = "no thermostats found" if( !$ret );
- return $ret;
- } elsif( $cmd eq "homecoachs" ) {
- my $homecoachs = netatmo_getHomecoachs($hash,1);
- Log3 $name, 5, "$name: homecoachs ".Dumper($homecoachs);
- my $ret;
- foreach my $homecoach (@{$homecoachs}) {
- $ret .= "\n" if( $ret );
- $ret .= "$homecoach->{_id}\t$homecoach->{firmware}\t$homecoach->{type}\t$homecoach->{station_name}";
- }
- $ret = "id\t\t\tfw\ttype\t name\n" . $ret if( $ret );
- $ret = "no homecoachs found" if( !$ret );
- return $ret;
- } elsif( $cmd eq "public" ) {
- my $station = '';
- my $addr = '';
- $station = shift @args if( $args[0] && $args[0] =~ m/[\da-f]{2}(:[\da-f]{2}){5}/ );
- if( @args && defined($args[0]) && ( $args[0] =~ m/^\d{5}$/
- || $args[0] =~ m/^a:/ ) ) {
- $addr = shift @args;
- $addr = substr($addr,2) if( $addr =~ m/^a:/ );
- my $bounds = netatmo_getLatLong( $hash,1,$addr );
- $args[0] = $bounds->{northeast}->{lat};
- $args[1] = $bounds->{northeast}->{lng};
- $args[2] = $bounds->{southwest}->{lat};
- $args[3] = $bounds->{southwest}->{lng};
- } elsif(defined($args[0]) && $args[0] =~ m/,/) {
- my @latlon1 = split( ',', $args[0] );
- if($args[1] !~ m/,/){
- $args[3] = undef;
- $args[2] = $args[1];
- } else {
- my @latlon2 = split( ',', $args[1] );
- $args[3] = $latlon2[1];
- $args[2] = $latlon2[0];
- }
- $args[1] = $latlon1[1];
- $args[0] = $latlon1[0];
- }
- my $devices = netatmo_getPublicDevices($hash, 1, $args[0], $args[1], $args[2], $args[3] );
- my $ret;
- my $addresscount = 0;
- if( ref($devices) eq "ARRAY" ) {
- my $csrftoken = (defined($FW_CSRF) ? $FW_CSRF : "&nocsrf");
- foreach my $device (@{$devices}) {
- next if(!defined($device->{_id}));
- next if( $station && $station ne $device->{_id} );
- my $idname = $device->{_id};
- $idname =~ s/:/_/g;
- next if(AttrVal("netatmo_D".$idname, "IODev", undef));
- next if(AttrVal("netatmo_D".$device->{_id}, "IODev", undef));
- next if(defined($modules{$hash->{TYPE}}{defptr}{"D$idname"}));
- next if(defined($modules{$hash->{TYPE}}{defptr}{"D$device->{_id}"}));
- $ret .= "\n" if( $ret );
- $ret .= sprintf( "%s<a href=\"https://www.google.com/maps/@%.8f,%.8f,19z\" target=\"gmaps\"> %.6f,%.6f %i m</a>", $device->{_id},
- $device->{place}->{location}->[1], $device->{place}->{location}->[0],
- $device->{place}->{location}->[1], $device->{place}->{location}->[0],
- $device->{place}->{altitude} );
- #$ret .= "\t";
- $addr = '';
- $addr .= netatmo_getAddress( $hash, 1, $device->{place}->{location}->[1], $device->{place}->{location}->[0] ) if($addresscount++ < AttrVal($name,"addresslimit",10));
- $addr .= " (address limit reached, " if($addresscount == AttrVal($name,"addresslimit",10)+2);
- $addr .= " change attribute addresslimit to see more) " if($addresscount == AttrVal($name,"addresslimit",10)+3);
- next if( ref($device->{measures}) ne "HASH" );
- my $ext;
- my $got_temp;
- my $got_press;
- foreach my $module ( sort keys %{$device->{measures}}) {
- next if( ref($device->{measures}->{$module}->{res}) ne "HASH" );
- $ext .= "$module ";
- $ext .= join(',', @{$device->{measures}->{$module}->{type}});
- $ext .= " ";
- foreach my $timestamp ( keys %{$device->{measures}->{$module}->{res}} ) {
- my $i = 0;
- foreach my $value ( @{$device->{measures}->{$module}->{res}->{$timestamp}} ) {
- my $type = $device->{measures}->{$module}->{type}[$i];
- if( $type eq "temperature" ) {
- $ret .= "\t";
- $ret .= " " if(int($value)<10);
- $ret .= sprintf( "%.1f \xc2\xb0C", $value );
- $got_temp = 1;
- } elsif( $type eq "humidity" ) {
- $ret .= "\t";
- $ret .= " " if(int($value)<10);
- $value = 99 if(int($value)>99);
- $ret .= sprintf( "%i %%", $value );
- } elsif( $type eq "pressure" ) {
- $ret .= "\t\t" if( !$got_temp );
- $ret .= "\t";
- $ret .= " " if(int($value)<1000);
- $ret .= sprintf( "%i hPa", $value );
- $got_press = 1;
- } elsif( $type eq "rain" ) {
- $ret .= "\t" if( !$got_temp );
- $ret .= "\t\t" if( !$got_press );
- #$ret .= "\t";
- $ret .= " ";
- $ret .= " " if(int($value)<10);
- $ret .= sprintf( "%i mm", $value );
- }
- else
- {
- Log3 $name, 2, "$name: unknown type ".$type;
- }
- ++$i;
- }
- last;
- }
- }
- my $got_rain = 0;
- foreach my $module ( keys %{$device->{measures}}) {
- my $value = $device->{measures}->{$module}->{rain_24h};
- if( defined($value) ) {
- $got_rain = 1;
- $ext .= "$module ";
- $ext .= "rain";
- $ext .= " ";
- if( defined($value) )
- {
- $ret .= "\t\t\t " if( !$got_press );
- $ret .= "\t ";
- $ret .= " " if(int($value)<10);
- $value = 99 if(int($value)>99);
- $ret .= sprintf( "%.1f mm", $value );
- }
- }
- }
- $ret .= "\t\t" if( !$got_rain );
- my $got_wind = 0;
- foreach my $module ( keys %{$device->{measures}}) {
- my $value = $device->{measures}->{$module}->{gust_strength};
- if( defined($value) ) {
- $got_wind = 1;
- $ext .= "$module ";
- $ext .= "windstrength,windangle,guststrength,gustangle";
- $ext .= " ";
- if( defined($value) )
- {
- $ret .= " ";
- $ret .= " " if(int($value)<10);
- $value = 99 if(int($value)>99);
- $ret .= sprintf( "%i km/h", $value );
- }
- }
- }
- $ret .= "\t" if( !$got_wind );
- $ret .= "\t $addr" if(defined($addr));
- #$ret .= "\n\tdefine netatmo_P$device->{_id} netatmo PUBLIC $device->{_id} $ext" if( $station );
- my $definelink = "<a href=\"#\" onclick=\"javascript:window.open((\'".$FW_ME."?cmd=define netatmo_D".$idname." net+++atmo PUBLIC ".$device->{_id}." ".$ext.$csrftoken."\').replace('+++',''), 'definewindow');\">=> </a>";
- $ret =~ s/$device->{_id}/$definelink/;
- }
- } else {
- $ret = $devices if( !ref($devices) );
- }
- $ret = " latitude,longitude alt\ttemp\thum\tpressure\t rain\t\twind\t address\n" . $ret if( $ret );
- $ret = "no devices found" if( !$ret );
- return $ret;
- }
- }
- return "Unknown argument $cmd, choose one of $list";
- }
- #########################
- sub
- netatmo_addExtension($) {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- #netatmo_removeExtension() ;
- my $url = "/netatmo";
- delete $data{FWEXT}{$url} if($data{FWEXT}{$url});
- Log3 $name, 1, "Starting Netatmo webhook for $name";
- $data{FWEXT}{$url}{deviceName} = $name;
- $data{FWEXT}{$url}{FUNC} = "netatmo_Webhook";
- $data{FWEXT}{$url}{LINK} = "netatmo";
-
- netatmo_registerWebhook($hash);
- }
- #########################
- sub
- netatmo_removeExtension($) {
- my ($hash) = @_;
- netatmo_dropWebhook($hash);
-
- my $url = "/netatmo";
- my $name = $data{FWEXT}{$url}{deviceName};
- Log3 $name, 3, "Stopping Netatmo webhook for $name";
- delete $data{FWEXT}{$url};
- }
- sub
- netatmo_registerWebhook($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- return undef if( !defined($hash->{IODev}) );
- my $iohash = $hash->{IODev};
- netatmo_refreshToken($iohash, defined($iohash->{access_token}));
- return undef if(!defined($iohash->{access_token}));
- Log3 $name, 3, "Registering Netatmo webhook";
- my $webhookurl = AttrVal($name,"webhookURL",undef);
- return undef if(!defined($webhookurl));
-
- HttpUtils_NonblockingGet({
- url => "https://".$iohash->{helper}{apiserver}."/api/addwebhook",
- timeout => 30,
- noshutdown => 1,
- data => { access_token => $iohash->{access_token}, url => $webhookurl, app_type => 'app_security', },
- hash => $hash,
- type => 'addwebhook',
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_dropWebhook($)
- {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- return undef if( !defined($hash->{IODev}) );
- my $iohash = $hash->{IODev};
- netatmo_refreshToken($iohash, defined($iohash->{access_token}));
- return undef if(!defined($iohash->{access_token}));
- Log3 $name, 3, "Dropping Netatmo webhook";
-
- HttpUtils_NonblockingGet({
- url => "https://".$iohash->{helper}{apiserver}."/api/dropwebhook",
- timeout => 30,
- noshutdown => 1,
- data => { access_token => $iohash->{access_token}, app_type => 'app_security', },
- hash => $hash,
- type => 'dropwebhook',
- callback => \&netatmo_dispatch,
- });
- }
- sub
- netatmo_webhookStatus($$$)
- {
- my($hash, $json, $hookstate) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 4, "$name: webhookStatus ($hookstate)";
- if( $json ) {
- $hash->{status} = $json->{status};
- $hash->{status} = $json->{error}{message} if( $json->{error} );
- $hookstate = "error" if( $json->{error} );
- readingsSingleUpdate($hash, "webhook", $hookstate, 1);
- }
- else
- {
- $hash->{status} = "error";
- readingsSingleUpdate($hash, "webhook", "error", 1);
- }
- return ( "text/plain; charset=utf-8",
- "JSON" );
- }
- sub netatmo_Webhook() {
- my ($request) = @_;
- my $hash = $modules{"netatmo"}{defptr}{"WEBHOOK"};
- if(!defined($hash)){
- Log3 "netatmo", 1, "Netatmo webhook hash not defined!";
- return ( "text/plain; charset=utf-8",
- "HASH" );
- }
- my $name = $hash->{NAME};
-
- my ($link,$data);
-
- if ( $request =~ m,^(\/[^/]+?)(?:\&|\?|\/\?|\/)(.*)?$, ) {
- $link = $1;
- $data = $2;
- } else {
- Log3 "netatmo", 1, "Netatmo webhook no data received!";
- return ( "text/plain; charset=utf-8",
- "NO" );
- }
- Log3 $name, 5, "Netatmo webhook JSON:\n".$data;
- my $json = eval { JSON->new->utf8(0)->decode($data) };
- if($@)
- {
- Log3 $name, 2, "$name: invalid json evaluation for webhook ".$@;
- return undef;
- }
- readingsBeginUpdate($hash);
-
- if(defined($json->{message})){
- my $eventmessage = $json->{message};
- $eventmessage =~ s/<\/b>//g;
- $eventmessage =~ s/<\/b>//g;
- readingsBulkUpdate( $hash, "state", $eventmessage );
- }
- readingsBulkUpdate( $hash, "event_type", $json->{event_type} ) if(defined($json->{event_type}));
- readingsBulkUpdate( $hash, "camera_id", $json->{camera_id} ) if(defined($json->{camera_id}));
- readingsBulkUpdate( $hash, "module_id", $json->{module_id} ) if(defined($json->{module_id}));
- readingsBulkUpdate( $hash, "person_id", $json->{persons}[0]{id} ) if(defined($json->{persons}[0]{id}));
- if(defined($json->{snapshot_id})) {
- readingsBulkUpdate( $hash, "snapshot", "https://api.netatmo.com/api/getcamerapicture?image_id=".$json->{snapshot_id}."&key=".$json->{snapshot_key}, 1 );
- }
- elsif(defined($json->{persons}[0]{face_id})) {
- readingsBulkUpdate( $hash, "snapshot", "https://api.netatmo.com/api/getcamerapicture?image_id=".$json->{persons}[0]{face_id}."&key=".$json->{persons}[0]{face_key}, 1 );
- }
- readingsEndUpdate( $hash, 1 );
- if(AttrVal($name,"webhookPoll","0") eq "1" && defined($json->{home_id}))
- {
- my $home = $modules{$hash->{TYPE}}{defptr}{"H$json->{home_id}"};
- netatmo_poll($home) if($home->{status} !~ /usage/ && $home->{status} !~ /too_many_connections/ && $home->{status} !~ /postponed/);;
- }
- return ( "text/plain; charset=utf-8",
- "{\"status\":\"ok\"}" );
- }
- sub netatmo_Attr($$$)
- {
- my ($cmd, $name, $attrName, $attrVal) = @_;
- my $orig = $attrVal;
- $attrVal = int($attrVal) if($attrName eq "interval" || $attrName eq "setpoint_duration");
- $attrVal = 15 if($attrName eq "setpoint_duration" && $attrVal < 15 && $attrVal != 0);
- return undef if(!defined($defs{$name}));
- if( $attrName eq "interval" ) {
- my $hash = $defs{$name};
- $attrVal = 60*5 if($hash->{SUBTYPE} ne "HOME" && $attrVal < 60*5 && $attrVal != 0);
- #\$attrVal = 2700 if(($attrVal < 2700 && ($hash->{SUBTYPE} eq "ACCOUNT" || $hash->{SUBTYPE} eq "FORECAST");
- $hash->{helper}{INTERVAL} = $attrVal if($attrVal);
- $hash->{helper}{INTERVAL} = 60*30 if( !$hash->{helper}{INTERVAL} );
- } elsif( $attrName eq "setpoint_duration" ) {
- my $hash = $defs{$name};
- #$hash->{SETPOINT_DURATION} = $attrVal;
- #$hash->{SETPOINT_DURATION} = 60 if( !$hash->{SETPOINT_DURATION} );
- } elsif( $attrName eq "serverAPI" ) {
- my $hash = $defs{$name};
- if( $cmd eq "set" && $attrVal ne "" ) {
- $hash->{helper}{apiserver} = $attrVal;
- } else {
- $hash->{helper}{apiserver} = "api.netatmo.com";
- }
- } elsif( $attrName eq "disable" ) {
- my $hash = $defs{$name};
- RemoveInternalTimer($hash);
- if( $cmd eq "set" && $attrVal ne "0" ) {
- } else {
- $attr{$name}{$attrName} = 0;
- netatmo_poll($hash);
- }
- }
- if( $cmd eq "set" ) {
- if( $orig ne $attrVal ) {
- $attr{$name}{$attrName} = $attrVal;
- return $attrName ." set to ". $attrVal;
- }
- }
- return;
- }
- sub netatmo_encrypt($)
- {
- my ($decoded) = @_;
- my $key = getUniqueId();
- my $encoded;
- return $decoded if( $decoded =~ /crypt:/ );
- for my $char (split //, $decoded) {
- my $encode = chop($key);
- $encoded .= sprintf("%.2x",ord($char)^ord($encode));
- $key = $encode.$key;
- }
- return 'crypt:'.$encoded;
- }
- sub netatmo_decrypt($)
- {
- my ($encoded) = @_;
- my $key = getUniqueId();
- my $decoded;
- return $encoded if( $encoded !~ /crypt:/ );
-
- $encoded = $1 if( $encoded =~ /crypt:(.*)/ );
- for my $char (map { pack('C', hex($_)) } ($encoded =~ /(..)/g)) {
- my $decode = chop($key);
- $decoded .= chr(ord($char)^ord($decode));
- $key = $decode.$key;
- }
- return $decoded;
- }
- ##########################
- sub netatmo_DbLog_splitFn($)
- {
- my ($event) = @_;
- my ($reading, $value, $unit) = "";
- my @parts = split(/ /,$event,3);
- $reading = $parts[0];
- $reading =~ tr/://d;
- $value = $parts[1];
- if($event =~ m/T: / && $event =~ m/H: /)
- {
- return undef; #dewpoint workaround - no logging
- }
- elsif($event =~ m/symbol/ || $event =~ m/message/)
- {
- $unit = ''; #symbols & text
- }
- elsif($event =~ m/trend/)
- {
- $unit = ''; #trends
- }
- elsif($event =~ m/date/ || $event =~ m/sunrise/ || $event =~ m/sunset/)
- {
- $unit = ''; #dates
- }
- elsif($event =~ m/temp/ || $event =~ m/dewpoint/)
- {
- $unit = "˚C";
- }
- elsif($event =~ m/humidity/)
- {
- $unit = '%';
- }
- elsif($event =~ m/pressure/)
- {
- $unit = 'mbar';
- }
- elsif($event =~ m/co2/)
- {
- $unit = 'ppm';
- }
- elsif($event =~ m/noise/)
- {
- $unit = 'dB';
- }
- elsif($event =~ m/rain/)
- {
- $unit = 'mm';
- }
- elsif($event =~ m/angle/ || $event =~ m/direction/)
- {
- $unit = "deg";
- }
- elsif($event =~ m/strength/ || $event =~ m/gust/)
- {
- $unit = 'km/h';
- }
- elsif($event =~ m/boilero/)
- {
- $unit = 'sec';
- }
- elsif($event =~ m/percent/)
- {
- $unit = '%';
- }
- elsif($event =~ m/sun/)
- {
- $unit = 'h';
- }
- elsif($event =~ m/air_/)
- {
- $unit = "ug/m3";
- }
- elsif($event =~ m/batteryPercent/)
- {
- $unit = "%";
- }
- elsif($event =~ m/batteryVoltage/)
- {
- $unit = "V";
- }
- else
- {
- $value = $parts[1];
- $value = $value." ".$parts[2] if(defined($parts[2]));
- }
- return ($reading, $value, $unit);
- }
- sub netatmo_weatherIcon()
- {
- my $svgheader = '<?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 500 500" enable-background="new 0 0 500 500" xml:space="preserve">';
-
- my $svgcontent = '<path id="smallcloud" opacity="1.0" fill="#000000" d="M162.8,46.7c-7.4-5.8-16.5-9.5-26.5-9.5C128.8,14.8,108.1,0,84.3,0h-1.6';
- $svgcontent .= ' c-1.1,0-2.2,0-3.3,0.1C71.4,0.7,62,3.1,58,6.9V6.6c0,0.1-0.2,0.2-0.3,0.3c-12.2,6.9-21.2,18-25.4,32.3C13.2,45.1-0.1,62.1-0.1,82.8';
- $svgcontent .= ' c0,25.5,20.7,46.2,46.2,46.2h88.6c25.5,0,45.6-20.8,45.6-46.2c0-5.3-0.5-10.6-2.7-15.9C173.5,56.2,171.8,52.7,162.8,46.7z';
- $svgcontent .= ' M134.7,104H46.1c-11.7,0-20.7-9.6-20.7-21.2c0-10.6,8-19.6,18.6-20.7l8-1.1c2.1,0,2.7-0.5,2.7-2.7l1.1-7.4';
- $svgcontent .= ' c1.6-14.8,13.8-26,28.6-26c15.4,0,27.6,11.1,29.2,26l1.1,8.5c0.5,1.6,1.6,2.6,3.2,2.6h17c11.1,0,20.7,9.6,20.7,20.7';
- $svgcontent .= ' C155.3,94.4,145.8,104,134.7,104z"/>';
-
- my $svgfooter = '</svg>';
-
- return $svgheader . $svgcontent . $svgfooter;
- }
- 1;
- =pod
- =item device
- =item summary Netatmo weather stations, thermostats and cameras connected via the official API
- =begin html
- <a name="netatmo"></a>
- <h3>netatmo</h3>
- <ul>
- FHEM module for netatmo weather stations, thermostats and cameras.<br><br>
- Notes:
- <ul>
- <li>JSON has to be installed on the FHEM host.</li>
- <li>You need to create an app <u><a href="https://dev.netatmo.com/dev/createanapp">here</a></u> to get your <i>client_id / client_secret</i>.<br />Request the full access scope including cameras and thermostats.</li>
- </ul><br>
- <a name="netatmo_Define"></a>
- <b>Define</b>
- <ul>
- <code>define <name> netatmo [ACCOUNT] <username> <password> <client_id> <client_secret></code><br>
- <code>define <name> netatmo <device></code><br>
- <br>
- Defines a netatmo device.<br><br>
- If a netatmo device of the account type is created all fhem devices for the netatmo devices are automaticaly created
- (if autocreate is not disabled).
- <br>
- Examples:
- <ul>
- <code>define netatmo netatmo ACCOUNT abc@test.com myPassword 2134123412399119d4123134 AkqcOIHqrasfdaLKcYgZasd987123asd</code><br>
- <code>define netatmo netatmo 2f:13:2b:93:12:31</code><br>
- <code>define netatmo netatmo MODULE 2f:13:2b:93:12:31 f1:32:b9:31:23:11</code><br>
- <code>define netatmo netatmo HOME 1234567890abcdef12345678</code><br>
- <code>define netatmo netatmo CAMERA 1234567890abcdef12345678 70:ee:12:34:56:78</code><br>
- <code>define netatmo netatmo PERSON 1234567890abcdef12345678 01234567-89ab-cdef-0123-456789abcdef</code><br>
- </ul>
- </ul><br>
- <a name="netatmo_Webhook"></a>
- <b>Webhook</b><br>
- <ul>
- <code>define netatmo netatmo WEBHOOK</code><br><br>
- Set your URL in attribute webhookURL, events from cameras will be received insantly
- </ul><br>
- <a name="netatmo_Readings"></a>
- <b>Readings</b>
- <ul>
- </ul><br>
- <a name="netatmo_Set"></a>
- <b>Set</b>
- <ul>
- <li>autocreate<br>
- Create fhem devices for all netatmo weather devices.</li>
- <li>autocreate_homes<br>
- Create fhem devices for all netatmo homes, cameras and persons.</li>
- <li>autocreate_thermostats<br>
- Create fhem devices for all netatmo relays and thermostats.</li>
- <li>autocreate_homecoachs<br>
- Create fhem devices for all netatmo homecoachs.</li>
- </ul><br>
- <a name="netatmo_Get"></a>
- <b>Get</b><br />
- ACCOUNT
- <ul>
- <li>devices<br>
- list the netatmo weather devices for this account</li>
- <li>home<br>
- list the netatmo home devices for this account</li>
- <li>update<br>
- trigger a global update for dashboard data</li>
- <li>public [<address>] <args><br>
- no arguments -> get all public stations in a radius of 0.025° around global fhem latitude/longitude<br>
- <rad> -> get all public stations in a radius of <rad>° around global fhem latitude/longitude<br>
- <lat> <lon> [<rad>] -> get all public stations in a radius of 0.025° or <rad>° around <lat>/<lon><br>
- <lat1> <lon1> <lat2> <lon2> -> get all public stations in the area of <lat1> <lon2> <lat2> <lon2><br>
- if <address> is given then list stations in the area of this address. can be given as 5 digit german postal code or a: followed by a textual address. all spaces have to be replaced by a +.<br>
- <lat> <lon> values can also be entered as a single coordinates parameter <lat>,<lon><br></li>
- </ul><br>
- DEVICE/MODULE
- <ul>
- <li>update<br>
- update the device readings</li>
- <li>updateAll<br>
- update the device readings after deleting all current readings</li>
- </ul><br>
- HOME
- <ul>
- <li>update<br>
- update the home events and all camera and person readings</li>
- </ul><br>
- CAMERA
- <ul>
- <li>ping<br>
- ping the camera and get the local command url</li>
- <li>live/_local<br>
- get the playlist for live video (internet or local network)</li>
- <li>video/_local <video_id><br>
- get the playlist for a video id (internet or local network)</li>
- </ul><br>
- PRESENCE
- <ul>
- <li>config<br>
- read the camera config</li>
- <li>timelapse<br>
- get the link for a timelapse video (local network)</li>
- </ul><br>
- PERSON
- <ul>
- <li>update<br>
- n/a</li>
- </ul><br>
- <a name="netatmo_Attr"></a>
- <b>Attributes</b>
- <ul>
- <li>interval<br>
- the interval in seconds used to check for new values.</li>
- <li>disable<br>
- 1 -> stop polling</li>
- <li>addresslimit<br>
- maximum number of addresses to resolve in public station searches (ACCOUNT - default: 10)</li>
- <li>setpoint_duration<br>
- setpoint duration in minutes (THERMOSTAT - default: 60)</li>
- <li>videoquality<br>
- video quality for playlists (HOME - default: medium)</li>
- <li>webhookURL<br>
- webhook URL - can include basic auth and ports: http://user:pass@your.url:8080/fhem/netatmo (WEBHOOK)</li>
- <li>webhookPoll<br>
- poll home after event from webhook (WEBHOOK - default: 0)</li>
- <li>ignored_device_ids<br>
- ids of devices/persons ignored on autocrate (ACCOUNT - comma separated)</li>
- </ul>
- </ul>
- =end html
- =cut
|