Lightify plugin/script

Got a good idea? You can suggest new features here.
Post Reply
holdestmade
Experienced User
Posts: 174
Joined: Thu Dec 04, 2014 2:44 pm

Lightify plugin/script

Post by holdestmade » Sun Nov 25, 2018 2:00 pm

Hi,

I am currently trying out some Osram lightify bulbs as got them on special offer.

I've managed to find a python module (https://github.com/tfriedel/python-lightify) and it loads into EG fine and I can control them and query the status of them with EG via python script.

What would be great is a plugin or maybe a start up script (as you did for me for Yeelight @kgschlosser) so I can get events in the log whenever the bulbs status changes.

Discovery is different in Lightify to Yeelight though
kgschlosser wrote:
Fri Dec 29, 2017 9:32 pm
heh ooops.

Code: Select all

import threading


class Yeelight(object):
    def __init__(self):
        # this is where you want to add the bulb entries. it has to be laid out
        # exactly like you see below
        self.bulbs = {
            'LivingRoom.Light': {
                'ip':            '192.168.1.212',
                'current_state': {}
            },
            # example addition
            # 'SomeNew.Light': {
            #     'ip':            '192.168.1.1',
            #     'current_state': {}
            # },
        }
        self._event = None
        self._thread = None

    def start(self):
        if self._thread is None:
            self._event = threading.Event()
            self._thread = threading.Thread(target=self.run)
            self._thread.daemon = True
            self._thread.start()

    def stop(self):
        if self._event is not None:
            self._event.set()
            self._thread.join(3.0)

    def run(self):
        from yeelight import Bulb

        for bulb_name, bulb_params in self.bulbs.items():
            try:
                bulb = Bulb(bulb_params['ip'])
                bulb_params['current_state'] = bulb.get_properties()
            except:
                eg.PrintError(
                    'Yeelight bulb {0} is not connecting on ip {1}.'.format(
                        bulb_name,
                        bulb_params['ip']
                    )
                )
                eg.PrintError('Removing bulb from being polled')
                del self.bulbs[bulb_name]

        while not self._event.isSet():
            for bulb_name, bulb_params in self.bulbs.items():
                try:
                    bulb = Bulb(bulb_params['ip'])
                    current_state = bulb.get_properties()
                    for key, value in current_state.items():
                        if (
                            key not in bulb_params['current_state'] or
                            value != bulb_params['current_state'][key]
                        ):
                            eg.TriggerEvent(
                                prefix='Yeelight',
                                suffix='{0}.{1}.{2}'.format(
                                    bulb_name,
                                    key,
                                    value
                                ),
                                payload=value
                            )
                            bulb_params['current_state'][key] = value
                except:
                    eg.PrintError(
                        'Yeelight bulb {0} is not connecting on ip {1}.'.format(
                            bulb_name,
                            bulb_params['ip']
                        )
                    )
                    eg.PrintError('Removing bulb from being polled')
                    del self.bulbs[bulb_name]
            self._event.wait(0.15)

        self._event = None
        self._thread = None

    def send(self, name, **params):
        # i do not know how to change states or whatever else
        # if you provide me this information i can add it here
        pass

    def get(self, name, params=None):
        if params is None:
            from copy import deepcopy
            return deepcopy(self.bulbs[name]['current_state'])

        if isinstance(params, (list, tuple)):
            res = {}
            bulb = self.bulbs[name]
            for key in params:
                if key in bulb['current_state']:
                    res[key] = bulb['current_state'][key]
                else:
                    res[key] = None
            return res
        else:
            if param in self.bulbs[name]['current_state']:
                return self.bulbs[name]['current_state'][param]

eg.globals.yeelight = Yeelight()
eg.globals.yeelight.start()

# if for some reason you want to stop the polling you need to call
# eg.globals.yeelight.stop()

# when you get me the information on how to send commands to the bulbs
# it would get used by calling send
# eg.globals.yeelight.send('LivingRoom.Light', command1=None, command2=None)

# the thread is set up as a daemon thread so it will close all nice nice when
# EG exits.

# you can use the below mechanism to grab current status data

# to get all statuses returned in a dict
# eg.globals.yeelight.get('LivingRoom.Light')

# to get a single status
# eg.globals.yeelight.get('LivingRoom.Light', 'power')

# to grab more then one status returned as a dict
# eg.globals.yeelight.get('LivingRoom.Light', ['power', 'bright'])

# if at any time there is no value for the status provided a 
# None will be returned for that status


Thanks in advance

User avatar
kgschlosser
Site Admin
Posts: 4313
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Lightify plugin/script

Post by kgschlosser » Sun Nov 25, 2018 8:33 pm

if you have enough knowledge to get it running in a script I think you should be able to write a plugin. I can walk you through it if ya like.

How I learned.. I downloaded a plugin that used the same type of connection specific bits that you need and modify it. I think you have enough knowledge to be able to do this.
If you like the work I have been doing then feel free to Image

holdestmade
Experienced User
Posts: 174
Joined: Thu Dec 04, 2014 2:44 pm

Re: Lightify plugin/script

Post by holdestmade » Mon Nov 26, 2018 9:44 am

Yes you're probably right, I'll give it a go

Get ready for the questions......

User avatar
kgschlosser
Site Admin
Posts: 4313
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Lightify plugin/script

Post by kgschlosser » Mon Nov 26, 2018 4:49 pm

no worries m8. I think you should be able to do it. If you have any questions google and I are your friends.
If you like the work I have been doing then feel free to Image

holdestmade
Experienced User
Posts: 174
Joined: Thu Dec 04, 2014 2:44 pm

Re: Lightify plugin/script

Post by holdestmade » Mon Nov 26, 2018 5:42 pm

I have it basically running, ie creating events whenever a bulb's status changes but its all long winded and uses repeated code for each bulb.

I just need to combine them and do a discovery to autopopulate the names etc and I should be good to go.

User avatar
kgschlosser
Site Admin
Posts: 4313
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Lightify plugin/script

Post by kgschlosser » Tue Nov 27, 2018 12:34 am

send it to me in a PM
If you like the work I have been doing then feel free to Image

holdestmade
Experienced User
Posts: 174
Joined: Thu Dec 04, 2014 2:44 pm

Re: Lightify plugin/script

Post by holdestmade » Tue Nov 27, 2018 11:33 am

Sent you a PM, thanks in advance

Post Reply