home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
71.58k stars 29.91k forks source link

LIFX support? #578

Closed avaidyam closed 8 years ago

avaidyam commented 8 years ago

Just acquired a pair of LIFX bulbs, and I'm not sure how to get them to work with HA. It looks like there's an existing Python API thing here.

I'm interested in porting this. Any thoughts?

fabaff commented 8 years ago

Just go ahead and create a light platform for LIFX. Take a look at the Development documentation, existing platforms, and the samples provides by lifxlan as a starting point.

avaidyam commented 8 years ago

@fabaff I've got a Python 3/LIFX 2-working implementation here. I'll work on building an HA component for it now.

avaidyam commented 8 years ago

@balloob @fabaff I'm having issues having my setup_platform being called. Any ideas? Here's what I've placed in my custom_components directory:

import lazylights

from colorsys import *
from homeassistant.components.light import (
    Light, ATTR_BRIGHTNESS, ATTR_RGB_COLOR)
#, ATTR_COLOR_TEMP)

DEPENDENCIES = []
#REQUIREMENTS = ['https://github.com/avaidyam/lazylights/']

def setup_platform(hass, config, add_devices_callback, discovery_info=None):
    """ Find and return LIFX lights. """
    bulbs = lazylights.find_bulbs(expected_bulbs=1)
    print(lazylights.get_state(bulbs))
    add_devices_callback([LIFXLight(n) for n in bulbs])

class LIFXLight(Light):
    """ Provides a LIFX bulb. """
    # pylint: disable=too-many-arguments
    def __init__(self, info):
        self._info = info

    @property
    def should_poll(self):
        return True

    @property
    def name(self):
        """ Returns the name of the device if any. """
        self._info = lazylights.get_state([self._info.bulb])
        return self._info.label

    @property
    def brightness(self):
        """ Brightness of this light between 0..255. """
        self._info = lazylights.get_state([self._info.bulb])
        return int((self._info.brightness / 65536) * 255)

    @property
    def rgb_color(self):
        """ rgb color value. """
        self._info = lazylights.get_state([self._info.bulb])
        return hls_to_rgb(self._info.hue, self._info.saturation, self._info.lightness)

    @property
    def color_temp(self):
        """ CT color temperature. """
        self._info = lazylights.get_state([self._info.bulb])
        return int(1000000 / self._info.kelvin)

    @property
    def is_on(self):
        """ True if device is on. """
        self._info = lazylights.get_state([self._info.bulb])
        return self._info.power == 65536

    def turn_on(self, **kwargs):
        """ Turn the device on. """
        self._info = lazylights.get_state([self._info.bulb])
        lazylights.set_power([self._info.bulb], True)

        bright = 100.0
        if ATTR_BRIGHTNESS in kwargs:
            bright = kwargs[ATTR_BRIGHTNESS]

        if ATTR_RGB_COLOR in kwargs:
            rgb = kwargs[ATTR_RGB_COLOR]
            hsl = hsl_to_rgb(rgb[0], rgb[1], rgb[2])
            hsl[2] = bright

            lazylights.set_state([self._info.bulb], hsl[0], hsl[1], hsl[2], 2000, 1, False)
        #elif ATTR_COLOR_TEMP in kwargs:
        #    kelvin = int(1000000 / kwargs[ATTR_COLOR_TEMP])
        #    lazylights.set_state([self._info.bulb], 0, 0, bright, kelvin, 1, False)

        self.update_ha_state()

    def turn_off(self, **kwargs):
        """ Turn the device off. """
        self._info = lazylights.get_state([self._info.bulb])
        lazylights.set_power([self._info.bulb], False)
        self.update_ha_state()

As a side-note, I can't get my REQUIREMENTS to work -- it crashes HA, and so does importing ATTR_COLOR_TEMP.

balloob commented 8 years ago

What is the file name / path you put your file in ?

avaidyam commented 8 years ago

.homeassistant/custom_components/lifx.py

In my configuration.yaml, I've added the line lifx:

balloob commented 8 years ago

Your requirements entry is wrong, have a look at other entries. you either have to specify a package, like lazylights==2.0.0rc1 or the location of a zip file containing the package with the version number added as a hash to the end of the url (see Vera lights for example)

For placing your file, it should be in the directory of the component you are creating a platform for. So if it is a light, it should be <config>/custom_components/light/lifx.py and then you can refer to it by:

light:
  platform: light
avaidyam commented 8 years ago

@balloob That fixed the loading issues, thanks! I've also begun using the LOGGER component to track changes in the state. But why is it I can't import ATTR_COLOR_TEMP, and why is this in Mireds and not Kelvin, which is far more widely known?

avaidyam commented 8 years ago

@balloob A new set of issues here:

Once I turn on a light, its state does not update, and when I try to toggle it off, it doesn't turn off.

import logging
import lazylights

from colorsys import *
from homeassistant.components.light import (
    Light, ATTR_BRIGHTNESS, ATTR_RGB_COLOR)

_LOGGER = logging.getLogger(__name__)

DEPENDENCIES = []
#REQUIREMENTS = ['https://github.com/avaidyam/lazylights/']

def setup_platform(hass, config, add_devices_callback, discovery_info=None):
    """ Find and return LIFX lights. """
    bulbs = lazylights.find_bulbs(expected_bulbs=1)
    _LOGGER.info(bulbs)
    add_devices_callback([LIFXLight(n) for n in bulbs])

class LIFXLight(Light):
    """ Provides a LIFX bulb. """

    def __init__(self, bulb):
        _LOGGER.info(bulb)
        self._bulb = bulb
        #self._info = lazylights.get_state(bulb)

    @property
    def should_poll(self):
        return True

    @property
    def name(self):
        """ Returns the name of the device if any. """
        self._info = lazylights.get_state([self._bulb])[0]
        return self._info.label.decode('utf-8').trim()

    @property
    def brightness(self):
        """ Brightness of this light between 0..255. """
        self._info = lazylights.get_state([self._bulb])[0]
        return int((self._info.brightness / 65536) * 255)

    @property
    def rgb_color(self):
        """ rgb color value. """
        self._info = lazylights.get_state([self._bulb])[0]
        return hls_to_rgb(self._info.hue, self._info.saturation, self._info.brightness)

    @property
    def color_temp(self):
        """ CT color temperature. """
        self._info = lazylights.get_state([self._bulb])[0]
        return int(1000000 / self._info.kelvin)

    @property
    def is_on(self):
        """ True if device is on. """
        self._info = lazylights.get_state([self._bulb])[0]
        return self._info.power == 65536

    def turn_on(self, **kwargs):
        """ Turn the device on. """
        self._info = lazylights.get_state([self._bulb])[0]
        lazylights.set_power([self._bulb], True)

        bright = 100.0
        if ATTR_BRIGHTNESS in kwargs:
            bright = kwargs[ATTR_BRIGHTNESS]

        if ATTR_RGB_COLOR in kwargs:
            rgb = kwargs[ATTR_RGB_COLOR]
            hsl = hsl_to_rgb(rgb[0], rgb[1], rgb[2])
            hsl[2] = bright

            lazylights.set_state([self._bulb], hsl[0], hsl[1], hsl[2], 2000, 1, False)
        elif "color_temp" in kwargs:
            kelvin = int(1000000 / kwargs[ATTR_COLOR_TEMP])
            lazylights.set_state([self._bulb], 0, 0, bright, kelvin, 1, False)
        else:
            lazylights.set_state([self._bulb], 0, 0, bright, 3000, 1, False)
        self.update_ha_state()

    def turn_off(self, **kwargs):
        """ Turn the device off. """
        #self._info = lazylights.get_state([self._bulb])[0]
        lazylights.set_power([self._bulb], False)
        self.update_ha_state()
balloob commented 8 years ago

It's difficult giving proper feedback as I can't give feedback per line as in a PR.

Implement an update method to fetch the state, it will be automatically called by HA after a turn_on/off call or when it is polling.

def update():
    self._info = lazylights.get_state([self._bulb])[0]

If it doesn't show as on, it probably means that is_on does not return True. Can you verify that self._info.power == 65536 ?

Your color is properly not working because you are referring to the method hls_to_rgb which doesn't seem to be defined? It expects a list of rgb values, ie [255, 255, 255]

avaidyam commented 8 years ago

@balloob Thanks so much for your help. When I know the component seems like it's working, I'll turn it into a PR.

I've implemented the update() method, and streamlined the other methods. I've also fixed the is_on() method: it turns out that sometimes, LIFX bulbs don't necessarily show 0xFFFFbut if they are on, they are guaranteed to be above 0x0000, which can be remedied by just checking > 0 instead.

Also, I'm importing hls_to_rgb() from colorsys. I did forget to translate values into the [0, 255] range though. I've fixed that, but my concern is: how do I get the frontend to show an RGB and CT selector like for Philips Hue bulbs?

robinic commented 8 years ago

Hi,

Awesome work, I would be very keen to give this a try with my 3 lifx bulbs, any chance I can grab the code? I have been trying to control the lifx through Maker and it would be great to have a more direct connection

Cheers,

avaidyam commented 8 years ago

@robinic Here.

robinic commented 8 years ago

Hi @avaidyam Thanks for that. I have installed and setup, without errors in the log, and can see lights in the developer component of the UI, but I don't see to have any way to control the lights? Any pointers?

Thanks

avaidyam commented 8 years ago

@robinic I would tell you if I could. I can only get it to turn on and off (and sometimes brightness). I'm trying to figure out how to control RGBK.

robinic commented 8 years ago

@avaidyam ahh understood. Safe to say I pick the wrong side going for lifx rather that Hue or WeMo, having a hard time getting everything setup. Appreciate your help!

avaidyam commented 8 years ago

@robinic I would suggest the opposite! It's more of Home Assistant issue. LIFX has been fabulous for me. If ANYONE can tell me how to show the RGB/K view that Hue bulbs get in HA, I'd be really thankful.

brusc commented 8 years ago

Would love to see LIFX support in HA as well. Not as savvy on the coding side though. Hopefully people will be inspired by new Christmas gifts to get this component implemented. :) I'll see if I can get things working with code described above. In the meantime, I'll try to get things working through IFTTT.

avaidyam commented 8 years ago

It works, but again, no idea how to show the RGB view. It shouldn't be this hard...

balloob commented 8 years ago

Make sure that the property rgb_color returns a valid color for the color picker to show up in the UI.

brusc commented 8 years ago

Thanks for the code, @avaidyam! Will there be formal LIFX support in HA at some point in the future? Not really sure how to go from where this stands now to ultimately LIFX being detected by the discovery component.

avaidyam commented 8 years ago

@balloob @benrusche I'll try to get the RGB selector working. Otherwise, yeah, LIFX more or less works. There seems to be a weird glitch here and there which I can try to fix up.

brusc commented 8 years ago

Sounds great. Thanks for your work on this.

brusc commented 8 years ago

Not to be the squeaky wheel, but I'm wondering how the LIFX component is coming along. I know there has been some demand in the chat from several people and I'm anxious as well to phase out my current IFTTT configuration. I installed @avaidyam 's LIFX API and it works well as a standalone library. I'm a bit confused how to call specific bulbs.

Is there anyway to guess at a timeline for this component? Not so much to rush, but just so I can gauge how much effort I should put into finding other workarounds with HA until then.

avaidyam commented 8 years ago

@benrusche Technically it's already done, with a bug or two. See below:

import lazylights

from colorsys import *
from homeassistant.components.light import (
    Light, ATTR_BRIGHTNESS, ATTR_RGB_COLOR, ATTR_COLOR_TEMP
)

DEPENDENCIES = []
REQUIREMENTS = ['git+https://github.com/avaidyam/lazylights']

def setup_platform(hass, config, add_devices_callback, discovery_info=None):
    """ Find and return LIFX lights. """
    bulbs = lazylights.find_bulbs(expected_bulbs=2)
    add_devices_callback([LIFXLight(n) for n in bulbs])

class LIFXLight(Light):
    """ Provides a LIFX bulb. """
    # pylint: disable=too-many-arguments
    def __init__(self, info):
        self._info = info

    @property
    def should_poll(self):
        return True

    @property
    def name(self):
        """ Returns the name of the device if any. """
        self._info = lazylights.get_state([self._info.bulb])
        return self._info.label

    @property
    def brightness(self):
        """ Brightness of this light between 0..255. """
        self._info = lazylights.get_state([self._info.bulb])
        return int((self._info.brightness / 65536) * 255)

    @property
    def rgb_color(self):
        """ rgb color value. """
        self._info = lazylights.get_state([self._info.bulb])
        return hls_to_rgb(self._info.hue, self._info.saturation, self._info.lightness)

    @property
    def color_temp(self):
        """ CT color temperature. """
        self._info = lazylights.get_state([self._info.bulb])
        return int(1000000 / self._info.kelvin)

    @property
    def is_on(self):
        """ True if device is on. """
        self._info = lazylights.get_state([self._info.bulb])
        return self._info.power == 65536

    def turn_on(self, **kwargs):
        """ Turn the device on. """
        self._info = lazylights.get_state([self._info.bulb])
        lazylights.set_power([self._info.bulb], True)

        bright = 100.0
        if ATTR_BRIGHTNESS in kwargs:
            bright = kwargs[ATTR_BRIGHTNESS]

        if ATTR_RGB_COLOR in kwargs:
            rgb = kwargs[ATTR_RGB_COLOR]
            hsl = hsl_to_rgb(rgb[0], rgb[1], rgb[2])
            hsl[2] = bright

            lazylights.set_state([self._info.bulb], hsl[0], hsl[1], hsl[2], 2000, 1, False)
        elif ATTR_COLOR_TEMP in kwargs:
            kelvin = int(1000000 / kwargs[ATTR_COLOR_TEMP])
            lazylights.set_state([self._info.bulb], 0, 0, bright, kelvin, 1, False)

        self.update_ha_state()

    def turn_off(self, **kwargs):
        """ Turn the device off. """
        self._info = lazylights.get_state([self._info.bulb])
        lazylights.set_power([self._info.bulb], False)
        self.update_ha_state()
brusc commented 8 years ago

Sweet! I attempted to import that into HA, but it is throwing an error in the log about dependencies.

15-12-30 19:17:57 homeassistant.bootstrap: Error during setup of component light
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/util/package.py", line 42, in check_package_exists
    req = pkg_resources.Requirement.parse(package)
  File "/usr/lib/python3/dist-packages/pkg_resources.py", line 2793, in parse
    reqs = list(parse_requirements(s))
  File "/usr/lib/python3/dist-packages/pkg_resources.py", line 2721, in parse_requirements
    "version spec")
  File "/usr/lib/python3/dist-packages/pkg_resources.py", line 2686, in scan_list
    raise ValueError(msg, line, "at", line[p:])
ValueError: ('Expected version spec in', 'git+https://github.com/avaidyam/lazylights', 'at', '+https://github.com/avaidyam/lazylights')

I attempted to follow @balloob's instructions above and changed the dependencies to

REQUIREMENTS = ['https://github.com/avaidyam/lazylights/archive/'
                'master.zip'
                '#lazylights==3.0.0']

but now I'm getting the message

15-12-30 19:13:17 homeassistant.loader: Loaded light.lifx from custom_components.light.lifx
15-12-30 19:13:17 homeassistant.components.light: Error while setting up platform lifx
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/helpers/entity_component.py", line 145, in _setup_platform
    self.hass, platform_config, self.add_entities, discovery_info)
  File "/var/opt/homeassistant/custom_components/light/lifx.py", line 16, in setup_platform
    add_devices_callback([LIFXLight(n) for n in bulbs])
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/helpers/entity_component.py", line 76, in add_entities
    self.entity_id_format, entity.name,
  File "/var/opt/homeassistant/custom_components/light/lifx.py", line 31, in name
    self._info = lazylights.get_state([self._info.bulb])

Any ideas on how to get this thing rolling?

avaidyam commented 8 years ago

I'll make an archive available ASAP.

In case you want it NOW, temporarily comment that line out, and copy the lazylights.py file over next to the lifx.py file. Let me know how it works for you!

Also if you can, multiply all the rgb_color outputs by 255 or something to see if that shows the color picker. I'm not at home with my LIFX bulbs right now.

brusc commented 8 years ago

So I moved lazylights.py next to the lifx.py file. And my requriements line is still

REQUIREMENTS = ['https://github.com/avaidyam/lazylights/archive/'
                'master.zip'
                '#lazylights==3.0.0']

I should comment the requirements line out? The error I'm getting in the log is

15-12-30 19:37:03 homeassistant.components.light: Error while setting up platform lifx
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/helpers/entity_component.py", line 145, in _setup_platform
    self.hass, platform_config, self.add_entities, discovery_info)
  File "/var/opt/homeassistant/custom_components/light/lifx.py", line 16, in setup_platform
    add_devices_callback([LIFXLight(n) for n in bulbs])
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/helpers/entity_component.py", line 76, in add_entities
    self.entity_id_format, entity.name,
  File "/var/opt/homeassistant/custom_components/light/lifx.py", line 31, in name
    self._info = lazylights.get_state([self._info.bulb])
AttributeError: 'Bulb' object has no attribute 'bulb'

Not sure what exactly that means.

avaidyam commented 8 years ago

You should comment out the 3 lines for REQUIREMENTS.

brusc commented 8 years ago

Gotcha. Yep, tried that. Still getting the same error. :(

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/helpers/entity_component.py", line 145, in _setup_platform
    self.hass, platform_config, self.add_entities, discovery_info)
  File "/var/opt/homeassistant/custom_components/light/lifx.py", line 16, in setup_platform
    add_devices_callback([LIFXLight(n) for n in bulbs])
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/helpers/entity_component.py", line 76, in add_entities
    self.entity_id_format, entity.name,
  File "/var/opt/homeassistant/custom_components/light/lifx.py", line 31, in name
    self._info = lazylights.get_state([self._info.bulb])
AttributeError: 'Bulb' object has no attribute 'bulb'
avaidyam commented 8 years ago

Huh, alright. Here's another version I had. Not sure from when, but see if this one works:

import logging
import lazylights

from colorsys import *
from homeassistant.components.light import (
    Light, ATTR_BRIGHTNESS, ATTR_RGB_COLOR)

_LOGGER = logging.getLogger(__name__)

DEPENDENCIES = []
#REQUIREMENTS = ['https://github.com/avaidyam/lazylights/']

def setup_platform(hass, config, add_devices_callback, discovery_info=None):
    """ Find and return LIFX lights. """
    bulbs = lazylights.find_bulbs(timeout=5)
    add_devices_callback([LIFXLight(n) for n in bulbs])

class LIFXLight(Light):
    """ Provides a LIFX bulb. """

    def __init__(self, bulb):
        self._bulb = bulb
        self._info = lazylights.get_state([self._bulb])[0]
        self._name = self._info.label.partition(b'\0')[0].decode('utf-8')

    @property
    def should_poll(self):
        return True

    @property
    def name(self):
        """ Returns the name of the device if any. """
        return self._name

    @property
    def brightness(self):
        """ Brightness of this light between 0..255. """
        return int((self._info.brightness / 65536) * 255)

    @property
    def rgb_color(self):
        """ rgb color value. """
        rgb = hls_to_rgb(self._info.hue, self._info.saturation, self._info.brightness)
        return [float(rgb[0]) * 255.0, float(rgb[1]) * 255.0, float(rgb[2]) * 255.0]

    @property
    def color_temp(self):
        """ CT color temperature. """
        return int(1000000 / self._info.kelvin)

    @property
    def is_on(self):
        """ True if device is on. """
        return self._info.power > 0

    def turn_on(self, **kwargs):
        """ Turn the device on. """
        lazylights.set_power([self._bulb], True)

        bright = 1.0
        if ATTR_BRIGHTNESS in kwargs:
            bright = float(kwargs[ATTR_BRIGHTNESS]) / 255.0

        if ATTR_RGB_COLOR in kwargs:
            rgb = kwargs[ATTR_RGB_COLOR]
            hsl = hsl_to_rgb(rgb[0], rgb[1], rgb[2])
            hsl[2] = bright

            lazylights.set_state([self._bulb], hsl[0], hsl[1], hsl[2], 2000, 1, False)
        elif "color_temp" in kwargs:
            kelvin = int(1000000 / kwargs[ATTR_COLOR_TEMP])
            lazylights.set_state([self._bulb], 0, 0.0, bright, kelvin, 1, False)
        else:
            lazylights.set_state([self._bulb], 0, 0.0, bright, 3000, 1, False)
        self.update_ha_state()

    def turn_off(self, **kwargs):
        """ Turn the device off. """
        lazylights.set_power([self._bulb], False)
        self.update_ha_state()

    def update():
        self._info = lazylights.get_state([self._bulb])[0]

Same instructions for using it. I should probably make this a PR or something.

brusc commented 8 years ago

Yep! That worked great. Looks like the card is showing in HA correctly and can control on off and brightness. No dice on color control though. Thanks for all your help on this! image

avaidyam commented 8 years ago

WHAT!?!? What black magic did you pull off???

avaidyam commented 8 years ago

Like seriously, WTF?? I SPENT A WEEK trying to get that up!!

Did you make zero changes?

brusc commented 8 years ago

Just used the code you gave me. :P I created a lifx.py file in /home assistant/custom_components/light and copied in the code you just gave me. Copied the lazylights.py file next to it. And added

light:
  platform: lifx

so my configuration.yaml file.

avaidyam commented 8 years ago

Damn, alright, will try this when I get home in a few days. That's nuts though, thanks!

brusc commented 8 years ago

Sure thing. Thanks for your help as well! I appreciate the code. Cheers!

avaidyam commented 8 years ago

Welcome @benrusche! If you know a little python, please help fix things up!

brusc commented 8 years ago

Alright, I think I've dorked around with this code enough for one day. This now supports brightness, color temperature, and RGB selection. There is still this error in the log and the state does not appear to be updating predictably.

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/core.py", line 821, in job_handler
    func(arg)
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/core.py", line 675, in _execute_service
    service(call)
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/core.py", line 547, in __call__
    self.func(call)
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/components/light/__init__.py", line 239, in handle_light_service
    light.update_ha_state(True)
  File "/usr/local/lib/python3.4/dist-packages/homeassistant/helpers/entity.py", line 99, in update_ha_state
    self.update()
TypeError: update() takes 0 positional arguments but 1 was given

I made a few changes that were all likely unnecessary. My Python skills are lackluster hence the ugly code. We need to get a pull request ASAP. :+1:

The changes are importing colorsys directly, in def rgb_color and def turn_on. Fun fact, the colorsys commands are rgb_to_hls not rgb_to_hsl. https://docs.python.org/3/library/colorsys.html

import logging
import lazylights
import colorsys

from homeassistant.components.light import (
    Light, ATTR_BRIGHTNESS, ATTR_RGB_COLOR, ATTR_COLOR_TEMP)

_LOGGER = logging.getLogger(__name__)

DEPENDENCIES = []
#REQUIREMENTS = ['https://github.com/avaidyam/lazylights/']

def setup_platform(hass, config, add_devices_callback, discovery_info=None):
    """ Find and return LIFX lights. """
    bulbs = lazylights.find_bulbs(timeout=5)
    add_devices_callback([LIFXLight(n) for n in bulbs])

class LIFXLight(Light):
    """ Provides a LIFX bulb. """

    def __init__(self, bulb):
        self._bulb = bulb
        self._info = lazylights.get_state([self._bulb])[0]
        self._name = self._info.label.partition(b'\0')[0].decode('utf-8')

    @property
    def should_poll(self):
        return True

    @property
    def name(self):
        """ Returns the name of the device if any. """
        return self._name

    @property
    def brightness(self):
        """ Brightness of this light between 0..255. """
        return int((self._info.brightness / 65535) * 255)

    @property
    def rgb_color(self):
        """ rgb color value. """
        h1, l1, s1 = float(self._info.hue / 65535.0), float(self._info.brightness / 65535.0), float(self._info.saturation / 65535.0)
        r1, g1, b1 = colorsys.hls_to_rgb(float(h1), float(l1), float(s1))
        return [float(r1 * 255.0), float(g1 * 255.0), float(b1 * 255.0)]

    @property
    def color_temp(self):
        """ CT color temperature. """
        return int(1000000 / self._info.kelvin)

    @property
    def is_on(self):
        """ True if device is on. """
        return self._info.power > 0

    def turn_on(self, **kwargs):
        """ Turn the device on. """
        lazylights.set_power([self._bulb], True)

        bright = 1.0
        if ATTR_BRIGHTNESS in kwargs:
            bright = float(kwargs[ATTR_BRIGHTNESS]) / 255.0

        if ATTR_RGB_COLOR in kwargs:
            rgb = kwargs[ATTR_RGB_COLOR]
            r2, g2, b2 = float(rgb[0] / 255.0), float(rgb[1] / 255.0), float(rgb[2] /255.0)
            h2, l2, s2 = colorsys.rgb_to_hls(r2, g2, b2)

            lazylights.set_state([self._bulb], h2 * 360, s2, l2, 2000, 1, False)

        elif "color_temp" in kwargs:
            kelvin = int(1000000 / kwargs[ATTR_COLOR_TEMP])
            lazylights.set_state([self._bulb], 0, 0.0, bright, kelvin, 1, False)
        else:
            lazylights.set_state([self._bulb], 0, 0.0, bright, 3000, 1, False)
        self.update_ha_state()

    def turn_off(self, **kwargs):
        """ Turn the device off. """
        lazylights.set_power([self._bulb], False)
        self.update_ha_state()

    def update():
        self._info = lazylights.get_state([self._bulb])[0]

Once we get a pull request, it would be great to add support for fading. Sorry again for the ugly code. Hopefully it can inspire somebody who actually knows what they are doing... :P

avaidyam commented 8 years ago

@benrusche Could it be that I forgot to add self?

def update(self):

might fix it. Can you give it a try? Once you report back I'll create a PR and hopefully it'll be in!

happyleavesaoc commented 8 years ago

Definitely get a PR open for this. Much easier to track changes and comment!

avaidyam commented 8 years ago

Good call, I'll do that anyway.

brusc commented 8 years ago

@avaidyam, adding self to def update worked great! State attributes appear to be updating correctly. Looks like the UI card is fully functional now. Added a few comments to the pull request.

avaidyam commented 8 years ago

Thanks so much for your help! :)

fabaff commented 8 years ago

LIFX support was merged (#930).

aymercury commented 8 years ago

Hi! I tried using LIFX bulbs with Flux component and got wrong colors, while the log was telling me that all is correct. I edited lifx.py in two places, replaced the current color temperature calculation with the following:

place 1

def color_temp(self):
    """Return the color temperature."""
    #temperature = int(TEMP_MIN_HASS + (TEMP_MAX_HASS - TEMP_MIN_HASS) *
    #                  (self._kel - TEMP_MIN) / (TEMP_MAX - TEMP_MIN))
    temperature = int(1000000 / self._kel)

place 2

    if ATTR_COLOR_TEMP in kwargs:
        # pylint: disable=fixme
        # TODO: Use color_temperature_mired_to_kelvin from util.color
        #kelvin = int(((TEMP_MAX - TEMP_MIN) *
        #              (kwargs[ATTR_COLOR_TEMP] - TEMP_MIN_HASS) /
        #              (TEMP_MAX_HASS - TEMP_MIN_HASS)) + TEMP_MIN)
        kelvin = int(1000000/kwargs[ATTR_COLOR_TEMP])

After that (and hass restart) I see the expected colors from the bulbs, and they match what flux tells them to be.

dicko72 commented 8 years ago

Hi, I've tried the flux switch platform tonight on LIFX and dont notice any changes apart from brightness. How do you validate what the lights are reporting in the logs? noob... :(

aymercury commented 8 years ago

I do this (typing from memory): tail -f /var/log/syslog | grep hass | grep -i 'lifx|flux|color_temp'

It shows me what flux requests and what bulbs do. Each bulb reports old and new brightness, rgb, color_temp values.

I have to say, after my two lifx.py changes above (I re-applied them after upgrade to 0.28.2), flux works perfectly!