def setUp(self): # pylint: disable=invalid-name """Set up things to be run when tests are started.""" self.hass = get_test_home_assistant() self.mock_storage = mock_storage() self.mock_storage.__enter__() mock_mqtt_component(self.hass) self.calls = []
def setup_method(self): """Set up things to be run when tests are started.""" self.hass = get_test_home_assistant() self.demo_provider = DemoProvider("en") self.default_tts_cache = self.hass.config.path(tts.DEFAULT_CACHE_DIR) self.mock_storage = mock_storage() self.mock_storage.__enter__() setup_component( self.hass, http.DOMAIN, {http.DOMAIN: { http.CONF_SERVER_PORT: get_test_instance_port() }}, )
def test_default_profiles_light(self): """Test default turn-on light profile for a specific light.""" platform = loader.get_component(self.hass, 'light.test') platform.init() user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE) real_isfile = os.path.isfile real_open = open def _mock_isfile(path): if path == user_light_file: return True return real_isfile(path) def _mock_open(path): if path == user_light_file: return StringIO(profile_data) return real_open(path) profile_data = "id,x,y,brightness\n" +\ "group.all_lights.default,.3,.5,200\n" +\ "light.ceiling_2.default,.6,.6,100\n" with mock.patch('os.path.isfile', side_effect=_mock_isfile): with mock.patch('builtins.open', side_effect=_mock_open): with mock_storage(): self.assertTrue( setup_component( self.hass, light.DOMAIN, {light.DOMAIN: { CONF_PLATFORM: 'test' }})) dev = next( filter(lambda x: x.entity_id == 'light.ceiling_2', platform.DEVICES)) light.turn_on(self.hass, dev.entity_id) self.hass.block_till_done() _, data = dev.last_call('turn_on') self.assertEqual( { light.ATTR_HS_COLOR: (50.353, 100), light.ATTR_BRIGHTNESS: 100 }, data)
def test_default_profiles_light(self): """Test default turn-on light profile for a specific light.""" platform = getattr(self.hass.components, "test.light") platform.init() user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE) real_isfile = os.path.isfile real_open = open def _mock_isfile(path): if path == user_light_file: return True return real_isfile(path) def _mock_open(path, *args, **kwargs): if path == user_light_file: return StringIO(profile_data) return real_open(path, *args, **kwargs) profile_data = ("id,x,y,brightness,transition\n" + "group.all_lights.default,.3,.5,200,0\n" + "light.ceiling_2.default,.6,.6,100,3\n") with mock.patch("os.path.isfile", side_effect=_mock_isfile), mock.patch( "builtins.open", side_effect=_mock_open), mock_storage(): assert setup_component(self.hass, light.DOMAIN, {light.DOMAIN: { CONF_PLATFORM: "test" }}) self.hass.block_till_done() dev = next( filter(lambda x: x.entity_id == "light.ceiling_2", platform.ENTITIES)) common.turn_on(self.hass, dev.entity_id) self.hass.block_till_done() _, data = dev.last_call("turn_on") assert { light.ATTR_HS_COLOR: (50.353, 100), light.ATTR_BRIGHTNESS: 100, light.ATTR_TRANSITION: 3, } == data
def test_default_profiles_group(self): """Test default turn-on light profile for all lights.""" platform = getattr(self.hass.components, "test.light") platform.init() user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE) real_isfile = os.path.isfile real_open = open def _mock_isfile(path): if path == user_light_file: return True return real_isfile(path) def _mock_open(path, *args, **kwargs): if path == user_light_file: return StringIO(profile_data) return real_open(path, *args, **kwargs) profile_data = ( "id,x,y,brightness,transition\ngroup.all_lights.default,.4,.6,99,2\n" ) with mock.patch("os.path.isfile", side_effect=_mock_isfile), mock.patch( "builtins.open", side_effect=_mock_open), mock_storage(): assert setup_component(self.hass, light.DOMAIN, {light.DOMAIN: { CONF_PLATFORM: "test" }}) self.hass.block_till_done() ent, _, _ = platform.ENTITIES common.turn_on(self.hass, ent.entity_id) self.hass.block_till_done() _, data = ent.last_call("turn_on") assert { light.ATTR_HS_COLOR: (71.059, 100), light.ATTR_BRIGHTNESS: 99, light.ATTR_TRANSITION: 2, } == data
def test_default_profiles_group(self): """Test default turn-on light profile for all lights.""" platform = loader.get_component(self.hass, 'light.test') platform.init() user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE) real_isfile = os.path.isfile real_open = open def _mock_isfile(path): if path == user_light_file: return True return real_isfile(path) def _mock_open(path): if path == user_light_file: return StringIO(profile_data) return real_open(path) profile_data = "id,x,y,brightness\n" +\ "group.all_lights.default,.4,.6,99\n" with mock.patch('os.path.isfile', side_effect=_mock_isfile): with mock.patch('builtins.open', side_effect=_mock_open): with mock_storage(): self.assertTrue( setup_component( self.hass, light.DOMAIN, {light.DOMAIN: { CONF_PLATFORM: 'test' }})) dev, _, _ = platform.DEVICES common.turn_on(self.hass, dev.entity_id) self.hass.block_till_done() _, data = dev.last_call('turn_on') self.assertEqual( { light.ATTR_HS_COLOR: (71.059, 100), light.ATTR_BRIGHTNESS: 99 }, data)
def test_default_profiles_light(self): """Test default turn-on light profile for a specific light.""" platform = loader.get_component(self.hass, 'light.test') platform.init() user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE) real_isfile = os.path.isfile real_open = open def _mock_isfile(path): if path == user_light_file: return True return real_isfile(path) def _mock_open(path): if path == user_light_file: return StringIO(profile_data) return real_open(path) profile_data = "id,x,y,brightness\n" +\ "group.all_lights.default,.3,.5,200\n" +\ "light.ceiling_2.default,.6,.6,100\n" with mock.patch('os.path.isfile', side_effect=_mock_isfile): with mock.patch('builtins.open', side_effect=_mock_open): with mock_storage(): assert setup_component( self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: 'test'}} ) dev = next(filter(lambda x: x.entity_id == 'light.ceiling_2', platform.DEVICES)) common.turn_on(self.hass, dev.entity_id) self.hass.block_till_done() _, data = dev.last_call('turn_on') assert { light.ATTR_HS_COLOR: (50.353, 100), light.ATTR_BRIGHTNESS: 100 } == data
def test_default_profiles_group(self): """Test default turn-on light profile for all lights.""" platform = loader.get_component(self.hass, 'light.test') platform.init() user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE) real_isfile = os.path.isfile real_open = open def _mock_isfile(path): if path == user_light_file: return True return real_isfile(path) def _mock_open(path): if path == user_light_file: return StringIO(profile_data) return real_open(path) profile_data = "id,x,y,brightness\n" +\ "group.all_lights.default,.4,.6,99\n" with mock.patch('os.path.isfile', side_effect=_mock_isfile): with mock.patch('builtins.open', side_effect=_mock_open): with mock_storage(): self.assertTrue(setup_component( self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: 'test'}} )) dev, _, _ = platform.DEVICES common.turn_on(self.hass, dev.entity_id) self.hass.block_till_done() _, data = dev.last_call('turn_on') self.assertEqual({ light.ATTR_HS_COLOR: (71.059, 100), light.ATTR_BRIGHTNESS: 99 }, data)
def hass_storage(): """Fixture to mock storage.""" with mock_storage() as stored_data: yield stored_data
def hass_storage(): """Fixture to mock storage.""" with mock_storage() as stored_data: yield stored_data
def setup_method(self): """Set up things to be run when tests are started.""" self.hass = get_test_home_assistant() self.mock_storage = mock_storage() self.mock_storage.__enter__() self.mock_mqtt = mock_mqtt_component(self.hass)
async def setUp(self): """Initialize this test class.""" self.hass = await async_test_home_assistant(self.loop) self.mock_storage = mock_storage() self.mock_storage.__enter__()