-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Add MQTT climate setting for current humidity #84592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
ef5303a
MQTT Climate: Add support for setting the current humidity via MQTT
Stonos 1375500
MQTT Climate: Add configuration constants related to setting the targ…
Stonos 5f1b7b5
MQTT Climate: Add support for setting the humidity's state topic & te…
Stonos ff85c71
MQTT Climate: Add support for setting the initial humidity
Stonos dac37f1
MQTT Climate: Add support for setting the humidity's command topic & …
Stonos bf6c3f3
MQTT Climate: Add support for setting the min/max humidity
Stonos 4096b88
MQTT Climate: Fix style & tests
Stonos 1654500
MQTT Climate: Set the initial humidity to None
Stonos cade91e
MQTT Climate: Rename _set_mqtt_attribute to _set_climate_attribute an…
Stonos d41926c
MQTT Climate: Copy humidity range validation from MQTT Humidifier
Stonos 39b0086
MQTT Climate: Remove CONF_HUMIDITY_INITIAL
Stonos 925d87d
MQTT Climate: Only enable support for TARGET_HUMIDITY when the comman…
Stonos 58d710a
MQTT Climate: Check if setting the target humidity is supported befor…
Stonos f29d2ca
MQTT Climate: Make sure that CONF_HUMIDITY_COMMAND_TOPIC has been con…
Stonos 6edfe3b
MQTT Climate: Fix broken tests
Stonos c56806a
MQTT Climate: Add test for optimistically setting the target humidity
Stonos 0051a2c
MQTT Climate: Remove references to "temperature" in handle_climate_at…
Stonos 81ca8dc
MQTT Climate: Add additional humidity-related tests
Stonos f88efcf
MQTT Climate: Remove supported feature check in handle_target_humidit…
Stonos 3410b31
MQTT Climate: Remove supported feature check in async_set_humidity
Stonos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,9 @@ | |
| ATTR_HVAC_MODE, | ||
| ATTR_TARGET_TEMP_HIGH, | ||
| ATTR_TARGET_TEMP_LOW, | ||
| DEFAULT_MAX_HUMIDITY, | ||
| DEFAULT_MAX_TEMP, | ||
| DEFAULT_MIN_HUMIDITY, | ||
| DEFAULT_MIN_TEMP, | ||
| FAN_AUTO, | ||
| FAN_HIGH, | ||
|
|
@@ -85,6 +87,8 @@ | |
| CONF_AWAY_MODE_STATE_TEMPLATE = "away_mode_state_template" | ||
| CONF_AWAY_MODE_STATE_TOPIC = "away_mode_state_topic" | ||
|
|
||
| CONF_CURRENT_HUMIDITY_TEMPLATE = "current_humidity_template" | ||
| CONF_CURRENT_HUMIDITY_TOPIC = "current_humidity_topic" | ||
| CONF_CURRENT_TEMP_TEMPLATE = "current_temperature_template" | ||
| CONF_CURRENT_TEMP_TOPIC = "current_temperature_topic" | ||
| CONF_FAN_MODE_COMMAND_TEMPLATE = "fan_mode_command_template" | ||
|
|
@@ -99,6 +103,12 @@ | |
| CONF_HOLD_STATE_TOPIC = "hold_state_topic" | ||
| CONF_HOLD_LIST = "hold_modes" | ||
|
|
||
| CONF_HUMIDITY_COMMAND_TEMPLATE = "target_humidity_command_template" | ||
| CONF_HUMIDITY_COMMAND_TOPIC = "target_humidity_command_topic" | ||
| CONF_HUMIDITY_STATE_TEMPLATE = "target_humidity_state_template" | ||
| CONF_HUMIDITY_STATE_TOPIC = "target_humidity_state_topic" | ||
| CONF_HUMIDITY_MAX = "max_humidity" | ||
| CONF_HUMIDITY_MIN = "min_humidity" | ||
| CONF_MODE_COMMAND_TEMPLATE = "mode_command_template" | ||
| CONF_MODE_COMMAND_TOPIC = "mode_command_topic" | ||
| CONF_MODE_LIST = "modes" | ||
|
|
@@ -164,8 +174,10 @@ | |
|
|
||
| VALUE_TEMPLATE_KEYS = ( | ||
| CONF_AUX_STATE_TEMPLATE, | ||
| CONF_CURRENT_HUMIDITY_TEMPLATE, | ||
| CONF_CURRENT_TEMP_TEMPLATE, | ||
| CONF_FAN_MODE_STATE_TEMPLATE, | ||
| CONF_HUMIDITY_STATE_TEMPLATE, | ||
| CONF_MODE_STATE_TEMPLATE, | ||
| CONF_POWER_STATE_TEMPLATE, | ||
| CONF_ACTION_TEMPLATE, | ||
|
|
@@ -178,6 +190,7 @@ | |
|
|
||
| COMMAND_TEMPLATE_KEYS = { | ||
| CONF_FAN_MODE_COMMAND_TEMPLATE, | ||
| CONF_HUMIDITY_COMMAND_TEMPLATE, | ||
| CONF_MODE_COMMAND_TEMPLATE, | ||
| CONF_PRESET_MODE_COMMAND_TEMPLATE, | ||
| CONF_SWING_MODE_COMMAND_TEMPLATE, | ||
|
|
@@ -191,9 +204,12 @@ | |
| CONF_ACTION_TOPIC, | ||
| CONF_AUX_COMMAND_TOPIC, | ||
| CONF_AUX_STATE_TOPIC, | ||
| CONF_CURRENT_HUMIDITY_TOPIC, | ||
| CONF_CURRENT_TEMP_TOPIC, | ||
| CONF_FAN_MODE_COMMAND_TOPIC, | ||
| CONF_FAN_MODE_STATE_TOPIC, | ||
| CONF_HUMIDITY_COMMAND_TOPIC, | ||
| CONF_HUMIDITY_STATE_TOPIC, | ||
| CONF_MODE_COMMAND_TOPIC, | ||
| CONF_MODE_STATE_TOPIC, | ||
| CONF_POWER_COMMAND_TOPIC, | ||
|
|
@@ -218,11 +234,36 @@ def valid_preset_mode_configuration(config: ConfigType) -> ConfigType: | |
| return config | ||
|
|
||
|
|
||
| def valid_humidity_range_configuration(config: ConfigType) -> ConfigType: | ||
| """Validate that the target_humidity range configuration is valid, throws if it isn't.""" | ||
| if config[CONF_HUMIDITY_MIN] >= config[CONF_HUMIDITY_MAX]: | ||
| raise ValueError("target_humidity_max must be > target_humidity_min") | ||
| if config[CONF_HUMIDITY_MAX] > 100: | ||
| raise ValueError("max_humidity must be <= 100") | ||
|
|
||
| return config | ||
|
|
||
|
|
||
| def valid_humidity_state_configuration(config: ConfigType) -> ConfigType: | ||
| """Validate that if CONF_HUMIDITY_STATE_TOPIC is set then CONF_HUMIDITY_COMMAND_TOPIC is also set.""" | ||
| if ( | ||
| CONF_HUMIDITY_STATE_TOPIC in config | ||
| and CONF_HUMIDITY_COMMAND_TOPIC not in config | ||
| ): | ||
| raise ValueError( | ||
| f"{CONF_HUMIDITY_STATE_TOPIC} cannot be used without {CONF_HUMIDITY_COMMAND_TOPIC}" | ||
| ) | ||
|
|
||
| return config | ||
|
|
||
|
|
||
| _PLATFORM_SCHEMA_BASE = MQTT_BASE_SCHEMA.extend( | ||
| { | ||
| vol.Optional(CONF_AUX_COMMAND_TOPIC): valid_publish_topic, | ||
| vol.Optional(CONF_AUX_STATE_TEMPLATE): cv.template, | ||
| vol.Optional(CONF_AUX_STATE_TOPIC): valid_subscribe_topic, | ||
| vol.Optional(CONF_CURRENT_HUMIDITY_TEMPLATE): cv.template, | ||
| vol.Optional(CONF_CURRENT_HUMIDITY_TOPIC): valid_subscribe_topic, | ||
|
Stonos marked this conversation as resolved.
|
||
| vol.Optional(CONF_CURRENT_TEMP_TEMPLATE): cv.template, | ||
| vol.Optional(CONF_CURRENT_TEMP_TOPIC): valid_subscribe_topic, | ||
| vol.Optional(CONF_FAN_MODE_COMMAND_TEMPLATE): cv.template, | ||
|
|
@@ -233,6 +274,16 @@ def valid_preset_mode_configuration(config: ConfigType) -> ConfigType: | |
| ): cv.ensure_list, | ||
| vol.Optional(CONF_FAN_MODE_STATE_TEMPLATE): cv.template, | ||
| vol.Optional(CONF_FAN_MODE_STATE_TOPIC): valid_subscribe_topic, | ||
| vol.Optional(CONF_HUMIDITY_COMMAND_TEMPLATE): cv.template, | ||
| vol.Optional(CONF_HUMIDITY_COMMAND_TOPIC): valid_publish_topic, | ||
| vol.Optional(CONF_HUMIDITY_MIN, default=DEFAULT_MIN_HUMIDITY): vol.Coerce( | ||
| float | ||
| ), | ||
| vol.Optional(CONF_HUMIDITY_MAX, default=DEFAULT_MAX_HUMIDITY): vol.Coerce( | ||
| float | ||
| ), | ||
| vol.Optional(CONF_HUMIDITY_STATE_TEMPLATE): cv.template, | ||
| vol.Optional(CONF_HUMIDITY_STATE_TOPIC): valid_subscribe_topic, | ||
|
Stonos marked this conversation as resolved.
|
||
| vol.Optional(CONF_MODE_COMMAND_TEMPLATE): cv.template, | ||
| vol.Optional(CONF_MODE_COMMAND_TOPIC): valid_publish_topic, | ||
| vol.Optional( | ||
|
|
@@ -313,6 +364,8 @@ def valid_preset_mode_configuration(config: ConfigType) -> ConfigType: | |
| cv.removed(CONF_HOLD_LIST), | ||
| _PLATFORM_SCHEMA_BASE, | ||
| valid_preset_mode_configuration, | ||
| valid_humidity_range_configuration, | ||
| valid_humidity_state_configuration, | ||
| ) | ||
|
|
||
| # Configuring MQTT Climate under the climate platform key was deprecated in HA Core 2022.6 | ||
|
|
@@ -337,6 +390,8 @@ def valid_preset_mode_configuration(config: ConfigType) -> ConfigType: | |
| cv.removed(CONF_HOLD_STATE_TOPIC), | ||
| cv.removed(CONF_HOLD_LIST), | ||
| valid_preset_mode_configuration, | ||
| valid_humidity_range_configuration, | ||
| valid_humidity_state_configuration, | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -396,6 +451,8 @@ def _setup_from_config(self, config: ConfigType) -> None: | |
| self._attr_hvac_modes = config[CONF_MODE_LIST] | ||
| self._attr_min_temp = config[CONF_TEMP_MIN] | ||
| self._attr_max_temp = config[CONF_TEMP_MAX] | ||
| self._attr_min_humidity = config[CONF_HUMIDITY_MIN] | ||
| self._attr_max_humidity = config[CONF_HUMIDITY_MAX] | ||
| self._attr_precision = config.get(CONF_PRECISION, super().precision) | ||
| self._attr_fan_modes = config[CONF_FAN_MODE_LIST] | ||
| self._attr_swing_modes = config[CONF_SWING_MODE_LIST] | ||
|
|
@@ -485,6 +542,9 @@ def _setup_from_config(self, config: ConfigType) -> None: | |
| ): | ||
| support |= ClimateEntityFeature.TARGET_TEMPERATURE_RANGE | ||
|
|
||
| if self._topic[CONF_HUMIDITY_COMMAND_TOPIC] is not None: | ||
| support |= ClimateEntityFeature.TARGET_HUMIDITY | ||
|
|
||
| if (self._topic[CONF_FAN_MODE_STATE_TOPIC] is not None) or ( | ||
| self._topic[CONF_FAN_MODE_COMMAND_TOPIC] is not None | ||
| ): | ||
|
|
@@ -554,23 +614,23 @@ def handle_action_received(msg: ReceiveMessage) -> None: | |
| add_subscription(topics, CONF_ACTION_TOPIC, handle_action_received) | ||
|
|
||
| @callback | ||
| def handle_temperature_received( | ||
| def handle_climate_attribute_received( | ||
|
Stonos marked this conversation as resolved.
|
||
| msg: ReceiveMessage, template_name: str, attr: str | ||
| ) -> None: | ||
| """Handle temperature coming via MQTT.""" | ||
| """Handle climate attributes coming via MQTT.""" | ||
| payload = render_template(msg, template_name) | ||
|
|
||
| try: | ||
| setattr(self, attr, float(payload)) | ||
| get_mqtt_data(self.hass).state_write_requests.write_state_request(self) | ||
| except ValueError: | ||
| _LOGGER.error("Could not parse temperature from %s", payload) | ||
| _LOGGER.error("Could not parse %s from %s", template_name, payload) | ||
|
|
||
| @callback | ||
| @log_messages(self.hass, self.entity_id) | ||
| def handle_current_temperature_received(msg: ReceiveMessage) -> None: | ||
| """Handle current temperature coming via MQTT.""" | ||
| handle_temperature_received( | ||
| handle_climate_attribute_received( | ||
| msg, CONF_CURRENT_TEMP_TEMPLATE, "_attr_current_temperature" | ||
| ) | ||
|
|
||
|
|
@@ -582,7 +642,7 @@ def handle_current_temperature_received(msg: ReceiveMessage) -> None: | |
| @log_messages(self.hass, self.entity_id) | ||
| def handle_target_temperature_received(msg: ReceiveMessage) -> None: | ||
| """Handle target temperature coming via MQTT.""" | ||
| handle_temperature_received( | ||
| handle_climate_attribute_received( | ||
| msg, CONF_TEMP_STATE_TEMPLATE, "_attr_target_temperature" | ||
| ) | ||
|
|
||
|
|
@@ -594,7 +654,7 @@ def handle_target_temperature_received(msg: ReceiveMessage) -> None: | |
| @log_messages(self.hass, self.entity_id) | ||
| def handle_temperature_low_received(msg: ReceiveMessage) -> None: | ||
| """Handle target temperature low coming via MQTT.""" | ||
| handle_temperature_received( | ||
| handle_climate_attribute_received( | ||
| msg, CONF_TEMP_LOW_STATE_TEMPLATE, "_attr_target_temperature_low" | ||
| ) | ||
|
|
||
|
|
@@ -606,14 +666,39 @@ def handle_temperature_low_received(msg: ReceiveMessage) -> None: | |
| @log_messages(self.hass, self.entity_id) | ||
| def handle_temperature_high_received(msg: ReceiveMessage) -> None: | ||
| """Handle target temperature high coming via MQTT.""" | ||
| handle_temperature_received( | ||
| handle_climate_attribute_received( | ||
| msg, CONF_TEMP_HIGH_STATE_TEMPLATE, "_attr_target_temperature_high" | ||
| ) | ||
|
|
||
| add_subscription( | ||
| topics, CONF_TEMP_HIGH_STATE_TOPIC, handle_temperature_high_received | ||
| ) | ||
|
|
||
| @callback | ||
| @log_messages(self.hass, self.entity_id) | ||
| def handle_current_humidity_received(msg: ReceiveMessage) -> None: | ||
| """Handle current humidity coming via MQTT.""" | ||
| handle_climate_attribute_received( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just for the record |
||
| msg, CONF_CURRENT_HUMIDITY_TEMPLATE, "_attr_current_humidity" | ||
| ) | ||
|
|
||
| add_subscription( | ||
| topics, CONF_CURRENT_HUMIDITY_TOPIC, handle_current_humidity_received | ||
| ) | ||
|
|
||
| @callback | ||
| @log_messages(self.hass, self.entity_id) | ||
| def handle_target_humidity_received(msg: ReceiveMessage) -> None: | ||
| """Handle target humidity coming via MQTT.""" | ||
|
|
||
| handle_climate_attribute_received( | ||
|
Stonos marked this conversation as resolved.
|
||
| msg, CONF_HUMIDITY_STATE_TEMPLATE, "_attr_target_humidity" | ||
| ) | ||
|
|
||
| add_subscription( | ||
| topics, CONF_HUMIDITY_STATE_TOPIC, handle_target_humidity_received | ||
| ) | ||
|
|
||
| @callback | ||
| def handle_mode_received( | ||
| msg: ReceiveMessage, template_name: str, attr: str, mode_list: str | ||
|
|
@@ -744,7 +829,7 @@ async def _publish(self, topic: str, payload: PublishPayloadType) -> None: | |
| self._config[CONF_ENCODING], | ||
| ) | ||
|
|
||
| async def _set_temperature( | ||
| async def _set_climate_attribute( | ||
| self, | ||
| temp: float | None, | ||
| cmnd_topic: str, | ||
|
|
@@ -770,23 +855,23 @@ async def async_set_temperature(self, **kwargs: Any) -> None: | |
| if (operation_mode := kwargs.get(ATTR_HVAC_MODE)) is not None: | ||
| await self.async_set_hvac_mode(operation_mode) | ||
|
|
||
| changed = await self._set_temperature( | ||
| changed = await self._set_climate_attribute( | ||
| kwargs.get(ATTR_TEMPERATURE), | ||
| CONF_TEMP_COMMAND_TOPIC, | ||
| CONF_TEMP_COMMAND_TEMPLATE, | ||
| CONF_TEMP_STATE_TOPIC, | ||
| "_attr_target_temperature", | ||
| ) | ||
|
|
||
| changed |= await self._set_temperature( | ||
| changed |= await self._set_climate_attribute( | ||
| kwargs.get(ATTR_TARGET_TEMP_LOW), | ||
| CONF_TEMP_LOW_COMMAND_TOPIC, | ||
| CONF_TEMP_LOW_COMMAND_TEMPLATE, | ||
| CONF_TEMP_LOW_STATE_TOPIC, | ||
| "_attr_target_temperature_low", | ||
| ) | ||
|
|
||
| changed |= await self._set_temperature( | ||
| changed |= await self._set_climate_attribute( | ||
| kwargs.get(ATTR_TARGET_TEMP_HIGH), | ||
| CONF_TEMP_HIGH_COMMAND_TOPIC, | ||
| CONF_TEMP_HIGH_COMMAND_TEMPLATE, | ||
|
|
@@ -798,6 +883,19 @@ async def async_set_temperature(self, **kwargs: Any) -> None: | |
| return | ||
| self.async_write_ha_state() | ||
|
|
||
| async def async_set_humidity(self, humidity: int) -> None: | ||
| """Set new target humidity.""" | ||
|
|
||
| await self._set_climate_attribute( | ||
|
Stonos marked this conversation as resolved.
|
||
| humidity, | ||
| CONF_HUMIDITY_COMMAND_TOPIC, | ||
| CONF_HUMIDITY_COMMAND_TEMPLATE, | ||
| CONF_HUMIDITY_STATE_TOPIC, | ||
| "_attr_target_humidity", | ||
| ) | ||
|
|
||
| self.async_write_ha_state() | ||
|
|
||
| async def async_set_swing_mode(self, swing_mode: str) -> None: | ||
| """Set new swing mode.""" | ||
| payload = self._command_templates[CONF_SWING_MODE_COMMAND_TEMPLATE](swing_mode) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.