def handle_message(self, payload): # send a message whilst receive window is open, this MUST be done first if len(self.send_queue) > 0: message = self.send_queue.pop(0) self.send_message(message) # logging.debug("Sent message %s" % str(message)) # logging.debug("MIHO013 send %s (%s remaining)" % (self.device_id, len(self.send_queue))) # check if it's time to refresh readings now = time.time() if self.voltageReadingPeriod is not None and ( self.lastVoltageReading is None or now - self.lastVoltageReading > self.voltageReadingPeriod): self.queue_message( OpenThings.Message(MIHO013_BATTERY_LEVEL, header=self.__class__.header())) self.lastVoltageReading = now if self.diagnosticsReadingPeriod is not None and ( self.lastDiagnosticsReading is None or now - self.lastDiagnosticsReading > self.diagnosticsReadingPeriod): self.queue_message( OpenThings.Message(MIHO013_DIAGNOSTICS, header=self.__class__.header())) self.lastDiagnosticsReading = now # extract data from message for rec in payload["recs"]: paramid = rec["paramid"] if "value" in rec: value = rec["value"] # logging.debug("MIHO013 new data %s %s %s" % (self.device_id, OpenThings.paramid_to_paramname(paramid), value)) if paramid == OpenThings.PARAM_TEMPERATURE: self.readings.ambient_temperature = value HandlerRegistry.handle_reading(self.uuid, 'ambient_temperature', value) if value < self._temperature_alarm_low: HandlerRegistry.alarm(self.uuid, 'ambient_temperature', value, 'Temperature Too Low') elif value > self._temperature_alarm_high: HandlerRegistry.alarm(self.uuid, 'ambient_temperature', value, 'Temperature Too High') elif paramid == OpenThings.PARAM_VOLTAGE: self.readings.battery_voltage = value HandlerRegistry.handle_reading(self.uuid, 'battery_voltage', value) if value < self._battery_alarm_low: HandlerRegistry.alarm(self.uuid, 'battery_voltage', value, 'Battery Running Low') elif paramid == OpenThings.PARAM_DIAGNOSTICS: self.readings.diagnostic_flags = value HandlerRegistry.handle_reading(self.uuid, 'diagnostic_flags', value) else: logging.debug("Unhandled param id %s" % str(paramid))
def set_valve_position(self, position: int): payload = OpenThings.Message(MIHO013_SET_VALVE_POSITION, header=self.__class__.header()).copyof() payload.set(recs_VALVE_POSITION_value=position) self._valvePosition = position # HandlerRegistry.handle_reading(self.uuid, 'valve_position', position) self.queue_message(payload)
def turn_off(self): # TODO: header construction should be in MiHomeDevice as it is shared? payload = OpenThings.Message(SWITCH, header=self.__class__.header()) payload.set(header_productid=self.__class__._product_id, header_sensorid=self.device_id, recs_SWITCH_STATE_value=False) self.send_message(payload)
def join_req(cls, mfrid, productid, deviceid): """Used for testing, synthesises a JOIN_REQ message from this device""" msg = OpenThings.Message(JOIN_REQ, header=cls.header()) msg["header_mfrid"] = mfrid msg["header_productid"] = productid msg["header_sensorid"] = deviceid return msg
def join_ack(self): """Send a join-ack to the real device""" # msg = OpenThings.Message(header_mfrid=MFRID_ENERGENIE, header_productid=self.product_id, header_sensorid=self.device_id) # msg[OpenThings.PARAM_JOIN] = {"wr":False, "typeid":OpenThings.Value.UINT, "length":0} # self.send_message(msg) payload = OpenThings.Message(JOIN_ACK, header=self.__class__.header()) payload.set(header_productid=self.__class__._product_id, header_sensorid=self.device_id) self.send_message(payload)
def set_setpoint_temperature(self, temperature: float): self.readings.setpoint_temperature = temperature # HandlerRegistry.handle_reading(self.uuid, 'setpoint_temperature', position) payload = OpenThings.Message(MIHO013_SET_TEMPERATURE, header=self.__class__.header()).copyof() if temperature < 0: temperature = 0 if temperature > 30: temperature = 30 payload.set(recs_TEMPERATURE_value=int(temperature * 256)) self.queue_message(payload)
def set_identify(self): self.queue_message( OpenThings.Message(MIHO013_IDENTIFY, header=self.__class__.header()).copyof())