mirror of
https://github.com/OMGeeky/homecontrol.esp-sensors.git
synced 2026-02-23 15:49:52 +01:00
refactor config logic
This commit is contained in:
@@ -2,12 +2,13 @@
|
||||
Example usage of the OLED display with temperature and humidity sensors.
|
||||
This example demonstrates how to use the configuration system to initialize sensors and displays.
|
||||
"""
|
||||
|
||||
import time
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the src directory to the Python path
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
|
||||
from src.esp_sensors.oled_display import OLEDDisplay
|
||||
from src.esp_sensors.dht22 import DHT22Sensor
|
||||
@@ -31,10 +32,12 @@ def main():
|
||||
|
||||
# Initialize a DHT22 sensor with configuration
|
||||
dht_sensor = DHT22Sensor(
|
||||
sensor_type="dht22", # This will load config for this sensor type
|
||||
config=config # Pass the loaded config
|
||||
# This will load config for this sensor type
|
||||
sensor_config=config, # Pass the loaded config
|
||||
)
|
||||
print(
|
||||
f"Created DHT22 sensor: {dht_sensor.name}, pin: {dht_sensor.pin}, interval: {dht_sensor.interval}s"
|
||||
)
|
||||
print(f"Created DHT22 sensor: {dht_sensor.name}, pin: {dht_sensor.pin}, interval: {dht_sensor.interval}s")
|
||||
|
||||
# Method 2: Initialize with some parameters from config, others specified directly
|
||||
print("\nMethod 2: Override some config parameters")
|
||||
@@ -44,11 +47,12 @@ def main():
|
||||
# These parameters will override the config values
|
||||
name="Custom Display",
|
||||
interval=1, # Update every second
|
||||
|
||||
# Other parameters will be loaded from config
|
||||
config=config
|
||||
config=config,
|
||||
)
|
||||
print(
|
||||
f"Created OLED display: {display.name}, size: {display.width}x{display.height}, interval: {display.interval}s"
|
||||
)
|
||||
print(f"Created OLED display: {display.name}, size: {display.width}x{display.height}, interval: {display.interval}s")
|
||||
|
||||
# Display initialization message
|
||||
display.clear()
|
||||
@@ -72,13 +76,9 @@ def main():
|
||||
name_str = f"Sensor: {dht_sensor.name}"
|
||||
|
||||
# Display values
|
||||
display.display_values([
|
||||
name_str,
|
||||
temp_str,
|
||||
hum_str,
|
||||
time_str,
|
||||
f"Demo ({i+1}/5)"
|
||||
])
|
||||
display.display_values(
|
||||
[name_str, temp_str, hum_str, time_str, f"Demo ({i+1}/5)"]
|
||||
)
|
||||
|
||||
# Print to console in simulation mode
|
||||
print(f"Updated display with: {temp_str}, {hum_str}")
|
||||
|
||||
Reference in New Issue
Block a user