diff --git a/Dockerfile b/Dockerfile
index 8ff8d3d..2b282aa 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -21,6 +21,7 @@ ENV MQTT_SERVER 127.0.0.1
ENV MQTT_PORT 1883
ENV THERMOSTAT_SERIAL XXXXXXXXXX
ENV THERMOSTAT_NAME Thermostat
+ENV LOG_LEVEL DEBUG
COPY ./thermostat_api_server.py /usr/bin/thermostat_api_server.py
diff --git a/README.md b/README.md
index fec773f..abf2fc1 100644
--- a/README.md
+++ b/README.md
@@ -28,6 +28,7 @@ services:
#- MQTT_PASSWORD=password
# Used in reply to thermostat
- API_SERVER_ADDRESS=10.0.1.22 # This should be the IP where a wifi client can access this container port 8080, NOT an internal docker IP
+ - LOG_LEVEL=DEBUG # DEBUG INFO
restart: always
```
### Home Assistant Configuration
diff --git a/thermostat_api_server.py b/thermostat_api_server.py
index a4c5e6d..c9a9d9c 100755
--- a/thermostat_api_server.py
+++ b/thermostat_api_server.py
@@ -12,6 +12,15 @@
import socketserver
import time
import json
+import logging
+
+logging.basicConfig(
+ level=os.environ['LOG_LEVEL'], format="%(asctime)s -- %(levelname)s -- %(message)s",
+ handlers=[
+ logging.StreamHandler()
+ ]
+)
+logger = logging.getLogger(__name__)
# Allow faster script restart
socketserver.TCPServer.allow_reuse_address = True
@@ -60,9 +69,9 @@
}
def on_connect(client, userdata, flags, rc):
- print("Connected to MQTT")
+ logging.info("Connected to MQTT")
client.subscribe(f"{thermostat_command_topic}/#")
- print(f'''Subscribed to {thermostat_command_topic}/#''')
+ logging.info(f'''Subscribed to {thermostat_command_topic}/#''')
client.publish(f'homeassistant/climate/{thermostat_serial}-climate/config', json.dumps(climate_configuration_payload), retain=True)
@@ -213,13 +222,13 @@ def on_connect(client, userdata, flags, rc):
}
client.publish(f'homeassistant/sensor/{thermostat_serial}-last-time/config', json.dumps(last_time_sensor_configuration_payload), retain=True)
- print('Published Config Entries')
+ logging.info('Published Config Entries')
def on_message(client, userdata, message):
global changes_pending
global candidate_configuration
message.payload = message.payload.decode("utf-8")
- print(f'''New message: {message.topic} {message.payload}''')
+ logging.info(f'''New message: {message.topic} {message.payload}''')
if message.topic == f"{thermostat_command_topic}/operating_mode":
new_operating_mode = message.payload
@@ -289,7 +298,7 @@ def do_GET(self):
elif "/config" in self.path:
global changes_pending
changes_pending = False
- print(f'''New configuration: {candidate_configuration}''')
+ logging.info(f'''New configuration: {candidate_configuration}''')
html = f'''{datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")}{candidate_configuration["mode"]}{candidate_configuration["fan"]}1012onhighclickoff00004545Zone 1{candidate_configuration["hold"]}{candidate_configuration["htsp"]}{candidate_configuration["clsp"]}'''
self.send_response(200)
self.send_header("Content-Length", len(html))
@@ -318,7 +327,7 @@ def do_POST(self):
final_locator = f'/{self.path.split("/")[-1:][0]}' # eg /status
if len(data) >= 45 and final_locator in paths:
- print(f"DEBUG: {final_locator} -- {data}")
+ logging.debug(f"{final_locator} -- {data}")
try:
# Parse and create dict of received message
root = ET.fromstring(data)
@@ -358,7 +367,7 @@ def do_POST(self):
self.send_empty_200()
elif "/status" in final_locator:
- print(f"DEBUG: {current_configuration}")
+ logging.debug(f"{current_configuration}")
current_configuration["last_communication"] = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
# Initialize candidate_configuration as current_configuration at first start
@@ -375,7 +384,7 @@ def do_POST(self):
self.send_no_changes()
elif changes_pending == True:
- print("Responding with change notice...")
+ logging.info("Responding with change notice...")
html = f'''{datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")}001440060864008640086400864000onoffoffoffoffoff'''
self.send_response(200)
self.send_header("Content-Length", str(len(html)))
@@ -408,7 +417,7 @@ class ThreadingSimpleServer(ThreadingMixIn, HTTPServer):
client.on_connect = on_connect
client.on_message = on_message
-print("Connecting to MQTT")
+logging.info("Connecting to MQTT")
client.connect(mqtt_address, mqtt_port)
client.loop_start()