def test_service_face(self, camera_mock, aioclient_mock): """Set up component, test person face services.""" aioclient_mock.get( self.endpoint_url.format("persongroups"), text=load_fixture("microsoft_face_persongroups.json"), ) aioclient_mock.get( self.endpoint_url.format("persongroups/test_group1/persons"), text=load_fixture("microsoft_face_persons.json"), ) aioclient_mock.get( self.endpoint_url.format("persongroups/test_group2/persons"), text=load_fixture("microsoft_face_persons.json"), ) self.config["camera"] = {"platform": "demo"} with assert_setup_component(3, mf.DOMAIN): setup_component(self.opp, mf.DOMAIN, self.config) assert len(aioclient_mock.mock_calls) == 3 aioclient_mock.post( self.endpoint_url.format( "persongroups/test_group2/persons/" "2ae4935b-9659-44c3-977f-61fac20d0538/persistedFaces"), status=200, text="{}", ) face_person(self.opp, "test_group2", "David", "camera.demo_camera") self.opp.block_till_done() assert len(aioclient_mock.mock_calls) == 4 assert aioclient_mock.mock_calls[3][2] == b"Test"
def setup_method(self): """Set up things to be run when tests are started.""" self.opp = get_test_open_peer_power() config = { ip.DOMAIN: { "platform": "demo" }, "camera": { "platform": "demo" } } with patch( "openpeerpower.components.demo.image_processing." "DemoImageProcessingFace.should_poll", new_callable=PropertyMock(return_value=False), ): setup_component(self.opp, ip.DOMAIN, config) self.opp.block_till_done() state = self.opp.states.get("camera.demo_camera") self.url = ( f"{self.opp.config.internal_url}{state.attributes.get(ATTR_ENTITY_PICTURE)}" ) self.face_events = [] @callback def mock_face_event(event): """Mock event.""" self.face_events.append(event) self.opp.bus.listen("image_processing.detect_face", mock_face_event)
def test_setup_with_invalid_language_config(self): """Test the platform setup with language configuration.""" setup_component(self.opp, "sensor", INVALID_CONFIG_LANG) self.opp.block_till_done() state = self.opp.states.get("sensor.dark_sky_summary") assert state is None
def setup_method(self): """Set up things to be run when tests are started.""" self.opp = get_test_open_peer_power() self.opp.data.pop(DATA_CUSTOM_COMPONENTS) setup_component( self.opp, http.DOMAIN, {http.DOMAIN: { http.CONF_SERVER_PORT: get_test_instance_port() }}, ) config = { ip.DOMAIN: { "platform": "test" }, "camera": { "platform": "demo" } } setup_component(self.opp, ip.DOMAIN, config) self.opp.block_till_done() state = self.opp.states.get("camera.demo_camera") self.url = ( f"{self.opp.config.internal_url}{state.attributes.get(ATTR_ENTITY_PICTURE)}" )
def test_setup_component_test_entities(self, aioclient_mock): """Set up component.""" aioclient_mock.get( self.endpoint_url.format("persongroups"), text=load_fixture("microsoft_face_persongroups.json"), ) aioclient_mock.get( self.endpoint_url.format("persongroups/test_group1/persons"), text=load_fixture("microsoft_face_persons.json"), ) aioclient_mock.get( self.endpoint_url.format("persongroups/test_group2/persons"), text=load_fixture("microsoft_face_persons.json"), ) with assert_setup_component(3, mf.DOMAIN): setup_component(self.opp, mf.DOMAIN, self.config) assert len(aioclient_mock.mock_calls) == 3 entity_group1 = self.opp.states.get("microsoft_face.test_group1") entity_group2 = self.opp.states.get("microsoft_face.test_group2") assert entity_group1 is not None assert entity_group2 is not None assert (entity_group1.attributes["Ryan"] == "25985303-c537-4467-b41d-bdb45cd95ca1") assert (entity_group1.attributes["David"] == "2ae4935b-9659-44c3-977f-61fac20d0538") assert (entity_group2.attributes["Ryan"] == "25985303-c537-4467-b41d-bdb45cd95ca1") assert (entity_group2.attributes["David"] == "2ae4935b-9659-44c3-977f-61fac20d0538")
def test_service_say_german_config(self, aioclient_mock): """Test service call say with german code in the config.""" calls = mock_service(self.opp, DOMAIN_MP, SERVICE_PLAY_MEDIA) self.form_data["hl"] = "de-de" aioclient_mock.post(self.url, data=self.form_data, status=200, content=b"test") config = { tts.DOMAIN: { "platform": "voicerss", "api_key": "1234567xx", "language": "de-de", } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.opp, tts.DOMAIN, config) self.opp.services.call( tts.DOMAIN, "voicerss_say", { "entity_id": "media_player.something", tts.ATTR_MESSAGE: "I person is on front of your door.", }, ) self.opp.block_till_done() assert len(calls) == 1 assert len(aioclient_mock.mock_calls) == 1 assert aioclient_mock.mock_calls[0][2] == self.form_data
def test_service_say_error_msg(self, aioclient_mock): """Test service call say with http error api message.""" calls = mock_service(self.opp, DOMAIN_MP, SERVICE_PLAY_MEDIA) aioclient_mock.post( self.url, data=self.form_data, status=200, content=b"The subscription does not support SSML!", ) config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}} with assert_setup_component(1, tts.DOMAIN): setup_component(self.opp, tts.DOMAIN, config) self.opp.services.call( tts.DOMAIN, "voicerss_say", { "entity_id": "media_player.something", tts.ATTR_MESSAGE: "I person is on front of your door.", }, ) self.opp.block_till_done() assert len(calls) == 0 assert len(aioclient_mock.mock_calls) == 1 assert aioclient_mock.mock_calls[0][2] == self.form_data
def test_service_say(self, aioclient_mock): """Test service call say.""" calls = mock_service(self.opp, DOMAIN_MP, SERVICE_PLAY_MEDIA) aioclient_mock.post(self.url, data=self.form_data, status=200, content=b"test") config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}} with assert_setup_component(1, tts.DOMAIN): setup_component(self.opp, tts.DOMAIN, config) self.opp.services.call( tts.DOMAIN, "voicerss_say", { "entity_id": "media_player.something", tts.ATTR_MESSAGE: "I person is on front of your door.", }, ) self.opp.block_till_done() assert len(calls) == 1 assert len(aioclient_mock.mock_calls) == 1 assert aioclient_mock.mock_calls[0][2] == self.form_data assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(".mp3") != -1
def test_disable_component_if_invalid_return(self): """Test disabling component if invalid return.""" mock_integration( self.opp, MockModule("disabled_component", setup=lambda opp, config: None)) assert not setup.setup_component(self.opp, "disabled_component", {}) assert "disabled_component" not in self.opp.config.components self.opp.data.pop(setup.DATA_SETUP) mock_integration( self.opp, MockModule("disabled_component", setup=lambda opp, config: False), ) assert not setup.setup_component(self.opp, "disabled_component", {}) assert "disabled_component" not in self.opp.config.components self.opp.data.pop(setup.DATA_SETUP) mock_integration( self.opp, MockModule("disabled_component", setup=lambda opp, config: True)) assert setup.setup_component(self.opp, "disabled_component", {}) assert "disabled_component" in self.opp.config.components
def test_service_say_timeout(self, aioclient_mock): """Test service call say with http timeout.""" calls = mock_service(self.opp, DOMAIN_MP, SERVICE_PLAY_MEDIA) aioclient_mock.post(self.url, data=self.form_data, exc=asyncio.TimeoutError()) config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}} with assert_setup_component(1, tts.DOMAIN): setup_component(self.opp, tts.DOMAIN, config) self.opp.services.call( tts.DOMAIN, "voicerss_say", { "entity_id": "media_player.something", tts.ATTR_MESSAGE: "I person is on front of your door.", }, ) self.opp.block_till_done() assert len(calls) == 0 assert len(aioclient_mock.mock_calls) == 1 assert aioclient_mock.mock_calls[0][2] == self.form_data
def test_setup_component_invalidprovince(self): """Set up workday component.""" with assert_setup_component(1, "binary_sensor"): setup_component(self.opp, "binary_sensor", self.config_invalidprovince) entity = self.opp.states.get("binary_sensor.workday_sensor") assert entity is None
def test_service_groups(self, mock_update, aioclient_mock): """Set up component, test groups services.""" aioclient_mock.put( self.endpoint_url.format("persongroups/service_group"), status=200, text="{}", ) aioclient_mock.delete( self.endpoint_url.format("persongroups/service_group"), status=200, text="{}", ) with assert_setup_component(3, mf.DOMAIN): setup_component(self.opp, mf.DOMAIN, self.config) create_group(self.opp, "Service Group") self.opp.block_till_done() entity = self.opp.states.get("microsoft_face.service_group") assert entity is not None assert len(aioclient_mock.mock_calls) == 1 delete_group(self.opp, "Service Group") self.opp.block_till_done() entity = self.opp.states.get("microsoft_face.service_group") assert entity is None assert len(aioclient_mock.mock_calls) == 2
def test_service_say_http_error(self): """Test service call say.""" calls = mock_service(self.opp, DOMAIN_MP, SERVICE_PLAY_MEDIA) config = {tts.DOMAIN: {"platform": "marytts"}} with assert_setup_component(1, tts.DOMAIN): setup_component(self.opp, tts.DOMAIN, config) with patch( "openpeerpower.components.marytts.tts.MaryTTS.speak", side_effect=Exception(), ) as mock_speak: self.opp.services.call( tts.DOMAIN, "marytts_say", { "entity_id": "media_player.something", tts.ATTR_MESSAGE: "OpenPeerPower", }, ) self.opp.block_till_done() mock_speak.assert_called_once() assert len(calls) == 0
def test_setup_with_alerts_config(self): """Test the platform setup with alert configuration.""" setup_component(self.opp, "sensor", VALID_CONFIG_ALERTS) self.opp.block_till_done() state = self.opp.states.get("sensor.dark_sky_alerts") assert state.state == "0"
def test_service_say(self): """Test service call say.""" calls = mock_service(self.opp, DOMAIN_MP, SERVICE_PLAY_MEDIA) config = {tts.DOMAIN: {"platform": "marytts"}} with assert_setup_component(1, tts.DOMAIN): setup_component(self.opp, tts.DOMAIN, config) with patch( "openpeerpower.components.marytts.tts.MaryTTS.speak", return_value=b"audio", ) as mock_speak: self.opp.services.call( tts.DOMAIN, "marytts_say", { "entity_id": "media_player.something", tts.ATTR_MESSAGE: "OpenPeerPower", }, ) self.opp.block_till_done() mock_speak.assert_called_once() mock_speak.assert_called_with("OpenPeerPower", {}) assert len(calls) == 1 assert calls[0].data[ATTR_MEDIA_CONTENT_ID].find(".wav") != -1
def mock_load_translation_files(files): """Mock load translation files.""" nonlocal load_count load_count += 1 # Mimic race condition by loading a component during setup setup_component(opp, "persistent_notification", {}) return {"component1": {"title": "world"}}
def test_circular_import(self): """Test we don't break doing circular import. This test will have test_component discover the switch.test_circular component while setting up. The supplied config will load test_component and will load switch.test_circular. That means that after startup, we will have test_component and switch setup. The test_circular platform has been loaded twice. """ component_calls = [] platform_calls = [] def component_setup(opp, config): """Set up mock component.""" discovery.load_platform(opp, "switch", "test_circular", "disc", config) component_calls.append(1) return True def setup_platform(opp, config, add_entities_callback, discovery_info=None): """Set up mock platform.""" platform_calls.append("disc" if discovery_info else "component") mock_integration(self.opp, MockModule("test_component", setup=component_setup)) # dependencies are only set in component level # since we are using manifest to hold them mock_integration( self.opp, MockModule("test_circular", dependencies=["test_component"])) mock_entity_platform(self.opp, "switch.test_circular", MockPlatform(setup_platform)) setup.setup_component( self.opp, "test_component", { "test_component": None, "switch": [{ "platform": "test_circular" }] }, ) self.opp.block_till_done() # test_component will only be setup once assert len(component_calls) == 1 # The platform will be setup once via the config in `setup_component` # and once via the discovery inside test_component. assert len(platform_calls) == 2 assert "test_component" in self.opp.config.components assert "switch" in self.opp.config.components
def test_setup_with_config(self): """Test the platform setup with configuration.""" setup_component(self.opp, "sensor", VALID_CONFIG_MINIMAL) self.opp.block_till_done() state = self.opp.states.get("sensor.dark_sky_summary") assert state is not None
def test_component_setup_with_validation_and_dependency(self): """Test all config is passed to dependencies.""" def config_check_setup(opp, config): """Test that config is passed in.""" if config.get("comp_a", {}).get("valid", False): return True raise Exception(f"Config not passed in: {config}") platform = MockPlatform() mock_integration(self.opp, MockModule("comp_a", setup=config_check_setup)) mock_integration( self.opp, MockModule("platform_a", setup=config_check_setup, dependencies=["comp_a"]), ) mock_entity_platform(self.opp, "switch.platform_a", platform) setup.setup_component( self.opp, "switch", { "comp_a": { "valid": True }, "switch": { "platform": "platform_a" } }, ) self.opp.block_till_done() assert "comp_a" in self.opp.config.components
def test_setup_component_noprovince(self): """Set up workday component.""" with assert_setup_component(1, "binary_sensor"): setup_component(self.opp, "binary_sensor", self.config_noprovince) self.opp.block_till_done() entity = self.opp.states.get("binary_sensor.workday_sensor") assert entity is not None
def test_setup_component_with_service(self): """Set up demo platform on image_process component test service.""" config = {ip.DOMAIN: {"platform": "demo"}} with assert_setup_component(1, ip.DOMAIN): setup_component(self.opp, ip.DOMAIN, config) assert self.opp.services.has_service(ip.DOMAIN, "scan")
def test_setup_component(self): """Set up ffmpeg component.""" with assert_setup_component(1, "binary_sensor"): setup_component(self.opp, "binary_sensor", self.config) self.opp.block_till_done() assert self.opp.data["ffmpeg"].binary == "ffmpeg" assert self.opp.states.get("binary_sensor.ffmpeg_motion") is not None
def test_setup_component_test_service(self): """Set up ffmpeg component test services.""" with assert_setup_component(1): setup_component(self.opp, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}}) assert self.opp.services.has_service(ffmpeg.DOMAIN, "start") assert self.opp.services.has_service(ffmpeg.DOMAIN, "stop") assert self.opp.services.has_service(ffmpeg.DOMAIN, "restart")
def test_platform_specific_config_validation(self): """Test platform that specifies config.""" platform_schema = PLATFORM_SCHEMA.extend({"valid": True}, extra=vol.PREVENT_EXTRA) mock_setup = Mock(spec_set=True) mock_entity_platform( self.opp, "switch.platform_a", MockPlatform(platform_schema=platform_schema, setup_platform=mock_setup), ) with assert_setup_component(0, "switch"): assert setup.setup_component( self.opp, "switch", {"switch": { "platform": "platform_a", "invalid": True }}, ) self.opp.block_till_done() assert mock_setup.call_count == 0 self.opp.data.pop(setup.DATA_SETUP) self.opp.config.components.remove("switch") with assert_setup_component(0): assert setup.setup_component( self.opp, "switch", { "switch": { "platform": "platform_a", "valid": True, "invalid_extra": True, } }, ) self.opp.block_till_done() assert mock_setup.call_count == 0 self.opp.data.pop(setup.DATA_SETUP) self.opp.config.components.remove("switch") with assert_setup_component(1, "switch"): assert setup.setup_component( self.opp, "switch", {"switch": { "platform": "platform_a", "valid": True }}, ) self.opp.block_till_done() assert mock_setup.call_count == 1
def test_yesterday(self, mock_date): """Test if yesterday are reported correctly.""" with assert_setup_component(1, "binary_sensor"): setup_component(self.opp, "binary_sensor", self.config_yesterday) self.opp.start() entity = self.opp.states.get("binary_sensor.workday_sensor") assert entity.state == "on"
def test_config_example2_add_holiday(self, mock_date): """Test if public holidays are reported correctly.""" with assert_setup_component(1, "binary_sensor"): setup_component(self.opp, "binary_sensor", self.config_example2) self.opp.start() entity = self.opp.states.get("binary_sensor.workday_sensor") assert entity.state == "off"
def test_config_remove_holidays_xmas(self, mock_date): """Test if removed holidays are reported correctly.""" with assert_setup_component(1, "binary_sensor"): setup_component(self.opp, "binary_sensor", self.config_remove_holidays) self.opp.start() entity = self.opp.states.get("binary_sensor.workday_sensor") assert entity.state == "on"
def test_setup_tests(self): """Set up test config and test it.""" with assert_setup_component(5): setup_component(self.opp, rc.DOMAIN, self.config) assert self.opp.services.has_service(rc.DOMAIN, "get_test") assert self.opp.services.has_service(rc.DOMAIN, "post_test") assert self.opp.services.has_service(rc.DOMAIN, "put_test") assert self.opp.services.has_service(rc.DOMAIN, "delete_test")
def test_public_holiday_noprovince(self, mock_date): """Test if public holidays are reported correctly.""" with assert_setup_component(1, "binary_sensor"): setup_component(self.opp, "binary_sensor", self.config_noprovince) self.opp.block_till_done() self.opp.start() entity = self.opp.states.get("binary_sensor.workday_sensor") assert entity.state == "on"
def test_setup_component(self): """Test setup component.""" config = { tts.DOMAIN: { "platform": "yandextts", "api_key": "1234567xx" } } with assert_setup_component(1, tts.DOMAIN): setup_component(self.opp, tts.DOMAIN, config)