Skip to content

Commit

Permalink
Merge pull request home-assistant#1520 from balloob/mqtt-fixes
Browse files Browse the repository at this point in the history
Type checks for MQTT config
  • Loading branch information
balloob committed Mar 12, 2016
2 parents 097ded7 + 025713e commit 9904667
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
17 changes: 9 additions & 8 deletions homeassistant/components/light/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_RGB_COLOR, Light)
from homeassistant.helpers.template import render_with_possible_json_value
from homeassistant.util import convert

_LOGGER = logging.getLogger(__name__)

Expand All @@ -32,20 +33,20 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):

add_devices_callback([MqttLight(
hass,
config.get('name', DEFAULT_NAME),
{key: config.get(key) for key in
convert(config.get('name'), str, DEFAULT_NAME),
{key: convert(config.get(key), str) for key in
(typ + topic
for typ in ('', 'brightness_', 'rgb_')
for topic in ('state_topic', 'command_topic'))},
{key: config.get(key + '_value_template')
{key: convert(config.get(key + '_value_template'), str)
for key in ('state', 'brightness', 'rgb')},
config.get('qos', DEFAULT_QOS),
convert(config.get('qos'), int, DEFAULT_QOS),
{
'on': config.get('payload_on', DEFAULT_PAYLOAD_ON),
'off': config.get('payload_off', DEFAULT_PAYLOAD_OFF)
'on': convert(config.get('payload_on'), str, DEFAULT_PAYLOAD_ON),
'off': convert(config.get('payload_off'), str, DEFAULT_PAYLOAD_OFF)
},
config.get('optimistic', DEFAULT_OPTIMISTIC),
config.get('brightness_scale', DEFAULT_BRIGHTNESS_SCALE)
convert(config.get('optimistic'), bool, DEFAULT_OPTIMISTIC),
convert(config.get('brightness_scale'), int, DEFAULT_BRIGHTNESS_SCALE)
)])


Expand Down
13 changes: 7 additions & 6 deletions homeassistant/components/switch/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from homeassistant.components.switch import SwitchDevice
from homeassistant.const import CONF_VALUE_TEMPLATE
from homeassistant.helpers import template
from homeassistant.util import convert

_LOGGER = logging.getLogger(__name__)

Expand All @@ -32,14 +33,14 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):

add_devices_callback([MqttSwitch(
hass,
config.get('name', DEFAULT_NAME),
convert(config.get('name'), str, DEFAULT_NAME),
config.get('state_topic'),
config.get('command_topic'),
config.get('qos', DEFAULT_QOS),
config.get('retain', DEFAULT_RETAIN),
config.get('payload_on', DEFAULT_PAYLOAD_ON),
config.get('payload_off', DEFAULT_PAYLOAD_OFF),
config.get('optimistic', DEFAULT_OPTIMISTIC),
convert(config.get('qos'), int, DEFAULT_QOS),
convert(config.get('retain'), bool, DEFAULT_RETAIN),
convert(config.get('payload_on'), str, DEFAULT_PAYLOAD_ON),
convert(config.get('payload_off'), str, DEFAULT_PAYLOAD_OFF),
convert(config.get('optimistic'), bool, DEFAULT_OPTIMISTIC),
config.get(CONF_VALUE_TEMPLATE))])


Expand Down
14 changes: 7 additions & 7 deletions tests/components/light/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ def test_controlling_state_via_topic(self):
'brightness_command_topic': 'test_light_rgb/brightness/set',
'rgb_state_topic': 'test_light_rgb/rgb/status',
'rgb_command_topic': 'test_light_rgb/rgb/set',
'qos': 0,
'payload_on': 'on',
'payload_off': 'off'
'qos': '0',
'payload_on': 1,
'payload_off': 0
}
}))

Expand All @@ -134,21 +134,21 @@ def test_controlling_state_via_topic(self):
self.assertIsNone(state.attributes.get('brightness'))
self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE))

fire_mqtt_message(self.hass, 'test_light_rgb/status', 'on')
fire_mqtt_message(self.hass, 'test_light_rgb/status', '1')
self.hass.pool.block_till_done()

state = self.hass.states.get('light.test')
self.assertEqual(STATE_ON, state.state)
self.assertEqual([255, 255, 255], state.attributes.get('rgb_color'))
self.assertEqual(255, state.attributes.get('brightness'))

fire_mqtt_message(self.hass, 'test_light_rgb/status', 'off')
fire_mqtt_message(self.hass, 'test_light_rgb/status', '0')
self.hass.pool.block_till_done()

state = self.hass.states.get('light.test')
self.assertEqual(STATE_OFF, state.state)

fire_mqtt_message(self.hass, 'test_light_rgb/status', 'on')
fire_mqtt_message(self.hass, 'test_light_rgb/status', '1')
self.hass.pool.block_till_done()

fire_mqtt_message(self.hass, 'test_light_rgb/brightness/status', '100')
Expand All @@ -159,7 +159,7 @@ def test_controlling_state_via_topic(self):
self.assertEqual(100,
light_state.attributes['brightness'])

fire_mqtt_message(self.hass, 'test_light_rgb/status', 'on')
fire_mqtt_message(self.hass, 'test_light_rgb/status', '1')
self.hass.pool.block_till_done()

fire_mqtt_message(self.hass, 'test_light_rgb/rgb/status',
Expand Down
10 changes: 5 additions & 5 deletions tests/components/switch/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ def test_controlling_state_via_topic(self):
'name': 'test',
'state_topic': 'state-topic',
'command_topic': 'command-topic',
'payload_on': 'beer on',
'payload_off': 'beer off'
'payload_on': 1,
'payload_off': 0
}
}))

state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state)
self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE))

fire_mqtt_message(self.hass, 'state-topic', 'beer on')
fire_mqtt_message(self.hass, 'state-topic', '1')
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')
fire_mqtt_message(self.hass, 'state-topic', '0')
self.hass.pool.block_till_done()

state = self.hass.states.get('switch.test')
Expand All @@ -57,7 +57,7 @@ def test_sending_mqtt_commands_and_optimistic(self):
'command_topic': 'command-topic',
'payload_on': 'beer on',
'payload_off': 'beer off',
'qos': 2
'qos': '2'
}
}))

Expand Down

0 comments on commit 9904667

Please sign in to comment.