Loop through messages and pass them on to right device

def message_worker(device):
    """Loop through messages and pass them on to right device"""
    _LOGGER.debug("Starting Worker Thread.")
    msg_q = device.messages

    while True:

        if not msg_q.empty():
            message = msg_q.get()

            data = {}
            try:
                data = json.loads(message.decode("utf-8"))
            except ValueError:
                _LOGGER.error("Received invalid message: %s", message)

            if 'device_id' in data:
                device_id = data.get('device_id')
                if device_id == device.device_id:
                    device.handle_event(data)
                else:
                    _LOGGER.warning("Received message for unknown device.")
            msg_q.task_done()

        time.sleep(0.2)