Yocto-ColorLedCluster integration for Home Assistant

Yocto-ColorLedCluster integration for Home Assistant

We've written a number of posts on Home Assistant, but for the most part they've been about retrieving measured values out of our modules. This week we experiment with Home Assistant integrations and how to use the Python library in an integration.





As we said earlier, it's already possible to control Yoctopuce modules via MQTT, but you had to manually forge the commands to send to the REST API.

In this post, we're going to create a custom "Yocto-ColorLedCluster" integration to control the RGB LEDs of Yocto-Color-V2, Yocto-MaxiBuzzer, and Yocto-MaxiKnob modules. The source code for this integration is available on GitHub, but to install it easily on Home Assistant we're going to use HACS.

HACS (Home Assistant Community Store) is a platform that enables Home Assistant users to easily install integrations, themes, and other unofficial widgets created by the community. It's thanks to this platform that we're going to publish our custom integration and make it accessible to everyone without having to tinker with their Home Assistant installation.

By default, HACS is not installed in Home Assistant, so the first step is to install HACS on our Home Assistant installation. There are a number of tutorials and videos that explain in detail how to do this, so we'll skip over this step fairly quickly. Basically, you need to...

  1. Add the HACS Add-on using this link
  2. Start the HACS Add-on
  3. Check that the logs are error-free
  4. Restart Home-Assistant
  5. Optional: Remove HACS from the Add-On list (HACS remains activated)


Once you've completed these steps, you should see a "HACS" menu in the sidebar.

HACS is installed
HACS is installed




Adding our integration repository

Once HACS is up and running, we can install various integrations or widgets listed by default by the HACS developers. For the moment, our extension is not on this list, but it's very easy to add it.

Go to the HACS section in Home Assistant, then click on the three dots at top right to open the menu and choose "Custom repositories". In the window that opens, paste the URL of our integration's GitHub repository (https://github.com/yoctopuce-examples/yocto_colorledcluster) in the dedicated field. Then select "Integration" as the category, click on "Add" and close the window. Once added, searching for "yocto" lists our integration.

Our integration is available in HACS
Our integration is available in HACS



At this point, the extension is accessible via HACS but is not yet installed locally. Therefore, you must select it and click Download. Once the download is complete, you need to restart Home Assistant, but you should normally get a notification offering you to do so automatically.


Using the integration

Once all these steps have been completed, you can use our integration like all official Home Assistant integrations.

Go to Home Assistant Settings in the Device & Service section and add a Yocto-ColorLedCluster integration. The configuration wizard opens and asks for the URL to be used to connect to the Yoctopuce module.

Integration installation wizard
Integration installation wizard



This URL is passed directly to our library's YAPI.RegisterHub method. So you don't necessarily need to specify the protocol and port. In 90% of cases, it's enough to use the IP address of your VirtualHub or YoctoHub on which the Yocto-Color-V2 is installed. If you've set up authentication, you can pass it on using the traditional format:http://name:password@address:port.

Once configured, the integration creates a Home Assistant entity per function per Yoctopuce module that has a YColorLedCluster interface. In our case, we have a Yocto-Color-V2 and a Yocto-MaxiBuzzer.



You can then use these entities like any other Light entity:

You can use our modules in the same way as any other Light entity
You can use our modules in the same way as any other Light entity




Customization

With this integration, our aim was mainly to test how integrations work and to have a reference implementation for customers who would like to use advanced features of our modules in Home Asssitant.

For these reasons, we've kept this integration as simple as possible. But if you want to make your own integration, you can copy ours and adapt it to your needs. The source code is available on GitHub:
https://github.com/yoctopuce-examples/yocto_colorledcluster

As you'll see, it's very easy to use the Yoctopuce library in an integration. Simply add yoctopuce==2.0.XXXX to the requirement list in the "manifest.json" file. This automatically installs our Python library from PyPI.

{
    "domain": "yocto_led",
    "name": "ColorLedCluster",
    "codeowners": [
      "@myself"
    ],
    "config_flow": true,
    "dependencies": [],
    "issue_tracker": "https://github.com/myself/myrepo/issues",
    "documentation": "https://github.com/myself/myrepo",
    "homekit": {},
    "iot_class": "local_polling",
    "requirements": ["yoctopuce==2.0.62875"],
    "version": "1.0.4",
    "ssdp": [],
    "zeroconf": []
}



Then, to use our library in the code, simply include the correct headers, prefixing them with "yoctopuce." as the library is installed from PyPI.

from yoctopuce.yocto_api import *
from yoctopuce.yocto_temperature import *



For the rest of the library, Yoctopuce works as usual.

Conclusion

You can now control RGB LEDs attached to a Yocto-Color-V2, Yocto-MaxiBuzzer, or Yocto-MaxiKnob. This allows you to integrate these modules into your Home Assistant installations and automation.

You also have a reference implementation if you want a more advanced widget.

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.