26 Star 86 Fork 19

Yeelight / miot-api

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 5.69 KB
一键复制 编辑 原始数据 按行查看 历史
Sheldon.li 提交于 2019-04-01 14:12 . Update README.md

StyleCI Build Status Build Status Build Status Total Downloads Latest Stable Version License 996.icu

小米IOT控制端API及小米IOT设备规范 SDK

安装

composer require yeelight/miot-api

使用

$appId = 'Your App-Id';
// 用户oauth取得的accessToken
$accessToken = 'user access token';
$api = new Api($appId, $accessToken);

// 默认为大陆host,如果要获取其他地区设备的时候,可以setHost设置host
$api->setHost('api.home.mi.com');

// 一次性获取到包含了 serialNumber (原did)的设备列表
$devices = $api->devicesList();

// 根据 piid 获取 属性值 
$properties = $api->properties([
    'M1GAxtaW9A0LXNwZWMtdjIVgoAFGA55ZWVsaW5rLWNvbG9AyMRUUGAg0NTk4OTg3NRVoAA.2.1',
    'M1GAxtaW9A0LXNwZWMtdjIVgoAFGA55ZWVsaW5rLWNvbG9AyMRUUGAg0NTk4OTg3NRVoAA.2.2',
]);
        
$properties = [
    'properties' => [
        [
            "pid" => "M1GAxtaW9A0LXNwZWMtdjIVgoAFGA15ZWVsaW5rLW1vbm8xFRQYCDEzMTgwNzc2FWYA.2.2",
            "value" => 75
        ]
    ]
];

// 设置属性
$api->setProperties($properties);


// 读取用户在米家设置好的场景列表
$secenes = $api->scenes();

// 主动触发场景
$scene_id = '1031976223';

$api->triggerScene($scene_id);

// 读取家庭列表
$homes = $api->homes();

// 订阅属性变化
$properties = [
    'M1GAxtaW9A0LXNwZWMtdjIVgoAFGA15ZWVsaW5rLW1vbm8xFRQYCDEzMTgwNzc2FWYA.2.2',
    'M1GAxtaW9A0LXNwZWMtdjIVgoAFGA55ZWVsaW5rLWNvbG9AyMRUUGAg0NTk4OTg3NRVoAA.2.1',
];
$customData = [
    'test' => 'test'
];
$receiverUrl = 'https://www.xxx.com/receiver';
$api->subscript($properties, $customData, $receiverUrl);

// 退订属性变化
$api->unSubscript($properties);

// 优雅的设置单个设备属性
$did = 'M1GAxtaW9A0LXNwZWMtdjIVgoAFGA55ZWVsaW5rLWNvbG9AyMRUUGAg0NTk2NTYwNRVoAA';
$type = "urn:miot-spec-v2:device:light:0000A001:yeelink-color1:1";
$data = [
    'on' => true,
    'brightness' => 99,
    'color-temperature' => 2100,
    'color' => 5777215
];
$requestInfo = $api->setPropertyGraceful($did, $type, $data);

// 优雅的设置多个设备属性
$data = [
    'M1GAxtaW9A0LXNwZWMtdjIVgoAFGA55ZWVsaW5rLWNvbG9AyMRUUGAg0NTk2NTYwNRVoAA' => [
        'type' => 'urn:miot-spec-v2:device:light:0000A001:yeelink-color1:1',
        'data' => [
            'on' => true,
            'brightness' => 99,
            'color-temperature' => 2100,
            'color' => 2777215
        ]
    ],
    'M1GAxtaW9A0LXNwZWMtdjIVgoAFGAt5ZWVsaW5rLWN0MhUUGAg4NzEzMDQyMhWcCAA' => [
        'type' => 'urn:miot-spec-v2:device:light:0000A001:yeelink-ct2:1',
        'data' => [
            'on' => true,
            'brightness' => 50,
            'color-temperature' => 3500
        ]
    ]
];
$requestInfo = $api->setPropertiesGraceful($data);

// 优雅的获取单个设备属性
$did = 'M1GAxtaW9A0LXNwZWMtdjIVgoAFGA55ZWVsaW5rLWNvbG9AyMRUUGAg0NTk2NTYwNRVoAA';
$type = "urn:miot-spec-v2:device:light:0000A001:yeelink-color1:1";
// data为空数组时,获取所有可读属性
$data = [
    'on',
    'brightness',
    'color-temperature',
    'color'
];
$attibutes = $api->getPropertyGraceful($did, $type, $data);

// 优雅的获取多个设备属性
$data = [
    'M1GAxtaW9A0LXNwZWMtdjIVgoAFGA55ZWVsaW5rLWNvbG9AyMRUUGAg0NTk2NTYwNRVoAA' => [
        'type' => 'urn:miot-spec-v2:device:light:0000A001:yeelink-color1:1',
        'data' => [
            'on',
            'brightness',
            'color-temperature',
            'color'
        ]
    ],
    'M1GAxtaW9A0LXNwZWMtdjIVgoAFGAt5ZWVsaW5rLWN0MhUUGAg4NzEzMDQyMhWcCAA' => [
        'type' => 'urn:miot-spec-v2:device:light:0000A001:yeelink-ct2:1',
        'data' => [] // 为空时,获取所有可读属性
    ]
];
$attibutes = $api->getPropertiesGraceful($data);

// 订阅设备的所有可订阅属性
$devices = $api->devicesList();
$customData = [
    'test' => 'test'
];
$receiverUrl = 'https://www.xxx.com/receiver';
$requestInfo = $api->subscriptByDevices($devices, $customData, $receiverUrl);

// 退订设备的所有订阅属性
$devices = $this->api->devicesList();
$requestInfo = $this->api->unSubscriptByDevices($devices);

参考资源

Security Vulnerabilities

If you discover a security vulnerability within this library, please send an e-mail to Sheldon Lee via lixiaodong@yeelight.com. All security vulnerabilities will be promptly addressed.

License

This library is open-sourced software licensed under the MIT license.

PHP
1
https://gitee.com/yeelight/miot-api.git
git@gitee.com:yeelight/miot-api.git
yeelight
miot-api
miot-api
master

搜索帮助