StyraHem / ShellyForHASS

Shelly smart home platform for Home Assistant
MIT License
619 stars 111 forks source link

[BUG] Brightness conversion is incorrect with python 3.x #636

Closed allistermaguire closed 2 years ago

allistermaguire commented 2 years ago

Environment

Describe the bug

Brightness for Dimmer 2 in Home Assistant for 100 percent is 254, it should be 255.

I have only noticed it now as I am using the scene snapshot functionality to record the existing state to re apply it later. Lights that are set at 100% will be 99% once the state is reapplied.

This looks to be caused by a differences between python 3.x and 2.7, and the conversion of brightness scale between Shelly and Home Assistant. For example the code below returns 254 in 3.x and 255 in 2.7.

@property
    def brightness(self):
        """Return the brightness of the light."""
        if self._brightness is None:
            return None
        return int(self._brightness * 2.55)

It would be a simple fix; use round() instead of int() when retuning or setting brightness.

Steps to Reproduce

View entity state in developer tools for a Dimmer 2 that is set to 100%.

Expected behavior

Dimmer 2 brightness should be 255.

Screenshots

Traceback/Error logs

Python 3.8.9 (default, Oct 26 2021, 07:25:53) 
[Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print(100*2.55)
254.99999999999997

Python 2.7.18 (default, Oct  2 2021, 04:20:38) 
[GCC Apple LLVM 13.0.0 (clang-1300.0.29.1) [+internal-os, ptrauth-isa=deploymen on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print(100*2.55)
255.0

Additional context

david-kalbermatten commented 2 years ago

Duplicate of #550 but admittedly this issue has a more concrete description of the problem and a suggestion for how to fix it

proxlx commented 2 years ago

Thank you for re flagging this! It causes real issues for me as I use a lot of scene snapshots in my setup which then decreases the level each time the snapshot is used. It seems such an easy fix so hopefully it gets updated. As a side note it’s not just Dimmer2 but Dimmer1 and also RGBW2 intensity levels it happens on

allistermaguire commented 2 years ago

See pull request #637