Brightness conversion for Abode dimmers#13711
Brightness conversion for Abode dimmers#13711syssi merged 5 commits intohome-assistant:devfrom shred86:dev
Conversation
With AbodePy 0.12.3, dimmers will now work but a conversion of the brightness is required. Additionally, when a brightness value of 100 is sent to Abode, 99 is returned causing AbodePy to throw an error so this component will send 99 instead of 100.
| if ATTR_BRIGHTNESS in kwargs and self._device.is_dimmable: | ||
| self._device.set_level(kwargs[ATTR_BRIGHTNESS]) | ||
| # Convert HASS brightness (0-255) to Abode brightness (0-100) | ||
| brightness = int(kwargs[ATTR_BRIGHTNESS] * 100 / 255) |
There was a problem hiding this comment.
If 99 is the upper limit the range should be mapped to 0...99.
There was a problem hiding this comment.
It's possible to pass a value of 100 to Abode but for some reason in the response it returns 99, so AbodePy throws an error when the passed value != return value. I'm new to Python but would something like this be a better solution?
brightness = int(max(min(kwargs[ATTR_BRIGHTNESS] * 100 / 255, 99), 0))
There was a problem hiding this comment.
It's just math: kwargs[ATTR_BRIGHTNESS] provides a value between 0 and 255:
0 * 100 / 255 = 0
128 * 100 / 255 = int(50.19) = 50
255 * 100 / 255 = 100
vs.
0 * 99 / 255 = 0
128 * 99 / 255 = int(49.69) = 49
255 * 99 / 255 = 99
There was a problem hiding this comment.
Checkout this one: #12203
It's not exactly your case but provides some support may be.
There was a problem hiding this comment.
Lol, very good point. ;)
Ok, I think this is a much better implementation which also incorporates the solution you linked to. Using the ceil function from the math module:
return ceil(int(self._device.brightness) * 255 / 100)
There was a problem hiding this comment.
Could you check another edge case? Is the light turned off if you set a brightness of 0 or is 0 the lowest brightness?
There was a problem hiding this comment.
When I set the brightness all the way to 0, the light is turned off.
| if self._device.is_dimmable and self._device.has_brightness: | ||
| return self._device.brightness | ||
| # Convert Abode brightness (0-100) to HASS brightness (0-255) | ||
| return int(self._device.brightness) * 255 / 100 |
There was a problem hiding this comment.
Does the device return 100% ever here? I assume the upper limit should be 99 again.
There was a problem hiding this comment.
The max value returned when setting the brightness is 99, but when HA first boots up and gets all of the devices from Abode, it will return 100 in this case.... so for this one I think we'll have to do something like:
return ceil(int(self._device.brightness) * 255 / 100)
But the "issue" now is there are times the return value can be off by 1 from the value that was sent (since we're dividing by 100 and not 99). Not really sure if it's an issue though.
Better implementation of the brightness conversion as it's less code and also provides a better UI experience as very small brightness values (i.e. 1, 2) won't be converted to 0.
Minor changes to pass checks and update to comments
| return self._device.brightness | ||
| # Convert Abode brightness (0-100) to HASS brightness (0-255) | ||
| # Abode will return 100 during initialization of Abode | ||
| return ceil(int(self._device.brightness) * 255 / 100) |
There was a problem hiding this comment.
99 * 255 / 100 = 252.45. It should be 255! What about brightness * 255 / 99
There was a problem hiding this comment.
Just realized your comment. Hmmm...
There was a problem hiding this comment.
Yeah I had it set to divide by 99 but noticed after restarting HA, Abode was returning a brightness value of 100 from one of my dimmers that was on which caused it to convert to 257. When you set the brightness to 100 is when it will only return 99.
There was a problem hiding this comment.
In this case I would remap the brightness of 100:
brightness = int(self._device.brightness)
if brightness == 100:
return 255
else
return ceil(brightness * 255 / 99)
Keeps the brightness value sent and returned from the device response consistent. However, during initialization and when a device refresh is received, Abode can return 100 thus we'll convert that case back to 99.
|
@syssi - Thanks for the help! Just made another update to the brightness conversion and tested on my local HA install. Everything is working as expected. If this is good to go, recommend we add it with #13709 which has already been approved. Otherwise, brightness functionality is not going to work properly without this conversion for Abode dimmers. |
|
Could you fix this lint issue: R: 75,12: Unnecessary "else" after "return" (no-else-return) |
| self._device.set_level(kwargs[ATTR_BRIGHTNESS]) | ||
| # Convert HASS brightness (0-255) to Abode brightness (0-99) | ||
| # If 100 is sent to Abode, response is 99 causing an error | ||
| self._device.set_level(ceil(kwargs[ATTR_BRIGHTNESS] * 99 / 255)) |
There was a problem hiding this comment.
Please device by 255.0:
>>> ceil(1 * 99 / 255)
0.0
>>> ceil(1 * 99 / 255.0)
1.0
| return 255 | ||
| else: | ||
| # Convert Abode brightness (0-99) to HASS brightness (0-255) | ||
| return ceil(brightness * 255 / 99) |
With AbodePy 0.12.3, dimmers will now work but a conversion of the brightness is required. Additionally, when a brightness value of 100 is sent to Abode, 99 is returned causing AbodePy to throw an error so this component will send 99 instead of 100. Keeps the brightness value sent and returned from the device response consistent. However, during initialization and when a device refresh is received, Abode can return 100 thus we'll convert that case back to 99.
With AbodePy 0.12.3, dimmers will now work but a conversion of the brightness is required. Additionally, when a brightness value of 100 is sent to Abode, 99 is returned causing AbodePy to throw an error so this component will send 99 instead of 100.
Description:
Related issue (if applicable): N/A
Pull request in home-assistant.github.io with documentation (if applicable): N/A
Example entry for
configuration.yaml(if applicable):Checklist:
tox. Your PR cannot be merged unless tests passIf the code communicates with devices, web services, or third-party tools:
REQUIREMENTSvariable Update AbodePy version to 0.12.3 #13709