Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
| Both sides previous revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
|
php:tado [2021/01/06 16:45] webproducer Zeitsteuerung hinzugefügt |
php:tado [2021/01/07 14:53] (aktuell) webproducer "weiterführende Links" hinzugefügt |
||
|---|---|---|---|
| Zeile 9: | Zeile 9: | ||
| class Tado | class Tado | ||
| { | { | ||
| + | const PRESENCE_HOME = 'HOME'; | ||
| + | const PRESENCE_AWAY = 'AWAY'; | ||
| + | |||
| private $token = ''; | private $token = ''; | ||
| private $homeId = 0; | private $homeId = 0; | ||
| private $zones = []; | private $zones = []; | ||
| - | private $password = ''; | + | private $endpoint = ''; |
| private $username = ''; | private $username = ''; | ||
| + | private $password = ''; | ||
| public function __construct() | public function __construct() | ||
| Zeile 20: | Zeile 24: | ||
| $content = file_get_contents( 'https://my.tado.com/webapp/env.js' ); | $content = file_get_contents( 'https://my.tado.com/webapp/env.js' ); | ||
| - | foreach( [ 'apiEndpoint', 'clientId', 'clientSecret' ] as $var ) | + | foreach( [ 'tgaRestApiV2Endpoint', 'apiEndpoint', 'clientId', 'clientSecret' ] as $var ) |
| { | { | ||
| preg_match( sprintf( "/%s: '(.*)'/", $var ), $content, $matches ); | preg_match( sprintf( "/%s: '(.*)'/", $var ), $content, $matches ); | ||
| Zeile 30: | Zeile 34: | ||
| } | } | ||
| + | $this->endpoint = $parameter[0]; | ||
| $this->setBearerToken( $parameter ); | $this->setBearerToken( $parameter ); | ||
| $this->setHomeId(); | $this->setHomeId(); | ||
| Zeile 37: | Zeile 42: | ||
| private function setBearerToken( $parameter ) | private function setBearerToken( $parameter ) | ||
| { | { | ||
| - | $data['client_id'] = $parameter[1]; | + | $data['client_id'] = $parameter[2]; |
| - | $data['client_secret'] = $parameter[2]; | + | $data['client_secret'] = $parameter[3]; |
| $data['grant_type'] = 'password'; | $data['grant_type'] = 'password'; | ||
| $data['scope'] = 'home.user'; | $data['scope'] = 'home.user'; | ||
| Zeile 44: | Zeile 49: | ||
| $data['password'] = $this->password; | $data['password'] = $this->password; | ||
| - | $result = $this->curl( $parameter[0] . '/token', 'POST', $data ); | + | $result = $this->curl( $parameter[1] . '/token', 'POST', $data ); |
| if( isset( $result->access_token ) ) | if( isset( $result->access_token ) ) | ||
| Zeile 54: | Zeile 59: | ||
| private function setHomeId() | private function setHomeId() | ||
| { | { | ||
| - | $result = $this->curl( 'https://my.tado.com/api/v1/me', 'GET' ); | + | $result = $this->curl( $this->endpoint . '/me', 'GET' ); |
| - | $this->homeId = $result->homeId; | + | $this->homeId = $result->homes[0]->id; |
| } | } | ||
| - | |||
| private function setZones() | private function setZones() | ||
| { | { | ||
| - | $result = $this->curl( sprintf( 'https://my.tado.com/api/v2/homes/%s/zones', $this->homeId ), 'GET' ); | + | $result = $this->curl( sprintf( $this->endpoint . '/homes/%s/zones', $this->homeId ), 'GET' ); |
| $this->zones = $result; | $this->zones = $result; | ||
| } | } | ||
| Zeile 82: | Zeile 86: | ||
| } | } | ||
| - | public function getSetting( $zone ) | + | public function getSetting( $zone = null ) |
| { | { | ||
| - | $path = sprintf( 'https://my.tado.com/api/v2/homes/%s/zones/%d/state', $this->homeId, $zone ); | + | if( $zone === null ) |
| + | { | ||
| + | $path = sprintf( $this->endpoint . '/homes/%s/state', $this->homeId, $zone ); | ||
| + | return $this->curl( $path, 'GET' ); | ||
| + | } | ||
| + | |||
| + | $path = sprintf( $this->endpoint . '/homes/%s/zones/%d/state', $this->homeId, $zone ); | ||
| $result = $this->curl( $path, 'GET' ); | $result = $this->curl( $path, 'GET' ); | ||
| unset( $result->setting->temperature->fahrenheit ); | unset( $result->setting->temperature->fahrenheit ); | ||
| return $result->setting; | return $result->setting; | ||
| + | } | ||
| + | |||
| + | public function setPresence( $presence ) | ||
| + | { | ||
| + | $data = new stdClass(); | ||
| + | $data->homePresence = $presence; | ||
| + | |||
| + | $path = sprintf( $this->endpoint . '/homes/%s/presenceLock', $this->homeId ); | ||
| + | $this->curl( $path, 'PUT', $data ); | ||
| } | } | ||
| Zeile 120: | Zeile 139: | ||
| $data->termination->type = 'TIMER'; | $data->termination->type = 'TIMER'; | ||
| $data->termination->durationInSeconds = $duration; | $data->termination->durationInSeconds = $duration; | ||
| - | + | } | |
| - | $this->curl( sprintf( 'https://my.tado.com/api/v2/homes/%s/zones/%d/overlay', $this->homeId, $zone ), 'PUT', json_encode( $data ) ); | + | |
| + | $this->curl( sprintf( $this->endpoint . '/homes/%s/zones/%d/overlay', $this->homeId, $zone ), 'PUT', $data ); | ||
| } | } | ||
| Zeile 136: | Zeile 156: | ||
| } | } | ||
| - | $this->curl( sprintf( 'https://my.tado.com/api/v2/homes/%s/zones/%d/overlay', $this->homeId, $zone ), 'DELETE' ); | + | $this->curl( sprintf( $this->endpoint . '/homes/%s/zones/%d/overlay', $this->homeId, $zone ), 'DELETE' ); |
| } | } | ||
| Zeile 163: | Zeile 183: | ||
| if( $method === 'PUT' ) | if( $method === 'PUT' ) | ||
| { | { | ||
| - | curl_setopt( $ch, CURLOPT_POSTFIELDS, $data ); | + | curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $data ) ); |
| } | } | ||
| else | else | ||
| Zeile 200: | Zeile 220: | ||
| $tado->removeManualControl( 'Wohnzimmer' ); | $tado->removeManualControl( 'Wohnzimmer' ); | ||
| </code> | </code> | ||
| + | |||
| + | ==== Beispiel 4: Status "HOME" setzen ==== | ||
| + | |||
| + | <code php> | ||
| + | $tado = new Tado; | ||
| + | $tado->setPresence( tado::PRESENCE_HOME ); | ||
| + | </code> | ||
| + | |||
| + | ==== Beispiel 5: Status "AWAY" setzen ==== | ||
| + | |||
| + | <code php> | ||
| + | $tado = new Tado; | ||
| + | $tado->setPresence( tado::PRESENCE_AWAY ); | ||
| + | </code> | ||
| + | |||
| + | ==== Weiterführende Links ==== | ||
| + | |||
| + | * [[http://blog.scphillips.com/posts/2017/01/the-tado-api-v2/|The Tado API v2]] | ||
| + | * [[https://shkspr.mobi/blog/2019/02/tado-api-guide-updated-for-2019/|Tado API Guide von Terence Eden's Block]] | ||
| + | |||
| + | |||