Nature Remo APIで色々やってみたメモ

家で、子どもたちの部屋にAmazon Echo、Nature Remo miniを設置して、電気やエアコンを操作できるようにしています。

が、もっと遊び倒せるはずなので、何ができるのかを色々試していけたら。

色々見ていたら、Nature Remoの公式APIがあったので、それで色々やってみました。

### Step1: Nature Remoから部屋の室温を取得する
#### 1-1: アクセストークン発行

Nature Remoにログインします。
https://home.nature.global/

![20201206_IMG1232_0000.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/1068dbbe-56fd-f23e-61cd-da5d008c24ab.png)
Googleのアカウントでログインしました。
![20201206_IMG1233_0000.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/299d72b3-6751-535a-3245-e687704b0701.png)
いっぱいアカウントがあるので、今回使うものをチョイス。
![20201206_IMG1235_0000.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/c1c379ff-4a83-64e7-3cba-46c08b74e9fd.png)
![20201206_IMG1237_0000.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/e883acba-7a87-bd35-aee2-32603ea254af.png)
もちろんAuthorizeします。
![20201206_IMG1238_0000.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/32b796b4-0162-faf9-0ee0-70e284e29c1d.png)
ようこそ！と言われたのでGo。
![20201206_IMG1239_0000.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/76983b17-c226-7bc9-05e2-c6a63d43520b.png)
過去にAlexaと接続した時に、この画面開いてアクセストークン発行したことがあるみたいです。（記憶なし）
アクセストークンを新たに発行します。
![20201206_IMG1240_0000.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/7c2ce2f1-e1a9-fa31-2a0c-8a789fe501a5.png)
Copyボタンを押せばアクセストークンがコピーできます。
無くしてはいけません。
![20201206_IMG1242_0000.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/f43fba01-17cf-98ea-55b2-ee0cfcd4c785.png)

#### 1-2: APIを叩いて、部屋の室温を取得する

Nature RemoのAPIは、[Swagger](https://swagger.io/)で公開されています。
https://swagger.nature.global/

この中の、**[GET] /1/devices**が室温を取得するために必要なAPIです。
![20201206_IMG1257_0000.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/244560/2eb2bf95-90d8-d8e2-4e8c-d2f77f150a04.png)

Terminalを開いて```curl```でAPIを実行します。
実行すると、JSONが返却されるため、```jq```で整形します。
そのため、事前に```curl```と```jq```はインストールしておいてください。

取得するコマンドは以下の通りです。
*AccessToken*の部分を先ほど取得したアクセストークンに置き換えます。

```terminal
$ curl -X GET "https://api.nature.global/1/devices" -H "Authorization: Bearer AccessToken" | jq
```
実行すると、以下のような結果が得られました。
現在の室温は、21.8度です。
実行後に出力されているスピードなどの部分は無視で。

```terminal
$ curl -X GET "https://api.nature.global/1/devices" -H "Authorization: Bearer AccessToken" | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   463  100   463    0     0    466      0 --:--:-- --:--:-- --:--:--   465
[
  {
    "name": "Kids Room",
    "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
    "created_at": "2019-01-02T03:16:29Z",
    "updated_at": "2020-12-06T03:25:19Z",
    "mac_address": "XX:XX:XX:XX:XX:XX",
    "serial_number": "XXXXXXXXX",
    "firmware_version": "Remo-mini/2.0.62-gf5b5d27",
    "temperature_offset": 0,
    "humidity_offset": 0,
    "users": [
      {
        "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
        "nickname": "hoge fuga",
        "superuser": true
      }
    ],
    "newest_events": {
      "te": {
        "val": 21.8,
        "created_at": "2020-12-06T02:56:32Z"
      }
    }
  }
]
```

室温だけを絞り込んで表示します。（室温が変わってるー）

```terminal
$ curl -X GET "https://api.nature.global/1/devices" -H "Authorization: Bearer AccessToken" | jq .[].newest_events.te.val
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   463  100   463    0     0    514      0 --:--:-- --:--:-- --:--:--   513
22.8
```

### Step2: Nature Remo APIを色々叩いてみる

#### [GET] /1/users/me

```terminal
$ curl -X GET "https://api.nature.global/1/users/me" -H "Authorization: Bearer AccessToken" | jq 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    73  100    73    0     0     89      0 --:--:-- --:--:-- --:--:--    89
{
  "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
  "nickname": "hoge fuga"
}
```

iPhoneのNature Remoアプリでアカウントのニックネームを変えたら、ちゃんと反映されるかをチェック。
変更後にもう一度叩いてみると・・・。

```terminal
$ curl -X GET "https://api.nature.global/1/users/me" -H "Authorization: Bearer TelVnc4wKuhfFGGHw6-wdj5-ZidD5qqxHV1Zo5rak90.TGjajbi-wDZdfBqNdATsgokjO733TojTVeykIgp5DbE" | jq 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    65  100    65    0     0     31      0  0:00:02  0:00:02 --:--:--    31
{
  "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
  "nickname": "TakSus"
}
```
変わりました〜。

#### [POST] /1/users/me

```curl```コマンドでニックネームを変更してみます。

```terminal
$ curl -X POST "https://api.nature.global/1/users/me" -H "Authorization: Bearer AccessToken" -d "nickname=TestName" | jq 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    84  100    67  100    17     79     20 --:--:-- --:--:-- --:--:--   100
{
  "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
  "nickname": "TestName"
}
```
ちゃんとiPhoneのアプリでも変更を確認できました。
一度アプリを切って、再起動しないと反映していることは確認できないようです。

#### [GET] /1/appliances

つまり、Nature Remoに繋がっている家電製品の一覧が出てくる、ってことかな？
叩いてみます。
・・・めっちゃ長いぞ！？

```terminal
$ curl -X GET "https://api.nature.global/1/appliances" -H "Authorization: Bearer TelVnc4wKuhfFGGHw6-wdj5-ZidD5qqxHV1Zo5rak90.TGjajbi-wDZdfBqNdATsgokjO733TojTVeykIgp5DbE" | jq 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  6801  100  6801    0     0   3388      0  0:00:02  0:00:02 --:--:--  3388
[
  {
    "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
    "device": {
      "name": "Kids Room",
      "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
      "created_at": "2019-01-02T03:16:29Z",
      "updated_at": "2020-12-06T12:47:26Z",
      "mac_address": "XX:XX:XX:XX:XX:XX",
      "serial_number": "000000000",
      "firmware_version": "Remo-mini/2.0.62-gf5b5d27",
      "temperature_offset": 0,
      "humidity_offset": 0
    },
    "model": null,
    "type": "IR",
    "nickname": "オーディオ",
    "image": "ico_audio",
    "settings": null,
    "aircon": null,
    "signals": [
      {
        "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
        "name": "電源",
        "image": "ico_io"
      }
    ]
  },
  {
    "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
    "device": {
      "name": "Kids Room",
      "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
      "created_at": "2019-01-02T03:16:29Z",
      "updated_at": "2020-12-06T12:47:26Z",
      "mac_address": "XX:XX:XX:XX:XX:XX",
      "serial_number": "000000000",
      "firmware_version": "Remo-mini/2.0.62-gf5b5d27",
      "temperature_offset": 0,
      "humidity_offset": 0
    },
    "model": null,
    "type": "IR",
    "nickname": "ライト",
    "image": "ico_light",
    "settings": null,
    "aircon": null,
    "signals": [
      {
        "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
        "name": "電源",
        "image": "ico_io"
      },
      {
        "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
        "name": "ライトオン",
        "image": "ico_on"
      }
    ]
  },
  {
    "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
    "device": {
      "name": "Kids Room",
      "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
      "created_at": "2019-01-02T03:16:29Z",
      "updated_at": "2020-12-06T12:47:26Z",
      "mac_address": "XX:XX:XX:XX:XX:XX",
      "serial_number": "000000000",
      "firmware_version": "Remo-mini/2.0.62-gf5b5d27",
      "temperature_offset": 0,
      "humidity_offset": 0
    },
    "model": null,
    "type": "IR",
    "nickname": "電気",
    "image": "ico_light",
    "settings": null,
    "aircon": null,
    "signals": [
      {
        "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
        "name": "部屋の電気オン",
        "image": "ico_on"
      },
      {
        "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
        "name": "部屋の電気オフ",
        "image": "ico_off"
      }
    ]
  },
  {
    "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
    "device": {
      "name": "Kids Room",
      "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
      "created_at": "2019-01-02T03:16:29Z",
      "updated_at": "2020-12-06T12:47:26Z",
      "mac_address": "XX:XX:XX:XX:XX:XX",
      "serial_number": "000000000",
      "firmware_version": "Remo-mini/2.0.62-gf5b5d27",
      "temperature_offset": 0,
      "humidity_offset": 0
    },
    "model": {
      "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
      "country": "JP",
      "manufacturer": "toshiba",
      "remote_name": "ct-90467",
      "name": "Toshiba TV 001",
      "image": "ico_tv"
    },
    "type": "TV",
    "nickname": "テレビ",
    "image": "ico_tv",
    "settings": null,
    "aircon": null,
    "signals": [],
    "tv": {
      "buttons": [
        {
          "name": "power",
          "image": "ico_io",
          "label": "TV_power"
        },
        {
          "name": "select-input-src",
          "image": "ico_input",
          "label": "TV_source"
        },
        {
          "name": "tv-schedule",
          "image": "ico_tv_guide",
          "label": "TV_schedule"
        },
        {
          "name": "mute",
          "image": "ico_mute",
          "label": "TV_mute"
        },
        {
          "name": "input-terrestrial",
          "image": "ico_tv",
          "label": "TV_terrestrial"
        },
        {
          "name": "input-bs",
          "image": "ico_bs",
          "label": "TV_BS"
        },
        {
          "name": "input-cs",
          "image": "ico_cs",
          "label": "TV_CS"
        },
        {
          "name": "select-audio",
          "image": "ico_select_audio",
          "label": "TV_select_audio"
        },
        {
          "name": "ch-1",
          "image": "ico_number_1",
          "label": "TV_1"
        },
        {
          "name": "ch-2",
          "image": "ico_number_2",
          "label": "TV_2"
        },
        {
          "name": "ch-3",
          "image": "ico_number_3",
          "label": "TV_3"
        },
        {
          "name": "ch-4",
          "image": "ico_number_4",
          "label": "TV_4"
        },
        {
          "name": "ch-5",
          "image": "ico_number_5",
          "label": "TV_5"
        },
        {
          "name": "ch-6",
          "image": "ico_number_6",
          "label": "TV_6"
        },
        {
          "name": "ch-7",
          "image": "ico_number_7",
          "label": "TV_7"
        },
        {
          "name": "ch-8",
          "image": "ico_number_8",
          "label": "TV_8"
        },
        {
          "name": "ch-9",
          "image": "ico_number_9",
          "label": "TV_9"
        },
        {
          "name": "ch-10",
          "image": "ico_number_10",
          "label": "TV_10"
        },
        {
          "name": "ch-11",
          "image": "ico_number_11",
          "label": "TV_11"
        },
        {
          "name": "ch-12",
          "image": "ico_number_12",
          "label": "TV_12"
        },
        {
          "name": "back",
          "image": "ico_return",
          "label": "TV_back"
        },
        {
          "name": "menu",
          "image": "ico_menu",
          "label": "TV_menu"
        },
        {
          "name": "display",
          "image": "ico_display",
          "label": "TV_display_change"
        },
        {
          "name": "d",
          "image": "ico_d",
          "label": "TV_data"
        },
        {
          "name": "ch-up",
          "image": "ico_arrow_top",
          "label": "TV_next_channel"
        },
        {
          "name": "ch-down",
          "image": "ico_arrow_bottom",
          "label": "TV_previous_channel"
        },
        {
          "name": "left",
          "image": "ico_arrow_left",
          "label": "TV_left"
        },
        {
          "name": "up",
          "image": "ico_arrow_top",
          "label": "TV_top"
        },
        {
          "name": "right",
          "image": "ico_arrow_right",
          "label": "TV_right"
        },
        {
          "name": "down",
          "image": "ico_arrow_bottom",
          "label": "TV_bottom"
        },
        {
          "name": "ok",
          "image": "ico_record",
          "label": "TV_ok"
        },
        {
          "name": "vol-up",
          "image": "ico_plus",
          "label": "TV_volume_up"
        },
        {
          "name": "vol-down",
          "image": "ico_minus",
          "label": "TV_volume_down"
        },
        {
          "name": "blue",
          "image": "ico_color_blue",
          "label": "TV_blue"
        },
        {
          "name": "red",
          "image": "ico_color_red",
          "label": "TV_red"
        },
        {
          "name": "green",
          "image": "ico_color_green",
          "label": "TV_green"
        },
        {
          "name": "yellow",
          "image": "ico_color_yellow",
          "label": "TV_yellow"
        },
        {
          "name": "fast-rewind",
          "image": "ico_backward",
          "label": "TV_fast_rewind"
        },
        {
          "name": "play",
          "image": "ico_play",
          "label": "TV_play"
        },
        {
          "name": "",
          "image": "",
          "label": ""
        },
        {
          "name": "fast-forward",
          "image": "ico_forward",
          "label": "TV_fast_forward"
        },
        {
          "name": "prev",
          "image": "ico_previous",
          "label": "TV_previous"
        },
        {
          "name": "pause",
          "image": "ico_pause",
          "label": "TV_pause"
        },
        {
          "name": "stop",
          "image": "ico_stop",
          "label": "TV_stop"
        },
        {
          "name": "next",
          "image": "ico_next",
          "label": "TV_next"
        },
        {
          "name": "clear-sound",
          "image": "",
          "label": "TV_clear_sound"
        },
        {
          "name": "rec-list",
          "image": "",
          "label": "TV_rec_list"
        },
        {
          "name": "settings",
          "image": "ico_setting",
          "label": "TV_settings"
        },
        {
          "name": "subtitle",
          "image": "ico_subtitle",
          "label": "TV_subtitle"
        },
        {
          "name": "exit",
          "image": "ico_cross",
          "label": "TV_exit"
        },
        {
          "name": "rewind-10-sec",
          "image": "ico_backward",
          "label": "TV_rewind_10sec"
        },
        {
          "name": "forward-30-sec",
          "image": "ico_forward",
          "label": "TV_forward_30sec"
        }
      ],
      "state": {
        "input": "t"
      }
    }
  },
  {
    "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
    "device": {
      "name": "Kids Room",
      "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
      "created_at": "2019-01-02T03:16:29Z",
      "updated_at": "2020-12-06T12:47:26Z",
      "mac_address": "XX:XX:XX:XX:XX:XX",
      "serial_number": "000000000",
      "firmware_version": "Remo-mini/2.0.62-gf5b5d27",
      "temperature_offset": 0,
      "humidity_offset": 0
    },
    "model": {
      "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
      "country": "JP",
      "manufacturer": "mitsubishi",
      "remote_name": "rh101-123dl",
      "series": "Mitsubishi AC",
      "name": "Mitsubishi AC 001",
      "image": "ico_ac_1"
    },
    "type": "AC",
    "nickname": "エアコン",
    "image": "ico_ac_1",
    "settings": {
      "temp": "24",
      "temp_unit": "c",
      "mode": "warm",
      "vol": "auto",
      "dir": "auto",
      "dirh": "",
      "button": "",
      "updated_at": "2020-12-06T12:47:26Z"
    },
    "aircon": {
      "range": {
        "modes": {
          "cool": {
            "temp": [
              "16",
              "17",
              "18",
              "19",
              "20",
              "21",
              "22",
              "23",
              "24",
              "25",
              "26",
              "27",
              "28",
              "29",
              "30",
              "31"
            ],
            "dir": [
              "1",
              "2",
              "3",
              "4",
              "5",
              "auto",
              "swing"
            ],
            "dirh": [
              ""
            ],
            "vol": [
              "1",
              "2",
              "3",
              "auto"
            ]
          },
          "dry": {
            "temp": [
              ""
            ],
            "dir": [
              "1",
              "2",
              "3",
              "4",
              "5",
              "auto",
              "swing"
            ],
            "dirh": [
              ""
            ],
            "vol": [
              "1",
              "2",
              "3",
              "auto"
            ]
          },
          "warm": {
            "temp": [
              "16",
              "17",
              "18",
              "19",
              "20",
              "21",
              "22",
              "23",
              "24",
              "25",
              "26",
              "27",
              "28",
              "29",
              "30",
              "31"
            ],
            "dir": [
              "1",
              "2",
              "3",
              "4",
              "5",
              "auto",
              "swing"
            ],
            "dirh": [
              ""
            ],
            "vol": [
              "1",
              "2",
              "3",
              "auto"
            ]
          }
        },
        "fixedButtons": [
          "power-off"
        ]
      },
      "tempUnit": "c"
    },
    "signals": []
  }
]
```
長いのは、テレビとエアコンか。
当然と言えば当然だけど、リモコンでできることが多ければ多いほど、取得できる値も多い。
ちゃんと東芝とか三菱とか、わかるもんですね。

### Next Step

APIで値が取れたり、更新できることはわかった。
これはあくまで```curl```で叩いただけなので、次はプログラムから叩きたい。
Lambdaを使うのが無難かな？