mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Tests for MQTT sensor/switch
This commit is contained in:
parent
6de64d7695
commit
d35f5b9f97
@ -124,14 +124,17 @@ def mock_http_component(hass):
|
|||||||
hass.config.components.append('http')
|
hass.config.components.append('http')
|
||||||
|
|
||||||
|
|
||||||
def mock_mqtt_component(hass):
|
@mock.patch('homeassistant.components.mqtt.MQTT')
|
||||||
with mock.patch('homeassistant.components.mqtt.MQTT'):
|
@mock.patch('homeassistant.components.mqtt.MQTT.publish')
|
||||||
mqtt.setup(hass, {
|
def mock_mqtt_component(hass, mock_mqtt, mock_mqtt_publish):
|
||||||
mqtt.DOMAIN: {
|
mqtt.setup(hass, {
|
||||||
mqtt.CONF_BROKER: 'mock-broker',
|
mqtt.DOMAIN: {
|
||||||
}
|
mqtt.CONF_BROKER: 'mock-broker',
|
||||||
})
|
}
|
||||||
hass.config.components.append(mqtt.DOMAIN)
|
})
|
||||||
|
hass.config.components.append(mqtt.DOMAIN)
|
||||||
|
|
||||||
|
return mock_mqtt_publish
|
||||||
|
|
||||||
|
|
||||||
class MockHTTP(object):
|
class MockHTTP(object):
|
||||||
|
@ -11,7 +11,7 @@ import homeassistant.components.automation as automation
|
|||||||
from tests.common import mock_mqtt_component, fire_mqtt_message
|
from tests.common import mock_mqtt_component, fire_mqtt_message
|
||||||
|
|
||||||
|
|
||||||
class TestAutomationState(unittest.TestCase):
|
class TestAutomationMQTT(unittest.TestCase):
|
||||||
""" Test the event automation. """
|
""" Test the event automation. """
|
||||||
|
|
||||||
def setUp(self): # pylint: disable=invalid-name
|
def setUp(self): # pylint: disable=invalid-name
|
||||||
|
@ -11,7 +11,7 @@ from homeassistant.components import automation, zone
|
|||||||
from tests.common import get_test_home_assistant
|
from tests.common import get_test_home_assistant
|
||||||
|
|
||||||
|
|
||||||
class TestAutomationEvent(unittest.TestCase):
|
class TestAutomationZone(unittest.TestCase):
|
||||||
""" Test the event automation. """
|
""" Test the event automation. """
|
||||||
|
|
||||||
def setUp(self): # pylint: disable=invalid-name
|
def setUp(self): # pylint: disable=invalid-name
|
||||||
|
0
tests/components/sensor/__init__.py
Normal file
0
tests/components/sensor/__init__.py
Normal file
41
tests/components/sensor/test_mqtt.py
Normal file
41
tests/components/sensor/test_mqtt.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
"""
|
||||||
|
tests.components.sensor.test_mqtt
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Tests mqtt sensor.
|
||||||
|
"""
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import homeassistant.core as ha
|
||||||
|
import homeassistant.components.sensor as sensor
|
||||||
|
from tests.common import mock_mqtt_component, fire_mqtt_message
|
||||||
|
|
||||||
|
|
||||||
|
class TestSensorMQTT(unittest.TestCase):
|
||||||
|
""" Test the MQTT sensor. """
|
||||||
|
|
||||||
|
def setUp(self): # pylint: disable=invalid-name
|
||||||
|
self.hass = ha.HomeAssistant()
|
||||||
|
mock_mqtt_component(self.hass)
|
||||||
|
|
||||||
|
def tearDown(self): # pylint: disable=invalid-name
|
||||||
|
""" Stop down stuff we started. """
|
||||||
|
self.hass.stop()
|
||||||
|
|
||||||
|
def test_setting_sensor_value_via_mqtt_message(self):
|
||||||
|
self.assertTrue(sensor.setup(self.hass, {
|
||||||
|
'sensor': {
|
||||||
|
'platform': 'mqtt',
|
||||||
|
'name': 'test',
|
||||||
|
'state_topic': 'test-topic',
|
||||||
|
'unit_of_measurement': 'fav unit'
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
fire_mqtt_message(self.hass, 'test-topic', '100')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
state = self.hass.states.get('sensor.test')
|
||||||
|
|
||||||
|
self.assertEqual('100', state.state)
|
||||||
|
self.assertEqual('fav unit',
|
||||||
|
state.attributes.get('unit_of_measurement'))
|
0
tests/components/switch/__init__.py
Normal file
0
tests/components/switch/__init__.py
Normal file
82
tests/components/switch/test_mqtt.py
Normal file
82
tests/components/switch/test_mqtt.py
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
"""
|
||||||
|
tests.components.switch.test_mqtt
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Tests mqtt switch.
|
||||||
|
"""
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from homeassistant.const import STATE_ON, STATE_OFF
|
||||||
|
import homeassistant.core as ha
|
||||||
|
import homeassistant.components.switch as switch
|
||||||
|
from tests.common import mock_mqtt_component, fire_mqtt_message
|
||||||
|
|
||||||
|
|
||||||
|
class TestSensorMQTT(unittest.TestCase):
|
||||||
|
""" Test the MQTT switch. """
|
||||||
|
|
||||||
|
def setUp(self): # pylint: disable=invalid-name
|
||||||
|
self.hass = ha.HomeAssistant()
|
||||||
|
self.mock_publish = mock_mqtt_component(self.hass)
|
||||||
|
|
||||||
|
def tearDown(self): # pylint: disable=invalid-name
|
||||||
|
""" Stop down stuff we started. """
|
||||||
|
self.hass.stop()
|
||||||
|
|
||||||
|
def test_controlling_state_via_topic(self):
|
||||||
|
self.assertTrue(switch.setup(self.hass, {
|
||||||
|
'switch': {
|
||||||
|
'platform': 'mqtt',
|
||||||
|
'name': 'test',
|
||||||
|
'state_topic': 'state-topic',
|
||||||
|
'command_topic': 'command-topic',
|
||||||
|
'payload_on': 'beer on',
|
||||||
|
'payload_off': 'beer off'
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.test')
|
||||||
|
self.assertEqual(STATE_OFF, state.state)
|
||||||
|
|
||||||
|
fire_mqtt_message(self.hass, 'state-topic', 'beer on')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.test')
|
||||||
|
self.assertEqual(STATE_ON, state.state)
|
||||||
|
|
||||||
|
fire_mqtt_message(self.hass, 'state-topic', 'beer off')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.test')
|
||||||
|
self.assertEqual(STATE_OFF, state.state)
|
||||||
|
|
||||||
|
def test_sending_mqtt_commands_and_optimistic(self):
|
||||||
|
self.assertTrue(switch.setup(self.hass, {
|
||||||
|
'switch': {
|
||||||
|
'platform': 'mqtt',
|
||||||
|
'name': 'test',
|
||||||
|
'command_topic': 'command-topic',
|
||||||
|
'payload_on': 'beer on',
|
||||||
|
'payload_off': 'beer off',
|
||||||
|
'qos': 2
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.test')
|
||||||
|
self.assertEqual(STATE_OFF, state.state)
|
||||||
|
|
||||||
|
switch.turn_on(self.hass, 'switch.test')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
self.assertEqual(('command-topic', 'beer on', 2),
|
||||||
|
self.mock_publish.mock_calls[-1][1])
|
||||||
|
state = self.hass.states.get('switch.test')
|
||||||
|
self.assertEqual(STATE_ON, state.state)
|
||||||
|
|
||||||
|
switch.turn_off(self.hass, 'switch.test')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
self.assertEqual(('command-topic', 'beer off', 2),
|
||||||
|
self.mock_publish.mock_calls[-1][1])
|
||||||
|
state = self.hass.states.get('switch.test')
|
||||||
|
self.assertEqual(STATE_OFF, state.state)
|
Loading…
x
Reference in New Issue
Block a user