예제 #1
0
def run():
    logging.basicConfig(level="DEBUG")
    load_dotenv(verbose=False)

    logger.info(os.getenv("MQTT_HOST"))
    hive_data = HiveData(
        os.getenv("INFLUX_HOST"),
        os.getenv("INFLUX_PORT"),
        os.getenv("INFLUX_BUCKET"),
        os.getenv("INFLUX_TOKEN"),
        os.getenv("INFLUX_ORG"),
    )

    loop = aio.new_event_loop()
    client = aio_mqtt.Client(loop=loop)
    loop.run_until_complete(init_models())
    tasks = [
        loop.create_task(mqtt_connect_forever(client)),
        loop.create_task(handle_message(client, hive_data, loop)),
    ]
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass
    finally:
        loop.run_until_complete(close(client, tasks))
        loop.close()
예제 #2
0
    def __init__(
        self,
        device_id: str,
        topic_root: str,
        host: str,
        port: int = None,
        user: ty.Optional[str] = None,
        password: ty.Optional[str] = None,
        reconnection_interval: int = 10,
        *,
        auto_discovery: bool,
        sensor_retain: bool,
        sensor_threshold: int,
        sensor_debounce_period: int,
        light_transition_period: float,
        light_notification_period: float,
        loop: ty.Optional[aio.AbstractEventLoop] = None,
    ) -> None:
        self.dev_id = device_id
        self._topic_root = topic_root
        self._topic_lwt = f'{topic_root}/status'
        self._mqtt_host = host
        self._mqtt_port = port
        self._mqtt_user = user
        self._mqtt_password = password

        self._will_message = aio_mqtt.PublishableMessage(
            topic_name=self._topic_lwt,
            payload='offline',
            qos=aio_mqtt.QOSLevel.QOS_1,
            retain=True,
        )

        self._auto_discovery = auto_discovery
        self._sensor_retain = sensor_retain
        self._sensor_threshold = sensor_threshold
        self._sensor_debounce_period = sensor_debounce_period
        self._light_transition_period = light_transition_period
        self._light_notification_period = light_notification_period
        self._light_last_sent = None

        self._reconnection_interval = reconnection_interval
        self._loop = loop or aio.get_event_loop()
        self._client = aio_mqtt.Client(
            loop=self._loop,
            client_id_prefix='lumimqtt_',
        )
        self._tasks: ty.List[aio.Future] = []

        self.sensors: ty.List[Sensor] = []
        self.lights: ty.List[Light] = []
        self.buttons: ty.List[Button] = []
        self.custom_commands: ty.List[Command] = []

        self._debounce_sensors: ty.Dict[Sensor, DebounceSensor] = {}
예제 #3
0
    def __init__(
        self,
        reconnection_interval: int = 10,
        loop: Optional[asyncio.AbstractEventLoop] = None,
    ) -> None:
        self._reconnection_interval = reconnection_interval
        self._loop = loop or asyncio.get_event_loop()
        self._client = aio_mqtt.Client(loop=self._loop)
        self._tasks = []

        self._bypass_decrypt = False
        self._co2 = co2.CO2monitor(bypass_decrypt=self._bypass_decrypt)
        self.device_name = \
            f'{self._co2.info["product_name"]}_' \
            f'{self._co2.info["serial_no"].replace(".", "_")}'

        self.mqtt_prefix = f'homeassistant/sensor/{self.device_name}'
예제 #4
0
async def add_mosquitto(request):
    authed = await auth_token(request)
    if not authed:
        return web.json_response(AnswerPattern.get_error_answer())
    name = request.rel_url.query.get("name", None)
    topic = request.rel_url.query.get("topic", None)
    if name is None or topic is None:
        return web.json_response(AnswerPattern.get_error_answer())
    cli = aio_mqtt.Client(loop=asyncio.get_event_loop())
    await cli.connect(MQTT.addr)
    await cli.subscribe((topic + '/' + name, aio_mqtt.QOSLevel.QOS_1))

    async def handle():
        async for message in cli.delivered_messages(topic + '/' + name):
            VariablesClass.variables[name] = Variable(
                float(message.payload.decode()), 0.3, False)

    asyncio.get_event_loop().create_task(handle())
    return web.json_response(AnswerPattern.get_success_answer())
예제 #5
0
def run():
    logging.basicConfig(level="DEBUG")
    load_dotenv(verbose=False)

    logger.info(os.getenv("MQTT_HOST"))

    loop = aio.new_event_loop()
    client = aio_mqtt.Client(loop=loop)
    tasks = [
        loop.create_task(mqtt_connect_forever(client)),
        loop.create_task(handle_message(client, loop)),
    ]
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass
    finally:
        loop.run_until_complete(close(client, tasks))
        loop.close()
예제 #6
0
    def __init__(
        self,
        host: str,
        port: int = None,
        user: ty.Optional[str] = None,
        password: ty.Optional[str] = None,
        reconnection_interval: int = 10,
        loop: ty.Optional[aio.AbstractEventLoop] = None,
        *,
        base_topic,
        mqtt_config_prefix,
    ) -> None:
        global BLUETOOTH_RESTARTING

        self._mqtt_host = host
        self._mqtt_port = port
        self._mqtt_user = user
        self._mqtt_password = password
        self._base_topic = base_topic
        self._mqtt_config_prefix = mqtt_config_prefix

        self._reconnection_interval = reconnection_interval
        self._loop = loop or aio.get_event_loop()
        BLUETOOTH_RESTARTING = aio.Lock()

        self._mqtt_client = aio_mqtt.Client(
            client_id_prefix=f'{base_topic}_',
            loop=self._loop,
        )

        self._device_managers: ty.Dict[Device, DeviceManager] = {}

        self.availability_topic = '/'.join((
            self._base_topic,
            self.BRIDGE_TOPIC,
            BRIDGE_STATE_TOPIC,
        ))

        self.device_registry: ty.List[Device] = []
예제 #7
0
 def __init__(self, url: str, db):
     super().__init__(url, db)
     self._client = aio_mqtt.Client(loop=self._loop)