Update and Lint FunHouse Home Assistant code to work with settings.toml
This commit is contained in:
parent
441221704e
commit
dd205f6bb3
2 changed files with 25 additions and 45 deletions
|
|
@ -1,7 +0,0 @@
|
||||||
FunHouse_HA_Companion/code.py 72: Using the global statement (global-statement)
|
|
||||||
FunHouse_HA_Companion/code.py 90: Unused argument 'userdata' (unused-argument)
|
|
||||||
FunHouse_HA_Companion/code.py 90: Unused argument 'result' (unused-argument)
|
|
||||||
FunHouse_HA_Companion/code.py 90: Unused argument 'payload' (unused-argument)
|
|
||||||
FunHouse_HA_Companion/code.py 97: Unused argument 'client' (unused-argument)
|
|
||||||
FunHouse_HA_Companion/code.py 102: Unused argument 'client' (unused-argument)
|
|
||||||
FunHouse_HA_Companion/code.py 120: Redefining name 'output' from outer scope (line 172) (redefined-outer-name)
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
# SPDX-FileCopyrightText: Copyright (c) 2021 Melissa LeBlanc-Williams for Adafruit Industries
|
# SPDX-FileCopyrightText: Copyright (c) 2021 Melissa LeBlanc-Williams for Adafruit Industries
|
||||||
#
|
#
|
||||||
# SPDX-License-Identifier: MIT
|
# SPDX-License-Identifier: MIT
|
||||||
|
import os
|
||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
from adafruit_display_shapes.circle import Circle
|
from adafruit_display_shapes.circle import Circle
|
||||||
|
|
@ -16,12 +17,6 @@ LIGHT_COMMAND_TOPIC = "funhouse/light/set"
|
||||||
INITIAL_LIGHT_COLOR = 0x008000
|
INITIAL_LIGHT_COLOR = 0x008000
|
||||||
USE_FAHRENHEIT = True
|
USE_FAHRENHEIT = True
|
||||||
|
|
||||||
try:
|
|
||||||
from secrets import secrets
|
|
||||||
except ImportError:
|
|
||||||
print("WiFi secrets are kept in secrets.py, please add them there!")
|
|
||||||
raise
|
|
||||||
|
|
||||||
funhouse = FunHouse(default_bg=0x0F0F00)
|
funhouse = FunHouse(default_bg=0x0F0F00)
|
||||||
funhouse.peripherals.dotstars.fill(INITIAL_LIGHT_COLOR)
|
funhouse.peripherals.dotstars.fill(INITIAL_LIGHT_COLOR)
|
||||||
|
|
||||||
|
|
@ -73,38 +68,19 @@ funhouse.display.show(funhouse.splash)
|
||||||
status = Circle(229, 10, 10, fill=0xFF0000, outline=0x880000)
|
status = Circle(229, 10, 10, fill=0xFF0000, outline=0x880000)
|
||||||
funhouse.splash.append(status)
|
funhouse.splash.append(status)
|
||||||
|
|
||||||
def update_enviro():
|
def connected(client, _userdata, _result, _payload):
|
||||||
global environment
|
|
||||||
|
|
||||||
temp = funhouse.peripherals.temperature
|
|
||||||
unit = "C"
|
|
||||||
if USE_FAHRENHEIT:
|
|
||||||
temp = temp * (9 / 5) + 32
|
|
||||||
unit = "F"
|
|
||||||
|
|
||||||
environment["temperature"] = temp
|
|
||||||
environment["pressure"] = funhouse.peripherals.pressure
|
|
||||||
environment["humidity"] = funhouse.peripherals.relative_humidity
|
|
||||||
environment["light"] = funhouse.peripherals.light
|
|
||||||
|
|
||||||
funhouse.set_text("{:.1f}{}".format(environment["temperature"], unit), temp_label)
|
|
||||||
funhouse.set_text("{:.1f}%".format(environment["humidity"]), hum_label)
|
|
||||||
funhouse.set_text("{}hPa".format(environment["pressure"]), pres_label)
|
|
||||||
|
|
||||||
|
|
||||||
def connected(client, userdata, result, payload):
|
|
||||||
status.fill = 0x00FF00
|
status.fill = 0x00FF00
|
||||||
status.outline = 0x008800
|
status.outline = 0x008800
|
||||||
print("Connected to MQTT! Subscribing...")
|
print("Connected to MQTT! Subscribing...")
|
||||||
client.subscribe(LIGHT_COMMAND_TOPIC)
|
client.subscribe(LIGHT_COMMAND_TOPIC)
|
||||||
|
|
||||||
|
|
||||||
def disconnected(client):
|
def disconnected(_client):
|
||||||
status.fill = 0xFF0000
|
status.fill = 0xFF0000
|
||||||
status.outline = 0x880000
|
status.outline = 0x880000
|
||||||
|
|
||||||
|
|
||||||
def message(client, topic, payload):
|
def message(_client, topic, payload):
|
||||||
print("Topic {0} received new value: {1}".format(topic, payload))
|
print("Topic {0} received new value: {1}".format(topic, payload))
|
||||||
if topic == LIGHT_COMMAND_TOPIC:
|
if topic == LIGHT_COMMAND_TOPIC:
|
||||||
settings = json.loads(payload)
|
settings = json.loads(payload)
|
||||||
|
|
@ -122,29 +98,28 @@ def message(client, topic, payload):
|
||||||
|
|
||||||
def publish_light_state():
|
def publish_light_state():
|
||||||
funhouse.peripherals.led = True
|
funhouse.peripherals.led = True
|
||||||
output = {
|
publish_output = {
|
||||||
"brightness": round(funhouse.peripherals.dotstars.brightness * 255),
|
"brightness": round(funhouse.peripherals.dotstars.brightness * 255),
|
||||||
"state": "on" if funhouse.peripherals.dotstars.brightness > 0 else "off",
|
"state": "on" if funhouse.peripherals.dotstars.brightness > 0 else "off",
|
||||||
"color": funhouse.peripherals.dotstars[0],
|
"color": funhouse.peripherals.dotstars[0],
|
||||||
}
|
}
|
||||||
# Publish the Dotstar State
|
# Publish the Dotstar State
|
||||||
print("Publishing to {}".format(LIGHT_STATE_TOPIC))
|
print("Publishing to {}".format(LIGHT_STATE_TOPIC))
|
||||||
funhouse.network.mqtt_publish(LIGHT_STATE_TOPIC, json.dumps(output))
|
funhouse.network.mqtt_publish(LIGHT_STATE_TOPIC, json.dumps(publish_output))
|
||||||
funhouse.peripherals.led = False
|
funhouse.peripherals.led = False
|
||||||
|
|
||||||
|
|
||||||
# Initialize a new MQTT Client object
|
# Initialize a new MQTT Client object
|
||||||
funhouse.network.init_mqtt(
|
funhouse.network.init_mqtt(
|
||||||
secrets["mqtt_broker"],
|
os.getenv("MQTT_BROKER"),
|
||||||
secrets["mqtt_port"],
|
os.getenv("MQTT_PORT"),
|
||||||
secrets["mqtt_username"],
|
os.getenv("MQTT_USERNAME"),
|
||||||
secrets["mqtt_password"],
|
os.getenv("MQTT_PASSWORD"),
|
||||||
)
|
)
|
||||||
funhouse.network.on_mqtt_connect = connected
|
funhouse.network.on_mqtt_connect = connected
|
||||||
funhouse.network.on_mqtt_disconnect = disconnected
|
funhouse.network.on_mqtt_disconnect = disconnected
|
||||||
funhouse.network.on_mqtt_message = message
|
funhouse.network.on_mqtt_message = message
|
||||||
|
|
||||||
print("Attempting to connect to {}".format(secrets["mqtt_broker"]))
|
print("Attempting to connect to {}".format(os.getenv("MQTT_BROKER")))
|
||||||
funhouse.network.mqtt_connect()
|
funhouse.network.mqtt_connect()
|
||||||
|
|
||||||
last_publish_timestamp = None
|
last_publish_timestamp = None
|
||||||
|
|
@ -162,7 +137,6 @@ if ENABLE_PIR:
|
||||||
last_peripheral_state["pir_sensor"] = funhouse.peripherals.pir_sensor
|
last_peripheral_state["pir_sensor"] = funhouse.peripherals.pir_sensor
|
||||||
|
|
||||||
environment = {}
|
environment = {}
|
||||||
update_enviro()
|
|
||||||
last_environment_timestamp = time.monotonic()
|
last_environment_timestamp = time.monotonic()
|
||||||
|
|
||||||
# Provide Initial light state
|
# Provide Initial light state
|
||||||
|
|
@ -172,7 +146,20 @@ while True:
|
||||||
if not environment or (
|
if not environment or (
|
||||||
time.monotonic() - last_environment_timestamp > ENVIRONMENT_CHECK_DELAY
|
time.monotonic() - last_environment_timestamp > ENVIRONMENT_CHECK_DELAY
|
||||||
):
|
):
|
||||||
update_enviro()
|
temp = funhouse.peripherals.temperature
|
||||||
|
unit = "C"
|
||||||
|
if USE_FAHRENHEIT:
|
||||||
|
temp = temp * (9 / 5) + 32
|
||||||
|
unit = "F"
|
||||||
|
|
||||||
|
environment["temperature"] = temp
|
||||||
|
environment["pressure"] = funhouse.peripherals.pressure
|
||||||
|
environment["humidity"] = funhouse.peripherals.relative_humidity
|
||||||
|
environment["light"] = funhouse.peripherals.light
|
||||||
|
|
||||||
|
funhouse.set_text("{:.1f}{}".format(environment["temperature"], unit), temp_label)
|
||||||
|
funhouse.set_text("{:.1f}%".format(environment["humidity"]), hum_label)
|
||||||
|
funhouse.set_text("{}hPa".format(environment["pressure"]), pres_label)
|
||||||
last_environment_timestamp = time.monotonic()
|
last_environment_timestamp = time.monotonic()
|
||||||
output = environment
|
output = environment
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue