Skip to content

Commit

Permalink
Added support for "temperature", "group", "light" and "device power" …
Browse files Browse the repository at this point in the history
…events

+ Added name of item to event message

(fixes: Receive Hue bridge group events #2)
  • Loading branch information
marcelschreiner committed Mar 17, 2024
1 parent eb6f5ae commit 956f3ee
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 21 deletions.
5 changes: 3 additions & 2 deletions get_api_key.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""A python script to get a hue api key from a Philips Hue bridge."""

import asyncio
from aiohue import create_app_key # pylint: disable=import-error
from aiohue import create_app_key # pylint: disable=import-error

# Insert the ip address of you Philips Hue bridge
HUE_IP = "192.168.1.123"
Expand All @@ -16,7 +17,7 @@ async def main():
api_key = await create_app_key(HUE_IP, "authentication_example")
print("Authentication succeeded, api key: ", api_key)
print("NOTE: Add hue_app_key in main.py for next connections, it does not expire.")
except Exception as exc: # pylint: disable=broad-exception-caught
except Exception as exc: # pylint: disable=broad-exception-caught
print("ERROR: ", str(exc))


Expand Down
78 changes: 59 additions & 19 deletions hue2lox.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""A python script to send Philips Hue accessory events
(Dimmer switch, motion sensor, ...) to a Loxone Miniserver."""

import asyncio
import requests
import socket
from aiohue import HueBridgeV2 # pylint: disable=import-error
from aiohue import HueBridgeV2 # pylint: disable=import-error

# Insert the ip address and API key of you Philips Hue bridge
HUE_IP = "192.168.1.123"
Expand All @@ -12,55 +14,93 @@
LOXONE_IP = "192.168.1.234"
LOXONE_UDP_PORT = 1234

# Global variable to store the names of the lights and sensors
# DO NOT CHANGE THIS VARIABLE
names = {}


def parse_event(event_type, item):
"""Parse Philips hue events"""
print("received event", event_type.value, item)
sensor_state = 0 # pylint: disable=unused-variable
sensor_id = ""
# print("received event", event_type.value, item)
state = 0 # pylint: disable=unused-variable
id = ""

if item.type.name == "BUTTON":
if item.button.last_event.value in ["initial_press", "repeat"]:
sensor_state = 1
state = 1
elif item.button.last_event.value in ["short_release", "long_release"]:
sensor_state = 0
state = 0
else:
# Leave function early
print("EVENT: unknown button event")
return
sensor_state = f"{item.metadata.control_id}/{sensor_state}"
sensor_id = item.id_v1
state = f"{item.metadata.control_id}/{state}"
id = item.id_v1

elif item.type.name == "MOTION":
if item.motion.motion:
sensor_state = 1
else:
sensor_state = 0
sensor_id = item.id_v1
state = int(item.motion.motion)
id = item.id_v1

elif item.type.name == "LIGHT_LEVEL":
sensor_state = item.light.light_level
sensor_id = item.id_v1
state = item.light.light_level
id = item.id_v1

elif item.type.name == "TEMPERATURE":
state = item.temperature.temperature
id = item.id_v1

elif item.type.name == "GROUPED_LIGHT":
state = int(item.on.on)
id = item.id_v1

elif item.type.name == "LIGHT":
state = int(item.is_on)
id = item.id_v1

elif item.type.name == "DEVICE_POWER":
state = item.power_state.battery_level
id = item.id_v1

# else:
# print("received event", event_type.value, item)
if id != "":
try:
event = f"hue_event{id}/{state} # {names[id]}"
except KeyError:
# If the name is not found, do not send include it in the event
event = f"hue_event{id}/{state}"

if sensor_id != "":
event = f"hue_event{sensor_id}/{sensor_state}"
print(f"EVENT: {event}")
# Send UDP packet to Loxone Miniserver
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(bytes(event, "utf-8"), (LOXONE_IP, LOXONE_UDP_PORT))


def get_names():
"""Get the names of the lights groups and sensors"""
url = f"http://{HUE_IP}/api/{HUE_API_KEY}"
response = requests.get(url)
data = response.json()

for type in ["lights", "groups", "sensors"]:
for key, value in data[type].items():
names[f"/{type}/{key}"] = value["name"]

# Special addition for the group "0" which is all lights
names["/groups/0"] = "All lights"


async def main():
"""Main application"""
async with HueBridgeV2(HUE_IP, HUE_API_KEY) as bridge:
print("Connected to bridge: ", bridge.bridge_id)
print("Getting light names...")
get_names()
print("Subscribing to events...")
bridge.subscribe(parse_event)
while True:
await asyncio.sleep(3600)
get_names()


try:
asyncio.run(main())
except KeyboardInterrupt:
Expand Down

0 comments on commit 956f3ee

Please sign in to comment.