Add discovery support to mqtt climate component. (#15085)
* Add discovery support to mqtt climate component. * - Fix flake8 error (./homeassistant/components/climate/mqtt.py:130:1: D202 No blank lines allowed after function docstring) - Fix test error (since climate component was expected not to work - changed it to "lock" component, which also does not have MQTT discovery support yet) * Fix old assert statement to reflect new lock component usage * Change invalid MQTT discovery component type from 'lock' to 'timer', since contrary to the documentation the lock component is properly supported when using MQTT discovery. * Make configuration of invalid MQTT config component a single point of entry to prevent missing the assertion later in the code when changing. * Add new testcases to cover not-yet-covered code paths in https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/climate/mqtt.py
This commit is contained in:
1 parent
d3ceb9080c
commit
73034c933e
4 files changed
+118
-4
No files matched your search
@@ -137,6 +137,37 @@ class TestMQTTClimate(unittest.TestCase):
|
||||
self.assertEqual("cool", state.attributes.get('operation_mode'))
|
||||
self.assertEqual("cool", state.state)
|
||||
|
||||
def test_set_operation_with_power_command(self):
|
||||
"""Test setting of new operation mode with power command enabled."""
|
||||
config = copy.deepcopy(DEFAULT_CONFIG)
|
||||
config['climate']['power_command_topic'] = 'power-command'
|
||||
assert setup_component(self.hass, climate.DOMAIN, config)
|
||||
|
||||
state = self.hass.states.get(ENTITY_CLIMATE)
|
||||
self.assertEqual("off", state.attributes.get('operation_mode'))
|
||||
self.assertEqual("off", state.state)
|
||||
climate.set_operation_mode(self.hass, "on", ENTITY_CLIMATE)
|
||||
self.hass.block_till_done()
|
||||
state = self.hass.states.get(ENTITY_CLIMATE)
|
||||
self.assertEqual("on", state.attributes.get('operation_mode'))
|
||||
self.assertEqual("on", state.state)
|
||||
self.mock_publish.async_publish.assert_has_calls([
|
||||
unittest.mock.call('power-command', 'ON', 0, False),
|
||||
unittest.mock.call('mode-topic', 'on', 0, False)
|
||||
])
|
||||
self.mock_publish.async_publish.reset_mock()
|
||||
|
||||
climate.set_operation_mode(self.hass, "off", ENTITY_CLIMATE)
|
||||
self.hass.block_till_done()
|
||||
state = self.hass.states.get(ENTITY_CLIMATE)
|
||||
self.assertEqual("off", state.attributes.get('operation_mode'))
|
||||
self.assertEqual("off", state.state)
|
||||
self.mock_publish.async_publish.assert_has_calls([
|
||||
unittest.mock.call('power-command', 'OFF', 0, False),
|
||||
unittest.mock.call('mode-topic', 'off', 0, False)
|
||||
])
|
||||
self.mock_publish.async_publish.reset_mock()
|
||||
|
||||
def test_set_fan_mode_bad_attr(self):
|
||||
"""Test setting fan mode without required attribute."""
|
||||
assert setup_component(self.hass, climate.DOMAIN, DEFAULT_CONFIG)
|
||||
@@ -241,6 +272,8 @@ class TestMQTTClimate(unittest.TestCase):
|
||||
self.assertEqual(21, state.attributes.get('temperature'))
|
||||
climate.set_operation_mode(self.hass, 'heat', ENTITY_CLIMATE)
|
||||
self.hass.block_till_done()
|
||||
state = self.hass.states.get(ENTITY_CLIMATE)
|
||||
self.assertEqual('heat', state.attributes.get('operation_mode'))
|
||||
self.mock_publish.async_publish.assert_called_once_with(
|
||||
'mode-topic', 'heat', 0, False)
|
||||
self.mock_publish.async_publish.reset_mock()
|
||||
@@ -252,6 +285,21 @@ class TestMQTTClimate(unittest.TestCase):
|
||||
self.mock_publish.async_publish.assert_called_once_with(
|
||||
'temperature-topic', 47, 0, False)
|
||||
|
||||
# also test directly supplying the operation mode to set_temperature
|
||||
self.mock_publish.async_publish.reset_mock()
|
||||
climate.set_temperature(self.hass, temperature=21,
|
||||
operation_mode="cool",
|
||||
entity_id=ENTITY_CLIMATE)
|
||||
self.hass.block_till_done()
|
||||
state = self.hass.states.get(ENTITY_CLIMATE)
|
||||
self.assertEqual('cool', state.attributes.get('operation_mode'))
|
||||
self.assertEqual(21, state.attributes.get('temperature'))
|
||||
self.mock_publish.async_publish.assert_has_calls([
|
||||
unittest.mock.call('mode-topic', 'cool', 0, False),
|
||||
unittest.mock.call('temperature-topic', 21, 0, False)
|
||||
])
|
||||
self.mock_publish.async_publish.reset_mock()
|
||||
|
||||
def test_set_target_temperature_pessimistic(self):
|
||||
"""Test setting the target temperature."""
|
||||
config = copy.deepcopy(DEFAULT_CONFIG)
|
||||
@@ -508,13 +556,28 @@ class TestMQTTClimate(unittest.TestCase):
|
||||
state = self.hass.states.get(ENTITY_CLIMATE)
|
||||
self.assertEqual("on", state.attributes.get('swing_mode'))
|
||||
|
||||
# Temperature
|
||||
# Temperature - with valid value
|
||||
self.assertEqual(21, state.attributes.get('temperature'))
|
||||
fire_mqtt_message(self.hass, 'temperature-state', '"1031"')
|
||||
self.hass.block_till_done()
|
||||
state = self.hass.states.get(ENTITY_CLIMATE)
|
||||
self.assertEqual(1031, state.attributes.get('temperature'))
|
||||
|
||||
# Temperature - with invalid value
|
||||
with self.assertLogs(level='ERROR') as log:
|
||||
fire_mqtt_message(self.hass, 'temperature-state', '"-INVALID-"')
|
||||
self.hass.block_till_done()
|
||||
state = self.hass.states.get(ENTITY_CLIMATE)
|
||||
# make sure, the invalid value gets logged...
|
||||
self.assertEqual(len(log.output), 1)
|
||||
self.assertEqual(len(log.records), 1)
|
||||
self.assertIn(
|
||||
"Could not parse temperature from -INVALID-",
|
||||
log.output[0]
|
||||
)
|
||||
# ... but the actual value stays unchanged.
|
||||
self.assertEqual(1031, state.attributes.get('temperature'))
|
||||
|
||||
# Away Mode
|
||||
self.assertEqual('off', state.attributes.get('away_mode'))
|
||||
fire_mqtt_message(self.hass, 'away-state', '"ON"')
|
||||
@@ -522,6 +585,17 @@ class TestMQTTClimate(unittest.TestCase):
|
||||
state = self.hass.states.get(ENTITY_CLIMATE)
|
||||
self.assertEqual('on', state.attributes.get('away_mode'))
|
||||
|
||||
# Away Mode with JSON values
|
||||
fire_mqtt_message(self.hass, 'away-state', 'false')
|
||||
self.hass.block_till_done()
|
||||
state = self.hass.states.get(ENTITY_CLIMATE)
|
||||
self.assertEqual('off', state.attributes.get('away_mode'))
|
||||
|
||||
fire_mqtt_message(self.hass, 'away-state', 'true')
|
||||
self.hass.block_till_done()
|
||||
state = self.hass.states.get(ENTITY_CLIMATE)
|
||||
self.assertEqual('on', state.attributes.get('away_mode'))
|
||||
|
||||
# Hold Mode
|
||||
self.assertEqual(None, state.attributes.get('hold_mode'))
|
||||
fire_mqtt_message(self.hass, 'hold-state', """
|
||||
@@ -538,6 +612,12 @@ class TestMQTTClimate(unittest.TestCase):
|
||||
state = self.hass.states.get(ENTITY_CLIMATE)
|
||||
self.assertEqual('on', state.attributes.get('aux_heat'))
|
||||
|
||||
# anything other than 'switchmeon' should turn Aux mode off
|
||||
fire_mqtt_message(self.hass, 'aux-state', 'somerandomstring')
|
||||
self.hass.block_till_done()
|
||||
state = self.hass.states.get(ENTITY_CLIMATE)
|
||||
self.assertEqual('off', state.attributes.get('aux_heat'))
|
||||
|
||||
# Current temperature
|
||||
fire_mqtt_message(self.hass, 'current-temperature', '"74656"')
|
||||
self.hass.block_till_done()
|
||||
|
||||
Reference in new issue
Block a user