def test_hue_activate_scene(self, mock_phue): """Test the hue_activate_scene service.""" with patch('homeassistant.helpers.discovery.load_platform'): bridge = hue.HueBridge('localhost', self.hass, hue.PHUE_CONFIG_FILE, None) bridge.setup() # No args self.hass.services.call(hue.DOMAIN, hue.SERVICE_HUE_SCENE, blocking=True) bridge.bridge.run_scene.assert_not_called() # Only one arg self.hass.services.call(hue.DOMAIN, hue.SERVICE_HUE_SCENE, {hue.ATTR_GROUP_NAME: 'group'}, blocking=True) bridge.bridge.run_scene.assert_not_called() self.hass.services.call(hue.DOMAIN, hue.SERVICE_HUE_SCENE, {hue.ATTR_SCENE_NAME: 'scene'}, blocking=True) bridge.bridge.run_scene.assert_not_called() # Both required args self.hass.services.call(hue.DOMAIN, hue.SERVICE_HUE_SCENE, { hue.ATTR_GROUP_NAME: 'group', hue.ATTR_SCENE_NAME: 'scene' }, blocking=True) bridge.bridge.run_scene.assert_called_once_with('group', 'scene')
def test_setup_bridge_registration_succeeds(self, mock_phue): """Test a registration success sequence.""" mock_bridge = mock_phue.Bridge mock_phue.PhueRegistrationException = Exception mock_bridge.side_effect = [ # First call, raise because not registered mock_phue.PhueRegistrationException(1, 2), # Second call, registration is done None, ] bridge = hue.HueBridge('localhost', self.hass, hue.PHUE_CONFIG_FILE, None) bridge.setup() self.assertFalse(bridge.configured) self.assertFalse(bridge.config_request_id is None) # Simulate the user confirming the registration self.hass.services.call( configurator.DOMAIN, configurator.SERVICE_CONFIGURE, {configurator.ATTR_CONFIGURE_ID: bridge.config_request_id}) self.hass.block_till_done() self.assertTrue(bridge.configured) self.assertTrue(bridge.config_request_id is None) # We should see a total of two identical calls args = call('localhost', config_file_path=get_test_config_dir(hue.PHUE_CONFIG_FILE)) mock_bridge.assert_has_calls([args, args]) # Make sure the request is done self.assertEqual(1, len(self.hass.states.all())) self.assertEqual('configured', self.hass.states.all()[0].state)
def test_setup_bridge_connection_refused(self, mock_phue): """Test a registration failed with a connection refused exception.""" mock_bridge = mock_phue.Bridge mock_bridge.side_effect = ConnectionRefusedError() bridge = hue.HueBridge('localhost', self.hass, hue.PHUE_CONFIG_FILE) bridge.setup() self.assertFalse(bridge.configured) self.assertTrue(bridge.config_request_id is None) mock_bridge.assert_called_once_with( 'localhost', config_file_path=get_test_config_dir(hue.PHUE_CONFIG_FILE))
def test_setup_bridge_registration_exception(self, mock_phue): """Test a registration failed with an exception.""" mock_bridge = mock_phue.Bridge mock_phue.PhueRegistrationException = Exception mock_bridge.side_effect = mock_phue.PhueRegistrationException(1, 2) bridge = hue.HueBridge('localhost', self.hass, hue.PHUE_CONFIG_FILE) bridge.setup() self.assertFalse(bridge.configured) self.assertFalse(bridge.config_request_id is None) self.assertTrue(isinstance(bridge.config_request_id, str)) mock_bridge.assert_called_once_with( 'localhost', config_file_path=get_test_config_dir(hue.PHUE_CONFIG_FILE))
def test_setup_bridge_registration_retry(self, mock_phue): """ Test a registration retry sequence. This may happen when we start the registration process, the user responds to the request but we fail to confirm it with the bridge. """ mock_bridge = mock_phue.Bridge mock_phue.PhueRegistrationException = Exception mock_bridge.side_effect = [ # First call, raise because not registered mock_phue.PhueRegistrationException(1, 2), # Second call, for whatever reason authentication fails mock_phue.PhueRegistrationException(1, 2), ] bridge = hue.HueBridge('localhost', self.hass, hue.PHUE_CONFIG_FILE, None) bridge.setup() self.assertFalse(bridge.configured) self.assertFalse(bridge.config_request_id is None) # Simulate the user confirming the registration self.hass.services.call( configurator.DOMAIN, configurator.SERVICE_CONFIGURE, {configurator.ATTR_CONFIGURE_ID: bridge.config_request_id}) self.hass.block_till_done() self.assertFalse(bridge.configured) self.assertFalse(bridge.config_request_id is None) # We should see a total of two identical calls args = call('localhost', config_file_path=get_test_config_dir(hue.PHUE_CONFIG_FILE)) mock_bridge.assert_has_calls([args, args]) # Make sure the request is done self.assertEqual(1, len(self.hass.states.all())) self.assertEqual('configure', self.hass.states.all()[0].state) self.assertEqual( 'Failed to register, please try again.', self.hass.states.all()[0].attributes.get(configurator.ATTR_ERRORS))
def test_setup_bridge_registration_fails(self, mock_phue): """ Test a registration failure sequence. This may happen when we start the registration process, the user responds to the request but the bridge has become unreachable. """ mock_bridge = mock_phue.Bridge mock_phue.PhueRegistrationException = Exception mock_bridge.side_effect = [ # First call, raise because not registered mock_phue.PhueRegistrationException(1, 2), # Second call, the bridge has gone away ConnectionRefusedError(), ] bridge = hue.HueBridge('localhost', self.hass, hue.PHUE_CONFIG_FILE, None) bridge.setup() self.assertFalse(bridge.configured) self.assertFalse(bridge.config_request_id is None) # Simulate the user confirming the registration self.hass.services.call( configurator.DOMAIN, configurator.SERVICE_CONFIGURE, {configurator.ATTR_CONFIGURE_ID: bridge.config_request_id}) self.hass.block_till_done() self.assertFalse(bridge.configured) self.assertFalse(bridge.config_request_id is None) # We should see a total of two identical calls args = call('localhost', config_file_path=get_test_config_dir(hue.PHUE_CONFIG_FILE)) mock_bridge.assert_has_calls([args, args]) # The request should still be pending self.assertEqual(1, len(self.hass.states.all())) self.assertEqual('configure', self.hass.states.all()[0].state)