import os import logging logging.basicConfig( format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s', level=logging.DEBUG if os.environ.get('DEBUG') else logging.INFO) #logging.getLogger('aiohttp').setLevel(logging.DEBUG if os.environ.get('DEBUG') else logging.WARNING) import asyncio from statistics import mean import w1therm import relays import data HI_SETPOINT = 40.0 LO_SETPOINT = HI_SETPOINT - 10.0 PUMP_RELAY = relays.RELAY1 FAN_RELAY = relays.RELAY3 async def get_temp(): temps = await w1therm.get_temperatures() if len(temps) != len(data.PROBES): raise temperature_log = [] for id_, temp in temps.items(): temperature_log.append('{}: {} C'.format(data.PROBES[id_], temp)) logging.info('Temperature probe ' + ', '.join(temperature_log)) temperature_list = list(temps.values()) temperature_mean = mean(temperature_list) return temperature_mean async def main(): relays.init_relays() relays.set_relay(PUMP_RELAY, relays.RELAY_ON) while True: try: temperature_mean = await get_temp() except: logging.error('Problem reading temperature probes! Turning fan on and sleeping 60s.') relays.set_relay(FAN_RELAY, relays.RELAY_ON) await asyncio.sleep(60) continue if temperature_mean > HI_SETPOINT: logging.info('Turning fan on') relays.set_relay(FAN_RELAY, relays.RELAY_ON) elif temperature_mean < LO_SETPOINT: logging.info('Turning fan off') relays.set_relay(FAN_RELAY, relays.RELAY_OFF) await asyncio.sleep(10) if __name__ == '__main__': loop = asyncio.get_event_loop() loop.run_until_complete(main()) loop.close()