mclarkk / lifxlan

Python library for accessing LIFX devices locally using the official LIFX LAN protocol.
MIT License
507 stars 116 forks source link

Make any light switch 'smart' with lifxlan #143

Open eidermar opened 4 years ago

eidermar commented 4 years ago

This is not really any issue but rather a call for help.

I'm completely frustrated with the state of available smart switches which are either privacy invasive, overly expensive and/or over-engineered for the simple task I want to do: Toggle my lights from a simple and cheap switch. I did use some dash buttons in the past but they are not available anymore and have some other downsides as well.

So I had an idea. Basically, as soon as a LIFX bulb is connected to a regular dumb power/light switch, this switch is already a smart switch. I only have to be able to monitor the state (in this case power state) of this one bulb via lifxlan protocol (on/off) and apply the same power state to all the other bulbs on my network. Voila, a simple and free 'smart' switch that is independent of phones, apps etc. and every guest is able to use without any further knowledge.

My problem is that although I did write some very simple scripts with lifxlan this is a little over my head. I would need some help to piece this together into a functional script. Ideally something that can run as a daemon (background task) on my Mac that is always on anyways.

Any help on how to achieve this would be very much appreciated!!!

eidermar commented 4 years ago

I was thinking of something like this:

import os
from lifxlan import Light, LifxLAN

#ping for bulb...
hostname = "192.168.0.xx" 
response = os.system("ping -c 1 -W 500 " + hostname)

#Check the response and set home status
if response == 0:
    HomeStatus = Home
else:
    HomeStatus = Away

  #Start the Loop
  while(not sleep(5)): #should allow to run as endless loop
    hostname = "192.168.0.xx" 
    response = os.system("ping -c 1 -W 500 " + hostname)

    #If bulb is detected by ping and home status was previuosly "away", turn on all lights with specific parameters
    if response == 0 and HomeStatus == Away:
        lifxlan.set_brightness_all_lights (0.55)
        lifxlan.set_colortemp_all_lights (2750)
        lifxlan.set_power_all_lights ("on")
        HomeStatus = Home

    #If bulb is not detected and home status was previously "home", turn off all lights
    elif response != 0 and HomeStatus == Home:
        lifxlan.set_power_all_lights ("off")
        HomeStatus = Away

    #For all other cases no action is needed and loop should continue scanning for changes in bulb availability

Does this make any sense? Is pinging the bulb a bad idea/practice? As I work with the power switch I can obviously not work with the power on/off value to determine the state of the bulb. (cannot test the code as I'm on a trip abroad right now)

mclarkk commented 3 years ago

This is a clever idea, and I'm interested to see how it went! I can think of a couple things that might be annoying:

  1. The lights occasionally disconnect from WiFi or discovery fails, at which point all your stuff would turn off temporarily.
  2. Upon receiving power it takes a while for the light to boot and reconnected to WiFi, so there would be some lag. Could be okay though depending on your needs.

How did it go?

davidcfk commented 3 years ago

Hmmm... but the problem is the physical switch would also be out of sync with the smart lights if you controlled it from your phone for whatever reason via another integration... There's just cheaper ways to build a controller I think that doesn't involve messing around with home circuitry when using something like lifxlan integrated into HASSIO or something.

mfarley281 commented 3 years ago

I used to use Amazon Dash buttons for home automation. We had ~20 around the house. About a year ago Amazon flipped some kind of remote kill switch and they all stopped work. Then I switched to: https://flic.io/ .. the only downside is you need a hub in your house somewhere for them to talk to. The upside is that there is almost no lag compared to the 3-5 seconds of lag on the Amazon Dash. They're super simple to setup and support hitting a REST URL on press.

davidcfk commented 3 years ago

flic2 is definitely the "in" thing right now!