Exemplo n.º 1
0
Arquivo: fan.py Projeto: wwtlong/core
def valid_fan_speed_configuration(config):
    """Validate that the fan speed configuration is valid, throws if it isn't."""
    if config.get(
            CONF_SPEED_COMMAND_TOPIC) and not speed_list_without_preset_modes(
                config.get(CONF_SPEED_LIST)):
        raise ValueError("No valid speeds configured")
    return config
Exemplo n.º 2
0
Arquivo: fan.py Projeto: wwtlong/core
    def _setup_from_config(self, config):
        """(Re)Setup the entity."""
        self._speed_range = (
            config.get(CONF_SPEED_RANGE_MIN),
            config.get(CONF_SPEED_RANGE_MAX),
        )
        self._topic = {
            key: config.get(key)
            for key in (
                CONF_STATE_TOPIC,
                CONF_COMMAND_TOPIC,
                CONF_PERCENTAGE_STATE_TOPIC,
                CONF_PERCENTAGE_COMMAND_TOPIC,
                CONF_PRESET_MODE_STATE_TOPIC,
                CONF_PRESET_MODE_COMMAND_TOPIC,
                CONF_SPEED_STATE_TOPIC,
                CONF_SPEED_COMMAND_TOPIC,
                CONF_OSCILLATION_STATE_TOPIC,
                CONF_OSCILLATION_COMMAND_TOPIC,
            )
        }
        self._value_templates = {
            CONF_STATE: config.get(CONF_STATE_VALUE_TEMPLATE),
            ATTR_PERCENTAGE: config.get(CONF_PERCENTAGE_VALUE_TEMPLATE),
            ATTR_PRESET_MODE: config.get(CONF_PRESET_MODE_VALUE_TEMPLATE),
            # ATTR_SPEED is deprecated in the schema, support will be removed after a quarter (2021.7)
            ATTR_SPEED: config.get(CONF_SPEED_VALUE_TEMPLATE),
            ATTR_OSCILLATING: config.get(CONF_OSCILLATION_VALUE_TEMPLATE),
        }
        self._command_templates = {
            CONF_STATE: config.get(CONF_COMMAND_TEMPLATE),
            ATTR_PERCENTAGE: config.get(CONF_PERCENTAGE_COMMAND_TEMPLATE),
            ATTR_PRESET_MODE: config.get(CONF_PRESET_MODE_COMMAND_TEMPLATE),
            ATTR_OSCILLATING: config.get(CONF_OSCILLATION_COMMAND_TEMPLATE),
        }
        self._payload = {
            "STATE_ON": config[CONF_PAYLOAD_ON],
            "STATE_OFF": config[CONF_PAYLOAD_OFF],
            "OSCILLATE_ON_PAYLOAD": config[CONF_PAYLOAD_OSCILLATION_ON],
            "OSCILLATE_OFF_PAYLOAD": config[CONF_PAYLOAD_OSCILLATION_OFF],
            # The use of legacy speeds is deprecated in the schema, support will be removed after a quarter (2021.7)
            "SPEED_LOW": config[CONF_PAYLOAD_LOW_SPEED],
            "SPEED_MEDIUM": config[CONF_PAYLOAD_MEDIUM_SPEED],
            "SPEED_HIGH": config[CONF_PAYLOAD_HIGH_SPEED],
            "SPEED_OFF": config[CONF_PAYLOAD_OFF_SPEED],
        }
        # The use of legacy speeds is deprecated in the schema, support will be removed after a quarter (2021.7)
        self._feature_legacy_speeds = not self._topic[
            CONF_SPEED_COMMAND_TOPIC] is None
        if self._feature_legacy_speeds:
            self._legacy_speeds_list = config[CONF_SPEED_LIST]
            self._legacy_speeds_list_no_off = speed_list_without_preset_modes(
                self._legacy_speeds_list)
        else:
            self._legacy_speeds_list = []

        self._feature_percentage = CONF_PERCENTAGE_COMMAND_TOPIC in config
        self._feature_preset_mode = CONF_PRESET_MODE_COMMAND_TOPIC in config
        if self._feature_preset_mode:
            self._speeds_list = speed_list_without_preset_modes(
                self._legacy_speeds_list + config[CONF_PRESET_MODES_LIST])
            self._preset_modes = (self._legacy_speeds_list +
                                  config[CONF_PRESET_MODES_LIST])
        else:
            self._speeds_list = speed_list_without_preset_modes(
                self._legacy_speeds_list)
            self._preset_modes = []

        if not self._speeds_list or self._feature_percentage:
            self._speed_count = 100
        else:
            self._speed_count = len(self._speeds_list)

        optimistic = config[CONF_OPTIMISTIC]
        self._optimistic = optimistic or self._topic[CONF_STATE_TOPIC] is None
        self._optimistic_oscillation = (
            optimistic or self._topic[CONF_OSCILLATION_STATE_TOPIC] is None)
        self._optimistic_percentage = (
            optimistic or self._topic[CONF_PERCENTAGE_STATE_TOPIC] is None)
        self._optimistic_preset_mode = (
            optimistic or self._topic[CONF_PRESET_MODE_STATE_TOPIC] is None)
        self._optimistic_speed = (optimistic or
                                  self._topic[CONF_SPEED_STATE_TOPIC] is None)

        self._supported_features = 0
        self._supported_features |= (
            self._topic[CONF_OSCILLATION_COMMAND_TOPIC] is not None
            and SUPPORT_OSCILLATE)
        if self._feature_preset_mode and self._speeds_list:
            self._supported_features |= SUPPORT_SET_SPEED
        if self._feature_percentage:
            self._supported_features |= SUPPORT_SET_SPEED
        if self._feature_legacy_speeds:
            self._supported_features |= SUPPORT_SET_SPEED
        if self._feature_preset_mode:
            self._supported_features |= SUPPORT_PRESET_MODE

        for tpl_dict in [self._command_templates, self._value_templates]:
            for key, tpl in tpl_dict.items():
                if tpl is None:
                    tpl_dict[key] = lambda value: value
                else:
                    tpl.hass = self.hass
                    tpl_dict[key] = tpl.async_render_with_possible_json_value