Refactor TemperatureMonitor: clean up comments and improve readability in temperature reporting logic

This commit is contained in:
Aaron 2025-11-04 19:07:57 -05:00
parent 0ceeb3ba41
commit c4593caf9f

View File

@ -28,8 +28,8 @@ class TemperatureMonitor(Monitor):
def __init__(self, pin=10, interval=300, alert_high=None, alert_low=None): def __init__(self, pin=10, interval=300, alert_high=None, alert_low=None):
super().__init__(interval) super().__init__(interval)
self.sensor = TemperatureSensor(pin=pin) self.sensor = TemperatureSensor(pin=pin)
self.alert_high = alert_high # Alert if temp goes above this self.alert_high = alert_high
self.alert_low = alert_low # Alert if temp goes below this self.alert_low = alert_low
def run(self): def run(self):
"""Read all sensors and report temperatures.""" """Read all sensors and report temperatures."""
@ -38,7 +38,6 @@ class TemperatureMonitor(Monitor):
print("No temperature readings available") print("No temperature readings available")
return return
# Format message
temp_msg = "🌡️ Temperature readings:\n" temp_msg = "🌡️ Temperature readings:\n"
alerts = [] alerts = []
@ -46,16 +45,13 @@ class TemperatureMonitor(Monitor):
sensor_id = rom.hex()[:8] sensor_id = rom.hex()[:8]
temp_msg += f"Sensor {sensor_id}: {temp:.1f}°F\n" temp_msg += f"Sensor {sensor_id}: {temp:.1f}°F\n"
# Check alerts
if self.alert_high and temp > self.alert_high: if self.alert_high and temp > self.alert_high:
alerts.append(f"⚠️ Sensor {sensor_id} HIGH: {temp:.1f}°F (threshold: {self.alert_high}°F)") alerts.append(f"⚠️ Sensor {sensor_id} HIGH: {temp:.1f}°F (threshold: {self.alert_high}°F)")
if self.alert_low and temp < self.alert_low: if self.alert_low and temp < self.alert_low:
alerts.append(f"⚠️ Sensor {sensor_id} LOW: {temp:.1f}°F (threshold: {self.alert_low}°F)") alerts.append(f"⚠️ Sensor {sensor_id} LOW: {temp:.1f}°F (threshold: {self.alert_low}°F)")
# Send regular update
send_discord_message(temp_msg.strip()) send_discord_message(temp_msg.strip())
# Send alerts separately
for alert in alerts: for alert in alerts:
send_discord_message(alert) send_discord_message(alert)