From ecef9f316c5ccda0dcdfde91d18f8985f3054a19 Mon Sep 17 00:00:00 2001 From: dentra Date: Mon, 29 Nov 2021 22:08:56 +0300 Subject: [PATCH] Tuya automation and text sensor support (#1684) --- components/text_sensor/tuya.rst | 35 ++++++++++++++++++ components/tuya.rst | 65 +++++++++++++++++++++++++++++++++ index.rst | 1 + 3 files changed, 101 insertions(+) create mode 100644 components/text_sensor/tuya.rst diff --git a/components/text_sensor/tuya.rst b/components/text_sensor/tuya.rst new file mode 100644 index 000000000..49c1a32ad --- /dev/null +++ b/components/text_sensor/tuya.rst @@ -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 `. + +See Also +-------- + +- :doc:`/components/tuya` +- :doc:`/components/text_sensor/index` +- :apiref:`tuya/text_sensor/tuya_text_sensor.h` +- :ghedit:`Edit` diff --git a/components/tuya.rst b/components/tuya.rst index 1ea533f78..b14446926 100644 --- a/components/tuya.rst +++ b/components/tuya.rst @@ -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`` +- *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 `. + + 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` diff --git a/index.rst b/index.rst index 9f5fb9b61..6b4cd2ec8 100644 --- a/index.rst +++ b/index.rst @@ -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 ------------------