Skip to content

Brightness conversion for Abode dimmers#13711

Merged
syssi merged 5 commits intohome-assistant:devfrom
shred86:dev
Apr 7, 2018
Merged

Brightness conversion for Abode dimmers#13711
syssi merged 5 commits intohome-assistant:devfrom
shred86:dev

Conversation

@shred86
Copy link
Copy Markdown
Contributor

@shred86 shred86 commented Apr 6, 2018

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:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

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

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.
@homeassistant
Copy link
Copy Markdown
Contributor

Hi @shred86,

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!

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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If 99 is the upper limit the range should be mapped to 0...99.

Copy link
Copy Markdown
Contributor Author

@shred86 shred86 Apr 6, 2018

Choose a reason for hiding this comment

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

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))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Checkout this one: #12203

It's not exactly your case but provides some support may be.

Copy link
Copy Markdown
Contributor Author

@shred86 shred86 Apr 6, 2018

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you check another edge case? Is the light turned off if you set a brightness of 0 or is 0 the lowest brightness?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

When I set the brightness all the way to 0, the light is turned off.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perfect!

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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does the device return 100% ever here? I assume the upper limit should be 99 again.

Copy link
Copy Markdown
Contributor Author

@shred86 shred86 Apr 6, 2018

Choose a reason for hiding this comment

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

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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

99 * 255 / 100 = 252.45. It should be 255! What about brightness * 255 / 99

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just realized your comment. Hmmm...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.
@shred86
Copy link
Copy Markdown
Contributor Author

shred86 commented Apr 6, 2018

@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.

@syssi
Copy link
Copy Markdown
Member

syssi commented Apr 6, 2018

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))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same here

@syssi syssi merged commit 2bf17cb into home-assistant:dev Apr 7, 2018
@fabaff fabaff mentioned this pull request Apr 7, 2018
2 tasks
@balloob balloob mentioned this pull request Apr 27, 2018
Adminiuga pushed a commit to Adminiuga/home-assistant that referenced this pull request Jun 25, 2018
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.
@home-assistant home-assistant locked and limited conversation to collaborators Jul 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants