Source code for libpurecoollink.dyson_pure_cool_link

"""Dyson pure cool link device."""

# pylint: disable=too-many-locals

import json
import logging
import time
import socket
from threading import Thread
from queue import Queue, Empty


import paho.mqtt.client as mqtt

from .dyson_device import DysonDevice, NetworkDevice, DEFAULT_PORT
from .utils import printable_fields, support_heating
from .dyson_pure_state import DysonPureHotCoolState, DysonPureCoolState, \
    DysonEnvironmentalSensorState
from .zeroconf import ServiceBrowser, Zeroconf

_LOGGER = logging.getLogger(__name__)





class EnvironmentalSensorThread(Thread):
    """Environmental Sensor thread.

    The device don't send environmental data if not asked.
    """

    def __init__(self, request_data_method, interval=30):
        """Create new Environmental Sensor thread."""
        Thread.__init__(self)
        self._interval = interval
        self._request_data_method = request_data_method
        self._stop_queue = Queue()

    def stop(self):
        """Stop the thread."""
        self._stop_queue.put_nowait(True)

    def run(self):
        """Start Refresh sensor state thread."""
        stopped = False
        while not stopped:
            self._request_data_method()
            try:
                stopped = self._stop_queue.get(timeout=self._interval)
            except Empty:
                # Thread has not been stopped
                pass