Skip to content

Added osramlightify groups.#7376

Merged
balloob merged 7 commits into
home-assistant:devfrom
deisi:lightify_groups
May 4, 2017
Merged

Added osramlightify groups.#7376
balloob merged 7 commits into
home-assistant:devfrom
deisi:lightify_groups

Conversation

@deisi
Copy link
Copy Markdown
Contributor

@deisi deisi commented Apr 30, 2017

Allows you to make use of the build in osram lightify groups. Group states get
handeled similar as in the case of phillips hue. A lightify group shows up as
light in the homeassistant webinterface. If one light of the
group is on, the complete group is considered to be on.

To use this feature, first define some groups within your lighrify bridge, then
add allow_lightify_groups=true to you osramlightify config.

Example entry for configuration.yaml (if applicable):

- platform: osramlightify
  host: IP-ADDRES
  allow_lightify_groups: true

Checklist:

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

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

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

If the code does not interact with devices:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • Tests have been added to verify that the new code works.

Allows you to make use of the build in osram lightify groups. Group states get
handeled similar as in the case of phillips hue. A lightify group shows up as
light in the homeassistant webinterface. If one light of the
group is on, the complete group is considered to be on.

To use this feature, first define some groups within your lighrify bridge, then
set add `allow_lightify_groups=true` to you osramlightify config.

It might look like:
````yaml
- platform: osramlightify
  host: IP-ADDRES
  allow_lightify_groups: true
```
@mention-bot
Copy link
Copy Markdown

@deisi, thanks for your PR! By analyzing the history of the files in this pull request, we identified @olimpiurob, @tfriedel and @tchellomello to be potential reviewers.

MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(milliseconds=100)
CONF_ALLOW_LIGHTIFY_GROUPS = "allow_lightify_groups"
DEFAULT_ALLOW_LIGHTIFY_GROUPS = False
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.

Let's default it to True

bridge.update_all_light_status()
bridge.update_group_list()
except socket.error:
errno, errstr = sys.exc_info()[:2]
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.

Wait, what are you doing here? You can just catch the actual error instead of hacking around it using exception frames:

except socket.error as err:
    if isinstance(err, socket.timeout):
        _LOGGER.error(…)

However, you are now swallowing all the not timeout related errors which means that something might go wrong but no one will know.

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 python 3 socket.error is an alias of OSError.

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.

Instead of checking the error type you can have multiple except statements. First one that catches TimeoutError, then a general one for OSError.


self._luminary.set_onoff(0)
self._state = False
self.schedule_update_ha_state()
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.

Can you remove this line? it's not needed since should_poll is defaulting to True and so this is automatically called.

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.

You mean the self._state = False ?
Well yes I can remove it, but I saw, that without it, HA gets a lot more unresponsive, because otherwise it the GUI jumps back to the on state and then after a couple of seconds it jumps back to off. If that is okay, I'm willing to remove it, I just felt it works better with.

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 not something that we should fix in a platform but instead fix for everyone at once. We already wait 2 seconds for a status update: https://github.com/home-assistant/home-assistant-polymer/blob/master/src/components/entity/ha-entity-toggle.html#L135-L136

Please remove this line.

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.

okay, done


class OsramLightifyLight(Light):
"""Representation of an Osram Lightify Light."""
class Luminary():
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.

Why not have this class already inherit from Light ?


The group is on, if any of the lights in on.
"""
states = []
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.

This is inefficient as you now first have to gather all results before checking if any are True.

lights = self._bridge.lights()
return any(lights[light_id].on() for light_id in self._light_ids)

super().update()
self._light_ids = self._luminary.lights()
self._light = self._bridge.lights()[self._light_ids[0]]
self._brightness = int(self._light.lum() * 2.25)
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.

Brightness goes to 255.

"""Update group status."""
super().update()
self._light_ids = self._luminary.lights()
self._light = self._bridge.lights()[self._light_ids[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.

Why are you storing the light as an instance variable?

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.

looking at it you are right I don't need to. I will remove it.

def update(self):
"""Update group status."""
super().update()
self._light_ids = self._luminary.lights()
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.

What is self._luminiary and is it only used to fetch the lights?

I think that things are getting very complicated because of the inheritance.

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.

I think that things are getting very complicated because of the inheritance.

I first stared out without, but my code became 90% redundant and so I splittend i t up. The only difference between the two is the update and get_state method.

What is self._luminiary and is it only used to fetch the lights?

Well its the main interface to the lightify API. Essentially all communication with the lights goes through the self._luminary obj. It is called like this, because it is the name of the class within the lightify API itself. I use it, because it is the common ground between lights and groups of lights just like throughout this module as well.

@balloob
Copy link
Copy Markdown
Member

balloob commented Apr 30, 2017

Can you also remove all the debug statements in properties ? It is slowing down Hass a lot and it's unnecessary.

@dale3h dale3h changed the title Added osramlighrify groups. Added osramlightify groups. May 2, 2017
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/light.osramlightify/
"""
import sys
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

'sys' imported but unused

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.

fixed

def setup_bridge(bridge, add_devices_callback):
"""Set up the Lightify bridge."""
def setup_bridge(bridge, add_devices_callback, add_groups):
"""Setup the Lightify bridge."""
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 revert this change.

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.

If I remove it, the DEFAULT_ALLOW_LIGHTIFY_GROUPS = True config becomes useless.

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.

never mind

@balloob
Copy link
Copy Markdown
Member

balloob commented May 3, 2017

This is ok to merge when the comment change has been reversed and linting has been fixed 🎉

@deisi
Copy link
Copy Markdown
Contributor Author

deisi commented May 4, 2017

I tried to rebase it, but I coudnt rebase anything before the merge. Shoud I make a new PR or is it okay the way it is?

@MartinHjelmare
Copy link
Copy Markdown
Member

MartinHjelmare commented May 4, 2017

You should revert the change to the doc string as @balloob asked, before we can merge. See comment above. Just make a new commit where you change it back to like it was before.

@balloob balloob merged commit e3bb45c into home-assistant:dev May 4, 2017
@balloob balloob mentioned this pull request May 5, 2017
@chimpy
Copy link
Copy Markdown

chimpy commented May 5, 2017

Awesome, this was a Feature Request I really wanted! Thank you! <3

@deisi
Copy link
Copy Markdown
Contributor Author

deisi commented May 5, 2017 via email

@duffrecords
Copy link
Copy Markdown

duffrecords commented May 12, 2017

This change breaks my Home Assistant setup. Each time I start HA the value of self._light_ids[0] seems to be incorrect but the error does not always happen with the same light. Maybe there's a discrepancy between the bridge and the states in HA. I've rolled back to 0.43.2 for now and the problem went away.

2017-05-10 13:31:46 ERROR (MainThread) [homeassistant.components.light] Error while setting up platform osramlightify
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity_component.py", line 155, in _async_setup_platform
    entity_platform.schedule_add_entities, discovery_info
  File "/opt/rh/rh-python35/root/usr/lib64/python3.5/asyncio/futures.py", line 358, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/opt/rh/rh-python35/root/usr/lib64/python3.5/asyncio/tasks.py", line 290, in _wakeup
    future.result()
  File "/opt/rh/rh-python35/root/usr/lib64/python3.5/asyncio/futures.py", line 274, in result
    raise self._exception
  File "/opt/rh/rh-python35/root/usr/lib64/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/light/osramlightify.py", line 60, in setup_platform
    setup_bridge(bridge, add_devices, add_groups)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/light/osramlightify.py", line 106, in setup_bridge
    update_lights()
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/util/__init__.py", line 303, in wrapper
    result = method(*args, **kwargs)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/util/__init__.py", line 303, in wrapper
    result = method(*args, **kwargs)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/light/osramlightify.py", line 97, in update_lights
    update_lights)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/light/osramlightify.py", line 268, in __init__
    super().__init__(group, update_lights)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/light/osramlightify.py", line 121, in __init__
    self.update()
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/light/osramlightify.py", line 282, in update
    light = self._bridge.lights()[self._light_ids[0]]
KeyError: 9518399593890067646

@deisi
Copy link
Copy Markdown
Contributor Author

deisi commented May 12, 2017 via email

@duffrecords
Copy link
Copy Markdown

I won't be available until Wednesday but I can help you after then.

@balloob
Copy link
Copy Markdown
Member

balloob commented May 13, 2017

Please open an issue and discuss a potential fix there.

@home-assistant home-assistant locked and limited conversation to collaborators May 13, 2017
@deisi deisi deleted the lightify_groups branch May 22, 2017 18:40
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants