def on_message(self, client: mqtt.Client, userdata, message: mqtt.MQTTMessage): # Check if this is a message that sets the light in a specific config if message.topic == self.set_topic: # Load the JSON state message_json = json.loads(message.payload) self.set_state(message_json) # Echo it back response = json.dumps(message_json) self.client.publish(self.state_topic, response, qos=1, retain=True) elif message.topic == self.scheduler_topic: payload = message.payload.decode("utf-8") if payload == "ON": self.device.on() elif payload == "OFF": self.device.off() elif payload == "BRI": self.device.brightness = 100 elif payload == "TEMP": self.device.temperature = 3700 elif payload == "COL": self.device.color = (19, 2, 150) else: # Pass it down SIISThing.on_message(self, client, userdata, message) return
def __init__(self): SIISThing.__init__(self, "sensor_1_hygro", 'urn:dev:siis:hygrometer', 'My Hygrometer', ['MultiLevelSensor'], 'A web connected hygrometer') self.humidity: Value = Value(50) self.update_period: float = 1000.0 self.add_property( Property(self, 'humidity', self.humidity, metadata={ '@type': 'LevelProperty', 'title': 'Humidity', 'type': 'number', 'unit': 'percent', 'description': 'Current air humdity', 'readOnly': True, 'multipleOf': 0.1, })) self.device = Barometer() self.timer: tornado.ioloop.PeriodicCallback = tornado.ioloop.PeriodicCallback( self.update_state, self.update_period) self.timer.start()
def __init__(self): SIISThing.__init__(self, "sensor_1_baro", 'urn:dev:siis:barometer', 'My Barometer', ['MultiLevelSensor'], 'A web connected barometer') self.pressure: Value = Value(1018.2) self.update_period: float = cfg.pin * 1000 self.add_property( Property(self, 'temperature', self.pressure, metadata={ '@type': 'LevelProperty', 'title': 'Pressure', 'type': 'number', 'minimum': 900, 'maximum': 1100, 'unit': 'hPa', 'description': 'Current atomspheric pressure', 'readOnly': True, 'multipleOf': 0.1, })) self.device = Barometer() self.timer: tornado.ioloop.PeriodicCallback = tornado.ioloop.PeriodicCallback( self.update_state, self.update_period) self.timer.start()
def on_connect(self, client: mqtt.Client, userdata, flags, rc): # Extend the on_connect method of SIISThing SIISThing.on_connect(self, client, userdata, flags, rc) self.client.publish(self.state_topic, payload=self.last_state, qos=1, retain=True)
def __init__(self): SIISThing.__init__( self, "smoke_1", 'urn:dev:siis:smoke', 'My Smoke Detector', ['Alarm'], 'A web connected smoke detector' ) self.state: Value = Value(False) self.update_period: float = 1000.0 self.add_property( Property(self, 'on', self.state, metadata={ '@type': 'AlarmProperty', 'title': 'Smoke detected', 'type': 'boolean', 'description': 'Whether smoke has been detected', 'readOnly': True, })) self.add_available_event( 'alarm', {'description': 'Smoke detected'} ) self.device = SmokeDetector(cfg.pin, self.activated, self.deactivated) self.timer: tornado.ioloop.PeriodicCallback = tornado.ioloop.PeriodicCallback( self.update_state, self.update_period ) self.timer.start()
def on_message(self, client: mqtt.Client, userdata, message: mqtt.MQTTMessage): if message.topic == self.scheduler_topic: payload = message.payload.decode("utf-8") self.auto_update = False self.fixed_state = payload else: SIISThing.on_message(self, client, userdata, message)
def __init__(self, name: str = "sensor_1"): SIISThing.__init__(self, name) self.temp_state_topic: str = cfg.base_topic + self.name + "/thermometer" + cfg.state_suffix self.humidity_state_topic: str = cfg.base_topic + self.name + "/hygrometer" + cfg.state_suffix self.pressure_state_topic: str = cfg.base_topic + self.name + "/barometer" + cfg.state_suffix self.thermometer = Thermometer() self.barometer = Barometer() self.hygrometer = Barometer()
def on_message(self, client: mqtt.Client, userdata, message: mqtt.MQTTMessage): if message.topic == self.scheduler_topic: print( f"Received an unexpected message from the scheduler: {message.payload.decode()}" ) else: # Pass it down SIISThing.on_message(self, client, userdata, message)
def on_message(self, client: mqtt.Client, userdata, message: mqtt.MQTTMessage): if message.topic == self.scheduler_topic: # The barometer should not receive anything from the scheduler logging.error( f"Reveiced an unexpected message from the scheduler: {message.payload.decode()}" ) else: # Pass it down SIISThing.on_message(self, client, userdata, message)
def on_message(self, client: mqtt.Client, userdata, message: mqtt.MQTTMessage): if message.topic == self.scheduler_topic: # Stop the timer to prevent automatic changes self.auto_update = False payload: str = message.payload.decode() if payload == "ON": self.state.notify_of_external_update(True) elif payload == "OFF": self.state.notify_of_external_update(False) else: # Pass it down SIISThing.on_message(self, client, userdata, message)
def on_message(self, client: mqtt.Client, userdata, message: mqtt.MQTTMessage): if message.topic == self.scheduler_topic: state = message.payload.decode("utf-8") print(f"Setting new state to {state}") self.last_state = state self.client.publish(self.state_topic, payload=state, qos=1, retain=True) else: # Pass it down SIISThing.on_message(self, client, userdata, message)
def on_message(self, client: mqtt.Client, userdata, message: mqtt.MQTTMessage): if message.topic == self.scheduler_topic: # Stop the timer to prevent automatic changes payload: str = message.payload.decode() if payload == "ON": self.perform_action("lock") # self.state.notify_of_external_update("locked") elif payload == "OFF": self.perform_action("unlock") # self.state.notify_of_external_update("unlocked") else: # Pass it down SIISThing.on_message(self, client, userdata, message)
def on_connect(self, client: mqtt.Client, userdata, flags, rc) -> None: SIISThing.on_connect(self, client, userdata, flags, rc) client.publish(self.temp_state_topic, payload=self.get_temp(), qos=1, retain=True) client.publish(self.humidity_state_topic, payload=self.get_humidity(), qos=1, retain=True) client.publish(self.pressure_state_topic, payload=self.get_pressure(), qos=1, retain=True)
def on_message(self, client: mqtt.Client, userdata, message: mqtt.MQTTMessage): # Check if this is a message that sets the lock if message.topic == self.set_topic or message.topic == self.scheduler_topic: # Read the target state lock: str = message.payload.decode("utf-8") print("Setting the lock to %s" % lock) self.last_state = lock # Echo it back response = lock client.publish(self.state_topic, response, qos=1, retain=True) else: # Pass the message down SIISThing.on_message(self, client, userdata, message)
def on_message(self, client: mqtt.Client, userdata, message: mqtt.MQTTMessage): if message.topic == self.scheduler_topic: payload: str = message.payload.decode() self.auto_update = False if payload == "ON": self.state.notify_of_external_update(True) self.add_event(AlarmEvent(self)) elif payload == "OFF": self.state.notify_of_external_update(False) else: logging.error(f"Received unexpected message: {payload}") else: # Pass it down SIISThing.on_message(self, client, userdata, message)
def on_message(self, client: mqtt.Client, userdata, message: mqtt.MQTTMessage): if message.topic == self.scheduler_topic: new_state = message.payload.decode("utf-8") _ = self.device.value logging.debug(f"Setting state to {new_state}") if new_state == "ON": self.state.notify_of_external_update(True) elif new_state == "OFF": self.state.notify_of_external_update(False) else: logging.error(f"Invalid state received: {new_state}") else: # Pass it down SIISThing.on_message(self, client, userdata, message)
def on_message(self, client: mqtt.Client, userdata, message: mqtt.MQTTMessage): "MQTT callback for when the client receives a message" # Check if this is a message that sets the light in a specific config if message.topic == self.target_temperature_set: # Read the target temp target_temp: float = float(message.payload) print("Setting the target temp at %0.1f" % target_temp) self.last_target = target_temp # Echo it back response = str(target_temp) self.client.publish(self.target_temperature_state, response, qos=1, retain=True) elif message.topic == self.mode_set_topic: # Read the fan setting mode_setting: str = message.payload.decode("utf-8") print("Setting mode to %s" % mode_setting) self.last_mode = mode_setting # Echo it back response = mode_setting self.client.publish(self.mode_state_topic, response, qos=1, retain=True) elif message.topic == self.scheduler_topic: # Detemine if it is a temp or mode update payload = message.payload.decode("utf-8") try: # Read the new outside temp temp: float = float(payload) print(f"Setting outside temp to {temp}C") self.outside_temp = temp except ValueError: # Set the new mode, disable auto_update self.auto_update = False state: str = payload if state == "OFF": self.set_action("off") elif state == "HEAT": self.set_action("heating") elif state == "COOL": self.set_action("cooling") else: # Pass the message down SIISThing.on_message(self, client, userdata, message)
def on_message(self, client: mqtt.Client, userdata, message: mqtt.MQTTMessage): if message.topic == self.scheduler_topic: payload: str = message.payload.decode() if payload == "ON": self.on_state.notify_of_external_update(True) elif payload == "OFF": self.on_state.notify_of_external_update(False) elif payload == "BRI": self.brightness_state.notify_of_external_update(100) elif payload == "TEMP": self.temperature_state.notify_of_external_update(3400) elif payload == "COL": self.color_state.notify_of_external_update("#130296") else: # Pass it down SIISThing.on_message(self, client, userdata, message)
def __init__(self): SIISThing.__init__(self, "tv_1", 'urn:dev:siis:tv', 'My TV', ['OnOffSwitch'], 'A web connected TV') self.state: Value = Value(False, self.set_state) self.add_property( Property(self, 'on', self.state, metadata={ '@type': 'OnOffProperty', 'title': 'On state', 'type': 'boolean', 'description': 'Whether the TV is on', 'readOnly': False, })) self.device = TV()
def __init__(self): SIISThing.__init__(self, "outlet_1", 'urn:dev:siis:outlet', 'My Outlet', ['SmartPlug'], 'A web connected outlet') self.state: Value = Value(False, self.set_value) self.add_property( Property(self, 'on', self.state, metadata={ '@type': 'OnOffProperty', 'title': 'On state', 'type': 'boolean', 'description': 'Whether the outlet is on', })) self.device = Relay(cfg.pin)
def __init__(self): SIISThing.__init__(self, "presence_1", 'urn:dev:siis:presence', 'My Presence Sensor', ['BinarySensor'], 'A web connected presence sensor') self.update_period: float = 10000.0 self.state: Value = Value(False) self.add_property( Property(self, 'state', self.state, metadata={ '@type': 'BooleanProperty', 'title': 'On/Off', 'type': 'boolean', 'description': 'Whether the switch is turned on', 'readOnly': True, })) self.device = PIR(cfg.pin, self.activated, self.deactivated)
def __init__(self): SIISThing.__init__( self, "lock_1", 'urn:dev:siis:lock', 'My Lock', ['Lock'], 'A web connected lock' ) self.state: Value = Value("unlocked", self.set_state) self.add_property( Property(self, 'locked', self.state, metadata={ '@type': 'LockedProperty', 'title': 'Lock state', 'type': 'string', 'description': 'Whether the lock is locked', 'readOnly': True, 'enum': ['locked, unlocked'], })) self.add_available_action( "lock", { "title": "Lock", "description": "Set the lock to locked state", }, LockAction ) self.add_available_action( "unlock", { "title": "Unlock", "description": "Set the lock to unlocked state", }, UnlockAction ) self.device = ServoMotor(cfg.pin)
def __init__(self, name: str = "hvac_1"): SIISThing.__init__(self, name) self.last_mode: str = "" self.last_action: str = "off" self.last_target: float = 0 self.last_temp: float = 20 self.outside_temp: float = 20 self.thermal_cond: float = 0.05 # 1/min self.heating_efficiency: float = 0.5 # C/min self.cooling_efficiency: float = 0.5 # C/min self.temp_state_topic: str = cfg.base_topic + self.name + "/temperature" + cfg.state_suffix self.action_state_topic: str = cfg.base_topic + self.name + "/action" + cfg.state_suffix self.mode_set_topic: str = cfg.base_topic + self.name + "/mode" + cfg.set_suffix self.mode_state_topic: str = cfg.base_topic + self.name + "/mode" + cfg.state_suffix self.target_temperature_set: str = cfg.base_topic + self.name + "/target_temperature" + cfg.set_suffix self.target_temperature_state: str = cfg.base_topic + self.name + "/target_temperature" + cfg.state_suffix self.thermometer: Thermometer = Thermometer() self.heater_cooler: Relay = Relay(cfg.pin)
def on_message(self, client: mqtt.Client, userdata, message: mqtt.MQTTMessage): "MQTT callback for when the client receives a message" if message.topic == self.scheduler_topic: payload = message.payload.decode("utf-8") try: new_outside_temp = float(payload) logging.debug( f"Setting outside temperature to {new_outside_temp}C") self.outside_temp = new_outside_temp except ValueError: state = payload self.auto_update = False if state == "OFF": self.state.notify_of_external_update("off") elif state == "HEAT": self.state.notify_of_external_update("heating") elif state == "COOL": self.state.notify_of_external_update("cooling") else: # Pass it down SIISThing.on_message(self, client, userdata, message)
def __init__(self): SIISThing.__init__(self, "switch_1", 'urn:dev:siis:switch', 'My Switch', ['BinarySensor'], 'A web connected lamp') self.update_period: float = cfg.update_delay * 1000 self.state: Value = Value(False) self.add_property( Property(self, 'state', self.state, metadata={ '@type': 'BooleanProperty', 'title': 'On/Off', 'type': 'boolean', 'description': 'Whether the switch is turned on', 'readOnly': True, })) self.device = Switch(cfg.pin) self.timer: tornado.ioloop.PeriodicCallback = tornado.ioloop.PeriodicCallback( self.update_state, self.update_period) self.timer.start()
def on_message(self, client: mqtt.Client, userdata, message: mqtt.MQTTMessage): # Check if this is a message that sets the TV if message.topic == self.set_topic: # Read the target temp tv_state: str = message.payload.decode("utf-8") print("Setting the TV to %s" % tv_state) self.set_state(tv_state) # Echo it back response = tv_state self.client.publish(self.state_topic, response, qos=1, retain=True) elif message.topic == self.scheduler_topic: tv_state = message.payload.decode("utf-8") print( f"Received message from Scheduler, setting new state to {tv_state}" ) self.set_state(tv_state) # Publish the state to Hass response = tv_state self.client.publish(self.state_topic, response, qos=1, retain=True) else: # Pass it down SIISThing.on_message(self, client, userdata, message)
def __init__(self): SIISThing.__init__(self, "sensor_1_therm", 'urn:dev:siis:thermometer', 'My Thermometer', ['TemperatureSensor'], 'A web connected thermometer') self.temp: Value = Value(20.0) self.update_period: float = cfg.pin * 1000 self.add_property( Property(self, 'temperature', self.temp, metadata={ '@type': 'TemperatureProperty', 'title': 'Temperature', 'type': 'number', 'description': 'Current temperature', 'readOnly': True, 'multipleOf': 0.1, })) self.device = Thermometer() self.timer: tornado.ioloop.PeriodicCallback = tornado.ioloop.PeriodicCallback( self.update_state, self.update_period) self.timer.start()
def on_connect(self, client: mqtt.Client, userdata, flags, rc): SIISThing.on_connect(self, client, userdata, flags, rc) client.publish(self.state_topic, payload=self.last_state, qos=1, retain=True)
def __init__(self, name: str = "presence_1"): SIISThing.__init__(self, name) self.last_state: str = "OFF" self.device: PIR = PIR(cfg.pin, self.activated, self.deactivated)
def __init__(self): SIISThing.__init__(self, "light_1", 'urn:dev:siis:light', 'My Lamp', ['OnOffSwitch', 'Light'], 'A web connected lamp') self.on_state: Value = Value(False, self.set_state) self.add_property( Property(self, 'on', self.on_state, metadata={ '@type': 'OnOffProperty', 'title': 'On/Off', 'type': 'boolean', 'description': 'Whether the lamp is turned on', })) self.brightness_state: Value = Value(100, self.set_brightness) self.add_property( Property(self, 'brightness', self.brightness_state, metadata={ '@type': 'BrightnessProperty', 'title': 'Brightness', 'type': 'integer', 'description': 'The level of light from 0-100', 'minimum': 0, 'maximum': 100, 'unit': 'percent', })) self.color_state: Value = Value("#FFFFFF", self.set_color) self.add_property( Property(self, 'color', self.color_state, metadata={ '@type': 'ColorProperty', 'title': 'Color', 'type': 'string', 'description': 'The color of the light in hex RGB', })) self.temperature_state: Value = Value(2700, self.set_temperature) self.add_property( Property(self, 'temperature', self.temperature_state, metadata={ '@type': 'ColorTemperatureProperty', 'title': 'Temperature', 'type': 'integer', 'description': 'The temperature of the light in Kelvin', 'minimum': 2200, 'maximum': 6500, 'unit': 'kelvin', })) self.color_mode_state: Value = Value( "temperature", lambda v: logging.debug("Color mode is now %s" % v)) self.add_property( Property(self, 'colorMode', self.color_mode_state, metadata={ '@type': 'ColorModeProperty', 'title': 'Color Mode', 'type': 'string', 'description': 'The color mode of the light', 'enum': ['color', 'temperature'], 'readOnly': True })) self.device = RGBLight(cfg.pin)