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

Add support for unique_id and available #2

Merged
merged 2 commits into from
Sep 3, 2020
Merged

Add support for unique_id and available #2

merged 2 commits into from
Sep 3, 2020

Conversation

postlund
Copy link
Collaborator

@postlund postlund commented Sep 2, 2020

This adds support for unique_id and available. Benefits are:

  • A config entry will be created so that entity name and friendly name can be changed from UI
  • The device will appear as unavailable if an update fails, e.g. due to network problems, which makes it easier to spot if a device is not working

@postlund
Copy link
Collaborator Author

postlund commented Sep 2, 2020

Disclaimer: I only have switches so only that platform is tested.

@rospogrigio
Copy link
Owner

Mmm... tried to install your modifications and localtuya won't even start, none of my devices is available and I don't get any log messages...

@postlund
Copy link
Collaborator Author

postlund commented Sep 3, 2020

That's strange. Do you mean that the entities themselves aren't created or are they created but just not usable in the UI?

@postlund
Copy link
Collaborator Author

postlund commented Sep 3, 2020

Did they get different entity ids?

@rospogrigio
Copy link
Owner

Sorry, my fault (created a wrong symlink for testing). I managed to run your code but I don't like the approach of exporting the unique_id and having them edited via the UI, because they are the same used by the official tuya integration, and my localtuya was thought to be used also in combination with the tuya integration, if anyone needs it (or also for debug purposes).
So I'd integrate the "available" status but would avoid the unique_id thing, unless I did not understand correctly how it works.
Comments on your side?

@postlund
Copy link
Collaborator Author

postlund commented Sep 3, 2020

Ok, great!

I understand your concern regarding that. We could just prefix the unique_id with localtuya_ and get the benefits without collisions between the integrations. If that's an ok alternative?

@rospogrigio
Copy link
Owner

Nice idea. Would rather limit to local_ instead of localtuya_ . Are you going to do it or shall I do the job?

@postlund
Copy link
Collaborator Author

postlund commented Sep 3, 2020

Sure! I can update and push it now.

Avoid collisions with the tuya integration.
@postlund
Copy link
Collaborator Author

postlund commented Sep 3, 2020

Should be ok now!

@rospogrigio
Copy link
Owner

Mmm, not working as expected... all my devices have been duplicated with "_2" as postfix; plus, we need to handle correctly the subswitches, since I get these errors:
2020-09-03 12:15:37 ERROR (MainThread) [homeassistant.components.switch] Platform localtuya does not generate unique IDs. ID local_72371410d8bfc0502f8f already exists - ignoring switch.luce_soggiorno_2
Tuya handles subswitches by appending "_id", where id is the dps of the subswitch (in my case, I get a _1 and a _7 for my double gang switch).

@postlund
Copy link
Collaborator Author

postlund commented Sep 3, 2020

Ok, so, I believe it works as expected but maybe not the way you want. A config entry (I.e. one unique_id) will map to one entity id. So a device implementing unique_id will be reserved an entity id, just so that you know that it is always the same. This is logical and a solution to an early problem in Home Assistant, where two "devices" with the same name (e.g. a light named outside and a switch named outside) would generate the same entity id. Depending on which happened to be loaded first, would get the id and the other one would get _2. So it's not predictable which device that get which entity id. Same issue still exists for entities not having a unique id.

Since the same physical device now is represented by two config entries, you will get two different entity ids since they are unique. You kinda worked around that by setting the entity id manually, something that isn't allowed anymore. Doing so did however bypass the entity Id generation (as long as only one integration is enabled at the time). It is however not meant to be used like that and thus not allowed. I can't force you into changing behavior but I would strongly advice you to.

The sub switches is something I will have to look in to. I guess they are for double gang switches for instances? I have one of those, not plugged in yet though.

@rospogrigio
Copy link
Owner

Yes, I get that error for a double gang switch, and for a plug that provides both a plug and a USB outlet, and that are seen as two devices.

@rospogrigio
Copy link
Owner

By the way, appending the _dps to distinguish the entities is what the tuya integration currently does (I get local_blablabla_1 and local_blablabla_7 for my plug device and USB entities).

@rospogrigio
Copy link
Owner

OK, i appended the _dps myself and all seems to be working fine. Will merge everything into the repo, thanks for the support and explanation!

@rospogrigio rospogrigio merged commit a0bf22c into rospogrigio:master Sep 3, 2020
@postlund
Copy link
Collaborator Author

postlund commented Sep 3, 2020

Awesome, thanks! 👍

rospogrigio pushed a commit that referenced this pull request Jan 3, 2022
i-am-shodan pushed a commit to i-am-shodan/localtuya that referenced this pull request Aug 18, 2022
Update sensor.py: Long term statistics
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants