gboudreau/nest-api

这是一个简单的库,可以让你监视和控制你的Nest学习恒温器和Nest保护器。

1.2.6 2020-07-08 16:41 UTC

README

这是一个PHP类,可以让你监视和控制你的Nest学习恒温器和Nest保护器。

请注意,自从我开始这个项目以来,Nest已经启动了一个官方的开发者计划。你可能最好使用官方API,而不是这里提供的这个PHP类,因为这个PHP类需要在纯文本中存储你的凭证,并且使用了移动和Web应用使用的非支持API。
例如,如果你正在构建一个严肃的商业应用程序,请注册Nest的开发者计划。如果你只是想为自己构建一些东西,那么你可能会对这个PHP类很满意。

特性

  • 缓存,以便在不需要时不需要重新登录。即更快操作。
  • 获取器
    • 当前和目标温度,湿度
    • 达到目标温度所需时间
    • 目标温度模式,风扇模式
    • 空调,加热和风扇状态:开启或关闭
    • 手动和自动离家模式
    • 位置信息
    • 网络信息(本地和WAN IP,MAC地址,在线状态)
    • 当前活动日程(按日)
    • 下一个计划事件
    • 过去10天的能源报告
    • 设备名称,设备列表
    • 电池级别(电压)
    • Nest保护器设备信息
  • 设置器
    • 目标温度(单个或范围)
    • 目标温度模式:冷却,加热,范围
    • 风扇模式:自动,开启,每小时分钟数
    • 风扇:每天日程(开始和停止时间)
    • 风扇:带定时器开启(X分钟后/小时后停止)
    • 节能(离家)模式:开启,关闭,最小/最大温度,自动离家
    • 双燃料:断点(当户外温度低于X时使用备用燃料),总是备用,总是主要
    • 安全温度(低温和高温)
    • 湿度(开启,关闭,%)
    • 关闭暖通空调

使用方法

你可以直接下载nest.class.php并require/include它,或者使用composer: require "gboudreau/nest-api": "dev-master"

有关详细信息,请参阅examples.php,但以下是快速入门指南。

<?php

require_once('nest.class.php');

// Use a Nest account:
$username = 'you@gmail.com';
$pasword = 'Something other than 1234 right?';
$nest = new Nest($username, $pasword);

// Or use a Google account (see instructions below on how to find those values):
$issue_token = 'https://#/o/oauth2/iframerpc?action=issueToken&response_type=token%20id_token&login_hint=UNIQUE_VALUE_HERE&client_id=733249279899-44tchle2kaa9afr5v9ov7jbuojfr9lrq.apps.googleusercontent.com&origin=https%3A%2F%2Fhome.nest.com&scope=openid%20profile%20email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fnest-account&ss_domain=https%3A%2F%2Fhome.nest.com';
$cookies = '#YOUR_COOKIES_HERE#'; // All on one line; remove any new-line character you might have
$nest = new Nest(NULL, NULL, $issue_token, $cookies);

// Get the device information:
$infos = $nest->getDeviceInfo();
print_r($infos);
    
// Print the current temperature
printf("Current temperature: %.02f degrees %s\n", $infos->current_state->temperature, $infos->scale);

// Cool to 23
$nest->setTargetTemperatureMode(TARGET_TEMP_MODE_COOL, 23.0);
    
// Set Away mode
$nest->setAway(TRUE);

// Turn off Away mode
$nest->setAway(FALSE);

getDeviceInfo()的示例输出

{
  "current_state": {
    "mode": "range",
    "temperature": 24.09999,
    "humidity": 42,
    "ac": false,
    "heat": false,
    "fan": true,
    "auto_away": 0,
    "manual_away": false,
    "leaf": false,
    "battery_level": 3.948
  },
  "target": {
    "mode": "range",
    "temperature": [
      23,
      26
    ],
    "time_to_target": 0
  },
  "serial_number": "01AB02BA117210S5",
  "scale": "C",
  "location": "1061f350-a2f1-111e-b9eb-123e8b139117",
  "network": {
    "online": true,
    "last_connection": "2012-09-30 21:26:25",
    "wan_ip": "173.246.19.71",
    "local_ip": "192.168.1.201",
    "mac_address": "18b430046194"
  }
}

使用try...catch来捕获可能发生的异常

try {
    $nest = new Nest(NULL, NULL, $issue_token, $cookies);
    // Execute all Nest-related code here
} catch (UnexpectedValueException $ex) {
    // Happens when the issue_token or cookie is not working, for whatever reason
    $error_message = $ex->getMessage();
    mail(...);
} catch (RuntimeException $ex) {
    // Probably a temporary server-error
} catch (Exception $ex) {
    // Other errors; should not happen if it worked in the past
}

// Continue your code here, for example to save the result in a database

使用Google账户

$issue_token$cookies的值对于你的Google账户是特定的。要获取它们,请按照以下步骤操作(只需进行一次,只要你的Google账户保持登录状态)。

  • 在Chrome浏览器中打开一个隐身标签页(或清除你的缓存)。
  • 打开开发者工具(视图/开发者/开发者工具)。
  • 点击网络标签页。确保已选中Preserve Log
  • 过滤器框中,输入issueToken
  • 转到https://home.nest.com,并点击使用Google登录。登录您的账户。
  • 一个网络调用(以iframerpc开头)将出现在开发者工具窗口中。点击它。
  • 头信息标签下,在常规部分,复制整个请求URL(以https://#开头,以nest.com结尾)。这是你的$issue_token
  • 过滤器框中,输入oauth2/iframe
  • 开发工具窗口中会出现几个网络调用。点击最后一个iframe调用。
  • 头部标签下,在请求头部中,复制整个cookie值(包括整个多行字符串以及许多字段/值对 - 不要包含Cookie:前缀)。这是你的$cookies;确保它都在一行上。

故障排除

如果你有任何问题,请尝试在PHP脚本顶部添加以下代码,以请求PHP输出所有错误和警告。

error_reporting(E_ALL);

致谢

主要使用JetBrains慷慨提供的免费开源许可版本的PHPStorm开发。谢谢大家!