Added a select entity to manually switch input.
This commit is contained in:
1 parent
1fd4dab916
commit
5dbfd2a805
3 files changed
+222
No files matched your search
@@ -504,3 +504,121 @@ class SpamSensorTests(unittest.TestCase):
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
|
||||
class SelectHarness(Harness):
|
||||
@property
|
||||
def args(self):
|
||||
return dict(super().args,
|
||||
input_selected_sensor='sensor.input', unreachable_sensor='binary_sensor.unreachable',
|
||||
input_select={'entity': 'select.vertex2_input', 'unique_id': 'vertex2_input',
|
||||
'connection_topic': 'appdaemon/connection'},
|
||||
input_config={0: {'name': 'AppleTV', 'icon': 'mdi:apple'},
|
||||
1: {'name': 'Xbox', 'icon': 'mdi:microsoft-xbox'},
|
||||
'default': {'name': 'None', 'icon': 'mdi:television-off'}})
|
||||
|
||||
|
||||
class InputSelectTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.app = app = SelectHarness()
|
||||
app.input_names = {'AppleTV': 0, 'Xbox': 1}
|
||||
app.input_selected_sensor = Mock()
|
||||
app.input_selected_sensor.get_state.return_value = 'AppleTV'
|
||||
app.unreachable_sensor.get_state.return_value = 'off'
|
||||
app.call_service = Mock()
|
||||
app.get_plugin_config = Mock(return_value={'will_topic': 'transport/connection'})
|
||||
app.listen_state = Mock()
|
||||
app.listen_event = Mock()
|
||||
app.send_cmd = Mock()
|
||||
app._initialize_input_select()
|
||||
|
||||
def published(self, suffix):
|
||||
return [c.kwargs['payload'] for c in self.app.call_service.call_args_list
|
||||
if c.args[0] == 'mqtt/publish'
|
||||
and c.kwargs['topic'] == self.app._select_topic + suffix][-1]
|
||||
|
||||
def test_discovery_options_and_initial_icon(self):
|
||||
import json
|
||||
discovery = next(c for c in self.app.call_service.call_args_list
|
||||
if c.kwargs.get('topic') == 'homeassistant/select/vertex2_input/config')
|
||||
config = json.loads(discovery.kwargs['payload'])
|
||||
self.assertEqual(config['options'], ['AppleTV', 'Xbox'])
|
||||
self.assertFalse(config['optimistic'])
|
||||
self.assertEqual(config['availability'][1]['topic'], 'appdaemon/connection')
|
||||
self.assertEqual(json.loads(self.published('/attributes'))['icon'], 'mdi:apple')
|
||||
|
||||
def test_user_command_sets_both_tx_without_changing_confirmed_state(self):
|
||||
app = self.app
|
||||
app.call_service.reset_mock()
|
||||
app._on_input_select_mqtt('MQTT_MESSAGE',
|
||||
{'topic': app._select_topic + '/set', 'payload': 'Xbox'}, {})
|
||||
app.send_cmd.assert_called_once_with('insel', '1 1')
|
||||
self.assertTrue(app._status_requested.is_set())
|
||||
app.input_selected_sensor.set_state.assert_not_called()
|
||||
app.call_service.assert_not_called()
|
||||
|
||||
def test_unknown_or_powered_off_command_is_rejected(self):
|
||||
app = self.app
|
||||
for plugged, value in ((True, 'invalid'), (False, 'Xbox')):
|
||||
app.plugged = plugged
|
||||
app._on_input_select_mqtt('MQTT_MESSAGE',
|
||||
{'topic': app._select_topic + '/set', 'payload': value}, {})
|
||||
app.send_cmd.assert_not_called()
|
||||
|
||||
def test_state_and_icon_follow_input_on_active_output(self):
|
||||
import json
|
||||
app = self.app
|
||||
state = {'name': 'AppleTV'}
|
||||
app.input_selected_sensor.get_state.side_effect = lambda: state['name']
|
||||
app.input_selected_sensor.set_state.side_effect = lambda **kw: state.update(name=kw.get('state', state['name']))
|
||||
app.input_selected_sensors = {
|
||||
app.generate_input_selected_sensor_name(0): Mock(),
|
||||
app.generate_input_selected_sensor_name(1): Mock(),
|
||||
}
|
||||
for index, name in enumerate(('AppleTV', 'Xbox')):
|
||||
app.input_selected_sensors[app.generate_input_selected_sensor_name(index)].get_state.return_value = name
|
||||
for index, expected, icon in ((1, 'Xbox', 'mdi:microsoft-xbox'), (0, 'AppleTV', 'mdi:apple')):
|
||||
app._refresh_selected_input_sensor(index)
|
||||
app._on_input_select_state(None, None, None, None, {})
|
||||
self.assertEqual(self.published('/state'), expected)
|
||||
self.assertEqual(json.loads(self.published('/attributes'))['icon'], icon)
|
||||
self.assertEqual(app._select_discovery_payload['icon'], icon)
|
||||
app._refresh_selected_input_sensor(None)
|
||||
app._sync_input_select()
|
||||
self.assertEqual(self.published('/availability'), 'offline')
|
||||
self.assertEqual(self.published('/state'), 'None')
|
||||
|
||||
def test_unreachable_and_power_off_mark_select_unavailable(self):
|
||||
app = self.app
|
||||
app.unreachable_sensor.get_state.return_value = 'on'
|
||||
app._sync_input_select()
|
||||
self.assertEqual(self.published('/availability'), 'offline')
|
||||
app.unreachable_sensor.get_state.return_value = 'off'
|
||||
app.plugged = False
|
||||
app._sync_input_select()
|
||||
self.assertEqual(self.published('/availability'), 'offline')
|
||||
|
||||
def test_reconnect_resubscribes_and_republishes_discovery(self):
|
||||
app = self.app
|
||||
for data in ({'state': 'Connected'}, {'topic': 'homeassistant/status', 'payload': 'online'}):
|
||||
app.call_service.reset_mock()
|
||||
app._on_input_select_mqtt('MQTT_MESSAGE', data, {})
|
||||
subscriptions = [c.kwargs['topic'] for c in app.call_service.call_args_list
|
||||
if c.args[0] == 'mqtt/subscribe']
|
||||
self.assertIn(app._select_topic + '/set', subscriptions)
|
||||
self.assertTrue(any(c.kwargs.get('topic', '').endswith('/config')
|
||||
for c in app.call_service.call_args_list))
|
||||
|
||||
def test_connection_topic_is_derived_from_shared_plugin(self):
|
||||
import json
|
||||
app = self.app
|
||||
app._input_select_config.pop('connection_topic')
|
||||
app.get_plugin_config.return_value = {
|
||||
'will_topic': 'transport/connection', 'birth_payload': 'ready', 'will_payload': 'gone'}
|
||||
app.call_service.reset_mock()
|
||||
app._discover_input_select()
|
||||
discovery = next(c for c in app.call_service.call_args_list
|
||||
if c.kwargs.get('topic') == 'homeassistant/select/vertex2_input/config')
|
||||
config = json.loads(discovery.kwargs['payload'])
|
||||
self.assertEqual(config['availability'][1], {
|
||||
'topic': 'transport/connection', 'payload_available': 'ready', 'payload_not_available': 'gone'})
|
||||
Reference in new issue
Block a user