Skip to content

Commit

Permalink
pain
Browse files Browse the repository at this point in the history
  • Loading branch information
Octelly committed Dec 7, 2024
1 parent 70fe0bc commit cba34d6
Show file tree
Hide file tree
Showing 3 changed files with 308 additions and 36 deletions.
53 changes: 46 additions & 7 deletions packages/misc/mqtt_linux/mqtt_linux/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,49 @@
from monitorcontrol import get_monitors
import json
from ha_mqtt_discoverable import Settings, DeviceInfo
from ha_mqtt_discoverable.sensors import Light, LightInfo
from paho.mqtt.client import Client, MQTTMessage

# Configure the required parameters for the MQTT broker
mqtt_settings = Settings.MQTT(host = "192.168.1.103")

device_info = DeviceInfo(name="desktop", identifiers="foo1bar2")

# Information about the light
light_info = LightInfo(
name="brightness",
unique_id="bar3foo4",
device=device_info,
brightness=True,
)

settings = Settings(mqtt=mqtt_settings, entity=light_info)

# To receive state commands from HA, define a callback function:
def my_callback(client: Client, user_data, message: MQTTMessage):

# Make sure received payload is json
try:
payload = json.loads(message.payload.decode())
except ValueError as error:
print("Ony JSON schema is supported for light entities!")
return

# Parse received dictionary
if "brightness" in payload:
print(payload["brightness"])
my_light.brightness(payload["brightness"])
elif "state" in payload:
if payload["state"] == light_info.payload_on:
print("on")
my_light.on()
else:
print("off")
my_light.off()
else:
print("Unknown payload")


my_light = Light(settings, my_callback)

def app() -> None:
for monitor in get_monitors():
with monitor:
try:
print(monitor.get_luminance())
except Exception as e:
print(e)
my_light.off()
Loading

0 comments on commit cba34d6

Please sign in to comment.