def run(voc, config): # FIXME: Allow MQTT credentials in voc.conf client_id = 'voc_{hostname}_{time}'.format(hostname=hostname(), time=time()) mqtt_config = read_mqtt_config() mqtt = paho.Client(client_id=client_id, clean_session=False) mqtt.username_pw_set(username=mqtt_config['username'], password=mqtt_config['password']) mqtt.tls_set(certs.where()) mqtt.on_connect = on_connect mqtt.on_disconnect = on_disconnect mqtt.on_publish = on_publish mqtt.on_message = on_message mqtt.on_subscribe = on_subscribe mqtt.event_connected = Event() mqtt.connect(host=mqtt_config['host'], port=int(mqtt_config['port'])) mqtt.loop_start() interval = int(config['interval']) _LOGGER.info(f'Polling every {interval} seconds') entities = {} while True: if not mqtt.event_connected.is_set(): _LOGGER.debug('Waiting for MQTT connection') mqtt.event_connected.wait() _LOGGER.debug('Connected') available = True for vehicle in voc.vehicles: if vehicle not in entities: _LOGGER.debug('creating vehicle %s', vehicle) dashboard = Dashboard(vehicle) dashboard.configurate(**config) entities[vehicle] = [ Entity(mqtt, instrument, config) for instrument in dashboard.instruments ] for entity in entities[vehicle]: _LOGGER.debug('%s: %s', entity.instrument.full_name, entity.state) entity.publish_discovery() entity.publish_availability(available) if available: entity.publish_state() sleep(interval) available = voc.update()