def mqtt_set_aux_mode_callback(self, client, userdata, message): try: payload = message.payload.decode().lower() logging.info( "Received aux mode update request: {}".format(payload)) self.boost_heat(payload) except Exception as e: logging.error('Unable to proces message.', e) MQTT.publish(self.topic, self.payload())
def mqtt_callback(self, client, userdata, message): try: # print(message) # logging.info("Received command message: {}".format(payload)) payload = message.payload.decode() logging.info("Received command message: {} on topic ".format(payload, message.topic)) if payload == 'ON': self.on() elif payload == 'OFF': self.off() except Exception as e: logging.error('Unable to proces message.', e) mqtt.publish(self.topic, self.payload())
def test_mqtt_subscribe(): MQTT.setup() def callback(client, data, message): print(message.payload) while True: res, id = MQTT.subscribe('hello', callback) if res == 0: break time.sleep(1) while True: pass
def mqtt_set_temperature_set_point_callback(self, client, userdata, message): try: payload = message.payload.decode() logging.info( "Received temperature set point update request: {}".format( message.payload)) payload = float(payload) if self.mode == HVAC.HEAT: self.set_point_heat = payload else: self.set_point_cool = payload except Exception as e: logging.error('Unable to proces message.', e) MQTT.publish(self.topic, self.payload())
def setup(self, lazy_setup=True): """ Setup Home Assistant MQTT discover for ibeacons. :return: None """ # setup GPIO g.setmode(g.BCM) if not type(self.pin) == list: self.pin = [self.pin] for pin in self.pin: g.setup(pin, g.IN) # for pin in self.pin: if not lazy_setup: self.setup_output() mqtt.subscribe(self.homeassistant_mqtt_config['command_topic'], self.mqtt_callback)
def setup(self): # config = json.dumps({'name': self.name, 'device_class': self.device_class}) device_config = { 'name': "Laundry Room Climate", 'identifiers': self.name, 'sw_version': 'rpi2mqtt', 'model': "DHT 22", 'manufacturer': 'Generic' } config = json.dumps({ 'name': self.name + '_temperature', 'device_class': 'temperature', 'unit_of_measurement': '°F', 'value_template': "{{ value_json.temperature }}", 'unique_id': self.name + '_temperature_rpi2mqtt', 'state_topic': self.topic, "json_attributes_topic": self.topic, 'device': device_config }) mqtt.publish( 'homeassistant/sensor/{}_{}/config'.format(self.name, 'temp'), config) config = json.dumps({ 'name': self.name + '_humidity', 'device_class': 'humidity', 'json_attributes_topic': self.topic, 'unit_of_measurement': '%', 'value_template': "{{ value_json.humidity }}", 'unique_id': self.name + '_humidity_rpi2mqtt', 'state_topic': self.topic, 'device': device_config }) mqtt.publish( 'homeassistant/sensor/{}_{}/config'.format(self.name, 'humidity'), config)
def callback(self, *args): mqtt.publish(self.topic, self.payload())
def publish_mqtt_discovery(self): mqtt.publish(self.homeassistant_mqtt_config_topic, self.homeassistant_mqtt_config_json) logging.debug("Published MQTT discovery config to {}".format( self.homeassistant_mqtt_config_topic))
def main(): config = None args = parser.parse_args() if args.generate_config: Config.generate_config('config.yaml') sys.exit(0) if args.install_service: username = input("User to run service as [pi]: ") or 'pi' config_path = input("Enter full path to config.yaml: ") # _path = input("Path rpi2mqtt executable (run `which rpi2mqtt`): ") _path = subprocess.check_output(['which', 'rpi2mqtt']).decode().strip() install_service(username, _path, config_path) sys.exit(0) scanner = None if args.config: config = Config.get_instance(filename=args.config) if not config: logging.error("No configuration file present.") sys.exit(1) # start MQTT client from rpi2mqtt.mqtt import MQTT MQTT.setup() sensor_list = [] if len(config.sensors) >0: for sensor in config.sensors: s = None if sensor.type == 'dht22': s = DHT(sensor.pin, sensor.topic, sensor.name, 'sensor', sensor.type) elif sensor.type == 'ibeacon': s = Scanner(sensor.name, sensor.topic, sensor.uuid, sensor.away_timeout) elif sensor.type == 'switch': s = Switch(sensor.name, sensor.pin, sensor.topic) elif sensor.type == 'reed': s = ReedSwitch(sensor.name, sensor.pin, sensor.topic, sensor.normally_open, sensor.get('device_type')) elif sensor.type == 'bme280': s = BME280(sensor.name, sensor.topic) elif sensor.type == 'hestiapi': s = HestiaPi(sensor.name, sensor.topic, sensor.heat_setpoint, sensor.cool_setpoint, dry_run=args.dry_run) elif sensor.type == 'onewire': s = OneWire(sensor.name, sensor.topic) else: logging.warn('Sensor {} found in config, but was not setup.'.format(sensor.name)) if s: sensor_list.append(s) try: scanner = BeaconScanner(sensor_list[1].process_ble_update) # TODO update to search sensor list and setup scanner accordingly. scanner.start() except: logging.error("Beacon scanner did not start") else: logging.warn("No sensors defined in {}".format(args.config)) try: while True: for sensor in sensor_list: sensor.callback() time.sleep(config.polling_interval) MQTT.ping_subscriptions() except: traceback.print_exc() MQTT.client.loop_stop() if scanner: scanner.stop()
def main(): logging.debug('debug message') logging.info('info message') logging.warning('warning') logging.error('error') MQTT.setup()