def __init__(self, state): self.update_on = ('valve_open', 'battery_level') state.mqtt = False self.mqtt = MQTT(config.NAME, secrets) valve = self.mqtt.add( 'Valve', Switch, opt=False, ## not optimistic ret=False, ## no retain icon="mdi:water-pump", ) def set_valve(msg): state.set(valve_open=msg == valve.ON) self.mqtt.sub(valve.cmd_tpc(), set_valve) if config.BATT: battery = self.mqtt.add('Battery', Battery) self._sched = False def pub_state(_): self._sched = False valve.set_state(state.valve_open) if config.BATT and hasattr(state, 'battery'): self.mqtt.set_attr('battery', state.battery) battery.set_state(state.battery) state.set(mqtt=self.mqtt.pub_state()) self.pub_state = pub_state
class Internet(): def __init__(self, state): self.update_on = ('count', 'battery_level') print(wifi.uid()) status_led.slow_blink() wifi.connect(secrets.WIFI_NAME, secrets.WIFI_PASSWORD) status_led.off() self.mqtt = MQTT(config.NAME, secrets) counter = self.mqtt.add( 'Counter', Sensor, unit=config.UNIT, icon='mdi:counter', ) if config.BATT: battery = self.mqtt.add('Battery', Battery) status_led.fast_blink() state.mqtt = self.mqtt.try_pub_cfg() status_led.off() self._sched = False def pub_state(_): status_led.on() self._sched = False counter.set_state(state.count) if config.BATT: battery.set_state(state.battery) counter.set_attr('battery', state.battery) state.set(mqtt=self.mqtt.pub_state()) status_led.off() self.pub_state = pub_state def update(self, state, changed): if not self._sched: self.pub_sched = True schedule(self.pub_state, 0) def start(self, state): self.pub_state(0) def run(self, state): self.mqtt.wait(led=status_led) def stop(self, state): self.mqtt.discon()
def __init__(self, state): self.update_on = ('count', 'battery_level') print(wifi.uid()) status_led.slow_blink() wifi.connect(secrets.WIFI_NAME, secrets.WIFI_PASSWORD) status_led.off() self.mqtt = MQTT(config.NAME, secrets) counter = self.mqtt.add( 'Counter', Sensor, unit=config.UNIT, icon='mdi:counter', ) if config.BATT: battery = self.mqtt.add('Battery', Battery) status_led.fast_blink() state.mqtt = self.mqtt.try_pub_cfg() status_led.off() self._sched = False def pub_state(_): status_led.on() self._sched = False counter.set_state(state.count) if config.BATT: battery.set_state(state.battery) counter.set_attr('battery', state.battery) state.set(mqtt=self.mqtt.pub_state()) status_led.off() self.pub_state = pub_state
def start(self, hub): if hub.internet: status_led.fast_blink() wifi.connect(secrets.WIFI_NAME, secrets.WIFI_PASSWORD) status_led.off() sleep(2) if wifi.is_connected(): status_led.fast_blink() self.mqtt = MQTT(config.NAME, secrets, uid=UID) self.mqtt.add('Water', Moisture, prim=True, key='water', off_dly=config.NORMAL_SLEEP).set_state(hub.water) enable = self.mqtt.add('Enable', Switch, icon="mdi:bell") def enable_rx(msg): hub.set(enable=msg == enable.ON) self.mqtt.sub(enable.cmd_tpc(), enable_rx) self.mqtt.add('Battery', Battery, key='bat').set_state(hub.battery) self.mqtt.set_attr('battery', hub.battery) self.mqtt.set_attr('rssi', wifi.rssi()) self.mqtt.do_pub_cfg = self.do_pub_cfg self.mqtt.connect() sleep(1) self.mqtt.pub_state() sleep(1) status_led.off()
def __init__(self, state): self.update_on = ('mode', 'tank_target_temp', 'tank_temp', 'pump', 'solar_temp') led = StatusLED(gpio=config.LED_GPIO) wifi.connect(secrets.WIFI_NAME, secrets.WIFI_PASSWORD, led=led) self.mqtt = MQTT(config.NAME, secrets, uid=UID, led=led) ctl = self.mqtt.add('Controller', Climate, key = 'ctl', max = config.TANK_MAX_TEMP) temp = self.mqtt.add('Solar', Temperature, key = 'sol') def set_mode(msg): if msg in MODES: state.set(mode = msg) self.mqtt.sub(ctl.mode_cmd_tpc(), set_mode) def set_targ(msg): state.set(tank_target_temp = round(float(msg))) self.mqtt.sub(ctl.targ_cmd_tpc(), set_targ) state.mqtt = self.mqtt.try_pub_cfg() self._sched = False def pub_state(_): self._sched = False ctl.set_mode(state.mode) ctl.set_targ(state.tank_target_temp) ctl.set_temp(state.tank_temp) ctl.set_actn('off' if state.mode == MODE_OFF else ('heating' if state.pump else 'idle')) temp.set_state(state.solar_temp) state.set( mqtt = self.mqtt.pub_state() ) self.pub_state = pub_state
def __init__(self, state): print(wifi.uid()) if reset_cause() != DEEPSLEEP_RESET: status_led.slow_blink() wifi.connect(secrets.WIFI_NAME, secrets.WIFI_PASSWORD) status_led.off() mqtt = MQTT(config.NAME, secrets, uid=UID) opt = dict(exp_aft=config.FREQ * 2.5) if 'temp' in state: from home_assistant.sensors.temperature import Temperature mqtt.add('Temp', Temperature, **opt).set_state(state['temp']) if 'humid' in state: from home_assistant.sensors.humidity import Humidity mqtt.add('Humid', Humidity, **opt).set_state(state['humid']) if 'lux' in state: from home_assistant.sensors.illuminance import Illuminance mqtt.add('Lux', Illuminance, **opt).set_state(state['lux']) if 'analog' in state: mqtt.add('Analog', Sensor, unit="%", icon="mdi:gauge", **opt).set_state(state['analog']) if 'battery' in state: from home_assistant.sensors.battery import Battery mqtt.add('Battery', Battery, key='bat', **opt).set_state(state['battery']) mqtt.set_attr('battery', state['battery']) mqtt.set_attr("freq", config.FREQ) if wifi.is_connected(): if reset_cause() != DEEPSLEEP_RESET: status_led.fast_blink() mqtt.connect() sleep(1) mqtt.pub_state() status_led.off() sleep(5) mqtt.discon() sleep(5)
def __init__(self, hub): self.update_on = set() led = StatusLED(gpio=config.LED_GPIO) wifi.connect(secrets.WIFI_NAME, secrets.WIFI_PASSWORD, led=led) self.mqtt = MQTT(config.NAME, secrets, uid = UID, led=led) if config.COMPNT != None: self.update_on.add(LIGHT) if config.COMPNT == LIGHT: light = self.mqtt.add(t(LIGHT), Light, prim = True, bri = config.BRIGHTNESS) if config.BRIGHTNESS: def bri_rx(msg): hub.set(brightness = round(float(msg))) self.mqtt.sub(light.bri_cmd_tpc(), bri_rx) elif config.COMPNT == SWITCH: light = self.mqtt.add(t(SWITCH), Switch, prim = True) else: print('ERR', COMPNT); self.mqtt.sub_dev_cmd(light.cmd_base_tpc()) def light_rx(msg): hub.set(light = msg == light.ON) self.mqtt.sub(light.cmd_tpc(), light_rx) if config.MOTN: self.update_on.add(MOTN) motion = self.mqtt.add(t(MOTN), Motion) enable = self.mqtt.add(t(EN), Switch) def enable_rx(msg): hub.set(enable = msg == enable.ON) self.mqtt.sub(enable.cmd_tpc(), enable_rx) if config.COMPNT != None: auto = self.mqtt.add(t(AUTO), Switch) def auto_rx(msg): hub.set(auto = msg == auto.ON) self.mqtt.sub(auto.cmd_tpc(), auto_rx) if config.BATT: self.update_on.add(BATT + '_level') battery = self.mqtt.add(t(BATT), Battery) self.mqtt.try_pub_cfg() self._sched = False def pub(_): self._sched = False if config.COMPNT != None: if hub.light_cmd: hub.light_cmd = False try: self.mqtt.publish(light.cmd_tpc(), light.ON if hub.light else light.OFF, retain = True) except: pass light.set_state(hub.light) if config.COMPNT == LIGHT and config.BRIGHTNESS: light.set_bri(hub.brightness) if config.MOTN: motion.set_state(hub.motion) enable.set_state(hub.enable) if config.COMPNT != None: auto.set_state(hub.auto) if config.BATT: battery.set_state(hub.battery) self.mqtt.set_attr(BATT, hub.battery) if wifi.is_connected(): self.mqtt.set_attr('rssi', wifi.rssi()) self.mqtt.pub_state() self.pub = pub
class Internet(): def __init__(self, hub): # Note, ESP8266 chip real-time clock will overflow every 7h45m hour = localtime()[3] print('hour', hour) self.do_pub_cfg = hour == 0 or reset_cause() != DEEPSLEEP_RESET print('do_pub_cfg', self.do_pub_cfg) hub.internet = hub.get('internet') or hub.water or self.do_pub_cfg def start(self, hub): if hub.internet: status_led.fast_blink() wifi.connect(secrets.WIFI_NAME, secrets.WIFI_PASSWORD) status_led.off() sleep(2) if wifi.is_connected(): status_led.fast_blink() self.mqtt = MQTT(config.NAME, secrets, uid=UID) self.mqtt.add('Water', Moisture, prim=True, key='water', off_dly=config.NORMAL_SLEEP).set_state(hub.water) enable = self.mqtt.add('Enable', Switch, icon="mdi:bell") def enable_rx(msg): hub.set(enable=msg == enable.ON) self.mqtt.sub(enable.cmd_tpc(), enable_rx) self.mqtt.add('Battery', Battery, key='bat').set_state(hub.battery) self.mqtt.set_attr('battery', hub.battery) self.mqtt.set_attr('rssi', wifi.rssi()) self.mqtt.do_pub_cfg = self.do_pub_cfg self.mqtt.connect() sleep(1) self.mqtt.pub_state() sleep(1) status_led.off() def run(self, hub): if hub.water: status_led.slow_blink() else: status_led.off() if hasattr(self, 'mqtt'): self.mqtt.wait() def stop(self, hub): if hasattr(self, 'mqtt'): self.mqtt.discon() wifi.disconnect() status_led.off()
def __init__(self, state): led = None if reset_cause() == DEEPSLEEP_RESET else StatusLED(config.LED) wifi.connect(secrets.WIFI_NAME, secrets.WIFI_PASSWORD, led=led) if wifi.is_connected(): print(wifi.mac(), wifi.ip(), wifi.rssi()) mqtt = MQTT(config.NAME, secrets, uid = UID, led=led) opt = dict(exp_aft = config.FREQ * 2.5) mqtt.add('Temp', Temperature, **opt).set_state(state['temp']) mqtt.add('Humid', Humidity, **opt).set_state(state['humid']) mqtt.set_attr("freq", config.FREQ) mqtt.do_pub_cfg = reset_cause() != DEEPSLEEP_RESET or ticks_us() % 10 == 0 mqtt.connect() sleep(1) mqtt.pub_state() sleep(5) mqtt.discon() sleep(5)
class Internet(): def __init__(self, state): self.update_on = ('valve_open', 'battery_level') state.mqtt = False self.mqtt = MQTT(config.NAME, secrets) valve = self.mqtt.add( 'Valve', Switch, opt=False, ## not optimistic ret=False, ## no retain icon="mdi:water-pump", ) def set_valve(msg): state.set(valve_open=msg == valve.ON) self.mqtt.sub(valve.cmd_tpc(), set_valve) if config.BATT: battery = self.mqtt.add('Battery', Battery) self._sched = False def pub_state(_): self._sched = False valve.set_state(state.valve_open) if config.BATT and hasattr(state, 'battery'): self.mqtt.set_attr('battery', state.battery) battery.set_state(state.battery) state.set(mqtt=self.mqtt.pub_state()) self.pub_state = pub_state def update(self, state, changed): if not self._sched: self._sched = True schedule(self.pub_state, 0) def run(self, state): print(wifi.uid()) status_led.slow_blink() wifi.connect(secrets.WIFI_NAME, secrets.WIFI_PASSWORD) status_led.fast_blink() if reset_cause() == DEEPSLEEP_RESET: self.mqtt.do_pub_cfg = False self.mqtt.connect() else: self.mqtt.try_pub_cfg() self.pub_state(None) status_led.off() self.mqtt.wait(led=status_led) def stop(self, state): self.mqtt.reconnect = False self.mqtt.discon()