見出し画像

「只今 Zoom 中」を知らせたい - その後

前回 Zoom 中に勝手に点灯するライトを作る話を書きましたが、その後問題が発生しました。

しばらく普通に使えていたのですが、ある時点からプラグにアクセスできなくなったのです。どうやらキャプチャーして使いまわしていたパケットの「有効期限」が切れたようです。まあセキュリティ上はその方が良いとは思いますけれど ...。

Meross IoT ライブラリ

ということで、やはり少し真面目にやらなけれればならないようですが、そもそも Meross というメーカーは API の仕様を一般には公開していないようです。

そこでさらに検索してみると Meross IoT という python のライブラリを公開している人がいました。

この冒頭を読むと ...

This library was built by looking at the traffic network between Meross App and the Meross backends, such as HTTP api and MQTT broker. Meross did not provide any official documentation and you should consider this library as unofficial and unsupported by Meross.

といったことが書いてあります。要するに「メーカーは公式仕様書を提供していないので、このライブラリはあくまでも勝手に解析した非公式なものだ」というわけです。当然ながら

For this reason, you should consider this library not for production use as there is no warranty that Meross does not change the way it works or explicitly blocks it. So far Meross guys seem happy about that but you should know that it might happen.

「何の保証もないので、いつ使えなくなるかわからない。注意して使ってね」とあります。

まあこのブログはティンカリングメモですので、「とりあえず使える」ならばOKです(もちろん仕事で使うときにはきちんと Meross と契約を交わして公式なドキュメントを提供して貰わなければなりません)ということで ON/OFF を行うスクリプトを差し替えることにしました。

Python 3.6 以上で動作するとドキュメントには書いてありますが、自分のマシンには Python 3.8.5 が入っているので、おそらくこれで大丈夫でしょう。

% python --version
Python 3.8.5
% 

ということで Python 3.6 以上が入っている状態で、コマンドプロンプトから pip (Pythonのライブラリマネージャ)でライブラリをインストールします。以下のコマンドを実行します。

% pip install meross-iot --upgrade

ON/OFF を行う Python アプリを作成

これで Meross IoT ライブラリが使えるようになった筈です。ドキュメントとサンプルを読みながら以下のプログラムを作成し、これを ON.py という名前にしました。

#!env python
import asyncio
import os
from meross_iot.http_api import MerossHttpClient
from meross_iot.manager import MerossManager
EMAIL = os.environ.get('MEROSS_EMAIL') # または "Meross Cloud 登録 email"
PASSWORD = os.environ.get('MEROSS_PASSWORD') # または "Meross Cloud 登録 パスワード"
async def main():
   # Setup the HTTP client API from user-password
   http_api_client = await MerossHttpClient.async_from_user_password(email=EMAIL, password=PASSWORD)
   # Setup and start the device manager
   manager = MerossManager(http_client=http_api_client)
   await manager.async_init()
   # Retrieve all the MSS110 devices that are registered on this account
   await manager.async_device_discovery()
   plugs = manager.find_devices(device_type="mss110")
   if len(plugs) < 1:
       print("No MSS110 plugs found...")
   else:
       # Turn it on channel 0
       dev = plugs[0]
       await dev.async_turn_on(channel=0)
   # Close the manager and logout from http_api
   manager.close()
   await http_api_client.async_logout()
if __name__ == '__main__':
   # On Windows + Python 3.8, you should uncomment the following
   # asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
   loop = asyncio.get_event_loop()
   loop.run_until_complete(main())
   loop.close()

環境変数 MEROSS_EMAL、MEROSS_PASSWORD にそれぞれ Meross デバイスに設定した電子メールアドレスとパスワードを設定すれば実行できます。

まとめ

やはりネットワーク上で拾ったパケットの使い回しでは、長くは使えませんでした。まあ前回も書いたようにそこがゆるゆるのままでは、セキュリティ的に問題ですので、そこは仕方ないことですね。とはいえ今回の方法では Meross 社のクラウドに対してユーザー認証をしに行きますので、結局インターネットを使うこととなってしまいました。

インターネットを使うなら IFTTT を使っても良かったのではということになりますね(笑)。とはいえIFTTTも本格的に使い出すと呼び出す頻度に応じて課金されたりするので油断はなりません。

現在は無事に Python 版を利用できています。

今回のティンカリング

所要時間

ライブラリの検索と python 環境整備(30分)

プログラミング (10分)-- サンプルを元に改造

費用

特になし

この記事が気に入ったらサポートをしてみませんか?