Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
Both sides previous revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
php:tado [2021/01/06 15:07] webproducer Erweiterung um die Zonen |
php:tado [2021/01/07 14:53] (aktuell) webproducer "weiterführende Links" hinzugefügt |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
====== Tado° ReST-API - Heizungssteuerung mit PHP ====== | ====== Tado° ReST-API - Heizungssteuerung mit PHP ====== | ||
- | Mit dieser PHP-Klasse ist es möglich, die Heizthermostate der Firma Tado mit PHP zu steuern. Hier sind noch Benutzername und Passwort zu ergänzen, wobei der Benutzername der E-Mail-Adresse, mit der Ihr Konto bei Tado angelegt wurde, entspricht. | + | Die Firma tado GmbH produziert smarte Heizkörper-Thermostate, die sehr einfach gegen herkömmliche manuelle Thermostate ausgetauscht werden können. |
+ | Neben den zahlreichen Features, welche die tado APP anbietet, besteht auch die Möglichkeit, die Thermostate über eine ReST-API anzusprechen. Mit der folgenden PHP-Klasse ist es möglich, die Thermostate per PHP auszulesen und zu steuern. | ||
+ | |||
+ | Um die Klasse benutzen zu können sind hier noch Benutzername und Passwort zu ergänzen, wobei der Benutzername der E-Mail-Adresse, mit der Ihr Konto bei Tado angelegt wurde, entspricht. | ||
<code php classTado.php> | <code php classTado.php> | ||
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 17: | 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 27: | Zeile 34: | ||
} | } | ||
+ | $this->endpoint = $parameter[0]; | ||
$this->setBearerToken( $parameter ); | $this->setBearerToken( $parameter ); | ||
$this->setHomeId(); | $this->setHomeId(); | ||
Zeile 34: | 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 41: | 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 51: | 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 79: | 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 90: | Zeile 112: | ||
* @param string $room | * @param string $room | ||
* @param float $grad | * @param float $grad | ||
+ | * @param integer $duration | ||
*/ | */ | ||
- | public function updateTemperature( string $room, float $grad ) | + | public function updateTemperature( string $room, float $grad, $duration = 0 ) |
{ | { | ||
$zone = $this->getZoneId( $room ); | $zone = $this->getZoneId( $room ); | ||
Zeile 107: | Zeile 130: | ||
$data->setting = $setting; | $data->setting = $setting; | ||
$data->termination = new stdClass(); | $data->termination = new stdClass(); | ||
- | $data->termination->type = 'MANUAL'; | + | |
- | $this->curl( sprintf( 'https://my.tado.com/api/v2/homes/%s/zones/%d/overlay', $this->homeId, $zone ), 'PUT', json_encode( $data ) ); | + | if( $duration === 0 ) |
+ | { | ||
+ | $data->termination->type = 'MANUAL'; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | $data->termination->type = 'TIMER'; | ||
+ | $data->termination->durationInSeconds = $duration; | ||
+ | } | ||
+ | |||
+ | $this->curl( sprintf( $this->endpoint . '/homes/%s/zones/%d/overlay', $this->homeId, $zone ), 'PUT', $data ); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * @param string $room | ||
+ | */ | ||
+ | public function removeManualControl( $room ) | ||
+ | { | ||
+ | $zone = $this->getZoneId( $room ); | ||
+ | |||
+ | if( $zone === false ) | ||
+ | { | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | $this->curl( sprintf( $this->endpoint . '/homes/%s/zones/%d/overlay', $this->homeId, $zone ), 'DELETE' ); | ||
} | } | ||
Zeile 135: | 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 150: | Zeile 198: | ||
</code> | </code> | ||
- | **Funktionsaufruf** | + | ==== Beispiel 1: Temperatur im Büro dauerhaft auf 20 Grad stellen ==== |
- | Mit diesem Aufruf wird die Temperatur von Zone 1 auf 17 Grad gestellt: | + | <code php> |
+ | $tado = new Tado; | ||
+ | $tado->updateTemperature( 'Büro', 20 ); | ||
+ | </code> | ||
+ | |||
+ | ==== Beispiel 2: Temperatur im Wohnzimmer für 90 Minuten auf 18 Grad stellen ==== | ||
+ | |||
+ | Hinweis: Die Dauer wird in Sekunden übergeben (90 Minuten = 5400 Sekunden): | ||
<code php> | <code php> | ||
$tado = new Tado; | $tado = new Tado; | ||
- | $tado->updateTemperature( 'Büro', 16 ); | + | $tado->updateTemperature( 'Wohnzimmer', 18, 5400 ); |
</code> | </code> | ||
+ | |||
+ | ==== Beispiel 3: Manuelle Steuerung im Wohnzimmer entfernen (intelligenter Zeitplan wird wieder aktiviert) ==== | ||
+ | |||
+ | <code php> | ||
+ | $tado = new Tado; | ||
+ | $tado->removeManualControl( 'Wohnzimmer' ); | ||
+ | </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]] | ||
+ | |||
+ |