diff --git a/tests/components/light/test_init.py b/tests/components/light/test_init.py index 2b2830503f4..fcbe89ee27b 100644 --- a/tests/components/light/test_init.py +++ b/tests/components/light/test_init.py @@ -9,10 +9,9 @@ import unittest import os import homeassistant.loader as loader -import homeassistant.util.color as color_util from homeassistant.const import ( ATTR_ENTITY_ID, STATE_ON, STATE_OFF, CONF_PLATFORM, - SERVICE_TURN_ON, SERVICE_TURN_OFF) + SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE) import homeassistant.components.light as light from tests.common import mock_service, get_test_home_assistant @@ -94,6 +93,23 @@ class TestLight(unittest.TestCase): self.assertEqual('entity_id_val', call.data[ATTR_ENTITY_ID]) self.assertEqual('transition_val', call.data[light.ATTR_TRANSITION]) + # Test toggle + toggle_calls = mock_service( + self.hass, light.DOMAIN, SERVICE_TOGGLE) + + light.toggle( + self.hass, entity_id='entity_id_val', transition='transition_val') + + self.hass.pool.block_till_done() + + self.assertEqual(1, len(toggle_calls)) + call = toggle_calls[-1] + + self.assertEqual(light.DOMAIN, call.domain) + self.assertEqual(SERVICE_TOGGLE, call.service) + self.assertEqual('entity_id_val', call.data[ATTR_ENTITY_ID]) + self.assertEqual('transition_val', call.data[light.ATTR_TRANSITION]) + def test_services(self): """ Test the provided services. """ platform = loader.get_component('light.test') @@ -109,7 +125,7 @@ class TestLight(unittest.TestCase): self.assertFalse(light.is_on(self.hass, dev2.entity_id)) self.assertFalse(light.is_on(self.hass, dev3.entity_id)) - # Test basic turn_on, turn_off services + # Test basic turn_on, turn_off, toggle services light.turn_off(self.hass, entity_id=dev1.entity_id) light.turn_on(self.hass, entity_id=dev2.entity_id) @@ -136,6 +152,24 @@ class TestLight(unittest.TestCase): self.assertFalse(light.is_on(self.hass, dev2.entity_id)) self.assertFalse(light.is_on(self.hass, dev3.entity_id)) + # toggle all lights + light.toggle(self.hass) + + self.hass.pool.block_till_done() + + self.assertTrue(light.is_on(self.hass, dev1.entity_id)) + self.assertTrue(light.is_on(self.hass, dev2.entity_id)) + self.assertTrue(light.is_on(self.hass, dev3.entity_id)) + + # toggle all lights + light.toggle(self.hass) + + self.hass.pool.block_till_done() + + self.assertFalse(light.is_on(self.hass, dev1.entity_id)) + self.assertFalse(light.is_on(self.hass, dev2.entity_id)) + self.assertFalse(light.is_on(self.hass, dev3.entity_id)) + # Ensure all attributes process correctly light.turn_on(self.hass, dev1.entity_id, transition=10, brightness=20) diff --git a/tests/components/media_player/test_init.py b/tests/components/media_player/test_init.py index 211626ea3fb..a0a7ebc9567 100644 --- a/tests/components/media_player/test_init.py +++ b/tests/components/media_player/test_init.py @@ -12,7 +12,8 @@ from homeassistant.const import ( STATE_OFF, SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_VOLUME_UP, SERVICE_VOLUME_DOWN, SERVICE_MEDIA_PLAY_PAUSE, SERVICE_MEDIA_PLAY, SERVICE_MEDIA_PAUSE, - SERVICE_MEDIA_NEXT_TRACK, SERVICE_MEDIA_PREVIOUS_TRACK, ATTR_ENTITY_ID) + SERVICE_MEDIA_NEXT_TRACK, SERVICE_MEDIA_PREVIOUS_TRACK, SERVICE_TOGGLE, + ATTR_ENTITY_ID) import homeassistant.components.media_player as media_player from tests.common import mock_service @@ -45,6 +46,7 @@ class TestMediaPlayer(unittest.TestCase): services = { SERVICE_TURN_ON: media_player.turn_on, SERVICE_TURN_OFF: media_player.turn_off, + SERVICE_TOGGLE: media_player.toggle, SERVICE_VOLUME_UP: media_player.volume_up, SERVICE_VOLUME_DOWN: media_player.volume_down, SERVICE_MEDIA_PLAY_PAUSE: media_player.media_play_pause, diff --git a/tests/components/test_init.py b/tests/components/test_init.py index cb170a5c24b..723149b19c6 100644 --- a/tests/components/test_init.py +++ b/tests/components/test_init.py @@ -10,7 +10,7 @@ from unittest.mock import patch import homeassistant.core as ha from homeassistant.const import ( - STATE_ON, STATE_OFF, SERVICE_TURN_ON, SERVICE_TURN_OFF) + STATE_ON, STATE_OFF, SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE) import homeassistant.components as comps from tests.common import get_test_home_assistant @@ -61,6 +61,18 @@ class TestComponentsCore(unittest.TestCase): self.assertEqual(1, len(runs)) + def test_toggle(self): + """ Test toggle method. """ + runs = [] + self.hass.services.register( + 'light', SERVICE_TOGGLE, lambda x: runs.append(1)) + + comps.toggle(self.hass, 'light.Bowl') + + self.hass.pool.block_till_done() + + self.assertEqual(1, len(runs)) + @patch('homeassistant.core.ServiceRegistry.call') def test_turn_on_to_not_block_for_domains_without_service(self, mock_call): self.hass.services.register('light', SERVICE_TURN_ON, lambda x: x)