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 temperature precision override for tuya platform #43970

Conversation

matitalatina
Copy link

@matitalatina matitalatina commented Dec 5, 2020

Proposed change

Tuya platform has some issues in temperature rounding for some devices.
For example, my BTH-002-GCLW thermostat is rounded to unit (18, 19, 20) even if it should show in HA halves rounding (19.5, 20, 20.5).
I'm not the only one with this issue as stated in this long thread.
So I decided to fix not only for myself, but I'm happy to contribute to this awesome project.
I simply added an option that let me override the default behavior.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

@homeassistant
Copy link
Contributor

Hi @matitalatina,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@project-bot project-bot bot added this to Needs review in Dev Dec 5, 2020
@probot-home-assistant
Copy link

Hey there @ollo69, mind taking a look at this pull request as its been labeled with an integration (tuya) you are listed as a codeowner for? Thanks!
(message by CodeOwnersMention)

@@ -40,7 +40,8 @@
"support_color": "Forzar soporte de color",
"temp_divider": "Divisor de los valores de temperatura (0 = usar valor por defecto)",
"tuya_max_coltemp": "Temperatura de color m\u00e1xima notificada por dispositivo",
"unit_of_measurement": "Unidad de temperatura utilizada por el dispositivo"
"unit_of_measurement": "Unidad de temperatura utilizada por el dispositivo",
"precision_override": "Precisi\u00f3n de temperatura"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not provide additional translations. Just adjusting string.json is enough, the rest is handled by the translation platform.

Please remove all additional translations.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ops, it was easier than I thought.
Added in string.json and removed from translations.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I removed all the translations and I kept only strings.json, I don't see any labels during testing in my local environment. Is that ok? How can I know that it will work?
I'm just worried.
If it's OK, please resolve this thread.

Thank you for your time.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ollo69 please help me here, too.

@3v1n0
Copy link
Contributor

3v1n0 commented Dec 5, 2020

I've proposed something similar in #43666, but I think it's better to rely on tuyah to get the precision, and leave HA only change that, no?

@3v1n0
Copy link
Contributor

3v1n0 commented Dec 5, 2020

Ah not only, this is not enough: you may see the right temperature, but not set it properly... You'd need https://github.com/PaulAnnekov/tuyaha/pull/54/files#diff-63c27f5cccf70e2d762795023d3a91279fcb4fb280ecd06da23a7d1a2b025590R167

@matitalatina
Copy link
Author

matitalatina commented Dec 5, 2020

I've proposed something similar in #43666, but I think it's better to rely on tuyah to get the precision, and leave HA only change that, no?

I just followed the design that someone else used to fix the "wrong temperature" issue in tuya platform. Add the ability to override the behavior if it's wrong. I suspect that this platform handles so many devices that it is difficult to get it right automatically.

I think that both PR can live together: your PR tries to get the precision automatically. Mine allows to override it if it's wrong again.

Ah not only, this is not enough: you may see the right temperature, but not set it properly... You'd need https://github.com/PaulAnnekov/tuyaha/pull/54/files#diff-63c27f5cccf70e2d762795023d3a91279fcb4fb280ecd06da23a7d1a2b025590R167

I didn't change the "set temperature part" because I can't test if it's working if you put, for example, 18.3 to a thermostat that supports only halves. I tried to be as more conservative as possible. The issue I try to solve here is reading the value that is different from what I see on the thermostat.

BTW I can take some time to test it. Thank you for your help.

@matitalatina matitalatina marked this pull request as draft December 5, 2020 17:01
@matitalatina
Copy link
Author

I want to take some time to fix also the "set temperature" part. As suggested @3v1n0

Dev automation moved this from Needs review to Review in progress Dec 5, 2020
homeassistant/components/tuya/climate.py Outdated Show resolved Hide resolved
homeassistant/components/tuya/const.py Outdated Show resolved Hide resolved
@matitalatina
Copy link
Author

matitalatina commented Dec 5, 2020

@3v1n0 I can't override the "set temperature part" without touching the external tuya library.
My PR can fix only "the reading part" at the moment. I think your solution is more complete.
I'll leave this PR in "draft mode" and I prefer to focus our effort on yours.
Do you agree?

@ollo69
Copy link
Contributor

ollo69 commented Dec 5, 2020

My opinion is that is normally better to work on library, on HA is not allowed in many case to force values provided by library.
Anyway it is probably better that you go ahead with this PR and verify if it is acceptable by HA team, than @3v1n0 can rebase his PR when upstream will be available.

@matitalatina
Copy link
Author

matitalatina commented Dec 5, 2020

Now the precision override works also when we set the temperature.
But it need also this PR on the external tuya library: PaulAnnekov/tuyaha#59
If I have to do something else please tell me. This is my first time so bear with me :).

@matitalatina matitalatina marked this pull request as ready for review December 5, 2020 21:34
@ollo69
Copy link
Contributor

ollo69 commented Dec 5, 2020

Now the precision override works also when we set the temperature.
But it need also this PR on the external tuya library: PaulAnnekov/tuyaha#59
If I have to do something else please tell me. This is my first time so bear with me :)

If you need upstream change you have to wait for upstream PR merged and new library version released and than update the library version used by the component in manifest.json and related files. Meanwhile you should revert this PR to draft.

@matitalatina matitalatina marked this pull request as draft December 6, 2020 07:44
@matitalatina matitalatina force-pushed the tuya-climate-override-precision branch from 45731fc to c944b0c Compare December 6, 2020 07:53
@matitalatina
Copy link
Author

Waiting for the PR on external tuya library PaulAnnekov/tuyaha#59

Comment on lines +212 to +213
if self._override_precision != PRECISION_DEFAULT:
return self._override_precision
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These days I have been thinking about this PR, target_temperature should be managed separately because could have different value not compatible with precision.
You should remove this part and continue with this PR that in this case will not require TuyaHA library change. We will manage the target_temperature with a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But just thinking more ... why this is required? What happen if we just set precision to PRECISION_TENTHS? There are some undesired side effect? I think that we are making this problem more complex. Thoughts?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thermostat needs also to be tweaked on target temperature step. In fact, I can only set one unit. But my thermostat support 0.5 step.
With this override I can put also 0.5 step (e.g. 18.5 C). But I realized that I need also this change in the library because there's a round(number) that prevent me to set 0.5 values.

So with both these changes I can set 17.5C 😊.

There's a drawback, as you pointed out: what happens if you set PRECISION_TENTH and your thermostat support only HALVES? My thermostat puts the lower possible bound (target temperature 17.9 C -> it sets 17.5C).

You should put the right precision on your thermostat. If you put higher precision, you're doing it wrong, but it's not a tragedy if all thermostats behave like mine (I obviously can't test it).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that precision controls the visualization, temperature_step the step used to set the target. This 2 value is not related, you can set PRECISION_TENTH for precision and PRECISION_HALVES (0.5) for temperature_step and everything will work as expected. So i think that leaving PRECISION_TENTH for precision is not an issue at all, it is only important to set proper value for temperature_step.
The changes to the library proposed by you can potentially break other devices that do not accept decimal value in the payload, so should be reviewed.
I already implemented in Tuya Custom all required changes, in options page you can change independently precision and temperature_step and also contains local library changes to properly manage the set temperature command with decimal values. Please temporally replace standard integration with this custom component to perform test with all possible combination, than we will choose the correct change to implement.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the custom library. It is easier to test.
But I don't need it, if I understood correctly.
We decided to add the precision override only in "read mode", so I revert the temperature step part and set temperature.
In that way, I don't need to change the external library, it's safer and easier to digest as pull request.
Is that ok?
If we agreed, I'll revert the changes and we fix the "set the temperature" part in another pr.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not exactly. My idea is that create an option to override precision is not required, because we can simply set to PRECISION_TENTH and forget it.
Than we need to fix temperature_step, and for this we need PRs for both TuyaHA and HA.
I already made changes in custom component, but I don't have a climate so I need confirmation that this solve the issue and work properly before pushing correct PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried all possible things.

All worked well except these:

  • If I don't check "Use divided Temperature value for set temperature command", the thermostat becomes unavailable and the logs say

    Logger: custom_components.tuya_custom.tuyaha.tuyaapi
    Source: custom_components/tuya_custom/tuyaha/tuyaapi.py:273
    Integration: Tuya Custom (documentation)
    First occurred: 5:50:03 PM (1 occurrences)
    Last logged: 5:50:03 PM
    
    control device error, error code is ValueOutOfRange
    

    I have divider 2.

  • If I select a different sensor in "Sensor for current temperature", I don't see the change in UI. If I go to my lovelace thermostat I always see the thermostat temperature and not the sensor I put in the select box.

This behavior is OK for me, but it worth mentioning:

  • If I put temperature_step to tenth even if my thermostat support halves, it sets the first lower bound acceptable. (E.g 18.9 -> it sets 18.5). This behavior is the same as I got in my PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your test.

  • About point 1, it is correct because is to manage different devices behavior. But probably I have to set default to True because seems the most frequent cases
  • Point 2 is only for devices that do not provide current temperature info, and anyway I cannot apply this change in official component because it is not allowed
  • Point 3 is ok, as you said is acceptable. Just wondering if tenth step make sense, but can be left if required, because we are not able to determinate the correct step from device payload.

I'm going to do some additional changes:

  • I will set default to true for point 1 as stated before
  • I will remove the option to change the precision, I think set it always to tenth is OK (less option, less confusion).
  • I will try to manage possible temperature step as library option because I'm not sure that is allowed define step directly in HA.

It would be great if you will make again some test when new version will be released.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested with the new version

  • I confirm it's true by default "Use divided Temperature value for set temperature command".
  • The integration is working as expected.

I had an issue in changing the divider: I changed the value, but the value on UI didn't reflect the changes. I restarted HASS and it worked again. In my log I had an issue with "limit login reached". Maybe it simply lost the connection and so he could not update the UI.

Now it's working

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Limit login can occur during 1st configuration. It is again a Tuya API limitation.
That's fine, I will start pushing PR with new changes to TuyaHA later tomorrow.

@@ -110,6 +112,7 @@ def __init__(self, tuya, platform):
self._def_hvac_mode = HVAC_MODE_AUTO
self._min_temp = None
self._max_temp = None
self._override_precision = PRECISION_DEFAULT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename self._precision_override

@github-actions
Copy link

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
Thank you for your contributions.

@github-actions github-actions bot added the stale label Jan 26, 2021
@majuss
Copy link
Contributor

majuss commented Jan 28, 2021

What happened to this PR?! @frenck

@frenck
Copy link
Member

frenck commented Jan 28, 2021

@majuss There are comments open, not being addressed and since the activity is running stale, it will be cleaned up after a month of 0 activity.

@majuss
Copy link
Contributor

majuss commented Jan 28, 2021

Thank you for the quick answer. So how can I help out? Do I need to open a PR myself?

@github-actions github-actions bot removed the stale label Jan 28, 2021
@matitalatina
Copy link
Author

We are blocked by this upstream pull request :(
At the end @ollo69 took ownership of this change. But unfortunately we are still waiting.

@frenck frenck added the waiting-for-upstream We're waiting for a change upstream label Jan 28, 2021
@ollo69
Copy link
Contributor

ollo69 commented Jan 28, 2021

Part of this PR target (precision property) is already solved by PR #44148 already merged in HA (will be available with next release).
The target_temperature_step should be managed by the library and not by HA, the related PR is mentioned by @matitalatina in previous post.

This PR is in conflict with PR #44148 and should be closed.

@matitalatina
Copy link
Author

Thank you guys.
I hope that this PR will be unlocked ASAP so we can add also the component I tested 1 month ago.

I'm closing this one.

Dev automation moved this from Review in progress to Cancelled Jan 28, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Jan 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
Dev
  
Cancelled
Development

Successfully merging this pull request may close these issues.

None yet

6 participants