Skip to content

Lagute LW-12 Wifi LED control#13307

Merged
MartinHjelmare merged 26 commits intohome-assistant:devfrom
jaypikay:lw12wifi
May 22, 2018
Merged

Lagute LW-12 Wifi LED control#13307
MartinHjelmare merged 26 commits intohome-assistant:devfrom
jaypikay:lw12wifi

Conversation

@jaypikay
Copy link
Copy Markdown
Contributor

@jaypikay jaypikay commented Mar 18, 2018

Description:

New platform added to support the Lagute LW-12 WiFi LED Controller. The controller is stateless and communicates via UDP. To handle the controller itself the python module python-lw12 is required and automatically installed by the REQUIREMENTS.

The platform supports 29 effects, brightness, rgb color and transition speed settings.

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#5264

Example entry for configuration.yaml:

light:
  - platform: lw12wifi
    # Host name or IP of LW-12 LED stripe to control
    host: 192.168.0.1
    # (Optional) Some firmware versions of the LW-12 controller listen
    # on different ports.
    port: 5000
    # (Optional) Friendly name
    name: LW-12 FC

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox.

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

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

  • New dependencies have been added to the REQUIREMENTS variable.
  • New dependencies are only imported inside functions that use them.
  • 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:

  • Tests have been added to verify that the new code works.

@jaypikay jaypikay requested a review from andrey-git as a code owner March 18, 2018 12:00
@homeassistant
Copy link
Copy Markdown
Contributor

Hi @jaypikay,

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!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

undefined name 'SUPPORT_FLASH'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

redefinition of unused 'DOMAIN' from line 36

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.

Platforms belong to a domain. lw12wifi belongs to light.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

'socket' imported but unused

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

'enum.Enum' imported but unused

Copy link
Copy Markdown
Member

@OttoWinter OttoWinter left a comment

Choose a reason for hiding this comment

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

I don't understand why we should have configuration options like effect, brightness or rgb. The user can manually specify those in service calls.

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.

The indentation makes this quite hard to read, could you maybe consider indenting the vol.All blocks?

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 do the following instead for None checks (I'm assuming this is checking for None):

if self._effect is None:
    return None
return self._effect.replace('_', ' ').title()

It's just so much easier to understand.

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 seems like a debugging statement for development and not for later use. The service calls will be logged anyway from the core.

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.

PEP8 recommends doing None checks with is not None:

if self._effect is not None:

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 a blocking method and it does I/O. It should therefore not be in an async context. Consider renaming the method to (especially when using time.sleep(0.25); it would block the entire core)

def turn_on(self, **kwargs):

The same applies for turn_off

@jaypikay
Copy link
Copy Markdown
Contributor Author

To answer the question why we should have configuration options like effect, brightness or rgb? It was my understanding to use these options, as they are provided by homeassistant.const. Should I rename rgb to default_color instead? It is also the only way to define a default behaviour for the lights, because the controller itself is very unreliable. I can remove it, but on the other hand I do not see a disadvantage in this.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

'asyncio' imported but unused

@OttoWinter
Copy link
Copy Markdown
Member

What I was trying to say is that I don't understand why we should provide these options at all. I mean if we don't know the state at startup, why should the configuration know for us what these brightness, color, ... values will be at startup?

This platform can't ask the device for states, ok. But then I would just assume the platform would initialize the light as 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.

The docstring should be:

"""Return True if unable to access real state of the entity."""

as from the entity definition.

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.

I think you would need another time.sleep(0.25) after this (and a couple of other places). Also I think halding kwargs with both rgb color and brightness can be handled a bit smarter. currently you're first sending the old brightness and then the new brightness from kwargs.

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.

Another time.sleep(.25) is not needed here, if we call normal turn_on without the brightness setting, the light will "forget" the brightness and set the new color. The platform would still list the brightness as defined by either the default setting defined in the configuration or by previous turn_on calls with brightness as argument.

@jaypikay
Copy link
Copy Markdown
Contributor Author

All settings but host are optional. Having the other values set are for a smoother user experience with these lights. Instead of defining a switch or similar, the light will turn on using the favored settings, when I click the power button. The RGB selection is not available when home assistant assumes the device as powered off.

I developed the platform using my experience with the hardware in mind and what would be nice to have. But I am happy to help by fixing my code.

Using service calls is possible and is at the moment the only way to set the transistion speed when using effects. I had hoped for a slider like brightness.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As a heads-up, we've just merged a big refactoring to how light platforms implement colors. You'll need to rebase on the latest dev. Platforms are now going to receive turn_on reqeusts with a hue/sat color, and need to report their current color in hue/sat.

Some examples of how to convert to/from RGB if that's the preferred platform interface are here:

https://github.com/home-assistant/home-assistant/blob/947218d51c94566feeb32c6f549493fa07683252/homeassistant/components/light/hyperion.py#L142-L143

https://github.com/home-assistant/home-assistant/blob/947218d51c94566feeb32c6f549493fa07683252/homeassistant/components/light/hyperion.py#L110-L113

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.

Thanks you this information. I will update the code to match the new requirements.

@jaypikay jaypikay changed the title Lw12wifi Lagute LW-12 Wifi LED control Mar 21, 2018
@jaypikay
Copy link
Copy Markdown
Contributor Author

Sorry for bumping, but do I need to improve something to get it merged in a future release, or simply wait?

Copy link
Copy Markdown
Member

@fabaff fabaff Apr 10, 2018

Choose a reason for hiding this comment

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

Move that to setup_platform and pass it down to LW12WiFi(). This way you don't need host and port.

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 use the same style for listing the features as other platform do.

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 did as in components/light/{group.py,mqtt_json.py,zha.py,zwave.py,} and other, sorry but I don't understand why this is not good for my solution but for others?

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 style with using mutable features with |= is usually only done when there are "dynamic" supported features. For example, light.mqtt_json only enables color when it's defined in the configuration. For these static integrations like lw12wifi, it's usually cleaner to define the supported features in a global constant like this:

https://github.com/home-assistant/home-assistant/blob/c3388d63a1929a6c197644d1ae1f622c4d58c793/homeassistant/components/media_player/cast.py#L40-L42

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.

rgb_color is not used by the light component base. This property can be removed.

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.

Typo, to -> too

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 probably doesn't matter too much, but you could implement RGB color here as it's done in the light base:

vol.All(vol.ExactSequence((cv.byte, cv.byte, cv.byte)),
                vol.Coerce(tuple)),

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 this parameter be renamed to transition_length or transition_speed or default_transition_length? I think just transition is not too descriptive.

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 it's an unknown effect, shouldn't we just raise HomeAssistantError. I'm not a huge fan of failing silently.

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.

So when using this, every time there's a turn_on call the transition length will be set for all future turn_on calls?

I think the transition length from the service call should just refer to a single request.

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.

Otto is correct. We should not store the transition speed as an instance variable.

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.

Users should not pass in RGB, TRANSITION. Please remove these config values.

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.

Should be removed.

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 you want to restore the previous known state after a restart, use helpers/restore_state.py (but do that in a new PR)

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 not being referenced in the config (and neither should it). Please remove.

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.

Possible to validate here that the connection has been established successfully?

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.

Unfortunatly not, it is an UDP connection, and the controller will never answerer to sent messages.

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.

Unfortunatly it is an UDP connection and the device does not send answers back.

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 add the property should_poll and have it return 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.

Might not be needed if #14229 gets merged before this PR.

@MartinHjelmare MartinHjelmare merged commit 72a1b7a into home-assistant:dev May 22, 2018
@jaypikay jaypikay deleted the lw12wifi branch May 26, 2018 13:27
@balloob balloob mentioned this pull request Jun 8, 2018
@home-assistant home-assistant locked and limited conversation to collaborators Sep 5, 2018
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