Skip to content

Commit

Permalink
add disable motion detection
Browse files Browse the repository at this point in the history
  • Loading branch information
awalford16 committed Nov 24, 2024
1 parent 7c1019b commit feac20c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 41 deletions.
20 changes: 20 additions & 0 deletions .deploy/docker-services/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: '2'
services:
homebridge:
image: oznu/homebridge
restart: always
network_mode: "host"
ports:
- 8181:8080
volumes:
- /exports/homebridge:/homebridge
environment:
- PUID=995
- PGID=100
- HOMEBRIDGE_CONFIG_UI=1
- HOMEBRIDGE_CONFIG_UI_PORT=8181
logging:
driver: json-file
options:
max-size: "10mb"
max-file: "1"
1 change: 1 addition & 0 deletions src/office-lights/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MQTT_SERVER="192.168.1.104"
23 changes: 17 additions & 6 deletions src/office-lights/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,33 @@
import time

hue = PhillipsHue()
IS_DISABLED = False


# Callback when a message is received from the broker
def on_message(client, userdata, message):
global IS_DISABLED
state = message.payload.decode()
print(f"Setting state to {state}")

if state == "OFF":
# Disable/enable office lights
if state == "DISABLE" or state == "ENABLE":
hue.change_light_state(Groups.OFFICE, False)
IS_DISABLED = state == "DISABLE"
print(f"Motion Disabled: {IS_DISABLED}")
return

if not hasattr(States, state):
print("Invalid State, will default to FOCUS")
state = "FOCUS"
if not IS_DISABLED:
print(f"Setting state to {state}")

hue.change_light_state(Groups.OFFICE, True, States[state])
if state == "OFF":
hue.change_light_state(Groups.OFFICE, False)
return

if not hasattr(States, state):
print("Invalid State, will default to FOCUS")
state = "FOCUS"

hue.change_light_state(Groups.OFFICE, True, States[state])


if __name__ == "__main__":
Expand Down
58 changes: 56 additions & 2 deletions src/office-lights/mqtt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import paho.mqtt.client as mqtt
from constants import MQTT_SERVER
import os
import sys

class MQTT():

class MQTT:
def __init__(self, on_message):
# Create an MQTT client instance
self.client = mqtt.Client()
Expand All @@ -12,7 +15,9 @@ def __init__(self, on_message):

# Set the broker address and port
if "MQTT_ADDRESS" not in os.environ:
raise EnvironmentError(f"The environment variable 'MQTT_ADDRESS' is not set.")
raise EnvironmentError(
f"The environment variable 'MQTT_ADDRESS' is not set."
)

broker_address = os.environ.get("MQTT_ADDRESS")
port = 1883 # Default MQTT port
Expand All @@ -30,3 +35,52 @@ def on_connect(self, client, userdata, flags, rc):
else:
print("Connection failed with code", rc)


#########################
# TESTING FUNCTIONALITY #
#########################
def test_on_publish(client, userdata, mid):
print("Message published")


def test_on_message(client, userdata, msg):
print(msg.topic + " " + str(msg.payload))


TEST_TOPIC = "office/lights"

if __name__ == "__main__":
# Arg parser to run tester
# Callback when the client publishes a message

# Create an MQTT client instance
client = mqtt.Client()

# Set the callback for publishing
client.on_publish = test_on_publish
client.on_message = test_on_message

# Set the broker address and port
port = 1883 # Default MQTT port

# Connect to the broker
client.connect(MQTT_SERVER, port)

if sys.argv[1] == "publish":
# Start the network loop to process outgoing messages
client.loop_start()

# Publish a message to a topic
message = "OFF" # Your message here
# client.publish(topic, message)
# client.publish(topic, "ALERT")
# client.publish(topic, message)
# client.publish(topic, "INVALID")
client.publish(TEST_TOPIC, message)

# Wait for a moment to allow the message to be sent
client.loop_stop()
else:
client.subscribe(TEST_TOPIC)
# Start the network loop to process outgoing messages
client.loop_forever()
33 changes: 0 additions & 33 deletions src/office-lights/pub.py

This file was deleted.

0 comments on commit feac20c

Please sign in to comment.