-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
308 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.