Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge upstream #20

Open
wants to merge 157 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
157 commits
Select commit Hold shift + click to select a range
50d7454
Components to integrations
wrt54g Jun 5, 2022
b5621b4
Merge branch 'master' into url
rospogrigio Jun 13, 2022
f795845
Merge branch 'master' into url
wrt54g Jul 3, 2022
f06b8cc
Added DPS value type configuration
deese Jul 7, 2022
364569b
Rebase from upstream
sibowler Jul 13, 2022
0fe5505
Fix NumberEntity HA changes and add step control
sibowler Jul 13, 2022
06968d8
Rebase merge fix. Earlier commit also included changes for manually s…
sibowler Jul 13, 2022
f85b25d
Fixing formatting/style errors
sibowler Jul 13, 2022
41b5be0
Adding notes to README
sibowler Jul 14, 2022
1c9a1e4
Spelling correction
sibowler Jul 14, 2022
489a6f0
Updating documentation
sibowler Jul 14, 2022
ad1f757
Implementing RESET Tuya function to enable devices which are stuck in…
sibowler Jul 19, 2022
8a696cb
Add "1/0" entry for HVAC_MODE_SETS
Jun 28, 2022
84e3af9
Merge branch 'master' into master
deese Aug 11, 2022
fdc9862
Update requirements_test.txt
rospogrigio Aug 16, 2022
205a844
Reverting requirements.txt
rospogrigio Aug 16, 2022
43c01ee
Refactor number entity overrides
markvader Jul 7, 2022
9d1f46d
Merge branch 'master' into master
rospogrigio Aug 30, 2022
2bae03c
Release 4.0.2
rospogrigio Aug 30, 2022
6a489c6
Merge branch 'master' into master
rospogrigio Aug 30, 2022
cf571c0
Merge branch 'master' into restore_state
sibowler Aug 31, 2022
8de987b
Fix for cover defect
sibowler Sep 1, 2022
8c48943
Fix for cover issues with new state restore functionality
sibowler Sep 4, 2022
19a0cb6
Fixing slight format error
sibowler Sep 4, 2022
7142304
Merge pull request #956 from sibowler/restore_state
rospogrigio Sep 5, 2022
21e6aad
Edited manifest.json to 4.1.0
rospogrigio Sep 5, 2022
fa55cd6
Ported README.md updates to info.md
rospogrigio Sep 5, 2022
46e5573
Removed trailing space
deese Sep 5, 2022
b108130
Merge branch 'master' into master
deese Sep 5, 2022
21f7af6
Implementing safe default for devices not requiring RESET
sibowler Sep 7, 2022
1db48e5
Updating readme information to reflect the change.
sibowler Sep 7, 2022
f7ce9be
Changes to make Passive DPS entites a configurable item
sibowler Sep 9, 2022
28cbfdb
Updating documentation
sibowler Sep 9, 2022
85a1ae0
Lint fix
deese Sep 29, 2022
64dcb15
Merge pull request #942 from deese/master
rospogrigio Sep 29, 2022
6500f58
Merge branch 'master' into fix_restart_behaviour
rospogrigio Oct 4, 2022
154b960
Merge pull request #1022 from sibowler/fix_restart_behaviour
rospogrigio Oct 11, 2022
ee25098
Update manifest.json
rospogrigio Oct 11, 2022
79856bb
Merge branch 'master' into url
rospogrigio Oct 12, 2022
fb0bdc5
Merge pull request #849 from wrt54g/url
rospogrigio Oct 12, 2022
8c597bb
Fix for AttributeError: 'TuyaMessage' object has no attribute 'releas…
sibowler Oct 16, 2022
831ae55
#711 Implemented m/p HVAC mode set
Daandeve Jan 24, 2022
a2e04b4
Merge pull request #712 from Daandeve/master
rospogrigio Nov 14, 2022
494d334
Allow calling localtuya.set_dp service by non-admin users
ov1d1u Nov 20, 2022
4a5bf83
Introduced pytuya with support for 3.4 protocol
rospogrigio Jan 7, 2023
5fbf5b3
Introduced 3.4 protocol option in config flow
rospogrigio Jan 7, 2023
24853b5
Fixed HEARTBEAT command
rospogrigio Jan 7, 2023
9d94f16
Added debugging
rospogrigio Jan 7, 2023
9efbcce
Fixed requested DPs for type_0d devices
rospogrigio Jan 8, 2023
f23a945
Fixed negotiation and sequence numbers for protocol 3.4
rospogrigio Jan 9, 2023
ecf66f7
Fixed sequence numbering on status update
rospogrigio Jan 9, 2023
0865aa6
Merge branch 'rospogrigio:master' into fix_semaphore_release_error
sibowler Jan 9, 2023
966c3ec
Fixed tox issues
rospogrigio Jan 9, 2023
f990e84
Fixed tox issues
rospogrigio Jan 9, 2023
33c92dc
Updated README.md and info.md
rospogrigio Jan 9, 2023
9c6daec
Merge branch 'protocol_3.4' of github.com:rospogrigio/localtuya into …
rospogrigio Jan 9, 2023
cdcfd1d
Fixed tox issues
rospogrigio Jan 9, 2023
d8acf60
Fix for version not being set yet when calling _setup_dispatcher
rospogrigio Jan 10, 2023
33e0033
Introduced the possibility to select which devices should have the py…
rospogrigio Jan 10, 2023
bc89105
Introduced device debugging option in README.md and info.md
rospogrigio Jan 10, 2023
957cf25
Introduced pytuya with support for 3.4 protocol
rospogrigio Jan 7, 2023
c6a9460
Introduced 3.4 protocol option in config flow
rospogrigio Jan 7, 2023
0fb6199
Fixed HEARTBEAT command
rospogrigio Jan 7, 2023
a3914cc
Added debugging
rospogrigio Jan 7, 2023
960f07b
Fixed requested DPs for type_0d devices
rospogrigio Jan 8, 2023
edc6752
Fixed negotiation and sequence numbers for protocol 3.4
rospogrigio Jan 9, 2023
c9d6bc5
Fixed sequence numbering on status update
rospogrigio Jan 9, 2023
3bf69d6
Fixed tox issues
rospogrigio Jan 9, 2023
39ef76b
Fixed tox issues
rospogrigio Jan 9, 2023
95b3113
Updated README.md and info.md
rospogrigio Jan 9, 2023
2f0f507
Fixed tox issues
rospogrigio Jan 9, 2023
0f3160f
Fix for version not being set yet when calling _setup_dispatcher
rospogrigio Jan 10, 2023
c379645
Introduced abort in config flow if all entities are deselected when e…
rospogrigio Jan 10, 2023
20ab401
Introduced IT and PT translations
rospogrigio Jan 10, 2023
1e3b047
Merge branch 'master' into device_debugging_option
rospogrigio Jan 10, 2023
1f85551
Merge pull request #1227 from rospogrigio/device_debugging_option
rospogrigio Jan 10, 2023
828000b
Update manifest.json for 5.0.0
rospogrigio Jan 10, 2023
f4164cd
New tuyadebug.tgz now supporting protocol 3.4
rospogrigio Jan 10, 2023
b9c0e5e
Update bug_report.md
rospogrigio Jan 12, 2023
c15fcaa
Fix for unknown state of covers/shutters
rospogrigio Jan 12, 2023
98cc44f
New tuyadebug.tgz now including a tinytuya test
rospogrigio Jan 12, 2023
6024604
Merge branch 'rospogrigio:master' into master
ov1d1u Jan 15, 2023
da6e929
Merge branch 'master' into fix_semaphore_release_error
sibowler Jan 23, 2023
4d3a6cc
Force to int
CloCkWeRX Jan 23, 2023
dccb328
Adjust input to avoid putting a string into an int
CloCkWeRX Jan 23, 2023
8ff194e
Adjust input to avoid putting a string into an int
CloCkWeRX Jan 23, 2023
77ac5a8
Swap to int
CloCkWeRX Jan 23, 2023
613bde4
Improved logging
rospogrigio Jan 24, 2023
7a97fdf
Improved stability and Fix local_key update to not be thwarted by ret…
rospogrigio Jan 24, 2023
adab66d
Merge branch 'sibowler-fix_semaphore_release_error' into stability_fixes
rospogrigio Jan 24, 2023
86eabbd
Merge branch 'sibowler-fix_semaphore_release_error' into stability_fixes
rospogrigio Jan 24, 2023
6371c97
Merge branch 'CloCkWeRX-fix-casting-issue' into stability_fixes
rospogrigio Jan 29, 2023
cd97287
Merge branch 'stability_fixes' of github.com:rospogrigio/localtuya-ho…
rospogrigio Jan 29, 2023
cf3221d
Introduced update of local_key when editing a device
rospogrigio Feb 6, 2023
06d54f9
Merge branch 'stability_fixes' of github.com:rospogrigio/localtuya in…
rospogrigio Feb 6, 2023
a47e7f7
Merge branch 'stability_fixes' of github.com:rospogrigio/localtuya in…
rospogrigio Feb 6, 2023
c909349
Improved logging
rospogrigio Feb 6, 2023
999d50e
Merge branch 'stability_fixes' of github.com:rospogrigio/localtuya-ho…
rospogrigio Feb 6, 2023
6d0e407
Tox fixes
rospogrigio Feb 7, 2023
febbf42
Tox fixes
rospogrigio Feb 7, 2023
f51d833
Tox fixes
rospogrigio Feb 7, 2023
fa301d1
Fix for not reconnecting after 'giving up'
rospogrigio Feb 7, 2023
d4af0f6
Tox fixes
rospogrigio Feb 7, 2023
6b9f3df
Tox fixes, reduced to 3.9 only
rospogrigio Feb 7, 2023
904f4fe
Add entities during 'Edit a device'
jlikeme Mar 16, 2023
7cb065b
revert the default region
jlikeme Mar 16, 2023
1691c5f
Fix depreciated async_get_registry
May 20, 2023
55338e2
Merge pull request #1 from oven-lab/oven-lab-Fix-depreciated-async_ge…
May 20, 2023
d241f03
Add HACS validation
May 20, 2023
e79520e
Remove extra key
May 20, 2023
861dcc5
Update manifest.json
May 20, 2023
de88ee5
Remove extra key
May 20, 2023
651dc8b
Update hacs.json
May 20, 2023
a47cdc4
Add Hassfest validation
May 20, 2023
2dbff47
Alphabetize manifest.json
May 20, 2023
7329aef
Delete combined.yaml
May 20, 2023
f2134b8
Update tox.yaml
May 20, 2023
8af1836
Update tox.ini
May 20, 2023
a987d35
Merge pull request #1400 from oven-lab/master
rospogrigio May 24, 2023
9df62b7
Merge branch 'master' into feat-add-entities
rospogrigio May 24, 2023
9d92cad
Reduced logging level for 'Command %d timed out waiting for sequence …
rospogrigio May 24, 2023
928f8e9
Merge branch 'master' into feat-add-entities
rospogrigio May 24, 2023
5f91417
Fixed tox errors
rospogrigio May 24, 2023
53e1ad8
Updated manifest.json
rospogrigio May 24, 2023
9d5984e
Fix for socket.send() exceptions in the logs
rospogrigio Jun 7, 2023
ba5670f
Fixing pylint error
rospogrigio Jun 7, 2023
f06e484
Merge pull request #1424 from rospogrigio/fix_socket.send_exceptions
rospogrigio Jun 7, 2023
2b3ade0
Merge branch 'rospogrigio:master' into master
ov1d1u Jul 17, 2023
62011a9
Fixed initialization with API but no internet.
Nealium Jul 26, 2023
b3afa14
fix bug when climate entity is using default min/max temp and temp un…
ncd7 Aug 18, 2023
2510542
Handle 3.4 protocol, fix CI (#1251)
dulyts Sep 12, 2023
fa8217c
Merge branch 'master' into offline_init
rospogrigio Sep 12, 2023
23b0cfd
Allow port reuse so that other integrations can read the same port (#…
CodeFoodPixels Sep 12, 2023
137ff78
Merge branch 'master' into offline_init
rospogrigio Sep 12, 2023
69774f5
Merge pull request #1465 from Nealium/offline_init
rospogrigio Sep 12, 2023
0c8fd9d
Fixing tox issues
rospogrigio Sep 15, 2023
bf8f2bb
Merge branch 'master' into master
rospogrigio Sep 16, 2023
180b664
Merge pull request #1135 from ov1d1u/master
rospogrigio Sep 22, 2023
1c173d9
Merge branch 'master' into climate-minmax-default-temp-when-in-F-fix
rospogrigio Sep 22, 2023
e5009fd
Merge pull request #1476 from ncd7/climate-minmax-default-temp-when-i…
rospogrigio Sep 22, 2023
9c0e733
Replaced SUPPORT_* with enums, set minimum HA version in HACS configu…
elad-bar Jan 5, 2024
490ad9e
Merge pull request #1613 from elad-bar/master
rospogrigio Jan 22, 2024
7b37f07
Add support for more ac/fan modes (#1389)
simontegelid Apr 15, 2024
d2fa4dc
added dict get() instead of assuming keys exist; now handles config_f…
rsnodgrass Oct 10, 2022
87d61ee
at init: Replace deprecated async entry setup call with suggestion.
estebanz01 Jul 5, 2024
1da7b31
Add discovered HVAC and PRESET options (#1177)
apbarratt Sep 13, 2024
e1fb0f2
fix: do not use deprecated features
Dim145 Jan 4, 2025
d57932e
fix: do not use deprecated attr in light entity
Dim145 Jan 4, 2025
42b6eb1
refactor: define MODE_MANUAL
Dim145 Jan 4, 2025
b01861d
fix: add FanEntityFeature.TURN_OFF/ON to fans
Dim145 Jan 4, 2025
65f3daf
fix: add color_mode property & adapt color mode method (is_xxx_mode)
Dim145 Jan 4, 2025
096568b
fix: remove useless test line in status_updated of light
Dim145 Jan 4, 2025
1369245
fix: climate deprecated attributes
Dim145 Jan 4, 2025
6202c74
add a subset of HVAC_MODE_SETS (#1875)
ialexyi Jan 14, 2025
12d40f8
fix(common.py) - Change loglevel for missing device configuration fro…
0Styless Jan 14, 2025
01a6f0f
Update README.md (#1808)
Handrail9 Jan 14, 2025
5f2c027
Update manifest.json
rospogrigio Jan 14, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ assignees: ''
Provide details about your environment.
-->
- Localtuya version: <!-- plugin version from HACS, master, commit id -->
- Last working localtuya version (if known and relevant):
- Home Assistant Core version: <!-- Configuration => Info -->
- [] Are you using the Home Assistant Tuya Cloud component ? <!-- if yes, put a x between the two [] => [x] -->
- [] Are you using the Tuya App in parallel ? <!-- if yes, put a x between the two [] => [x] -->
- [] Does the device work using the Home Assistant Tuya Cloud component ? <!-- if yes, put a x between the two [] => [x] -->
- [] Does the device work using the Tinytuya (https://github.com/jasonacox/tinytuya) command line tool ? <!-- if yes, put a x between the two [] => [x] -->
- [] Was the device working with earlier versions of localtuya ? Which one? <!-- if yes, put a x between the two [] => [x] -->
- [] Are you using the Tuya/SmartLife App in parallel ? <!-- if yes, put a x between the two [] => [x] -->

## Steps to reproduce
<!--
Expand All @@ -37,32 +38,27 @@ assignees: ''
2.
3.

## Configuration `configuration.yaml` or `config_flow`
<!--
Fill this with the yaml or config_flow configuration of the failing device. Even if it seems unimportant to you.
Remove personal information and local key.
-->
```yaml

```

## DP dump
<!--
Paste here a DP dump, see https://github.com/rospogrigio/localtuya/wiki/HOWTO-get-a-DPs-dump
You can also try to qualify your device using the procedure described https://github.com/rospogrigio/localtuya/wiki/Qualifying-a-device
-->

## Provide Home Assistant taceback/logs
## Provide Home Assistant traceback/logs
<!--
Provide traces if they are relevant. If the problem is reproducible, try to set the log level to debug for this component at least:
In Dev Tools => Services:
Provide logs if they are relevant. In detail, it is useful to be able to compare working with non-working situations, such as HA logs compared to the output of the tuyadebug script or the tinytuya CLI tool.
To increase the debugging level of HA for the devices, check the "enable debug" button when configuring the device, and set the log level to debug for this component at least:
In configuration.yaml:

logger.set_level
custom_components.localtuya: debug

custom_components.localtuya.pytuya: debug
-->
```txt

```
put your log output between these markers
```


## Additional information
<!-- Put here any information that you think it may be relevant -->
26 changes: 0 additions & 26 deletions .github/workflows/combined.yaml

This file was deleted.

7 changes: 4 additions & 3 deletions .github/workflows/tox.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Tox PR CI

on: [pull_request]
on:
pull_request:
workflow_dispatch:

jobs:
build:
Expand All @@ -14,8 +16,7 @@ jobs:
platform:
- ubuntu-latest
python-version:
- 3.7
- 3.8
- 3.9
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: HACS Validate

on:
push:
pull_request:
schedule:
- cron: "0 0 * * *"
workflow_dispatch:

jobs:
validate-hacs:
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v3"
- name: HACS validation
uses: "hacs/action@main"
with:
category: "integration"
- name: Hassfest validation
uses: home-assistant/actions/hassfest@master
52 changes: 44 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The following Tuya device types are currently supported:

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

> **Currently, only Tuya protocols 3.1 and 3.3 are supported (3.4 is not).**
> **Currently, Tuya protocols from 3.1 to 3.4 are supported.**

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.

Expand Down Expand Up @@ -52,7 +52,7 @@ The Cloud API configuration page will appear, requesting to input your Tuya IoT

To setup a Tuya IoT Platform account and setup a project in it, refer to the instructions for the official Tuya integration:
https://www.home-assistant.io/integrations/tuya/
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:
The Client ID and Secret can be found at `Cloud > Development > Overview` and the User ID can be found in the "Link Tuya App Account" subtab within the Cloud project:

![user_id.png](https://github.com/rospogrigio/localtuya-homeassistant/blob/master/img/8-user_id.png)

Expand Down Expand Up @@ -92,9 +92,13 @@ If you have selected one entry, you only need to input the device's Friendly Nam

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.

Setting the 'Manual DPS To Add' is optional, it is only needed if the device doesn't advertise the DPS correctly until the entity has been properly initiailised. This setting can often be avoided by first connecting/initialising the device with the Tuya App, then closing the app and then adding the device in the integration. **Note: Any DPS added using this option will have a -1 value during setup.**

Setting the 'DPIDs to send in RESET command' is optional. It is used when a device doesn't respond to any Tuya commands after a power cycle, but can be connected to (zombie state). This scenario mostly occurs when the device is blocked from accessing the internet. The DPids will vary between devices, but typically "18,19,20" is used. If the wrong entries are added here, then the device may not come out of the zombie state. Typically only sensor DPIDs entered here.

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

![image](https://user-images.githubusercontent.com/1082213/146664103-ac40319e-f934-4933-90cf-2beaff1e6bac.png)
![image](https://github.com/rospogrigio/localtuya-homeassistant/blob/master/img/2-device.png)


Then, it's time to add the entities: this step will take place several times. First, select the entity type from the drop-down menu to set it up.
Expand All @@ -103,8 +107,11 @@ After you have defined all the needed entities, leave the "Do not add more entit
![entity_type](https://github.com/rospogrigio/localtuya-homeassistant/blob/master/img/3-entity_type.png)

For each entity, the associated DP has to be selected. All the options requiring to select a DP will provide a drop-down menu showing
all the available DPs found on the device (with their current status!!) for easy identification. Each entity type has different options
to be configured. Here is an example for the "switch" entity:
all the available DPs found on the device (with their current status!!) for easy identification.

**Note: If your device requires an LocalTuya to send an initialisation value to the entity for it to work, this can be configured (in supported entities) through the 'Passive entity' option. Optionally you can specify the initialisation value to be sent**

Each entity type has different options to be configured. Here is an example for the "switch" entity:

![entity](https://github.com/rospogrigio/localtuya-homeassistant/blob/master/img/4-entity.png)

Expand Down Expand Up @@ -148,6 +155,32 @@ You can obtain Energy monitoring (voltage, current) in two different ways:
unit_of_measurement: 'W'
```

# Climates

There are a multitude of Tuya based climates out there, both heaters,
thermostats and ACs. The all seems to be integrated in different ways and it's
hard to find a common DP mapping. Below are a table of DP to product mapping
which are currently seen working. Use it as a guide for your own mapping and
please contribute to the list if you have the possibility.

| DP | Moes BHT 002 | Qlima WMS S + SC52 (AB;AF) | Avatto |
|-----|---------------------------------------------------------|---------------------------------------------------------|--------------------------------------------|
| 1 | ID: On/Off<br>{true, false} | ID: On/Off<br>{true, false} | ID: On/Off<br>{true, false} |
| 2 | Target temperature<br>Integer, scaling: 0.5 | Target temperature<br>Integer, scaling 1 | Target temperature<br>Integer, scaling 1 |
| 3 | Current temperature<br>Integer, scaling: 0.5 | Current temperature<br>Integer, scaling: 1 | Current temperature<br>Integer, scaling: 1 |
| 4 | Mode<br>{0, 1} | Mode<br>{"hot", "wind", "wet", "cold", "auto"} | ? |
| 5 | Eco mode<br>? | Fan mode<br>{"strong", "high", "middle", "low", "auto"} | ? |
| 15 | Not supported | Supported, unknown<br>{true, false} | ? |
| 19 | Not supported | Temperature unit<br>{"c", "f"} | ? |
| 23 | Not supported | Supported, unknown<br>Integer, eg. 68 | ? |
| 24 | Not supported | Supported, unknown<br>Integer, eg. 64 | ? |
| 101 | Not supported | Outdoor temperature<br>Integer. Scaling: 1 | ? |
| 102 | Temperature of external sensor<br>Integer, scaling: 0.5 | Supported, unknown<br>Integer, eg. 34 | ? |
| 104 | Supported, unknown<br>{true, false(?)} | Not supported | ? |

[Moes BHT 002](https://community.home-assistant.io/t/moes-bht-002-thermostat-local-control-tuya-based/151953/47)
[Avatto thermostat](https://pl.aliexpress.com/item/1005001605377377.html?gatewayAdapt=glo2pol)

# Debugging

Whenever you write a bug report, it helps tremendously if you include debug logs directly (otherwise we will just ask for them and it will take longer). So please enable debug logs like this and include them in your issue:
Expand All @@ -157,21 +190,22 @@ logger:
default: warning
logs:
custom_components.localtuya: debug
custom_components.localtuya.pytuya: debug
```

Then, edit the device that is showing problems and check the "Enable debugging for this device" button.

# Notes:

* Do not declare anything as "tuya", such as by initiating a "switch.tuya". Using "tuya" launches Home Assistant's built-in, cloud-based Tuya integration in lieu of localtuya.

# To-do list:

* Create a (good and precise) sensor (counter) for Energy (kWh) -not just Power, but based on it-.
Ideas: Use: https://www.home-assistant.io/components/integration/ and https://www.home-assistant.io/components/utility_meter/
Ideas: Use: https://www.home-assistant.io/integrations/integration/ and https://www.home-assistant.io/integrations/utility_meter/

* Everything listed in https://github.com/rospogrigio/localtuya-homeassistant/issues/15

* Support devices that use Tuya protocol v.3.4

# Thanks to:

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.
Expand All @@ -180,6 +214,8 @@ TradeFace, for being the only one to provide the correct code for communication

sean6541, for the working (standard) Python Handler for Tuya devices.

jasonacox, for the TinyTuya project from where I could import the code to communicate with devices using protocol 3.4.

postlund, for the ideas, for coding 95% of the refactoring and boosting the quality of this repo to levels hard to imagine (by me, at least) and teaching me A LOT of how things work in Home Assistant.

<a href="https://www.buymeacoffee.com/rospogrigio" target="_blank"><img src="https://bmc-cdn.nyc3.digitaloceanspaces.com/BMC-button-images/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: auto !important;width: auto !important;" ></a>
Expand Down
31 changes: 15 additions & 16 deletions custom_components/localtuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,16 @@ def _device_discovered(device):
)
new_data[ATTR_UPDATED_AT] = str(int(time.time() * 1000))
hass.config_entries.async_update_entry(entry, data=new_data)
device = hass.data[DOMAIN][TUYA_DEVICES][device_id]
if not device.connected:
device.async_connect()

elif device_id in hass.data[DOMAIN][TUYA_DEVICES]:
# _LOGGER.debug("Device %s found with IP %s", device_id, device_ip)
_LOGGER.debug("Device %s found with IP %s", device_id, device_ip)

device = hass.data[DOMAIN][TUYA_DEVICES].get(device_id)
if not device:
_LOGGER.warning(f"Could not find device for device_id {device_id}")
elif not device.connected:
device.async_connect()

device = hass.data[DOMAIN][TUYA_DEVICES][device_id]
if not device.connected:
device.async_connect()

def _shutdown(event):
"""Clean up resources when shutting down."""
Expand All @@ -167,7 +168,7 @@ async def _async_reconnect(now):
_handle_reload,
)

hass.helpers.service.async_register_admin_service(
hass.services.async_register(
DOMAIN, SERVICE_SET_DP, _handle_set_dp, schema=SERVICE_SET_DP_SCHEMA
)

Expand Down Expand Up @@ -255,8 +256,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
res = await tuya_api.async_get_access_token()
if res != "ok":
_LOGGER.error("Cloud API connection failed: %s", res)
_LOGGER.info("Cloud API connection succeeded.")
res = await tuya_api.async_get_devices_list()
else:
_LOGGER.info("Cloud API connection succeeded.")
res = await tuya_api.async_get_devices_list()
hass.data[DOMAIN][DATA_CLOUD] = tuya_api

async def setup_entities(device_ids):
Expand All @@ -268,12 +270,9 @@ async def setup_entities(device_ids):
)
hass.data[DOMAIN][TUYA_DEVICES][dev_id] = TuyaDevice(hass, entry, dev_id)

await asyncio.gather(
*[
hass.config_entries.async_forward_entry_setup(entry, platform)
for platform in platforms
]
)
# Setup all platforms at once, letting HA handling each platform and avoiding
# potential integration restarts while elements are still initialising.
await hass.config_entries.async_forward_entry_setups(entry, platforms)

for dev_id in device_ids:
hass.data[DOMAIN][TUYA_DEVICES][dev_id].async_connect()
Expand Down
7 changes: 7 additions & 0 deletions custom_components/localtuya/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def device_class(self):

def status_updated(self):
"""Device status was updated."""
super().status_updated()

state = str(self.dps(self._dp_id)).lower()
if state == self._config[CONF_STATE_ON].lower():
self._is_on = True
Expand All @@ -63,6 +65,11 @@ def status_updated(self):
"State for entity %s did not match state patterns", self.entity_id
)

# No need to restore state for a sensor
async def restore_state_when_connected(self):
"""Do nothing for a sensor."""
return


async_setup_entry = partial(
async_setup_entry, DOMAIN, LocaltuyaBinarySensor, flow_schema
Expand Down
Loading