Tuya automation and text sensor support (#1684)

This commit is contained in:
dentra 2021-11-29 22:08:56 +03:00 committed by GitHub
parent 2c6552dcb6
commit ecef9f316c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,35 @@
Tuya Text Sensor
================
.. seo::
:description: Instructions for setting up a Tuya device sensor.
:image: tuya.png
The ``tuya`` text sensor platform creates a sensor from a tuya component
and requires :doc:`/components/tuya` to be configured.
You can create the text sensor as follows:
.. code-block:: yaml
# Create a sensor
text_sensor:
- platform: "tuya"
name: "MyTextSensor"
sensor_datapoint: 18
Configuration variables:
------------------------
- **id** (*Optional*, :ref:`config-id`): Manually specify the ID used for code generation.
- **name** (**Required**, string): The name of the sensor.
- **sensor_datapoint** (**Required**, int): The datapoint id number of the sensor.
- All other options from :ref:`Text Sensor <config-text_sensor>`.
See Also
--------
- :doc:`/components/tuya`
- :doc:`/components/text_sensor/index`
- :apiref:`tuya/text_sensor/tuya_text_sensor.h`
- :ghedit:`Edit`

View File

@ -60,6 +60,70 @@ Configuration variables:
- **ignore_mcu_update_on_datapoints** (*Optional*, list): A list of datapoints to ignore MCU updates for. Useful for certain broken/erratic hardware and debugging.
Automations:
- **on_datapoint_update**: (*Optional*): An automation to perform when a Tuya datapoint update is received. See :ref:`tuya-on_datapoint_update`.
Tuya Automation
---------------
.. _tuya-on_datapoint_update:
``on_datapoint_update``
***********************
This automation will be triggered when a a Tuya datapoint update is received.
A variable ``x`` is passed to the automation for use in lambdas.
The type of ``x`` variable is depending on ``datapoint_type`` configuration variable:
- *raw*: ``x`` is ``std::vector<uint8_t>``
- *string*: ``x`` is ``std::string``
- *bool*: ``x`` is ``bool``
- *int*: ``x`` is ``int``
- *uint*: ``x`` is ``uint32_t``
- *enum*: ``x`` is ``uint8_t``
- *bitmask*: ``x`` is ``uint32_t``
- *any*: ``x`` is :apistruct:`tuya::TuyaDatapoint`
.. code-block:: yaml
tuya:
on_datapoint_update:
- sensor_datapoint: 6
datapoint_type: raw
then:
- lambda: |-
ESP_LOGD("main", "on_datapoint_update %s", hexencode(x).c_str());
id(voltage).publish_state((x[0] << 8 | x[1]) * 0.1);
id(current).publish_state((x[3] << 8 | x[4]) * 0.001);
id(power).publish_state((x[6] << 8 | x[7]) * 0.1);
- sensor_datapoint: 7 # sample dp
datapoint_type: string
then:
- lambda: |-
ESP_LOGD("main", "on_datapoint_update %s", x.c_str());
- sensor_datapoint: 8 # sample dp
datapoint_type: bool
then:
- lambda: |-
ESP_LOGD("main", "on_datapoint_update %s", ONOFF(x));
- sensor_datapoint: 6
datapoint_type: any # this is optional
then:
- lambda: |-
if (x.type == tuya::TuyaDatapointType::RAW) {
ESP_LOGD("main", "on_datapoint_update %s", hexencode(x.value_raw).c_str());
} else {
ESP_LOGD("main", "on_datapoint_update %hhu", x.type);
}
Configuration variables:
- **sensor_datapoint** (*Required*, int): The datapoint id number of the sensor.
- **datapoint_type** (*Required*, string): The datapoint type one of *raw*, *string*, *bool*, *int*, *uint*, *enum*, *bitmask* or *any*.
- See :ref:`Automation <automation>`.
See Also
--------
@ -69,5 +133,6 @@ See Also
- :doc:`/components/climate/tuya`
- :doc:`/components/binary_sensor/tuya`
- :doc:`/components/sensor/tuya`
- :doc:`/components/text_sensor/tuya`
- :apiref:`tuya/tuya.h`
- :ghedit:`Edit`

View File

@ -524,6 +524,7 @@ Text Sensor Components
Template Text Sensor, components/text_sensor/template, description.svg
Custom Text Sensor, components/text_sensor/custom, language-cpp.svg
Nextion Text Sensor, components/text_sensor/nextion, nextion.jpg
Tuya Text Sensor, components/text_sensor/tuya, tuya.png
Climate Components
------------------