mqtt fan percentage to speed_range and received speed_state fix (#49060)

* percentage to speed_range and get speed state fix

* Update homeassistant/components/mqtt/fan.py

* Update homeassistant/components/mqtt/fan.py

* Update homeassistant/components/mqtt/fan.py

* Update homeassistant/components/mqtt/fan.py

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Jan Bouwhuis 2021-04-12 01:53:07 +02:00 committed by GitHub
parent 41ff6fc278
commit 74d7293ab8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 10 deletions

View File

@ -1,6 +1,7 @@
"""Support for MQTT fans.""" """Support for MQTT fans."""
import functools import functools
import logging import logging
import math
import voluptuous as vol import voluptuous as vol
@ -441,7 +442,6 @@ class MqttFan(MqttEntity, FanEntity):
) )
return return
if not self._feature_percentage:
if speed in self._legacy_speeds_list_no_off: if speed in self._legacy_speeds_list_no_off:
self._percentage = ordered_list_item_to_percentage( self._percentage = ordered_list_item_to_percentage(
self._legacy_speeds_list_no_off, speed self._legacy_speeds_list_no_off, speed
@ -592,7 +592,7 @@ class MqttFan(MqttEntity, FanEntity):
This method is a coroutine. This method is a coroutine.
""" """
percentage_payload = int( percentage_payload = math.ceil(
percentage_to_ranged_value(self._speed_range, percentage) percentage_to_ranged_value(self._speed_range, percentage)
) )
mqtt_payload = self._command_templates[ATTR_PERCENTAGE](percentage_payload) mqtt_payload = self._command_templates[ATTR_PERCENTAGE](percentage_payload)

View File

@ -618,7 +618,7 @@ async def test_sending_mqtt_commands_with_alternate_speed_range(hass, mqtt_mock)
"percentage_state_topic": "percentage-state-topic1", "percentage_state_topic": "percentage-state-topic1",
"percentage_command_topic": "percentage-command-topic1", "percentage_command_topic": "percentage-command-topic1",
"speed_range_min": 1, "speed_range_min": 1,
"speed_range_max": 100, "speed_range_max": 3,
}, },
{ {
"platform": "mqtt", "platform": "mqtt",
@ -651,9 +651,25 @@ async def test_sending_mqtt_commands_with_alternate_speed_range(hass, mqtt_mock)
state = hass.states.get("fan.test1") state = hass.states.get("fan.test1")
assert state.attributes.get(ATTR_ASSUMED_STATE) assert state.attributes.get(ATTR_ASSUMED_STATE)
await common.async_set_percentage(hass, "fan.test1", 33)
mqtt_mock.async_publish.assert_called_once_with(
"percentage-command-topic1", "1", 0, False
)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get("fan.test1")
assert state.attributes.get(ATTR_ASSUMED_STATE)
await common.async_set_percentage(hass, "fan.test1", 66)
mqtt_mock.async_publish.assert_called_once_with(
"percentage-command-topic1", "2", 0, False
)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get("fan.test1")
assert state.attributes.get(ATTR_ASSUMED_STATE)
await common.async_set_percentage(hass, "fan.test1", 100) await common.async_set_percentage(hass, "fan.test1", 100)
mqtt_mock.async_publish.assert_called_once_with( mqtt_mock.async_publish.assert_called_once_with(
"percentage-command-topic1", "100", 0, False "percentage-command-topic1", "3", 0, False
) )
mqtt_mock.async_publish.reset_mock() mqtt_mock.async_publish.reset_mock()
state = hass.states.get("fan.test1") state = hass.states.get("fan.test1")