Skip to content

Commit cada522

Browse files
authored
Merge pull request #829 from rospogrigio/localtuya_4.0
Localtuya 4.0
2 parents 9189fe9 + b1b6c3e commit cada522

20 files changed

+2014
-744
lines changed

README.md

+66-91
Original file line numberDiff line numberDiff line change
@@ -2,129 +2,95 @@
22

33
A Home Assistant custom Integration for local handling of Tuya-based devices.
44

5-
This custom integration updates device status via push updates instead of polling, so status updates are fast (even when manually operated).
5+
This custom integration updates device status via pushing updates instead of polling, so status updates are fast (even when manually operated).
6+
The integration also supports the Tuya IoT Cloud APIs, for the retrieval of info and of the local_keys of the devices.
7+
8+
9+
**NOTE: The Cloud API account configuration is not mandatory (LocalTuya can work also without it) but is strongly suggested for easy retrieval (and auto-update after re-pairing a device) of local_keys. Cloud API calls are performed only at startup, and when a local_key update is needed.**
10+
611

712
The following Tuya device types are currently supported:
8-
* 1 and multiple gang switches
9-
* Wi-Fi smart plugs (including those with additional USB plugs)
13+
* Switches
1014
* Lights
1115
* Covers
1216
* Fans
13-
* Climates (soon)
17+
* Climates
18+
* Vacuums
1419

1520
Energy monitoring (voltage, current, watts, etc.) is supported for compatible devices.
1621

22+
> **Currently, only Tuya protocols 3.1 and 3.3 are supported (3.4 is not).**
23+
1724
This repository's development began as code from [@NameLessJedi](https://github.com/NameLessJedi), [@mileperhour](https://github.com/mileperhour) and [@TradeFace](https://github.com/TradeFace). Their code was then deeply refactored to provide proper integration with Home Assistant environment, adding config flow and other features. Refer to the "Thanks to" section below.
1825

1926

2027
# Installation:
2128

22-
Copy the localtuya folder and all of its contents into your Home Assistant's custom_components folder. This folder is usually inside your `/config` folder. If you are running Hass.io, use SAMBA to copy the folder over. If you are running Home Assistant Supervised, the custom_components folder might be located at `/usr/share/hassio/homeassistant`. You may need to create the `custom_components` folder and then copy the localtuya folder and all of its contents into it
29+
The easiest way, if you are using [HACS](https://hacs.xyz/), is to install LocalTuya through HACS.
2330

24-
Alternatively, you can install localtuya through [HACS](https://hacs.xyz/) by adding this repository.
31+
For manual installation, copy the localtuya folder and all of its contents into your Home Assistant's custom_components folder. This folder is usually inside your `/config` folder. If you are running Hass.io, use SAMBA to copy the folder over. If you are running Home Assistant Supervised, the custom_components folder might be located at `/usr/share/hassio/homeassistant`. You may need to create the `custom_components` folder and then copy the localtuya folder and all of its contents into it.
2532

2633

2734
# Usage:
2835

29-
**NOTE: You must have your Tuya device's Key and ID in order to use localtuya. There are several ways to obtain the localKey depending on your environment and the devices you own. A good place to start getting info is https://github.com/codetheweb/tuyapi/blob/master/docs/SETUP.md .**
36+
**NOTE: You must have your Tuya device's Key and ID in order to use LocalTuya. The easiest way is to configure the Cloud API account in the integration. If you choose not to do it, there are several ways to obtain the local_keys depending on your environment and the devices you own. A good place to start getting info is https://github.com/codetheweb/tuyapi/blob/master/docs/SETUP.md .**
3037

3138

32-
**NOTE - Nov 2020: If you plan to integrate these devices on a network that has internet and blocking their internet access, you must also block DNS requests (to the local DNS server, e.g. 192.168.1.1). If you only block outbound internet, then the device will sit in a zombie state; it will refuse / not respond to any connections with the localkey. Therefore, you must first connect the devices with an active internet connection, grab each device localkey, and implement the block.**
39+
**NOTE 2: If you plan to integrate these devices on a network that has internet and blocking their internet access, you must also block DNS requests (to the local DNS server, e.g. 192.168.1.1). If you only block outbound internet, then the device will sit in a zombie state; it will refuse / not respond to any connections with the localkey. Therefore, you must first connect the devices with an active internet connection, grab each device localkey, and implement the block.**
3340

34-
Devices can be configured in two ways:
3541

36-
# Option one: YAML config files
42+
# Adding the Integration
3743

38-
Add the proper entry to your configuration.yaml file. Several example configurations for different device types are provided below. Make sure to save when you are finished editing configuration.yaml.
3944

40-
```yaml
41-
localtuya:
42-
- host: 192.168.1.x
43-
device_id: xxxxx
44-
local_key: xxxxx
45-
friendly_name: Tuya Device
46-
protocol_version: "3.3"
47-
scan_interval: # optional, only needed if energy monitoring values are not updating
48-
seconds: 30 # Values less than 10 seconds may cause stability issues
49-
entities:
50-
- platform: binary_sensor
51-
friendly_name: Plug Status
52-
id: 1
53-
device_class: power
54-
state_on: "true" # Optional
55-
state_off: "false" # Optional
56-
57-
- platform: cover
58-
friendly_name: Device Cover
59-
id: 2
60-
open_close_cmds: ["on_off","open_close"] # Optional, default: "on_off"
61-
positioning_mode: ["none","position","timed"] # Optional, default: "none"
62-
currpos_dps: 3 # Optional, required only for "position" mode
63-
setpos_dps: 4 # Optional, required only for "position" mode
64-
span_time: 25 # Full movement time: Optional, required only for "timed" mode
65-
66-
- platform: fan
67-
friendly_name: Device Fan
68-
id: 3 # dps for on/off state
69-
fan_direction: 4 # Optional, dps for fan direction
70-
fan_direction_fwd: forward # String for the forward direction
71-
fan_direction_rev: reverse # String for the reverse direction
72-
fan_ordered_list: low,medium,high,auto # Optional, If this is used it will not use the min and max integers.
73-
fan_oscilating_control: 4 # Optional, dps for fan osciallation
74-
fan_speed_control: 3 # Optional, if ordered list not used, dps for speed control
75-
fan_speed_min: 1 # Optional, if ordered list not used, minimum integer for speed range
76-
fan_speed_max: 10 # Optional, if ordered list not used, maximum integer for speed range
77-
78-
79-
- platform: light
80-
friendly_name: Device Light
81-
id: 4 # Usually 1 or 20
82-
color_mode: 21 # Optional, usually 2 or 21, default: "none"
83-
brightness: 22 # Optional, usually 3 or 22, default: "none"
84-
color_temp: 23 # Optional, usually 4 or 23, default: "none"
85-
color_temp_reverse: false # Optional, default: false
86-
color: 24 # Optional, usually 5 (RGB_HSV) or 24 (HSV), default: "none"
87-
brightness_lower: 29 # Optional, usually 0 or 29, default: 29
88-
brightness_upper: 1000 # Optional, usually 255 or 1000, default: 1000
89-
color_temp_min_kelvin: 2700 # Optional, default: 2700
90-
color_temp_max_kelvin: 6500 # Optional, default: 6500
91-
scene: 25 # Optional, usually 6 (RGB_HSV) or 25 (HSV), default: "none"
92-
music_mode: False # Optional, some use internal mic, others, phone mic. Only internal mic is supported, default: "False"
93-
94-
- platform: sensor
95-
friendly_name: Plug Voltage
96-
id: 20
97-
scaling: 0.1 # Optional
98-
device_class: voltage # Optional
99-
unit_of_measurement: "V" # Optional
100-
101-
- platform: switch
102-
friendly_name: Plug
103-
id: 1
104-
current: 18 # Optional
105-
current_consumption: 19 # Optional
106-
voltage: 20 # Optional
107-
```
45+
**NOTE: starting from v4.0.0, configuration using YAML files is no longer supported. The integration can only be configured using the config flow.**
46+
47+
48+
To start configuring the integration, just press the "+ADD INTEGRATION" button in the Settings - Integrations page, and select LocalTuya from the drop-down menu.
49+
The Cloud API configuration page will appear, requesting to input your Tuya IoT Platform account credentials:
50+
51+
![cloud_setup](https://github.com/rospogrigio/localtuya-homeassistant/blob/master/img/9-cloud_setup.png)
52+
53+
To setup a Tuya IoT Platform account and setup a project in it, refer to the instructions for the official Tuya integration:
54+
https://www.home-assistant.io/integrations/tuya/
55+
The place to find the Client ID and Secret is described in this link (in the ["Get Authorization Key"](https://www.home-assistant.io/integrations/tuya/#get-authorization-key) paragraph), while the User ID can be found in the "Link Tuya App Account" subtab within the Cloud project:
56+
57+
![user_id.png](https://github.com/rospogrigio/localtuya-homeassistant/blob/master/img/8-user_id.png)
58+
59+
> **Note: as stated in the above link, if you already have an account and an IoT project, make sure that it was created after May 25, 2021 (due to changes introduced in the cloud for Tuya 2.0). Otherwise, you need to create a new project. See the following screenshot for where to check your project creation date:**
60+
61+
![project_date](https://github.com/rospogrigio/localtuya-homeassistant/blob/master/img/6-project_date.png)
62+
63+
After pressing the Submit button, the first setup is complete and the Integration will be added.
10864

109-
Note that a single device can contain several different entities. Some examples:
110-
- a cover device might have 1 (or many) cover entities, plus a switch to control backlight
111-
- a multi-gang switch will contain several switch entities, one for each gang controlled
65+
> **Note: it is not mandatory to input the Cloud API credentials: you can choose to tick the "Do not configure a Cloud API account" button, and the Integration will be added anyway.**
11266
113-
Restart Home Assistant when finished editing.
67+
After the Integration has been set up, devices can be added and configured pressing the Configure button in the Integrations page:
11468

115-
# Option two: Using config flow
69+
![integration_configure](https://github.com/rospogrigio/localtuya-homeassistant/blob/master/img/10-integration_configure.png)
11670

117-
Start by going to Configuration - Integration and pressing the "+" button to create a new Integration, then select LocalTuya in the drop-down menu.
118-
Wait for 6 seconds for the scanning of the devices in your LAN. Then, a drop-down menu will appear containing the list of detected devices: you can
119-
select one of these, or manually input all the parameters.
71+
72+
# Integration Configuration menu
73+
74+
The configuration menu is the following:
75+
76+
![config_menu](https://github.com/rospogrigio/localtuya-homeassistant/blob/master/img/11-config_menu.png)
77+
78+
From this menu, you can select the "Reconfigure Cloud API account" to edit your Tuya Cloud credentials and settings, in case they have changed or if the integration was migrated from v.3.x.x versions.
79+
80+
You can then proceed Adding or Editing your Tuya devices.
81+
82+
# Adding/editing a device
83+
84+
If you select to "Add or Edit a device", a drop-down menu will appear containing the list of detected devices (using auto-discovery if adding was selected, or the list of already configured devices if editing was selected): you can select one of these, or manually input all the parameters selecting the "..." option.
12085

12186
> **Note: The tuya app on your device must be closed for the following steps to work reliably.**
12287
88+
12389
![discovery](https://github.com/rospogrigio/localtuya-homeassistant/blob/master/img/1-discovery.png)
12490

125-
If you have selected one entry, you only need to input the device's Friendly Name and the localKey.
91+
If you have selected one entry, you only need to input the device's Friendly Name and localKey. These values will be automatically retrieved if you have configured your Cloud API account, otherwise you will need to input them manually.
12692

127-
Setting the scan interval is optional, only needed if energy/power values are not updating frequently enough by default. Values less than 10 seconds may cause stability issues.
93+
Setting the scan interval is optional, it is only needed if energy/power values are not updating frequently enough by default. Values less than 10 seconds may cause stability issues.
12894

12995
Once you press "Submit", the connection is tested to check that everything works.
13096

@@ -147,15 +113,22 @@ Once you configure the entities, the procedure is complete. You can now associat
147113
![success](https://github.com/rospogrigio/localtuya-homeassistant/blob/master/img/5-success.png)
148114

149115

116+
# Migration from LocalTuya v.3.x.x
117+
118+
If you upgrade LocalTuya from v3.x.x or older, the config entry will automatically be migrated to the new setup. Everything should work as it did before the upgrade, apart from the fact that in the Integration tab you will see just one LocalTuya integration (showing the number of devices and entities configured) instead of several Integrations grouped within the LocalTuya Box. This will happen both if the old configuration was done using YAML files and with the config flow. Once migrated, you can just input your Tuya IoT account credentials to enable the support for the Cloud API (and benefit from the local_key retrieval and auto-update): see [Configuration menu](https://github.com/rospogrigio/localtuya#integration-configuration-menu).
119+
120+
If you had configured LocalTuya using YAML files, you can delete all its references from within the YAML files because they will no longer be considered so they might bring confusion (only the logger configuration part needs to be kept, of course, see [Debugging](https://github.com/rospogrigio/localtuya#debugging) ).
121+
122+
150123
# Energy monitoring values
151124

152125
You can obtain Energy monitoring (voltage, current) in two different ways:
153126

154127
1) Creating individual sensors, each one with the desired name.
155128
Note: Voltage and Consumption usually include the first decimal. You will need to scale the parament by 0.1 to get the correct values.
156-
1) Access the voltage/current/current_consumption attributes of a switch, and define template sensors
129+
2) Access the voltage/current/current_consumption attributes of a switch, and define template sensors
157130
Note: these values are already divided by 10 for Voltage and Consumption
158-
1) On some devices, you may find that the energy values are not updating frequently enough by default. If so, set the scan interval (see above) to an appropriate value. Settings below 10 seconds may cause stability issues, 30 seconds is recommended.
131+
3) On some devices, you may find that the energy values are not updating frequently enough by default. If so, set the scan interval (see above) to an appropriate value. Settings below 10 seconds may cause stability issues, 30 seconds is recommended.
159132

160133
```
161134
sensor:
@@ -197,6 +170,8 @@ logger:
197170
198171
* Everything listed in https://github.com/rospogrigio/localtuya-homeassistant/issues/15
199172
173+
* Support devices that use Tuya protocol v.3.4
174+
200175
# Thanks to:
201176
202177
NameLessJedi https://github.com/NameLessJedi/localtuya-homeassistant and mileperhour https://github.com/mileperhour/localtuya-homeassistant being the major sources of inspiration, and whose code for switches is substantially unchanged.

0 commit comments

Comments
 (0)