async def test_setup_recovers_when_setup_raises(opp): """Test the setup if exceptions are happening.""" platform1_setup = Mock(side_effect=Exception("Broken")) platform2_setup = Mock(return_value=None) mock_entity_platform(opp, "test_domain.mod1", MockPlatform(platform1_setup)) mock_entity_platform(opp, "test_domain.mod2", MockPlatform(platform2_setup)) component = EntityComponent(_LOGGER, DOMAIN, opp) assert not platform1_setup.called assert not platform2_setup.called component.setup( OrderedDict( [ (DOMAIN, {"platform": "mod1"}), (f"{DOMAIN} 2", {"platform": "non_exist"}), (f"{DOMAIN} 3", {"platform": "mod2"}), ] ) ) await opp.async_block_till_done() assert platform1_setup.called assert platform2_setup.called
async def test_setup_does_discovery(mock_setup_component, mock_setup, opp): """Test setup for discovery.""" component = EntityComponent(_LOGGER, DOMAIN, opp) component.setup({}) discovery.load_platform( opp, DOMAIN, "platform_test", {"msg": "discovery_info"}, {DOMAIN: {}} ) await opp.async_block_till_done() assert mock_setup.called assert ("platform_test", {}, {"msg": "discovery_info"}) == mock_setup.call_args[0]
async def test_set_scan_interval_via_config(mock_track, opp): """Test the setting of the scan interval via configuration.""" def platform_setup(opp, config, add_entities, discovery_info=None): """Test the platform setup.""" add_entities([MockEntity(should_poll=True)]) mock_entity_platform(opp, "test_domain.platform", MockPlatform(platform_setup)) component = EntityComponent(_LOGGER, DOMAIN, opp) component.setup( {DOMAIN: {"platform": "platform", "scan_interval": timedelta(seconds=30)}} ) await opp.async_block_till_done() assert mock_track.called assert timedelta(seconds=30) == mock_track.call_args[0][2]
async def test_setup_loads_platforms(opp): """Test the loading of the platforms.""" component_setup = Mock(return_value=True) platform_setup = Mock(return_value=None) mock_integration(opp, MockModule("test_component", setup=component_setup)) # mock the dependencies mock_integration(opp, MockModule("mod2", dependencies=["test_component"])) mock_entity_platform(opp, "test_domain.mod2", MockPlatform(platform_setup)) component = EntityComponent(_LOGGER, DOMAIN, opp) assert not component_setup.called assert not platform_setup.called component.setup({DOMAIN: {"platform": "mod2"}}) await opp.async_block_till_done() assert component_setup.called assert platform_setup.called
async def test_set_entity_namespace_via_config(opp): """Test setting an entity namespace.""" def platform_setup(opp, config, add_entities, discovery_info=None): """Test the platform setup.""" add_entities([MockEntity(name="beer"), MockEntity(name=None)]) platform = MockPlatform(platform_setup) mock_entity_platform(opp, "test_domain.platform", platform) component = EntityComponent(_LOGGER, DOMAIN, opp) component.setup({DOMAIN: {"platform": "platform", "entity_namespace": "yummy"}}) await opp.async_block_till_done() assert sorted(opp.states.async_entity_ids()) == [ "test_domain.yummy_beer", "test_domain.yummy_unnamed_device", ]