Esempio n. 1
0
  def on_command(self, topic, value):
    from bluepy import btle
    topic_without_prefix = topic.replace('{}/'.format(self.topic_prefix), '')
    device_name, method, _ = topic_without_prefix.split('/')

    data = self.devices[device_name]

    value = value.decode('utf-8')

    if method == STATE_AWAY:
      method = "mode"
      value = self.ModesMapper.on_off_to_mode(value)

    # It needs to be on separate if because first if can change method
    if method == "mode":
      value = self._modes_mapper.get_reverse_mapping(value)
    elif method == "target_temperature":
      value = float(value)

    _LOGGER.info("Setting %s to %s on %s device '%s' (%s)", method, value, repr(self), device_name, data["mac"])
    try:
      retry(setattr, exception_type=btle.BTLEException)(data["thermostat"], method, value)
    except btle.BTLEException as e:
      logger.log_exception(_LOGGER, "Error setting %s to %s on %s device '%s' (%s): %s", method, value, repr(self), device_name, data["mac"], type(e).__name__)
      return []

    try:
      return retry(self.update_device_state, exception_type=btle.BTLEException)(device_name, data["thermostat"])
    except btle.BTLEException as e:
      logger.log_exception(_LOGGER, "Error during update of %s device '%s' (%s): %s", repr(self), device_name, data["mac"], type(e).__name__, suppress=True)
      return []
Esempio n. 2
0
    def status_update(self):
        from bluepy import btle

        _LOGGER.info("Updating %d %s devices", len(self.devices), repr(self))
        for name, data in self.devices.items():
            _LOGGER.debug("Updating %s device '%s' (%s)", repr(self), name,
                          data["mac"])
            thermostat = data["thermostat"]
            try:
                retry(thermostat.update,
                      retries=self.update_retries,
                      exception_type=btle.BTLEException)()
            except btle.BTLEException as e:
                logger.log_exception(
                    _LOGGER,
                    "Error during update of %s device '%s' (%s): %s",
                    repr(self),
                    name,
                    data["mac"],
                    type(e).__name__,
                    suppress=True,
                )
            else:
                yield retry(self.present_device_state,
                            retries=self.update_retries,
                            exception_type=btle.BTLEException)(name,
                                                               thermostat)
Esempio n. 3
0
    def status_update(self):
        _LOGGER.info("Updating %d %s devices", len(self.devices), repr(self))

        for name, data in self.devices.items():
            _LOGGER.debug("Updating %s device '%s' (%s)", repr(self), name,
                          data["mac"])
            from btlewrap import BluetoothBackendException

            try:
                with timeout(self.per_device_timeout,
                             exception=DeviceTimeoutError):
                    yield retry(self.update_device_state,
                                retries=self.update_retries,
                                exception_type=BluetoothBackendException)(
                                    name, data["poller"])
            except BluetoothBackendException as e:
                logger.log_exception(
                    _LOGGER,
                    "Error during update of %s device '%s' (%s): %s",
                    repr(self),
                    name,
                    data["mac"],
                    type(e).__name__,
                    suppress=True,
                )
            except DeviceTimeoutError:
                logger.log_exception(
                    _LOGGER,
                    "Time out during update of %s device '%s' (%s)",
                    repr(self),
                    name,
                    data["mac"],
                    suppress=True,
                )
Esempio n. 4
0
    def handle_mqtt_command(self, topic, value):
        topic_without_prefix = topic.replace("{}/".format(self.topic_prefix),
                                             "")
        device_name, field, action = topic_without_prefix.split("/")
        ret = []

        if device_name in self.devices:
            data = self.devices[device_name]
            _LOGGER.debug("On command got device %s %s", device_name, data)
        else:
            _LOGGER.error("Ignore command because device %s is unknown",
                          device_name)
            return ret

        value = value.decode("utf-8")
        if field == "positionState" and action == "set":
            ret += self.set_state(value, device_name)
        elif field == "targetPosition" and action == "set":
            ret += self.set_position(value, device_name)
        elif field.startswith('timer') and action == "set":
            ret += self.set_timer_state(int(field[-1]), value, device_name)
        elif field == "get" or action == "get":
            ret += retry(self.single_device_status_update,
                         retries=self.update_retries)(device_name, data)

        return ret
Esempio n. 5
0
 def status_update(self):
     _LOGGER.info("Updating %d %s devices", len(self.devices), repr(self))
     ret = []
     for name, data in self.devices.items():
         _LOGGER.debug("Updating %s device '%s' (%s)", repr(self), name,
                       data["mac"])
         from btlewrap import BluetoothBackendException
         try:
             ret += retry(self.update_device_state)(name, data["poller"])
         except BluetoothBackendException as e:
             logger.log_exception(
                 _LOGGER,
                 "Error during update of %s device '%s' (%s): %s",
                 repr(self),
                 name,
                 data["mac"],
                 type(e).__name__,
                 suppress=True)
         except TimeoutError as e:
             logger.log_exception(
                 _LOGGER,
                 "Timeout during update of %s device '%s' (%s): %s",
                 repr(self),
                 name,
                 data["mac"],
                 type(e).__name__,
                 suppress=True)
     return ret
Esempio n. 6
0
  def status_update(self):
    from bluepy import btle

    ret = []
    _LOGGER.info("Updating %d %s devices", len(self.devices), repr(self))
    for name, data in self.devices.items():
      _LOGGER.debug("Updating %s device '%s' (%s)", repr(self), name, data["mac"])
      try:
        ret += retry(self.update_device_state, exception_type=btle.BTLEException)(name, data["thermostat"])
      except btle.BTLEException as e:
        logger.log_exception(_LOGGER, "Error during update of %s device '%s' (%s): %s", repr(self), name, data["mac"], type(e).__name__, suppress=True)
    return ret
Esempio n. 7
0
    def on_command(self, topic, value):
        from bluepy import btle
        from eq3bt import Mode

        default_fallback_mode = Mode.Auto

        topic_without_prefix = topic.replace("{}/".format(self.topic_prefix),
                                             "")
        device_name, method, _ = topic_without_prefix.split("/")

        if device_name in self.devices:
            data = self.devices[device_name]
            thermostat = data["thermostat"]
        else:
            logger.log_exception(
                _LOGGER, "Ignore command because device %s is unknown",
                device_name)
            return []

        value = value.decode("utf-8")
        if method == "mode":
            state_mapping = {
                STATE_HEAT: Mode.Manual,
                STATE_AUTO: Mode.Auto,
                STATE_OFF: Mode.Closed,
            }
            if value in state_mapping:
                value = state_mapping[value]
            else:
                logger.log_exception(_LOGGER, "Invalid mode setting %s", value)
                return []

        elif method == "hold":
            if value == HOLD_BOOST:
                method = "mode"
                value = Mode.Boost
            elif value in (HOLD_COMFORT, HOLD_ECO):
                method = "preset"
            elif value == "off":
                method = "mode"
                value = default_fallback_mode
            else:
                logger.log_exception(_LOGGER, "Invalid hold setting %s", value)
                return []

        elif method == "away":
            method = "mode"
            if value == 'OFF':
                value = default_fallback_mode
            else:
                value = Mode.Away
        elif method == "target_temperature":
            value = float(value)

        _LOGGER.info(
            "Setting %s to %s on %s device '%s' (%s)",
            method,
            value,
            repr(self),
            device_name,
            data["mac"],
        )
        try:
            if method == "preset":
                if value == HOLD_COMFORT:
                    retry(thermostat.activate_comfort,
                          retries=self.command_retries,
                          exception_type=btle.BTLEException)()
                else:
                    retry(thermostat.activate_eco,
                          retries=self.command_retries,
                          exception_type=btle.BTLEException)()
            else:
                retry(setattr,
                      retries=self.command_retries,
                      exception_type=btle.BTLEException)(thermostat, method,
                                                         value)
        except btle.BTLEException as e:
            logger.log_exception(
                _LOGGER,
                "Error setting %s to %s on %s device '%s' (%s): %s",
                method,
                value,
                repr(self),
                device_name,
                data["mac"],
                type(e).__name__,
            )
            return []

        return retry(self.present_device_state,
                     retries=self.command_retries,
                     exception_type=btle.BTLEException)(device_name,
                                                        thermostat)
Esempio n. 8
0
 def on_command(self, topic, value):
     _LOGGER.info("On command called with %s %s", topic, value)
     return retry(self.handle_mqtt_command,
                  retries=self.command_retries)(topic, value)
Esempio n. 9
0
    def status_update(self):
        _LOGGER.info("Updating %d %s devices", len(self.devices), repr(self))

        for device_name, data in self.devices.items():
            yield retry(self.single_device_status_update,
                        retries=self.update_retries)(device_name, data)