Information
We are currently investigating an issue with the editor of some pages. Please save your work and avoid to create new pages until this banner is gone.
To control a smart bulb using ACS we will implement a python component using the Component Generator tool.
First, it can be useful to have in mind the device specifications and properties available through the Tuya API Explorer:
For simplicity we will only use the "switch_led" property which allows us to turn the light on and off, but of course you can expand the functionality to control more properties.
python -m pip install tinytuya
The component generation tool uses a template in YML to create the component's IDL and implementation.
working_dir: ~/workspace prefix: ACSIOT module: acsiot component_name: TuyaBulb functions: - 'void turnOn(in string api_region, in string api_key, in string api_secret, in string device_id)' - 'void turnOff(in string api_region, in string api_key, in string api_secret, in string device_id)'
The "working_dir" is where the component's IDL and implementation directories will be created, the rest is kind of self explanatory .
In short, the component generator tool works as follows:
> python create_acs_component.py --help Usage: create_acs_component.py [OPTIONS] COMPONENT_YAML_PATH Creates an ACS component from a YAML definition. Options: -l, --language [python|cpp|java] Component's implementation language. [default: python] --version Show the version and exit. --help Show this message and exit.
So in our case we will run:
> python create_acs_component.py -l python path/to/tuya_bulb.yml
If everything is ok the generator will create the component's IDL and implementation directories and files. Also it will add the component to the CDB. The output will be something like:
In order to have a working component in valid python we will have to modify the boilerplate code as follows:
# Client stubs and definitions, such as structs, enums, etc. import acsiot # Skeleton infrastructure for server implementation import acsiot__POA # Base component implementation from Acspy.Servants.ACSComponent import ACSComponent # Services provided by the container to the component from Acspy.Servants.ContainerServices import ContainerServices # Basic component lifecycle (initialize, execute, cleanUp and aboutToAbort methods) from Acspy.Servants.ComponentLifecycle import ComponentLifecycle import tinytuya tinytuya.set_debug(True) class TuyaBulbImpl(acsiot__POA.TuyaBulb, ACSComponent, ContainerServices, ComponentLifecycle): def __init__(self): ACSComponent.__init__(self) ContainerServices.__init__(self) self._logger = self.getLogger() def turnOn(self, api_region, api_key, api_secret, device_id): # raise NotImplementedError("This function should do something") client = tinytuya.Cloud( apiRegion=api_region, apiKey=api_key, apiSecret=api_secret ) commands = { 'commands': [{ 'code': 'switch_led', 'value': True }] } print("Sending command...") result = client.sendcommand(device_id, commands) print("Results\n:", result) def turnOff(self, api_region, api_key, api_secret, device_id): # raise NotImplementedError("This function should do something") client = tinytuya.Cloud( apiRegion=api_region, apiKey=api_key, apiSecret=api_secret ) commands = { 'commands': [{ 'code': 'switch_led', 'value': False }] } print("Sending command...") result = client.sendcommand(device_id, commands) print("Results\n:", result)
cd idlTuyaBulb/src make all make install
cd pyTuyaBulb/src make all install
In one terminal run:
acsStop acsStart
In a second terminal run:
acsStartContainer -py aragornContainer
And in a third terminal run:
from Acspy.Clients.SimpleClient import PySimpleClient api_region = 'my_region' api_key = 'my_api_key' api_secret = 'my_api_secret' device_id = 'my_device_id' client = PySimpleClient() bulb = client.getComponent("TuyaBulbPython") bulb.turnOn(api_region, api_key, api_secret, device_id) bulb.turnOff(api_region, api_key, api_secret, device_id)