Skip to content

Commit 545c236

Browse files
authored
Merge pull request #1507 from makermelissa/master
FunHouse Companion guide changes
2 parents 1d2dab6 + 85b5057 commit 545c236

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

FunHouse_HA_Companion/code.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from adafruit_funhouse import FunHouse
1212

1313
PUBLISH_DELAY = 60
14-
ENVIRONMENT_CHECK_DELAY = 1
14+
ENVIRONMENT_CHECK_DELAY = 5
1515
ENABLE_PIR = True
1616
MQTT_TOPIC = "funhouse/state"
1717
LIGHT_STATE_TOPIC = "funhouse/light/state"
@@ -78,17 +78,19 @@
7878

7979
def update_enviro():
8080
global environment
81-
environment["temperature"] = aht20.temperature
82-
environment["pressure"] = dps310.pressure
83-
environment["humidity"] = aht20.relative_humidity
84-
environment["light"] = funhouse.peripherals.light
8581

86-
temp = environment["temperature"]
82+
temp = aht20.temperature
8783
unit = "C"
8884
if USE_FAHRENHEIT:
8985
temp = temp * (9 / 5) + 32
9086
unit = "F"
91-
funhouse.set_text("{:.1f}{}".format(temp, unit), temp_label)
87+
88+
environment["temperature"] = temp
89+
environment["pressure"] = dps310.pressure
90+
environment["humidity"] = aht20.relative_humidity
91+
environment["light"] = funhouse.peripherals.light
92+
93+
funhouse.set_text("{:.1f}{}".format(environment["temperature"], unit), temp_label)
9294
funhouse.set_text("{:.1f}%".format(environment["humidity"]), hum_label)
9395
funhouse.set_text("{}kPa".format(environment["light"]), pres_label)
9496

@@ -109,7 +111,6 @@ def message(client, topic, payload):
109111
print("Topic {0} received new value: {1}".format(topic, payload))
110112
if topic == LIGHT_COMMAND_TOPIC:
111113
settings = json.loads(payload)
112-
print(settings)
113114
if settings["state"] == "on":
114115
if "brightness" in settings:
115116
funhouse.peripherals.dotstars.brightness = settings["brightness"] / 255
@@ -125,7 +126,7 @@ def message(client, topic, payload):
125126
def publish_light_state():
126127
funhouse.peripherals.led = True
127128
output = {
128-
"brightness": funhouse.peripherals.dotstars.brightness * 255,
129+
"brightness": round(funhouse.peripherals.dotstars.brightness * 255),
129130
"state": "on" if funhouse.peripherals.dotstars.brightness > 0 else "off",
130131
"color": funhouse.peripherals.dotstars[0],
131132
}
@@ -148,6 +149,7 @@ def publish_light_state():
148149

149150
print("Attempting to connect to {}".format(secrets["mqtt_broker"]))
150151
funhouse.network.mqtt_connect()
152+
151153
last_publish_timestamp = None
152154

153155
last_peripheral_state = {
@@ -187,16 +189,16 @@ def publish_light_state():
187189

188190
if funhouse.peripherals.slider is not None:
189191
output["slider"] = funhouse.peripherals.slider
192+
peripheral_state_changed = True
190193

191-
# every PUBLISH_DELAY, write temp/hum/press
194+
# Every PUBLISH_DELAY, write temp/hum/press/light or if a peripheral changed
192195
if (
193196
last_publish_timestamp is None
194197
or peripheral_state_changed
195198
or (time.monotonic() - last_publish_timestamp) > PUBLISH_DELAY
196199
):
197-
print("Sending data to MQTT!")
198200
funhouse.peripherals.led = True
199-
print("Publishing to %s" % MQTT_TOPIC)
201+
print("Publishing to {}".format(MQTT_TOPIC))
200202
funhouse.network.mqtt_publish(MQTT_TOPIC, json.dumps(output))
201203
funhouse.peripherals.led = False
202204
last_publish_timestamp = time.monotonic()

0 commit comments

Comments
 (0)