『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
楼主: Yonsm

[修仙教程] MiService - MiIO/MIoT 协议工具,获取Token超级简单

  [复制链接]

2

主题

89

帖子

833

积分

高级会员

Rank: 4

积分
833
金钱
744
HASS币
0
发表于 2021-3-6 23:43:32 | 显示全部楼层
小白弱弱的问一句,ha是在docker环境下,是在ha里安装miservice,还是主机里安装
回复

使用道具 举报

1

主题

198

帖子

2175

积分

金牌会员

Rank: 6Rank: 6

积分
2175
金钱
1977
HASS币
0
发表于 2021-3-7 00:57:05 | 显示全部楼层
a5855631 发表于 2021-3-6 16:33
比较新的python版本安装的时候就会出现上面的提示,下载旧版本就可以正常安装上了 ...

我试了两个版本的python
3.7 & 3.8 问题都一样,不过我手动安装了,功能很强大!
回复

使用道具 举报

1

主题

198

帖子

2175

积分

金牌会员

Rank: 6Rank: 6

积分
2175
金钱
1977
HASS币
0
发表于 2021-3-7 01:02:21 | 显示全部楼层
WeDone 发表于 2021-3-6 23:43
小白弱弱的问一句,ha是在docker环境下,是在ha里安装miservice,还是主机里安装 ...

miservice 应该是在linux + python 3 环境下安装
回复

使用道具 举报

2

主题

21

帖子

203

积分

论坛分享达人

积分
203
金钱
182
HASS币
0
发表于 2021-3-7 11:33:02 | 显示全部楼层
膜拜大佬
回复

使用道具 举报

4

主题

91

帖子

687

积分

高级会员

Rank: 4

积分
687
金钱
596
HASS币
0
发表于 2021-3-7 13:59:41 | 显示全部楼层
firewater 发表于 2021-3-7 00:57
我试了两个版本的python
3.7 & 3.8 问题都一样,不过我手动安装了,功能很强大! ...

我安装了登录不上,你配置用户名和密码是直接写用户名,还是需要空格/符号之类的不?我各种试了都一致无法登录
回复

使用道具 举报

21

主题

294

帖子

2551

积分

论坛技术达人

积分
2551
金钱
2207
HASS币
180

教程狂人

 楼主| 发表于 2021-3-7 14:07:10 | 显示全部楼层
a5855631 发表于 2021-3-7 13:59
我安装了登录不上,你配置用户名和密码是直接写用户名,还是需要空格/符号之类的不?我各种试了都一致无 ...

手机号、miid 均可
回复

使用道具 举报

4

主题

91

帖子

687

积分

高级会员

Rank: 4

积分
687
金钱
596
HASS币
0
发表于 2021-3-7 14:12:16 | 显示全部楼层
Yonsm 发表于 2021-3-7 14:07
手机号、miid 均可

Exception on login 1826158****: Cannot connect to host account.xiaomi.com:443 ssl:True [SSLCertVerificationError: (1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1108)')]
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/aiohttp/connector.py", line 969, in _wrap_create_connection
    return await self._loop.create_connection(*args, **kwargs)  # type: ignore  # noqa
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py", line 1050, in create_connection
    transport, protocol = await self._create_connection_transport(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py", line 1080, in _create_connection_transport
    await waiter
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/sslproto.py", line 529, in data_received
    ssldata, appdata = self._sslpipe.feed_ssldata(data)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/sslproto.py", line 189, in feed_ssldata
    self._sslobj.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 944, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1108)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/miservice/miaccount.py", line 56, in login
    resp = await self._serviceLogin(f'serviceLogin?sid={sid}&_json=true')
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/miservice/miaccount.py", line 94, in _serviceLogin
    async with self.session.request('GET' if data is None else 'POST', url, data=data, cookies=cookies, headers=headers) as r:
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/aiohttp/client.py", line 1117, in __aenter__
    self._resp = await self._coro
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/aiohttp/client.py", line 520, in _request
    conn = await self._connector.connect(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/aiohttp/connector.py", line 535, in connect
    proto = await self._create_connection(req, traces, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/aiohttp/connector.py", line 892, in _create_connection
    _, proto = await self._create_direct_connection(req, traces, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/aiohttp/connector.py", line 1051, in _create_direct_connection
    raise last_exc
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/aiohttp/connector.py", line 1020, in _create_direct_connection
    transp, proto = await self._wrap_create_connection(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/aiohttp/connector.py", line 971, in _wrap_create_connection
    raise ClientConnectorCertificateError(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorCertificateError: Cannot connect to host account.xiaomi.com:443 ssl:True [SSLCertVerificationError: (1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1108)')]
Error https://api.io.mi.com/app/home/device_list: Login failed
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/bin/micli.py", line 55, in <module>
    loop.run_until_complete(main(username, password, did, ' '.join(argv[1:])))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/Library/Frameworks/Python.framework/Versions/3.8/bin/micli.py", line 31, in main
    result = await miio_command(service, did, text, sys.argv[0] + ' ')
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/miservice/miiocommand.py", line 63, in miio_command
    return await service.device_list(argc > 0 and argv[0], argc > 1 and string_to_value(argv[1]), argc > 2 and argv[2])
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/miservice/miioservice.py", line 97, in device_list
    result = await self.miio_request('/home/device_list', {'getVirtualModel': bool(getVirtualModel), 'getHuamiDevices': int(getHuamiDevices)})
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/miservice/miioservice.py", line 58, in miio_request
    return (await self.account.mi_request('xiaomiio', self.server + uri, prepare_data, headers))['result']
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/miservice/miaccount.py", line 134, in mi_request
    raise Exception(error)
Exception: Error https://api.io.mi.com/app/home/device_list: Login failed
回复

使用道具 举报

21

主题

294

帖子

2551

积分

论坛技术达人

积分
2551
金钱
2207
HASS币
180

教程狂人

 楼主| 发表于 2021-3-7 19:53:34 | 显示全部楼层
本帖最后由 Yonsm 于 2021-3-7 20:01 编辑
a5855631 发表于 2021-3-7 14:12
Exception on login 1826158****: Cannot connect to host account.xiaomi.com:443 ssl:True [SSLCertVer ...

SSL整数检查不通过,可能是你用了代理,或者本机ca bundles不包含小米的ssl跟证书。
1. 如果你是在 ZhiMi 中使用 MiService,可以在 ZhiMi/__init__.py 中把:
async_get_clientsession(hass)
改成:
async_get_clientsession(hass, verify_ssl=False)


2. 如果你是使用micli.py,可以在 micli.py 关闭SSL整数检查看看:
async with ClientSession() as session:
改成:
async with ClientSession(connector=aiohttp.TCPConnector(verify_ssl=False)) as session:



回复

使用道具 举报

23

主题

125

帖子

2033

积分

金牌会员

Rank: 6Rank: 6

积分
2033
金钱
1908
HASS币
10
发表于 2021-3-7 20:42:02 | 显示全部楼层
Yonsm 发表于 2021-3-7 19:53
SSL整数检查不通过,可能是你用了代理,或者本机ca bundles不包含小米的ssl跟证书。
1. 如果你是在 ZhiMi  ...

建议把账号密码获取改成读取txt文件,这样方便些,不用设置变量了

with open("pass.txt", "r") as f:
    data = f.read()
回复

使用道具 举报

2

主题

89

帖子

833

积分

高级会员

Rank: 4

积分
833
金钱
744
HASS币
0
发表于 2021-3-7 21:25:32 | 显示全部楼层
老大麻烦看看,不能执行文本命令
设备是 红米 小爱音箱 Play
接口能力如下:
micli.py spec xiaomi.wifispeaker.l7a
{
  "type": "urn:miot-spec-v2:device:speaker:0000A015:xiaomi-l7a:1",
  "description": "Speaker",
  "services": [
    {
      "iid": 1,
      "type": "urn:miot-spec-v2:service:device-information:00007801:xiaomi-l7a:1",
      "description": "Device Information",
      "properties": [
        {
          "iid": 1,
          "type": "urn:miot-spec-v2:property:manufacturer:00000001:xiaomi-l7a:1",
          "description": "Device Manufacturer",
          "format": "string",
          "access": [
            "read"
          ]
        },
        {
          "iid": 2,
          "type": "urn:miot-spec-v2:property:model:00000002:xiaomi-l7a:1",
          "description": "Device Model",
          "format": "string",
          "access": [
            "read"
          ]
        },
        {
          "iid": 3,
          "type": "urn:miot-spec-v2:property:serial-number:00000003:xiaomi-l7a:1",
          "description": "Device Serial Number",
          "format": "string",
          "access": [
            "read"
          ]
        },
        {
          "iid": 4,
          "type": "urn:miot-spec-v2:property:firmware-revision:00000005:xiaomi-l7a:1",
          "description": "Current Firmware Version",
          "format": "string",
          "access": [
            "read"
          ]
        }
      ]
    },
    {
      "iid": 2,
      "type": "urn:miot-spec-v2:service:speaker:0000781C:xiaomi-l7a:1",
      "description": "Speaker",
      "properties": [
        {
          "iid": 1,
          "type": "urn:miot-spec-v2:property:volume:00000013:xiaomi-l7a:1",
          "description": "Volume",
          "format": "uint8",
          "access": [
            "read",
            "write",
            "notify"
          ],
          "unit": "percentage",
          "value-range": [
            3,
            100,
            1
          ]
        },
        {
          "iid": 2,
          "type": "urn:miot-spec-v2:property:mute:00000040:xiaomi-l7a:1",
          "description": "Mute",
          "format": "bool",
          "access": [
            "read",
            "write",
            "notify"
          ]
        }
      ]
    },
    {
      "iid": 3,
      "type": "urn:miot-spec-v2:service:microphone:00007826:xiaomi-l7a:1",
      "description": "Microphone",
      "properties": [
        {
          "iid": 1,
          "type": "urn:miot-spec-v2:property:mute:00000040:xiaomi-l7a:1",
          "description": "Mute",
          "format": "bool",
          "access": [
            "read",
            "write",
            "notify"
          ]
        }
      ]
    },
    {
      "iid": 4,
      "type": "urn:miot-spec-v2:service:play-control:0000781D:xiaomi-l7a:1",
      "description": "Play Control",
      "properties": [
        {
          "iid": 1,
          "type": "urn:miot-spec-v2:property:playing-state:00000041:xiaomi-l7a:1",
          "description": "Playing State",
          "format": "uint8",
          "access": [
            "read",
            "notify"
          ],
          "value-list": [
            {
              "value": 0,
              "description": "Idle"
            },
            {
              "value": 1,
              "description": "Playing"
            }
          ]
        }
      ],
      "actions": [
        {
          "iid": 1,
          "type": "urn:miot-spec-v2:action:pause:0000280C:xiaomi-l7a:1",
          "description": "Pause",
          "in": [],
          "out": []
        },
        {
          "iid": 2,
          "type": "urn:miot-spec-v2:action:play:0000280B:xiaomi-l7a:1",
          "description": "Play",
          "in": [],
          "out": []
        },
        {
          "iid": 3,
          "type": "urn:miot-spec-v2:action:next:0000280F:xiaomi-l7a:1",
          "description": "Next",
          "in": [],
          "out": []
        },
        {
          "iid": 4,
          "type": "urn:miot-spec-v2:action:previous:0000280E:xiaomi-l7a:1",
          "description": "Previous",
          "in": [],
          "out": []
        }
      ]
    },
    {
      "iid": 5,
      "type": "urn:miot-spec-v2:service:intelligent-speaker:0000789B:xiaomi-l7a:1",
      "description": "Intelligent Speaker",
      "properties": [
        {
          "iid": 1,
          "type": "urn:miot-spec-v2:property:text-content:000000FA:xiaomi-l7a:1",
          "description": "Text Content",
          "format": "string",
          "access": []
        },
        {
          "iid": 2,
          "type": "urn:miot-spec-v2:property:silent-execution:000000FB:xiaomi-l7a:1",
          "description": "Silent Execution",
          "format": "uint8",
          "access": [],
          "value-list": [
            {
              "value": 0,
              "description": "On"
            },
            {
              "value": 1,
              "description": "Off"
            }
          ]
        }
      ],
      "actions": [
        {
          "iid": 1,
          "type": "urn:miot-spec-v2:action:play-text:00002841:xiaomi-l7a:1",
          "description": "Play Text",
          "in": [
            1
          ],
          "out": []
        },
        {
          "iid": 2,
          "type": "urn:miot-spec-v2:action:wake-up:0000283F:xiaomi-l7a:1",
          "description": "Wake Up",
          "in": [],
          "out": []
        },
        {
          "iid": 3,
          "type": "urn:miot-spec-v2:action:play-radio:00002840:xiaomi-l7a:1",
          "description": "Play Radio",
          "in": [],
          "out": []
        },
        {
          "iid": 4,
          "type": "urn:miot-spec-v2:action:play-music:00002846:xiaomi-l7a:1",
          "description": "Play Music",
          "in": [],
          "out": []
        },
        {
          "iid": 5,
          "type": "urn:miot-spec-v2:action:execute-text-directive:00002842:xiaomi-l7a:1",
          "description": "Execute Text Directive",
          "in": [
            1,
            2
          ],
          "out": []
        }
      ]
    },
    {
      "iid": 6,
      "type": "urn:miot-spec-v2:service:clock:0000789A:xiaomi-l7a:1",
      "description": "Clock",
      "actions": [
        {
          "iid": 1,
          "type": "urn:miot-spec-v2:action:stop-alarm:00002843:xiaomi-l7a:1",
          "description": "Stop Alarm",
          "in": [],
          "out": []
        }
      ]
    }
  ]
}



micli.py 5-4 查询天气 #1返回如下:

{
  "did": "290072327",
  "miid": 0,
  "siid": 5,
  "aiid": 5,
  "code": -704220025,
  "exe_time": 0,
  "net_cost": 0,
  "ot_cost": 0,
  "otlocalts": 0,
  "oa_cost": 0,
  "_oa_rpc_cost": 0
}
micli.py 5-5 查询天气 #1
返回如下:

{
  "did": "290072327",
  "miid": 0,
  "siid": 5,
  "aiid": 5,
  "code": -704220025,
  "exe_time": 0,
  "net_cost": 0,
  "ot_cost": 0,
  "otlocalts": 0,
  "oa_cost": 0,
  "_oa_rpc_cost": 0
}



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-5 22:50 , Processed in 0.078936 second(s), 30 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表