def test_custom_state_payload(self): """Test the state payload.""" assert setup_component(self.hass, switch.DOMAIN, { switch.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'payload_on': 1, 'payload_off': 0, 'state_on': "HIGH", 'state_off': "LOW", } }) state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state) self.assertFalse(state.attributes.get(ATTR_ASSUMED_STATE)) fire_mqtt_message(self.hass, 'state-topic', 'HIGH') self.hass.block_till_done() state = self.hass.states.get('switch.test') self.assertEqual(STATE_ON, state.state) fire_mqtt_message(self.hass, 'state-topic', 'LOW') self.hass.block_till_done() state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state)
def test_arm_night_via_command_topic(self): """Test arming night via command topic.""" assert setup_component(self.hass, alarm_control_panel.DOMAIN, { alarm_control_panel.DOMAIN: { 'platform': 'manual_mqtt', 'name': 'test', 'pending_time': 1, 'state_topic': 'alarm/state', 'command_topic': 'alarm/command', 'payload_arm_night': 'ARM_NIGHT', } }) entity_id = 'alarm_control_panel.test' self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) # Fire the arm command via MQTT; ensure state changes to pending fire_mqtt_message(self.hass, 'alarm/command', 'ARM_NIGHT') self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, self.hass.states.get(entity_id).state) # Fast-forward a little bit future = dt_util.utcnow() + timedelta(seconds=1) with patch(('homeassistant.components.alarm_control_panel.manual_mqtt.' 'dt_util.utcnow'), return_value=future): fire_time_changed(self.hass, future) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_NIGHT, self.hass.states.get(entity_id).state)
def test_setting_sensor_value_via_mqtt_message(self): """Test the setting of the value via MQTT.""" self.hass.config.components = ['mqtt'] assert _setup_component(self.hass, binary_sensor.DOMAIN, { binary_sensor.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'test-topic', 'payload_on': 'ON', 'payload_off': 'OFF', } }) state = self.hass.states.get('binary_sensor.test') self.assertEqual(STATE_OFF, state.state) fire_mqtt_message(self.hass, 'test-topic', 'ON') self.hass.block_till_done() state = self.hass.states.get('binary_sensor.test') self.assertEqual(STATE_ON, state.state) fire_mqtt_message(self.hass, 'test-topic', 'OFF') self.hass.block_till_done() state = self.hass.states.get('binary_sensor.test') self.assertEqual(STATE_OFF, state.state)
def test_state_via_template(self): """Test the controlling state via topic.""" self.assertTrue(setup_component(self.hass, cover.DOMAIN, { cover.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'qos': 0, 'value_template': '{{ (value | multiply(0.01)) | int }}', } })) state = self.hass.states.get('cover.test') self.assertEqual(STATE_UNKNOWN, state.state) fire_mqtt_message(self.hass, 'state-topic', '10000') self.hass.block_till_done() state = self.hass.states.get('cover.test') self.assertEqual(STATE_OPEN, state.state) fire_mqtt_message(self.hass, 'state-topic', '99') self.hass.block_till_done() state = self.hass.states.get('cover.test') self.assertEqual(STATE_CLOSED, state.state)
def test_custom_availability_payload(self): """Test availability by custom payload with defined topic.""" self.assertTrue(setup_component(self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'test_light/set', 'brightness_command_topic': 'test_light/bright', 'rgb_command_topic': "test_light/rgb", 'availability_topic': 'availability-topic', 'payload_available': 'good', 'payload_not_available': 'nogood' } })) state = self.hass.states.get('light.test') self.assertEqual(STATE_UNAVAILABLE, state.state) fire_mqtt_message(self.hass, 'availability-topic', 'good') self.hass.block_till_done() state = self.hass.states.get('light.test') self.assertNotEqual(STATE_UNAVAILABLE, state.state) fire_mqtt_message(self.hass, 'availability-topic', 'nogood') self.hass.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_UNAVAILABLE, state.state)
def test_set_target_temperature_pessimistic(self): """Test setting the target temperature.""" config = copy.deepcopy(DEFAULT_CONFIG) config['climate']['temperature_state_topic'] = 'temperature-state' assert setup_component(self.hass, climate.DOMAIN, config) state = self.hass.states.get(ENTITY_CLIMATE) assert state.attributes.get('temperature') is None common.set_operation_mode(self.hass, 'heat', ENTITY_CLIMATE) self.hass.block_till_done() common.set_temperature(self.hass, temperature=47, entity_id=ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) assert state.attributes.get('temperature') is None fire_mqtt_message(self.hass, 'temperature-state', '1701') self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) assert 1701 == state.attributes.get('temperature') fire_mqtt_message(self.hass, 'temperature-state', 'not a number') self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) assert 1701 == state.attributes.get('temperature')
def test_disarm_pending_via_command_topic(self): """Test disarming pending alarm via command topic.""" assert setup_component(self.hass, alarm_control_panel.DOMAIN, { alarm_control_panel.DOMAIN: { 'platform': 'manual_mqtt', 'name': 'test', 'pending_time': 1, 'state_topic': 'alarm/state', 'command_topic': 'alarm/command', 'payload_disarm': 'DISARM', } }) entity_id = 'alarm_control_panel.test' self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) alarm_control_panel.alarm_trigger(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, self.hass.states.get(entity_id).state) # Now that we're pending, receive a command to disarm fire_mqtt_message(self.hass, 'alarm/command', 'DISARM') self.hass.block_till_done() self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state)
def test_controlling_state_via_topic(self): """Test the controlling state via topic.""" assert setup_component(self.hass, lock.DOMAIN, { lock.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'payload_lock': 'LOCK', 'payload_unlock': 'UNLOCK' } }) state = self.hass.states.get('lock.test') self.assertEqual(STATE_UNLOCKED, state.state) self.assertFalse(state.attributes.get(ATTR_ASSUMED_STATE)) fire_mqtt_message(self.hass, 'state-topic', 'LOCK') self.hass.block_till_done() state = self.hass.states.get('lock.test') self.assertEqual(STATE_LOCKED, state.state) fire_mqtt_message(self.hass, 'state-topic', 'UNLOCK') self.hass.block_till_done() state = self.hass.states.get('lock.test') self.assertEqual(STATE_UNLOCKED, state.state)
def test_default_availability_payload(self): """Test availability by default payload with defined topic.""" assert setup_component(self.hass, alarm_control_panel.DOMAIN, { alarm_control_panel.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'alarm/state', 'command_topic': 'alarm/command', 'code': '1234', 'availability_topic': 'availability-topic' } }) state = self.hass.states.get('alarm_control_panel.test') assert STATE_UNAVAILABLE == state.state fire_mqtt_message(self.hass, 'availability-topic', 'online') self.hass.block_till_done() state = self.hass.states.get('alarm_control_panel.test') assert STATE_UNAVAILABLE != state.state fire_mqtt_message(self.hass, 'availability-topic', 'offline') self.hass.block_till_done() state = self.hass.states.get('alarm_control_panel.test') assert STATE_UNAVAILABLE == state.state
def test_set_operation_pessimistic(self): """Test setting operation mode in pessimistic mode.""" config = copy.deepcopy(DEFAULT_CONFIG) config['climate']['mode_state_topic'] = 'mode-state' assert setup_component(self.hass, climate.DOMAIN, config) state = self.hass.states.get(ENTITY_CLIMATE) assert state.attributes.get('operation_mode') is None assert "unknown" == state.state common.set_operation_mode(self.hass, "cool", ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) assert state.attributes.get('operation_mode') is None assert "unknown" == state.state fire_mqtt_message(self.hass, 'mode-state', 'cool') self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) assert "cool" == state.attributes.get('operation_mode') assert "cool" == state.state fire_mqtt_message(self.hass, 'mode-state', 'bogus mode') self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) assert "cool" == state.attributes.get('operation_mode') assert "cool" == state.state
def test_controlling_state_via_topic_and_json_message(self): """Test the controlling state via topic and JSON message.""" assert setup_component(self.hass, lock.DOMAIN, { lock.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'payload_lock': 'LOCK', 'payload_unlock': 'UNLOCK', 'value_template': '{{ value_json.val }}' } }) state = self.hass.states.get('lock.test') self.assertEqual(STATE_UNLOCKED, state.state) fire_mqtt_message(self.hass, 'state-topic', '{"val":"LOCK"}') self.hass.block_till_done() state = self.hass.states.get('lock.test') self.assertEqual(STATE_LOCKED, state.state) fire_mqtt_message(self.hass, 'state-topic', '{"val":"UNLOCK"}') self.hass.block_till_done() state = self.hass.states.get('lock.test') self.assertEqual(STATE_UNLOCKED, state.state)
def test_setting_sensor_value_via_mqtt_message(self): self.assertTrue( binary_sensor.setup( self.hass, { "binary_sensor": { "platform": "mqtt", "name": "test", "state_topic": "test-topic", "payload_on": "ON", "payload_off": "OFF", } }, ) ) state = self.hass.states.get("binary_sensor.test") self.assertEqual(STATE_OFF, state.state) fire_mqtt_message(self.hass, "test-topic", "ON") self.hass.pool.block_till_done() state = self.hass.states.get("binary_sensor.test") self.assertEqual(STATE_ON, state.state) fire_mqtt_message(self.hass, "test-topic", "OFF") self.hass.pool.block_till_done() state = self.hass.states.get("binary_sensor.test") self.assertEqual(STATE_OFF, state.state)
def test_default_availability_payload(self): """Test availability by default payload with defined topic.""" self.assertTrue(setup_component(self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt_template', 'name': 'test', 'command_topic': 'test_light_rgb/set', 'command_on_template': 'on,{{ transition }}', 'command_off_template': 'off,{{ transition|d }}', 'availability_topic': 'availability-topic' } })) state = self.hass.states.get('light.test') self.assertEqual(STATE_UNAVAILABLE, state.state) fire_mqtt_message(self.hass, 'availability-topic', 'online') self.hass.block_till_done() state = self.hass.states.get('light.test') self.assertNotEqual(STATE_UNAVAILABLE, state.state) fire_mqtt_message(self.hass, 'availability-topic', 'offline') self.hass.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_UNAVAILABLE, state.state)
def test_tilt_via_topic(self): """Test tilt by updating status via MQTT.""" self.assertTrue(setup_component(self.hass, cover.DOMAIN, { cover.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'qos': 0, 'payload_open': 'OPEN', 'payload_close': 'CLOSE', 'payload_stop': 'STOP', 'tilt_command_topic': 'tilt-command-topic', 'tilt_status_topic': 'tilt-status-topic', 'tilt_opened_value': 400, 'tilt_closed_value': 125 } })) fire_mqtt_message(self.hass, 'tilt-status-topic', '0') self.hass.block_till_done() current_cover_tilt_position = self.hass.states.get( 'cover.test').attributes['current_tilt_position'] self.assertEqual(0, current_cover_tilt_position) fire_mqtt_message(self.hass, 'tilt-status-topic', '50') self.hass.block_till_done() current_cover_tilt_position = self.hass.states.get( 'cover.test').attributes['current_tilt_position'] self.assertEqual(50, current_cover_tilt_position)
def test_set_cover_position(self): """Test setting cover position.""" self.assertTrue(setup_component(self.hass, cover.DOMAIN, { cover.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'set_position_topic': 'position-topic', 'payload_open': 'OPEN', 'payload_close': 'CLOSE', 'payload_stop': 'STOP' } })) state_attributes_dict = self.hass.states.get( 'cover.test').attributes self.assertFalse('current_position' in state_attributes_dict) self.assertFalse('current_tilt_position' in state_attributes_dict) self.assertTrue(4 & self.hass.states.get( 'cover.test').attributes['supported_features'] == 4) fire_mqtt_message(self.hass, 'state-topic', '22') self.hass.block_till_done() state_attributes_dict = self.hass.states.get( 'cover.test').attributes self.assertTrue('current_position' in state_attributes_dict) self.assertFalse('current_tilt_position' in state_attributes_dict) current_cover_position = self.hass.states.get( 'cover.test').attributes['current_position'] self.assertEqual(22, current_cover_position)
def test_controlling_state_via_topic(self): """Test the controlling state via topic.""" assert setup_component(self.hass, switch.DOMAIN, { switch.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'payload_on': 1, 'payload_off': 0 } }) state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state) self.assertFalse(state.attributes.get(ATTR_ASSUMED_STATE)) fire_mqtt_message(self.hass, 'state-topic', '1') self.hass.block_till_done() state = self.hass.states.get('switch.test') self.assertEqual(STATE_ON, state.state) fire_mqtt_message(self.hass, 'state-topic', '0') self.hass.block_till_done() state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state)
def test_controlling_state_via_topic_and_json_message(self): """Test the controlling state via topic and JSON message.""" assert setup_component(self.hass, switch.DOMAIN, { switch.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'payload_on': 'beer on', 'payload_off': 'beer off', 'value_template': '{{ value_json.val }}' } }) state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state) fire_mqtt_message(self.hass, 'state-topic', '{"val":"beer on"}') self.hass.block_till_done() state = self.hass.states.get('switch.test') self.assertEqual(STATE_ON, state.state) fire_mqtt_message(self.hass, 'state-topic', '{"val":"beer off"}') self.hass.block_till_done() state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state)
def test_availability_by_defaults(self): """Test availability by defaults with defined topic.""" self.assertTrue(setup_component(self.hass, cover.DOMAIN, { cover.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'availability_topic': 'availability-topic' } })) state = self.hass.states.get('cover.test') self.assertEqual(STATE_UNAVAILABLE, state.state) fire_mqtt_message(self.hass, 'availability-topic', 'online') self.hass.block_till_done() state = self.hass.states.get('cover.test') self.assertNotEqual(STATE_UNAVAILABLE, state.state) fire_mqtt_message(self.hass, 'availability-topic', 'offline') self.hass.block_till_done() state = self.hass.states.get('cover.test') self.assertEqual(STATE_UNAVAILABLE, state.state)
def test_force_update_enabled(self): """Test force update option.""" mock_component(self.hass, 'mqtt') assert setup_component(self.hass, sensor.DOMAIN, { sensor.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'test-topic', 'unit_of_measurement': 'fav unit', 'force_update': True } }) events = [] @ha.callback def callback(event): events.append(event) self.hass.bus.listen(EVENT_STATE_CHANGED, callback) fire_mqtt_message(self.hass, 'test-topic', '100') self.hass.block_till_done() self.assertEqual(1, len(events)) fire_mqtt_message(self.hass, 'test-topic', '100') self.hass.block_till_done() self.assertEqual(2, len(events))
def test_no_color_brightness_color_temp_white_val_if_no_topics(self): """Test for no RGB, brightness, color temp, effect, white val or XY.""" assert setup_component(self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt_json', 'name': 'test', 'state_topic': 'test_light_rgb', 'command_topic': 'test_light_rgb/set', } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.assertEqual(40, state.attributes.get(ATTR_SUPPORTED_FEATURES)) self.assertIsNone(state.attributes.get('rgb_color')) self.assertIsNone(state.attributes.get('brightness')) self.assertIsNone(state.attributes.get('color_temp')) self.assertIsNone(state.attributes.get('effect')) self.assertIsNone(state.attributes.get('white_value')) self.assertIsNone(state.attributes.get('xy_color')) self.assertIsNone(state.attributes.get('hs_color')) fire_mqtt_message(self.hass, 'test_light_rgb', '{"state":"ON"}') self.hass.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertIsNone(state.attributes.get('rgb_color')) self.assertIsNone(state.attributes.get('brightness')) self.assertIsNone(state.attributes.get('color_temp')) self.assertIsNone(state.attributes.get('effect')) self.assertIsNone(state.attributes.get('white_value')) self.assertIsNone(state.attributes.get('xy_color')) self.assertIsNone(state.attributes.get('hs_color'))
def test_availability_by_custom_payload(self): """Test availability by custom payload with defined topic.""" self.assertTrue(setup_component(self.hass, cover.DOMAIN, { cover.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'availability_topic': 'availability-topic', 'payload_available': 'good', 'payload_not_available': 'nogood' } })) state = self.hass.states.get('cover.test') self.assertEqual(STATE_UNAVAILABLE, state.state) fire_mqtt_message(self.hass, 'availability-topic', 'good') self.hass.block_till_done() state = self.hass.states.get('cover.test') self.assertNotEqual(STATE_UNAVAILABLE, state.state) fire_mqtt_message(self.hass, 'availability-topic', 'nogood') self.hass.block_till_done() state = self.hass.states.get('cover.test') self.assertEqual(STATE_UNAVAILABLE, state.state)
def test_no_color_or_brightness_if_no_config(self): \ # pylint: disable=invalid-name """Test if there is no color and brightness if they aren't defined.""" self.hass.config.components = ['mqtt'] assert _setup_component(self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt_json', 'name': 'test', 'state_topic': 'test_light_rgb', 'command_topic': 'test_light_rgb/set', } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.assertIsNone(state.attributes.get('rgb_color')) self.assertIsNone(state.attributes.get('brightness')) fire_mqtt_message(self.hass, 'test_light_rgb', '{"state":"ON"}') self.hass.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertIsNone(state.attributes.get('rgb_color')) self.assertIsNone(state.attributes.get('brightness'))
def test_force_update_enabled(self): """Test force update option.""" mock_component(self.hass, 'mqtt') assert setup_component(self.hass, binary_sensor.DOMAIN, { binary_sensor.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'test-topic', 'payload_on': 'ON', 'payload_off': 'OFF', 'force_update': True } }) events = [] @ha.callback def callback(event): """Verify event got called.""" events.append(event) self.hass.bus.listen(EVENT_STATE_CHANGED, callback) fire_mqtt_message(self.hass, 'test-topic', 'ON') self.hass.block_till_done() self.assertEqual(1, len(events)) fire_mqtt_message(self.hass, 'test-topic', 'ON') self.hass.block_till_done() self.assertEqual(2, len(events))
def test_controlling_state_via_topic(self): """Test the controlling state via topic.""" with assert_setup_component(1): assert setup_component(self.hass, garage_door.DOMAIN, { garage_door.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'state_open': 1, 'state_closed': 0, 'service_open': 1, 'service_close': 0 } }) state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_CLOSED, state.state) self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE)) fire_mqtt_message(self.hass, 'state-topic', '1') self.hass.block_till_done() state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_OPEN, state.state) fire_mqtt_message(self.hass, 'state-topic', '0') self.hass.block_till_done() state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_CLOSED, state.state)
def test_custom_availability_payload(self): """Test availability by custom payload with defined topic.""" assert setup_component(self.hass, sensor.DOMAIN, { sensor.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'test-topic', 'availability_topic': 'availability-topic', 'payload_available': 'good', 'payload_not_available': 'nogood' } }) state = self.hass.states.get('sensor.test') assert STATE_UNAVAILABLE == state.state fire_mqtt_message(self.hass, 'availability-topic', 'good') self.hass.block_till_done() state = self.hass.states.get('sensor.test') assert STATE_UNAVAILABLE != state.state fire_mqtt_message(self.hass, 'availability-topic', 'nogood') self.hass.block_till_done() state = self.hass.states.get('sensor.test') assert STATE_UNAVAILABLE == state.state
def test_default_availability_payload(self): """Test availability by default payload with defined topic.""" self.assertTrue(setup_component(self.hass, lock.DOMAIN, { lock.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'payload_lock': 'LOCK', 'payload_unlock': 'UNLOCK', 'availability_topic': 'availability-topic' } })) state = self.hass.states.get('lock.test') self.assertEqual(STATE_UNAVAILABLE, state.state) fire_mqtt_message(self.hass, 'availability-topic', 'online') self.hass.block_till_done() state = self.hass.states.get('lock.test') self.assertNotEqual(STATE_UNAVAILABLE, state.state) fire_mqtt_message(self.hass, 'availability-topic', 'offline') self.hass.block_till_done() state = self.hass.states.get('lock.test') self.assertEqual(STATE_UNAVAILABLE, state.state)
def test_controlling_state_via_topic_and_json_message(self): """Test the controlling state via topic and JSON message.""" with assert_setup_component(1): assert setup_component(self.hass, garage_door.DOMAIN, { garage_door.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'state_open': 'beer open', 'state_closed': 'beer closed', 'service_open': 'beer service open', 'service_close': 'beer service close', 'value_template': '{{ value_json.val }}' } }) state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_CLOSED, state.state) fire_mqtt_message(self.hass, 'state-topic', '{"val":"beer open"}') self.hass.block_till_done() state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_OPEN, state.state) fire_mqtt_message(self.hass, 'state-topic', '{"val":"beer closed"}') self.hass.block_till_done() state = self.hass.states.get('garage_door.test') self.assertEqual(STATE_CLOSED, state.state)
def test_availability_by_defaults(self): """Test availability by defaults with defined topic.""" assert setup_component(self.hass, binary_sensor.DOMAIN, { binary_sensor.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'availability_topic': 'availability-topic' } }) state = self.hass.states.get('binary_sensor.test') assert STATE_UNAVAILABLE == state.state fire_mqtt_message(self.hass, 'availability-topic', 'online') self.hass.block_till_done() state = self.hass.states.get('binary_sensor.test') assert STATE_UNAVAILABLE != state.state fire_mqtt_message(self.hass, 'availability-topic', 'offline') self.hass.block_till_done() state = self.hass.states.get('binary_sensor.test') assert STATE_UNAVAILABLE == state.state
def test_no_color_brightness_color_temp_white_xy_if_no_topics(self): \ # pylint: disable=invalid-name """Test if there is no color and brightness if no topic.""" with assert_setup_component(1, light.DOMAIN): assert setup_component(self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'test_light_rgb/status', 'command_topic': 'test_light_rgb/set', } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.assertIsNone(state.attributes.get('rgb_color')) self.assertIsNone(state.attributes.get('brightness')) self.assertIsNone(state.attributes.get('color_temp')) self.assertIsNone(state.attributes.get('white_value')) self.assertIsNone(state.attributes.get('xy_color')) fire_mqtt_message(self.hass, 'test_light_rgb/status', 'ON') self.hass.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertIsNone(state.attributes.get('rgb_color')) self.assertIsNone(state.attributes.get('brightness')) self.assertIsNone(state.attributes.get('color_temp')) self.assertIsNone(state.attributes.get('white_value')) self.assertIsNone(state.attributes.get('xy_color'))
def test_if_fires_on_topic_match(self): """Test if message is fired on topic match.""" assert setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'mqtt', 'topic': 'test-topic' }, 'action': { 'service': 'test.automation', 'data_template': { 'some': '{{ trigger.platform }} - {{ trigger.topic }}' ' - {{ trigger.payload }}' }, } } }) fire_mqtt_message(self.hass, 'test-topic', 'test_payload') self.hass.block_till_done() self.assertEqual(1, len(self.calls)) self.assertEqual('mqtt - test-topic - test_payload', self.calls[0].data['some']) automation.turn_off(self.hass) self.hass.block_till_done() fire_mqtt_message(self.hass, 'test-topic', 'test_payload') self.hass.block_till_done() self.assertEqual(1, len(self.calls))
def test_unique_id(self): """Test unique id option only creates one switch per unique_id.""" assert setup_component(self.hass, switch.DOMAIN, { switch.DOMAIN: [{ 'platform': 'mqtt', 'name': 'Test 1', 'state_topic': 'test-topic', 'command_topic': 'command-topic', 'unique_id': 'TOTALLY_UNIQUE' }, { 'platform': 'mqtt', 'name': 'Test 2', 'state_topic': 'test-topic', 'command_topic': 'command-topic', 'unique_id': 'TOTALLY_UNIQUE' }] }) fire_mqtt_message(self.hass, 'test-topic', 'payload') self.hass.block_till_done() assert len(self.hass.states.async_entity_ids()) == 2
def test_update_state_via_state_topic(self): """Test updating with via state topic.""" self.assertTrue(alarm_control_panel.setup(self.hass, { 'alarm_control_panel': { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'alarm/state', 'command_topic': 'alarm/command', }})) entity_id = 'alarm_control_panel.test' self.assertEqual(STATE_UNKNOWN, self.hass.states.get(entity_id).state) for state in (STATE_ALARM_DISARMED, STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_AWAY, STATE_ALARM_PENDING, STATE_ALARM_TRIGGERED): fire_mqtt_message(self.hass, 'alarm/state', state) self.hass.pool.block_till_done() self.assertEqual(state, self.hass.states.get(entity_id).state)
def test_single_level_wildcard_topic_not_matching(self): """Test not matching single level wildcard topic.""" dev_id = 'paulus' entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id) subscription = '/location/+/paulus' topic = '/location/paulus' location = 'work' self.hass.config.components = set(['mqtt', 'zone']) assert setup_component( self.hass, device_tracker.DOMAIN, { device_tracker.DOMAIN: { CONF_PLATFORM: 'mqtt', 'devices': { dev_id: subscription } } }) fire_mqtt_message(self.hass, topic, location) self.hass.block_till_done() assert self.hass.states.get(entity_id) is None
def test_update_with_json_attrs_and_template(self): """Test attributes get extracted from a JSON result.""" mock_component(self.hass, 'mqtt') assert setup_component(self.hass, sensor.DOMAIN, { sensor.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'test-topic', 'unit_of_measurement': 'fav unit', 'value_template': '{{ value_json.val }}', 'json_attributes': 'val' } }) fire_mqtt_message(self.hass, 'test-topic', '{ "val": "100" }') self.hass.block_till_done() state = self.hass.states.get('sensor.test') assert '100' == \ state.attributes.get('val') assert '100' == state.state
def test_multi_level_wildcard_topic(self): """Test multi level wildcard topic.""" dev_id = 'paulus' entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id) subscription = '/location/#' topic = '/location/room/paulus' location = 'work' self.hass.config.components = set(['mqtt', 'zone']) assert setup_component( self.hass, device_tracker.DOMAIN, { device_tracker.DOMAIN: { CONF_PLATFORM: 'mqtt', 'devices': { dev_id: subscription } } }) fire_mqtt_message(self.hass, topic, location) self.hass.block_till_done() self.assertEqual(location, self.hass.states.get(entity_id).state)
def test_multi_level_wildcard_topic(self): """Test multi level wildcard topic.""" dev_id = 'zanzito' subscription = 'location/#' topic = 'location/zanzito' location = json.dumps(LOCATION_MESSAGE) assert setup_component( self.hass, device_tracker.DOMAIN, { device_tracker.DOMAIN: { CONF_PLATFORM: 'mqtt_json', 'devices': { dev_id: subscription } } }) fire_mqtt_message(self.hass, topic, location) self.hass.block_till_done() state = self.hass.states.get('device_tracker.zanzito') assert state.attributes.get('latitude') == 2.0 assert state.attributes.get('longitude') == 1.0
def test_update_with_json_attrs_bad_JSON(self, mock_logger): """Test attributes get extracted from a JSON result.""" mock_component(self.hass, 'mqtt') assert setup_component( self.hass, sensor.DOMAIN, { sensor.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'test-topic', 'unit_of_measurement': 'fav unit', 'json_attributes': 'val' } }) fire_mqtt_message(self.hass, 'test-topic', 'This is not JSON') self.hass.block_till_done() state = self.hass.states.get('sensor.test') assert state.attributes.get('val') is None assert mock_logger.warning.called assert mock_logger.debug.called
def test_controlling_state_via_topic_with_templates(self): self.assertTrue(light.setup(self.hass, { 'light': { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'test_light_rgb/status', 'command_topic': 'test_light_rgb/set', 'brightness_state_topic': 'test_light_rgb/brightness/status', 'rgb_state_topic': 'test_light_rgb/rgb/status', 'state_value_template': '{{ value_json.hello }}', 'brightness_value_template': '{{ value_json.hello }}', 'rgb_value_template': '{{ value_json.hello | join(",") }}', } })) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.assertIsNone(state.attributes.get('brightness')) self.assertIsNone(state.attributes.get('rgb_color')) fire_mqtt_message(self.hass, 'test_light_rgb/rgb/status', '{"hello": [1, 2, 3]}') fire_mqtt_message(self.hass, 'test_light_rgb/status', '{"hello": "ON"}') fire_mqtt_message(self.hass, 'test_light_rgb/brightness/status', '{"hello": "50"}') self.hass.pool.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual(50, state.attributes.get('brightness')) self.assertEqual([1, 2, 3], state.attributes.get('rgb_color'))
def test_controlling_state_via_topic_with_templates(self): """Test the setting og the state with a template.""" self.hass.config.components = ['mqtt'] assert _setup_component(self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'test_light_rgb/status', 'command_topic': 'test_light_rgb/set', 'brightness_state_topic': 'test_light_rgb/brightness/status', 'rgb_state_topic': 'test_light_rgb/rgb/status', 'state_value_template': '{{ value_json.hello }}', 'brightness_value_template': '{{ value_json.hello }}', 'rgb_value_template': '{{ value_json.hello | join(",") }}', } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.assertIsNone(state.attributes.get('brightness')) self.assertIsNone(state.attributes.get('rgb_color')) fire_mqtt_message(self.hass, 'test_light_rgb/rgb/status', '{"hello": [1, 2, 3]}') fire_mqtt_message(self.hass, 'test_light_rgb/status', '{"hello": "ON"}') fire_mqtt_message(self.hass, 'test_light_rgb/brightness/status', '{"hello": "50"}') self.hass.pool.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual(50, state.attributes.get('brightness')) self.assertEqual([1, 2, 3], state.attributes.get('rgb_color'))
def test_set_away_mode_pessimistic(self): """Test setting of the away mode.""" config = copy.deepcopy(DEFAULT_CONFIG) config['climate']['away_mode_state_topic'] = 'away-state' assert setup_component(self.hass, climate.DOMAIN, config) state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual('off', state.attributes.get('away_mode')) common.set_away_mode(self.hass, True, ENTITY_CLIMATE) 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', 'ON') self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual('on', state.attributes.get('away_mode')) fire_mqtt_message(self.hass, 'away-state', 'OFF') 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', 'nonsense') self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual('off', state.attributes.get('away_mode'))
def test_brightness_from_rgb_controlling_scale(self): """Test the brightness controlling scale.""" with assert_setup_component(1, light.DOMAIN): assert setup_component( self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'test_scale_rgb/status', 'command_topic': 'test_scale_rgb/set', 'rgb_state_topic': 'test_scale_rgb/rgb/status', 'rgb_command_topic': 'test_scale_rgb/rgb/set', 'qos': 0, 'payload_on': 'on', 'payload_off': 'off' } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.assertIsNone(state.attributes.get('brightness')) self.assertFalse(state.attributes.get(ATTR_ASSUMED_STATE)) fire_mqtt_message(self.hass, 'test_scale_rgb/status', 'on') fire_mqtt_message(self.hass, 'test_scale_rgb/rgb/status', '255,0,0') self.hass.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(255, state.attributes.get('brightness')) fire_mqtt_message(self.hass, 'test_scale_rgb/rgb/status', '127,0,0') self.hass.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(127, state.attributes.get('brightness'))
def test_position_via_template(self): """Test the controlling state via topic.""" self.assertTrue(setup_component(self.hass, cover.DOMAIN, { cover.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'position_topic': 'get-position-topic', 'command_topic': 'command-topic', 'qos': 0, 'value_template': '{{ (value | multiply(0.01)) | int }}' } })) state = self.hass.states.get('cover.test') self.assertEqual(STATE_UNKNOWN, state.state) fire_mqtt_message(self.hass, 'get-position-topic', '10000') self.hass.block_till_done() state = self.hass.states.get('cover.test') self.assertEqual(STATE_OPEN, state.state) fire_mqtt_message(self.hass, 'get-position-topic', '5000') self.hass.block_till_done() state = self.hass.states.get('cover.test') self.assertEqual(STATE_OPEN, state.state) fire_mqtt_message(self.hass, 'get-position-topic', '99') self.hass.block_till_done() state = self.hass.states.get('cover.test') self.assertEqual(STATE_CLOSED, state.state)
def test_set_aux_pessimistic(self): """Test setting of the aux heating in pessimistic mode.""" config = copy.deepcopy(DEFAULT_CONFIG) config['climate']['aux_state_topic'] = 'aux-state' assert setup_component(self.hass, CLIMATE_DOMAIN, config) state = self.hass.states.get(ENTITY_CLIMATE) assert 'off' == state.attributes.get('aux_heat') common.set_aux_heat(self.hass, True, ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) assert 'off' == state.attributes.get('aux_heat') fire_mqtt_message(self.hass, 'aux-state', 'ON') self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) assert 'on' == state.attributes.get('aux_heat') fire_mqtt_message(self.hass, 'aux-state', 'OFF') self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) assert 'off' == state.attributes.get('aux_heat') fire_mqtt_message(self.hass, 'aux-state', 'nonsense') self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) assert 'off' == state.attributes.get('aux_heat')
def test_battery_template(self): """Test that you can use non-default templates for battery_level.""" self.default_config.update({ mqtt.CONF_SUPPORTED_FEATURES: mqtt.services_to_strings(mqtt.ALL_SERVICES), mqtt.CONF_BATTERY_LEVEL_TOPIC: "retroroomba/battery_level", mqtt.CONF_BATTERY_LEVEL_TEMPLATE: "{{ value }}" }) self.assertTrue( setup_component(self.hass, vacuum.DOMAIN, { vacuum.DOMAIN: self.default_config, })) fire_mqtt_message(self.hass, 'retroroomba/battery_level', '54') self.hass.block_till_done() state = self.hass.states.get('vacuum.mqtttest') self.assertEqual(54, state.attributes.get(ATTR_BATTERY_LEVEL)) self.assertEqual(state.attributes.get(ATTR_BATTERY_ICON), 'mdi:battery-50')
def test_non_json_message(self): """Test receiving a non JSON message.""" dev_id = 'zanzito' topic = 'location/zanzito' location = 'home' assert setup_component( self.hass, device_tracker.DOMAIN, { device_tracker.DOMAIN: { CONF_PLATFORM: 'mqtt_json', 'devices': { dev_id: topic } } }) with self.assertLogs(level='ERROR') as test_handle: fire_mqtt_message(self.hass, topic, location) self.hass.block_till_done() self.assertIn( "ERROR:homeassistant.components.device_tracker.mqtt_json:" "Error parsing JSON payload: home", test_handle.output[0])
def test_state_change_via_topic(self): \ # pylint: disable=invalid-name """Test state change via topic.""" with assert_setup_component(1, light.DOMAIN): assert setup_component(self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt_template', 'name': 'test', 'state_topic': 'test_light_rgb', 'command_topic': 'test_light_rgb/set', 'command_on_template': 'on,' '{{ brightness|d }},' '{{ color_temp|d }},' '{{ white_value|d }},' '{{ red|d }}-' '{{ green|d }}-' '{{ blue|d }}', 'command_off_template': 'off', 'state_template': '{{ value.split(",")[0] }}' } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.assertIsNone(state.attributes.get('rgb_color')) self.assertIsNone(state.attributes.get('brightness')) self.assertIsNone(state.attributes.get('color_temp')) self.assertIsNone(state.attributes.get('white_value')) self.assertFalse(state.attributes.get(ATTR_ASSUMED_STATE)) fire_mqtt_message(self.hass, 'test_light_rgb', 'on') self.hass.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertIsNone(state.attributes.get('rgb_color')) self.assertIsNone(state.attributes.get('brightness')) self.assertIsNone(state.attributes.get('color_temp')) self.assertIsNone(state.attributes.get('white_value'))
def test_update_state_via_state_topic(self): """Test updating with via state topic.""" assert setup_component(self.hass, alarm_control_panel.DOMAIN, { alarm_control_panel.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'alarm/state', 'command_topic': 'alarm/command', } }) entity_id = 'alarm_control_panel.test' assert STATE_UNKNOWN == \ self.hass.states.get(entity_id).state for state in (STATE_ALARM_DISARMED, STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_NIGHT, STATE_ALARM_PENDING, STATE_ALARM_TRIGGERED): fire_mqtt_message(self.hass, 'alarm/state', state) self.hass.block_till_done() assert state == self.hass.states.get(entity_id).state
def test_no_color_or_brightness_if_no_topics(self): self.assertTrue(light.setup(self.hass, { 'light': { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'test_light_rgb/status', 'command_topic': 'test_light_rgb/set', } })) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.assertIsNone(state.attributes.get('rgb_color')) self.assertIsNone(state.attributes.get('brightness')) fire_mqtt_message(self.hass, 'test_light_rgb/status', 'ON') self.hass.pool.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertIsNone(state.attributes.get('rgb_color')) self.assertIsNone(state.attributes.get('brightness'))
def test_controlling_scale(self): """Test the controlling scale.""" self.hass.config.components = ['mqtt'] with assert_setup_component(1): assert setup_component( self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'test_scale/status', 'command_topic': 'test_scale/set', 'brightness_state_topic': 'test_scale/brightness/status', 'brightness_command_topic': 'test_scale/brightness/set', 'brightness_scale': '99', 'qos': 0, 'payload_on': 'on', 'payload_off': 'off' } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.assertIsNone(state.attributes.get('brightness')) self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE)) fire_mqtt_message(self.hass, 'test_scale/status', 'on') self.hass.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual(255, state.attributes.get('brightness')) fire_mqtt_message(self.hass, 'test_scale/status', 'off') self.hass.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) fire_mqtt_message(self.hass, 'test_scale/status', 'on') self.hass.block_till_done() fire_mqtt_message(self.hass, 'test_scale/brightness/status', '99') self.hass.block_till_done() light_state = self.hass.states.get('light.test') self.hass.block_till_done() self.assertEqual(255, light_state.attributes['brightness'])
def test_state_via_template(self): """Test the controlling state via topic.""" assert setup_component( self.hass, cover.DOMAIN, { cover.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'qos': 0, 'value_template': '\ {% if (value | multiply(0.01) | int) == 0 %}\ closed\ {% else %}\ open\ {% endif %}' } }) state = self.hass.states.get('cover.test') assert STATE_UNKNOWN == state.state fire_mqtt_message(self.hass, 'state-topic', '10000') self.hass.block_till_done() state = self.hass.states.get('cover.test') assert STATE_OPEN == state.state fire_mqtt_message(self.hass, 'state-topic', '99') self.hass.block_till_done() state = self.hass.states.get('cover.test') assert STATE_CLOSED == state.state
def test_default_availability_payload(self): """Test availability by default payload with defined topic.""" self.default_config.update( {'availability_topic': 'availability-topic'}) assert setup_component(self.hass, vacuum.DOMAIN, { vacuum.DOMAIN: self.default_config, }) state = self.hass.states.get('vacuum.mqtttest') assert STATE_UNAVAILABLE == state.state fire_mqtt_message(self.hass, 'availability-topic', 'online') self.hass.block_till_done() state = self.hass.states.get('vacuum.mqtttest') assert STATE_UNAVAILABLE != state.state fire_mqtt_message(self.hass, 'availability-topic', 'offline') self.hass.block_till_done() state = self.hass.states.get('vacuum.mqtttest') assert STATE_UNAVAILABLE == state.state
def test_show_color_temp_only_if_command_topic(self): """Test the color temp only if a command topic is present.""" self.hass.config.components = ['mqtt'] assert _setup_component(self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'color_temp_command_topic': 'test_light_rgb/brightness/set', 'command_topic': 'test_light_rgb/set', 'state_topic': 'test_light_rgb/status' } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.assertIsNone(state.attributes.get('color_temp')) fire_mqtt_message(self.hass, 'test_light_rgb/status', 'ON') self.hass.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual(150, state.attributes.get('color_temp'))
def test_custom_availability_payload(self): """Test availability by custom payload with defined topic.""" config = copy.deepcopy(DEFAULT_CONFIG) config['climate']['availability_topic'] = 'availability-topic' config['climate']['payload_available'] = 'good' config['climate']['payload_not_available'] = 'nogood' assert setup_component(self.hass, CLIMATE_DOMAIN, config) state = self.hass.states.get('climate.test') assert STATE_UNAVAILABLE == state.state fire_mqtt_message(self.hass, 'availability-topic', 'good') self.hass.block_till_done() state = self.hass.states.get('climate.test') assert STATE_UNAVAILABLE != state.state fire_mqtt_message(self.hass, 'availability-topic', 'nogood') self.hass.block_till_done() state = self.hass.states.get('climate.test') assert STATE_UNAVAILABLE == state.state
def test_receiving_remote_event_fires_hass_event(self): """"Test the receiving of the remotely fired event.""" sub_topic = 'foo' assert self.add_eventstream(sub_topic=sub_topic) self.hass.block_till_done() calls = [] @callback def listener(_): calls.append(1) self.hass.bus.listen_once('test_event', listener) self.hass.block_till_done() payload = json.dumps( {'event_type': 'test_event', 'event_data': {}}, cls=JSONEncoder ) fire_mqtt_message(self.hass, sub_topic, payload) self.hass.block_till_done() assert 1 == len(calls)
def test_set_hold_pessimistic(self): """Test setting the hold mode in pessimistic mode.""" config = copy.deepcopy(DEFAULT_CONFIG) config['climate']['hold_state_topic'] = 'hold-state' assert setup_component(self.hass, CLIMATE_DOMAIN, config) state = self.hass.states.get(ENTITY_CLIMATE) assert state.attributes.get('hold_mode') is None common.set_hold_mode(self.hass, 'on', ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) assert state.attributes.get('hold_mode') is None fire_mqtt_message(self.hass, 'hold-state', 'on') self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) assert 'on' == state.attributes.get('hold_mode') fire_mqtt_message(self.hass, 'hold-state', 'off') self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) assert 'off' == state.attributes.get('hold_mode')
def test_set_fan_mode_pessimistic(self): """Test setting of new fan mode in pessimistic mode.""" config = copy.deepcopy(DEFAULT_CONFIG) config['climate']['fan_mode_state_topic'] = 'fan-state' assert setup_component(self.hass, CLIMATE_DOMAIN, config) state = self.hass.states.get(ENTITY_CLIMATE) assert state.attributes.get('fan_mode') is None common.set_fan_mode(self.hass, 'high', ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) assert state.attributes.get('fan_mode') is None fire_mqtt_message(self.hass, 'fan-state', 'high') self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) assert 'high' == state.attributes.get('fan_mode') fire_mqtt_message(self.hass, 'fan-state', 'bogus mode') self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) assert 'high' == state.attributes.get('fan_mode')
def test_set_swing_pessimistic(self): """Test setting swing mode in pessimistic mode.""" config = copy.deepcopy(DEFAULT_CONFIG) config['climate']['swing_mode_state_topic'] = 'swing-state' assert setup_component(self.hass, climate.DOMAIN, config) state = self.hass.states.get(ENTITY_CLIMATE) assert state.attributes.get('swing_mode') is None common.set_swing_mode(self.hass, 'on', ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) assert state.attributes.get('swing_mode') is None fire_mqtt_message(self.hass, 'swing-state', 'on') self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) assert "on" == state.attributes.get('swing_mode') fire_mqtt_message(self.hass, 'swing-state', 'bogus state') self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) assert "on" == state.attributes.get('swing_mode')
def test_incomplete_message(self): """Test receiving an incomplete message.""" dev_id = 'zanzito' topic = 'location/zanzito' location = json.dumps(LOCATION_MESSAGE_INCOMPLETE) assert setup_component( self.hass, device_tracker.DOMAIN, { device_tracker.DOMAIN: { CONF_PLATFORM: 'mqtt_json', 'devices': { dev_id: topic } } }) with self.assertLogs(level='ERROR') as test_handle: fire_mqtt_message(self.hass, topic, location) self.hass.block_till_done() assert "ERROR:homeassistant.components.device_tracker.mqtt_json:" \ "Skipping update for following data because of missing " \ "or malformatted data: {\"longitude\": 2.0}" in \ test_handle.output[0]
def test_show_xy_if_only_command_topic(self): """Test the xy if only a command topic is present.""" config = {light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'xy_command_topic': 'test_light_rgb/xy/set', 'command_topic': 'test_light_rgb/set', 'state_topic': 'test_light_rgb/status', }} with assert_setup_component(1, light.DOMAIN): assert setup_component(self.hass, light.DOMAIN, config) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.assertIsNone(state.attributes.get('xy_color')) fire_mqtt_message(self.hass, 'test_light_rgb/status', 'ON') self.hass.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual((0.323, 0.329), state.attributes.get('xy_color'))
def test_brightness_scale(self): """Test for brightness scaling.""" assert setup_component( self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt_json', 'name': 'test', 'state_topic': 'test_light_bright_scale', 'command_topic': 'test_light_bright_scale/set', 'brightness': True, 'brightness_scale': 99 } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.assertIsNone(state.attributes.get('brightness')) self.assertFalse(state.attributes.get(ATTR_ASSUMED_STATE)) # Turn on the light fire_mqtt_message(self.hass, 'test_light_bright_scale', '{"state":"ON"}') self.hass.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual(255, state.attributes.get('brightness')) # Turn on the light with brightness fire_mqtt_message(self.hass, 'test_light_bright_scale', '{"state":"ON",' '"brightness": 99}') self.hass.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual(255, state.attributes.get('brightness'))