Control smart light "Yeelight" from Python without using the cloud

yeelight.jpg (The photo is from the product introduction page of SoftBank Selection)

Summary of 3 lines

1.First of all

On April 24, 2020, SoftBank C & S released the smart light "Yeelight" for the domestic market in Japan. This smart light has a function called "LAN Control" (described later), and I've been interested in it for a long time, but since it was released in Japan, I bought it immediately and tried to control it from Python. Of course, the skill is perfect.

2. What is Yeelight?

"Yeelight" (Japanese official page) is a brand name of smart light and is a product of "Xiaomi" (Xiaomi, Xiaomi), which is famous for smartphones and home appliances. There are various products such as light bulb type, tape type, and lantern type, but here we will talk about the light bulb type smart light.

The model "YLDP13YL" purchased this time has Wi-Fi connection, 24-bit color (about 16.77 million colors), smart speaker support (Google Home, Alexa, Apple HomeKit), and features similar to general light bulb type smart lights. However, it has a characteristic function called "LAN Control".

"LAN Control" will be described later, but for other specifications of Yeelight, see Product Introduction Page of SoftBank Selection. Please refer.

In addition to the above SoftBank selection, you can purchase from Amazon.co.jp, Yodobashi.com, etc.

Site model 1 Set of 2 Set of 4
Amazon.co.jp Multicolor 3,300 yen 5,940 yen 11,880 yen
Light bulb color (dimming only) ¥ 2,200 3,960 yen 7,920 yen
Yodobashi.com Multicolor ¥ 3,630 - -

3. What is LAN Control?

As the name suggests, Yeelight's characteristic function "LAN Control" is a function for controlling lights via a LAN (Local Area Network), and its API is open to the public. Since it is completed within the LAN without going through the cloud, control with less delay can be performed. In addition, since the API is open to the public, you can freely control it from your own application.

See Yeelight's Developer Page (https://www.yeelight.com/en_US/developer) for more information. (The page loads very slowly, so please be patient)

4. yeelight package

Without having to read the API specification above, Python has a volunteer yeelight package that you can easily control. At the time of writing this article, it was 0.5.1.

mac$ pip install yeelight==0.5.1

The API is broadly divided into light discovery and control. Let's take a look at each.

4.1. Search

After setting up Yeelight and enabling "LAN Control", you can detect the light with yeelight.discover_bulbs (). Since it uses UDP multicast internally, it must be implemented within the reach of it. (In general, I think it's okay if you are connected to the same Wi-Fi network)

A specific example is shown below.

mac$ python
Python 3.7.0 (default, Jun 28 2018, 07:39:16)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import yeelight
>>> yeelight.discover_bulbs()
[{'ip': '192.168.1.81', 'port': 55443, 'capabilities': {'id': '0x0000000012429e65', 'model': 'color4', 'fw_ver': '27', 'support': 'get_prop set_default set_power toggle set_bright set_scene cron_add cron_get cron_del start_cf stop_cf set_ct_abx adjust_ct set_name set_adjust adjust_bright adjust_color set_rgb set_hsv set_music', 'power': 'on', 'bright': '100', 'color_mode': '2', 'ct': '2000', 'rgb': '9109504', 'hue': '0', 'sat': '100', 'name': ''}}]

If you already know the IP address of the write, this step is not necessary. You can also check the IP address of the light from the Yeelight app.

4.2. Control

If you know the IP address of the write, you can control the write. There is no authentication. Please be careful about the network configuration.

A specific example is shown below. This is an example of lighting ON, OFF, ON / OFF switching, brightness setting, color setting by RGB value, color setting by HSV value, color setting by color temperature, and property acquisition in order from the top.

mac$ python
Python 3.7.0 (default, Jun 28 2018, 07:39:16)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import yeelight
>>> bulb = yeelight.Bulb("192.168.1.81")
>>> bulb.turn_on()
'ok'
>>> bulb.turn_off()
'ok'
>>> bulb.toggle()
'ok'
>>> bulb.set_brightness(50)
'ok'
>>> bulb.set_rgb(255, 0, 0)
'ok'
>>> bulb.set_hsv(320, 100, 50)
'ok'
>>> bulb.set_color_temp(3000)
'ok'
>>> bulb.get_properties()
{'power': 'on', 'bright': '100', 'ct': '3000', 'rgb': '8432127', 'hue': '221', 'sat': '49', 'color_mode': '2', 'flowing': '0', 'delayoff': '0', 'music_on': '0', 'name': None, 'bg_power': None, 'bg_flowing': None, 'bg_ct': None, 'bg_bright': None, 'bg_hue': None, 'bg_sat': None, 'bg_rgb': None, 'nl_br': None, 'active_mode': None, 'current_brightness': '100'}

There are many other functions, so please refer to the API specification for developers and the API reference of the yeelight package.

Then have a good house hack!

Recommended Posts

Control smart light "Yeelight" from Python without using the cloud
Using Cloud Storage from Python3 (Introduction)
Operate the schedule app using python from iphone
Study from the beginning of Python Hour8: Using packages
A little bit from Python using the Jenkins API
Flatten using Python yield from
I tried using the Python library from Ruby with PyCall
Recursively copy files from the directory directly under the directory using Python
View using the python module of Nifty Cloud mobile backend
Instrument control using Python [pyvisa]
Touch NoSQL with Python using the Oracle NoSQL Database Cloud Simulator
Control the motor with a motor driver using python on Raspberry Pi 3!
How to get followers and followers from python using the Mastodon API
Operate Maya from an external Python interpreter using the rpyc module
[Python + heroku] From the state without Python to displaying something on heroku (Part 1)
[Python + heroku] From the state without Python to displaying something on heroku (Part 2)
Try using FireBase Cloud Firestore in Python for the time being
Using Rstan from Python with PypeR
[Python3] Google translate google translate without using api
Notes on using MeCab from Python
Existence from the viewpoint of Python
Study from Python Hour2: Control statements
Slice without using Python, colon (:). a.__getitem__ (slice (3,5)).
Use the Flickr API from Python
Extract the targz file using python
Try using the Python Cmd module
Run Ansible from Python using API
Precautions when using phantomjs from python
Access spreadsheets using OAuth 2.0 from Python
Try using Amazon DynamoDB from Python
I tried to deliver mail from Node.js and Python using the mail delivery service (SendGrid) of IBM Cloud!
Display the result of video analysis using Cloud Video Intelligence API from Colaboratory.
Use python on Raspberry Pi 3 to light the LED with switch control!
Try using the Python web framework Django (1)-From installation to server startup
Detect Japanese characters from images using Google's Cloud Vision API in Python
Search for synonyms from the word list (csv) using Python Japanese WordNet
How to build an application from the cloud using the Django web framework
Run Cloud Dataflow (Python) from App Engine
Try using the Wunderlist API in Python
From Python to using MeCab (and CaboCha)
Try using the Kraken API in Python
Behind the flyer: Using Docker with Python
Download the file from S3 using boto.
Learning notes from the beginning of Python 1
Control the Matrix LED panel from ROS
Tweet using the Twitter API in Python
[Control engineering] Graphing transfer functions using Python
Try using Python with Google Cloud Functions
Working with OpenStack using the Python SDK
Use Google Cloud Vision API from Python
I tried using UnityCloudBuild API from Python
Reboot the router using Python, Selenium, PhantomJS
Launch the Python interpreter from Git bash
From Python 3.4, pip becomes the standard installer! ??
Operate Sakura's cloud object storage from Python
Learning notes from the beginning of Python 2
[Python] Get the main color from the screenshot
python setup.py test the code using multiprocess
The key to deploying Flask apps using Python Buildpack on Cloud Foundry (Diego)
Region extraction method using cellular automaton Try region extraction from the image with growcut (Python)
[IBM Cloud] I tried to access the Db2 on Cloud table from Cloud Funtions (python)