diff --git a/homeassistant/components/switch/command_line.py b/homeassistant/components/switch/command_line.py index e2954f90945..1ea108559e6 100644 --- a/homeassistant/components/switch/command_line.py +++ b/homeassistant/components/switch/command_line.py @@ -9,7 +9,8 @@ import subprocess import voluptuous as vol -from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA) +from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA, + ENTITY_ID_FORMAT) from homeassistant.const import ( CONF_FRIENDLY_NAME, CONF_SWITCHES, CONF_VALUE_TEMPLATE, CONF_COMMAND_OFF, CONF_COMMAND_ON, CONF_COMMAND_STATE) @@ -36,7 +37,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): devices = config.get(CONF_SWITCHES, {}) switches = [] - for device_name, device_config in devices.items(): + for object_id, device_config in devices.items(): value_template = device_config.get(CONF_VALUE_TEMPLATE) if value_template is not None: @@ -45,11 +46,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None): switches.append( CommandSwitch( hass, - device_config.get(CONF_FRIENDLY_NAME, device_name), + object_id, + device_config.get(CONF_FRIENDLY_NAME, object_id), device_config.get(CONF_COMMAND_ON), device_config.get(CONF_COMMAND_OFF), device_config.get(CONF_COMMAND_STATE), - value_template, + value_template ) ) @@ -63,11 +65,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class CommandSwitch(SwitchDevice): """Representation a switch that can be toggled using shell commands.""" - def __init__(self, hass, name, command_on, command_off, - command_state, value_template): + def __init__(self, hass, object_id, friendly_name, command_on, + command_off, command_state, value_template): """Initialize the switch.""" self._hass = hass - self._name = name + self.entity_id = ENTITY_ID_FORMAT.format(object_id) + self._name = friendly_name self._state = False self._command_on = command_on self._command_off = command_off diff --git a/tests/components/switch/test_command_line.py b/tests/components/switch/test_command_line.py index e0f81ec9ee0..0d940451763 100644 --- a/tests/components/switch/test_command_line.py +++ b/tests/components/switch/test_command_line.py @@ -162,16 +162,41 @@ class TestCommandSwitch(unittest.TestCase): """Test with state value.""" self.hass = get_test_home_assistant() - # Set state command to false - statecmd = False + # args: hass, device_name, friendly_name, command_on, command_off, + # command_state, value_template + init_args = [ + self.hass, + "test_device_name", + "Test friendly name!", + "echo 'on command'", + "echo 'off command'", + False, + None + ] - no_state_device = command_line.CommandSwitch(self.hass, "Test", "echo", - "echo", statecmd, None) + no_state_device = command_line.CommandSwitch(*init_args) self.assertTrue(no_state_device.assumed_state) # Set state command - statecmd = 'cat {}' + init_args[-2] = 'cat {}' - state_device = command_line.CommandSwitch(self.hass, "Test", "echo", - "echo", statecmd, None) + state_device = command_line.CommandSwitch(*init_args) self.assertFalse(state_device.assumed_state) + + def test_entity_id_set_correctly(self): + """Test that entity_id is set correctly from object_id""" + self.hass = get_test_home_assistant() + + init_args = [ + self.hass, + "test_device_name", + "Test friendly name!", + "echo 'on command'", + "echo 'off command'", + False, + None + ] + + test_switch = command_line.CommandSwitch(*init_args) + self.assertEqual(test_switch.entity_id, 'switch.test_device_name') + self.assertEqual(test_switch.name, 'Test friendly name!')