Nature Remo APIを使ってChatGPTに接続家電一覧を取得してもらおう - NEXTAltair's diary
前の続き一覧を取得しただけで操作できる利点が何一つ活かせてなかったので今回はそのへんをやってく
ChatGPTの能力を生かした使い方とも思えないが、とりあえずエアコンの操作機能を足す
先にアクション用YAML
openapi: 3.0.3
info:
# APIの基本的な情報を含みます(タイトル、バージョン、説明など)
title: Nature Remo for GPT
version: 1
servers:
# APIのエンドポイントを定義
paths:
#利用可能なエンドポイントと、それぞれのHTTPメソッドに対する操作を記述
/1/appliances:
#家電一覧の表示と情報の取得
get:
summary: Remoに登録されている家電の一覧を取得
operationId: getAppliances
responses:
"200":
description: 接続された家電の一覧を取得しました
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Appliance"
"400":
$ref: '#/components/responses/BadRequest'
"401":
$ref: "#/components/responses/Unauthorized"
/1/devices:
#Remoデバイスと測定値の取得
get:
summary: デバイスのStatusを取得
operationId: NatureRemoStatus
parameters:
- name: deviceId
in: path
required: true
schema:
type: string
description: Nature Remoのステータス
responses:
"200":
description: デバイスステータスの取得に成功
content:
application/json:
schema:
type: object
properties:
humidity:
type: number
format: float
description: 湿度
illuminance:
type: number
format: float
description: 照度
motion:
type: number
description: 動き
temperature:
type: number
format: float
description: 温度
"404":
$ref: "#/components/responses/NotFound"
/1/appliances/{applianceid}/aircon_settings:
post:
summary: エアコンの設定を更新
operationId: updateAirconSettings
tags:
- Aircon
parameters:
- name: applianceid
in: path
required: true
schema:
type: string
description: 制御対象エアコンのID
requestBody:
required: true
content:
application/x-www-form-urlencoded:
schema:
type: object
properties:
air_direction:
type: string
description: AC 空気の方向。 空欄は自動
default: "auto" # デフォルトで自動風向を設定
air_direction_h:
type: string
description: AC水平風向。
air_volume:
type: string
description: |-
AC風量。 空欄は自動。数字は風量
エアコンが受け付ける風量の範囲はエアコンの機種や運転モードによって異なる
特定のエアコン モデルと動作モードの範囲については、応答内の「AirConRangeMode」情報を確認
default: "auto" # デフォルトで自動風量を設定
button:
type: string
description: |-
ボタン。 エアコンの電源をオフにしたい場合「power-off」を指定
空欄は電源が入っていることを意味します。
default: "" # 電源ONのデフォルト値
operation_mode:
type: string
description: |-
AC動作モード。 エアコンが受け付ける運転モードの範囲はエアコンの機種によって異る。
特定のエアコン モデルの範囲については、応答内の「AirConRangeMode」情報を確認
default: "auto" # デフォルトモードとして自動を設定
temperature:
type: string
description: |-
温度。 文字列形式の温度。
ユニットは Aircon オブジェクトに記述
エアコンが許容できる温度範囲はエアコンの機種や運転モードによって異る
特定のエアコン モデルと動作モードの範囲については、応答内の「AirConRangeMode」情報を確認
default: "0" # 日立のエアコンはオートモードでは基準温度-3から+3 なので0をデフォルト値として設定
temperature_unit:
type: string
description: 温度単位。 不明の場合は「c」または「f」または「''」。
default: "c"
required:
- temperature
- operation_mode
- air_volume
- air_direction
- button
responses:
"200":
description: エアコン設定を正常に更新
content:
application/json:
schema:
type: object
properties:
status:
type: string
description: エアコンの状態
"400":
$ref: '#/components/responses/BadRequest'
"404":
$ref: '#/components/responses/NotFound'
components:
# 再利用可能なスキーマやパラメータ、セキュリティスキームなどを定義
responses:
# エラーが発生した場合のレスポンスは共通であるためここ定義して使い回す
400 Bad Request:
description: 構文が正しくないため、リクエストをサーバーが理解できませんでした。
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: Bad Request
401 Unauthorized:
description: リクエストには認証が必要です。
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: Unauthorized
403 Forbidden:
description: リクエストする権限がありません。
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: Forbidden
404 NotFound:
description: 家電かデバイスのIDを確認してください。
content:
application/json:
schema:
type: object
properties:
error:
type: string
message:
type: string
example: Device not found
405 Method Not Allowed:
description: リクエストされたメソッドは許可されていません。
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: Method Not Allowed
408 Request Timeout:
description: サーバーがリクエストを処理するのに時間がかかりすぎました。
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: Request Timeout
500 Internal Server Error:
description: サーバーでエラーが発生しました。
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: Internal Server Error
502 Bad Gateway:
description: ゲートウェイまたはプロキシサーバーが不正な応答を受信しました。
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: Bad Gateway
503 Service Unavailable:
description: サービスが一時的に利用できません。
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: Service Unavailable
bearerAuth:
description: OAuth2トークンを使ってAPIにアクセス
type: oauth2
flows:
clientCredentials:
scopes: {}
schemas:
# リクエストとレスポンスで使用されるデータ構造を定義
Appliance:
type: object
properties:
id:
type: string
description: Unique identifier for the appliance
type:
type: string
description: Type of the appliance
nickname:
type: string
description: User-defined name of the appliance
Dummy:
type: object
properties:
dummyField:
type: string
description: This is a dummy field to avoid schema object errors
security:
# グローバルセキュリティ要件を指定
# OAuth2トークンを使う場合bearerAuthを利用することが重要
- bearerAuth: []
書き方があってるか保証はできないがChatGPTがうまいこと解釈してくれて動かす
無駄は多いい気がするが、ChatGPTが渡されたJSONをいい感じに解釈して動いてくれるのに今のところはOKとする
2.今回のポイント応答内の「AirConRangeMode」
エアコンごとのモードや設定できる値の範囲が違うの
1度参照してやらないと操作中にエラーが出る
/1/devices:でそのへんのパラメーターをGetできるんで
APIからエアコンのAirConRangeModeで有効な値調べてって言うと出るよ
エアコン「Hitachi AC 055」の設定範囲は以下の通りです:
- **温度範囲**:
- **自動モード**: -3°C から 3°C
- **冷房モード**: 16°C から 32°C
- **除湿モード**: -3°C から 3°C
- **暖房モード**: 16°C から 32°C
- **風量設定**:
- **自動モード**: "1", "2", "auto"
- **冷房モード**: "1", "2", "3", "4", "5", "auto"
- **除湿モード**: "1", "2", "3", "4", "5", "auto"
- **暖房モード**: "1", "2", "3", "4", "5", "auto"
- **風向設定 (縦方向)**:
- 全モード共通: "1", "2", "3", "4", "5", "6", "7", "auto", "swing"
- **風向設定 (横方向)**:
- 全モード共通: "1", "2", "3", "4", "5", "6", "7", "8", "9", "swing"
- **固定ボタン**: "power-off"
これらの範囲内でエアコンの設定を行うことができます。
3.プロンプトで善きに計らってもらう
あらかじめAirConRangeModeを取得するようなコードが必要かと思ったけど
このGPTは、スマートホームデバイスのAPI連携とコントロールを手伝うために設計されています。チャット開始時にNatureAPIを利用してNature Remo3から部屋の環境情報を自動的に取得し、それらのIDを記憶しておきます。日本語でユーザーからの具体的な指示に従い、適切なAPIコールを行う方法や、デバイスを効率的に操作するためのアドバイスを提供します。エアコンの設定を頼まれた際には、まずgetAppliancesを使ってAirConRangeModeから有効な値を取得し、その情報に基づいて操作を行います。ユーザーの質問や要望には簡潔に応答し、技術的な支援を行います。ユーザーがAPIのアクションの作成を頼んだときは、GPTsで使えるYAML形式のコードを提供します。
これで何とかなる
人感センサーのテストもしたらなんかタイムスタンプおかしい・・・
参考
今更聞けない(?)Swaggerについて
OpenAPIに入門する
Nature Inc. | Nature Developer Page