def test_pararell_updates_async_platform_with_constant(hass): """Warn we log when platform setup takes a long time.""" platform = MockPlatform() @asyncio.coroutine def mock_update(*args, **kwargs): pass platform.async_setup_platform = mock_update platform.PARALLEL_UPDATES = 1 loader.set_component('test_domain.platform', platform) component = EntityComponent(_LOGGER, DOMAIN, hass) component._platforms = {} yield from component.async_setup({ DOMAIN: { 'platform': 'platform', } }) handle = list(component._platforms.values())[-1] assert handle.parallel_updates is not None
async def test_parallel_updates_sync_platform_with_constant(hass): """Test sync platform can set parallel_updates limit.""" platform = MockPlatform() platform.PARALLEL_UPDATES = 2 loader.set_component(hass, 'test_domain.platform', platform) component = EntityComponent(_LOGGER, DOMAIN, hass) component._platforms = {} await component.async_setup({ DOMAIN: { 'platform': 'platform', } }) handle = list(component._platforms.values())[-1] assert handle.parallel_updates == 2 class SyncEntity(MockEntity): """Mock entity that has update.""" async def update(self): pass entity = SyncEntity() await handle.async_add_entities([entity]) assert entity.parallel_updates is not None assert entity.parallel_updates._value == 2
def test_set_scan_interval_via_platform(self, mock_track): """Test the setting of the scan interval via platform.""" def platform_setup(hass, config, add_devices, discovery_info=None): """Test the platform setup.""" add_devices([EntityTest(should_poll=True)]) platform = MockPlatform(platform_setup) platform.SCAN_INTERVAL = 30 loader.set_component("test_domain.platform", platform) component = EntityComponent(_LOGGER, DOMAIN, self.hass) component.setup({DOMAIN: {"platform": "platform"}}) assert mock_track.called assert [0, 30] == list(mock_track.call_args[1]["second"])
def test_set_scan_interval_via_platform(self, mock_track): def platform_setup(hass, config, add_devices, discovery_info=None): add_devices([EntityTest(should_poll=True)]) platform = MockPlatform(platform_setup) platform.SCAN_INTERVAL = 30 loader.set_component('test_domain.platform', platform) component = EntityComponent(_LOGGER, DOMAIN, self.hass) component.setup({ DOMAIN: { 'platform': 'platform', } }) assert mock_track.called assert [0, 30] == list(mock_track.call_args[1]['second'])
def test_set_scan_interval_via_platform(self, mock_track): """Test the setting of the scan interval via platform.""" def platform_setup(hass, config, add_devices, discovery_info=None): """Test the platform setup.""" add_devices([EntityTest(should_poll=True)]) platform = MockPlatform(platform_setup) platform.SCAN_INTERVAL = timedelta(seconds=30) loader.set_component('test_domain.platform', platform) component = EntityComponent(_LOGGER, DOMAIN, self.hass) component.setup({ DOMAIN: { 'platform': 'platform', } }) assert mock_track.called assert timedelta(seconds=30) == mock_track.call_args[0][2]
async def test_platform_error_slow_setup(hass, caplog): """Don't block startup more than SLOW_SETUP_MAX_WAIT.""" with patch.object(entity_platform, "SLOW_SETUP_MAX_WAIT", 0): called = [] async def setup_platform(*args): called.append(1) await asyncio.sleep(1) platform = MockPlatform(async_setup_platform=setup_platform) component = EntityComponent(_LOGGER, DOMAIN, hass) mock_entity_platform(hass, "test_domain.test_platform", platform) await component.async_setup({DOMAIN: {"platform": "test_platform"}}) await hass.async_block_till_done() assert len(called) == 1 assert "test_domain.test_platform" not in hass.config.components assert "test_platform is taking longer than 0 seconds" in caplog.text
async def test_reload_platform(hass): """Test the polling of only updated entities.""" component_setup = Mock(return_value=True) setup_called = [] async def setup_platform(*args): setup_called.append(args) mock_integration(hass, MockModule(DOMAIN, setup=component_setup)) mock_integration(hass, MockModule(PLATFORM, dependencies=[DOMAIN])) mock_platform = MockPlatform(async_setup_platform=setup_platform) mock_entity_platform(hass, f"{DOMAIN}.{PLATFORM}", mock_platform) component = EntityComponent(_LOGGER, DOMAIN, hass) await component.async_setup( {DOMAIN: { "platform": PLATFORM, "sensors": None }}) await hass.async_block_till_done() assert component_setup.called assert f"{DOMAIN}.{PLATFORM}" in hass.config.components assert len(setup_called) == 1 platform = async_get_platform_without_config_entry(hass, PLATFORM, DOMAIN) assert platform.platform_name == PLATFORM assert platform.domain == DOMAIN yaml_path = path.join( _get_fixtures_base_path(), "fixtures", "helpers/reload_configuration.yaml", ) with patch.object(config, "YAML_CONFIG_FILE", yaml_path): await async_reload_integration_platforms(hass, PLATFORM, [DOMAIN]) assert len(setup_called) == 2 existing_platforms = async_get_platforms(hass, PLATFORM) for existing_platform in existing_platforms: existing_platform.config_entry = "abc" assert not async_get_platform_without_config_entry(hass, PLATFORM, DOMAIN)
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.MagicMock(spec_set=True) loader.set_component( 'switch.platform_a', MockPlatform(platform_schema=platform_schema, setup_platform=mock_setup)) with assert_setup_component(0): assert bootstrap.setup_component( self.hass, 'switch', {'switch': { 'platform': 'platform_a', 'invalid': True }}) assert mock_setup.call_count == 0 self.hass.config.components.remove('switch') with assert_setup_component(0): assert bootstrap.setup_component( self.hass, 'switch', { 'switch': { 'platform': 'platform_a', 'valid': True, 'invalid_extra': True, } }) assert mock_setup.call_count == 0 self.hass.config.components.remove('switch') with assert_setup_component(1): assert bootstrap.setup_component( self.hass, 'switch', {'switch': { 'platform': 'platform_a', 'valid': True }}) assert mock_setup.call_count == 1
async def test_setup_entry_platform_not_ready(hass, caplog): """Test when an entry is not ready yet.""" async_setup_entry = Mock(side_effect=PlatformNotReady) platform = MockPlatform(async_setup_entry=async_setup_entry) config_entry = MockConfigEntry() ent_platform = MockEntityPlatform( hass, platform_name=config_entry.domain, platform=platform ) with patch.object(entity_platform, "async_call_later") as mock_call_later: assert not await ent_platform.async_setup_entry(config_entry) full_name = f"{ent_platform.domain}.{config_entry.domain}" assert full_name not in hass.config.components assert len(async_setup_entry.mock_calls) == 1 assert "Platform test not ready yet" in caplog.text assert len(mock_call_later.mock_calls) == 1
def test_set_scan_interval_via_config(self, mock_track): def platform_setup(hass, config, add_devices, discovery_info=None): add_devices([EntityTest(should_poll=True)]) loader.set_component('test_domain.platform', MockPlatform(platform_setup)) component = EntityComponent(_LOGGER, DOMAIN, self.hass) component.setup( {DOMAIN: { 'platform': 'platform', 'scan_interval': 30, }}) assert mock_track.called assert [0, 30] == list(mock_track.call_args[1]['second'])
async def test_device_info_called(hass): """Test device info is forwarded correctly.""" registry = await hass.helpers.device_registry.async_get_registry() hub = registry.async_get_or_create(config_entry_id='123', connections=set(), identifiers={('hue', 'hub-id')}, manufacturer='manufacturer', model='hub') async def async_setup_entry(hass, config_entry, async_add_entities): """Mock setup entry method.""" async_add_entities([ # Invalid device info MockEntity(unique_id='abcd', device_info={}), # Valid device info MockEntity(unique_id='qwer', device_info={ 'identifiers': {('hue', '1234')}, 'connections': {('mac', 'abcd')}, 'manufacturer': 'test-manuf', 'model': 'test-model', 'name': 'test-name', 'sw_version': 'test-sw', 'via_hub': ('hue', 'hub-id'), }), ]) return True platform = MockPlatform(async_setup_entry=async_setup_entry) config_entry = MockConfigEntry(entry_id='super-mock-id') entity_platform = MockEntityPlatform(hass, platform_name=config_entry.domain, platform=platform) assert await entity_platform.async_setup_entry(config_entry) await hass.async_block_till_done() device = registry.async_get_device({('hue', '1234')}, set()) assert device is not None assert device.identifiers == {('hue', '1234')} assert device.connections == {('mac', 'abcd')} assert device.manufacturer == 'test-manuf' assert device.model == 'test-model' assert device.name == 'test-name' assert device.sw_version == 'test-sw' assert device.hub_device_id == hub.id
async def test_parallel_updates_no_update_method(hass): """Test platform parallel_updates default set to 0.""" platform = MockPlatform() mock_entity_platform(hass, "test_domain.platform", platform) component = EntityComponent(_LOGGER, DOMAIN, hass) component._platforms = {} await component.async_setup({DOMAIN: {"platform": "platform"}}) await hass.async_block_till_done() handle = list(component._platforms.values())[-1] entity = MockEntity() await handle.async_add_entities([entity]) assert entity.parallel_updates is None
async def test_setup_reload_service(hass): """Test setting up a reload service.""" component_setup = Mock(return_value=True) setup_called = [] async def setup_platform(*args): setup_called.append(args) mock_integration(hass, MockModule(DOMAIN, setup=component_setup)) mock_integration(hass, MockModule(PLATFORM, dependencies=[DOMAIN])) mock_platform = MockPlatform(async_setup_platform=setup_platform) mock_entity_platform(hass, f"{DOMAIN}.{PLATFORM}", mock_platform) component = EntityComponent(_LOGGER, DOMAIN, hass) await component.async_setup( {DOMAIN: { "platform": PLATFORM, "sensors": None }}) await hass.async_block_till_done() assert component_setup.called assert f"{DOMAIN}.{PLATFORM}" in hass.config.components assert len(setup_called) == 1 await async_setup_reload_service(hass, PLATFORM, [DOMAIN]) yaml_path = path.join( _get_fixtures_base_path(), "fixtures", "helpers/reload_configuration.yaml", ) with patch.object(config, "YAML_CONFIG_FILE", yaml_path): await hass.services.async_call( PLATFORM, SERVICE_RELOAD, {}, blocking=True, ) await hass.async_block_till_done() assert len(setup_called) == 2
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_dependencies_platform(hass): """Test we setup the dependencies of a platform. We're explicitly testing that we process dependencies even if a component with the same name has already been loaded. """ mock_integration( hass, MockModule("test_component", dependencies=["test_component2"])) mock_integration(hass, MockModule("test_component2")) mock_entity_platform(hass, "test_domain.test_component", MockPlatform()) component = EntityComponent(_LOGGER, DOMAIN, hass) await component.async_setup({DOMAIN: {"platform": "test_component"}}) await hass.async_block_till_done() assert "test_component" in hass.config.components assert "test_component2" in hass.config.components assert "test_domain.test_component" in hass.config.components
async def test_platform_error_slow_setup(hass, caplog): """Don't block startup more than SLOW_SETUP_MAX_WAIT.""" with patch.object(entity_platform, 'SLOW_SETUP_MAX_WAIT', 0): called = [] async def setup_platform(*args): called.append(1) await asyncio.sleep(1, loop=hass.loop) platform = MockPlatform(async_setup_platform=setup_platform) component = EntityComponent(_LOGGER, DOMAIN, hass) mock_entity_platform(hass, 'test_domain.test_platform', platform) await component.async_setup({DOMAIN: { 'platform': 'test_platform', }}) assert len(called) == 1 assert 'test_domain.test_platform' not in hass.config.components assert 'test_platform is taking longer than 0 seconds' in caplog.text
def test_pararell_updates_sync_platform(hass): """Warn we log when platform setup takes a long time.""" platform = MockPlatform() loader.set_component('test_domain.platform', platform) component = EntityComponent(_LOGGER, DOMAIN, hass) component._platforms = {} yield from component.async_setup({ DOMAIN: { 'platform': 'platform', } }) handle = list(component._platforms.values())[-1] assert handle.parallel_updates is not None
async def test_device_info_invalid_url(hass, caplog): """Test device info is forwarded correctly.""" registry = dr.async_get(hass) registry.async_get_or_create( config_entry_id="123", connections=set(), identifiers={("hue", "via-id")}, manufacturer="manufacturer", model="via", ) async def async_setup_entry(hass, config_entry, async_add_entities): """Mock setup entry method.""" async_add_entities([ # Valid device info, but invalid url MockEntity( unique_id="qwer", device_info={ "identifiers": {("hue", "1234")}, "configuration_url": "foo://192.168.0.100/config", }, ), ]) return True platform = MockPlatform(async_setup_entry=async_setup_entry) config_entry = MockConfigEntry(entry_id="super-mock-id") entity_platform = MockEntityPlatform(hass, platform_name=config_entry.domain, platform=platform) assert await entity_platform.async_setup_entry(config_entry) await hass.async_block_till_done() assert len(hass.states.async_entity_ids()) == 1 device = registry.async_get_device({("hue", "1234")}) assert device is not None assert device.identifiers == {("hue", "1234")} assert device.configuration_url is None assert ( "Ignoring invalid device configuration_url 'foo://192.168.0.100/config'" in caplog.text)
async def test_device_info_not_overrides(hass): """Test device info is forwarded correctly.""" registry = dr.async_get(hass) device = registry.async_get_or_create( config_entry_id="bla", connections={("mac", "abcd")}, manufacturer="test-manufacturer", model="test-model", ) assert device.manufacturer == "test-manufacturer" assert device.model == "test-model" async def async_setup_entry(hass, config_entry, async_add_entities): """Mock setup entry method.""" async_add_entities( [ MockEntity( unique_id="qwer", device_info={ "connections": {("mac", "abcd")}, "default_name": "default name 1", "default_model": "default model 1", "default_manufacturer": "default manufacturer 1", }, ) ] ) return True platform = MockPlatform(async_setup_entry=async_setup_entry) config_entry = MockConfigEntry(entry_id="super-mock-id") entity_platform = MockEntityPlatform( hass, platform_name=config_entry.domain, platform=platform ) assert await entity_platform.async_setup_entry(config_entry) await hass.async_block_till_done() device2 = registry.async_get_device(set(), {("mac", "abcd")}) assert device2 is not None assert device.id == device2.id assert device2.manufacturer == "test-manufacturer" assert device2.model == "test-model"
async def test_device_info_change_to_no_url(hass, caplog): """Test device info changes to no URL.""" registry = dr.async_get(hass) registry.async_get_or_create( config_entry_id="123", connections=set(), identifiers={("mqtt", "via-id")}, manufacturer="manufacturer", model="via", configuration_url="homeassistant://config/mqtt", ) async def async_setup_entry(hass, config_entry, async_add_entities): """Mock setup entry method.""" async_add_entities( [ # Valid device info, with homeassistant url MockEntity( unique_id="qwer", device_info={ "identifiers": {("mqtt", "1234")}, "configuration_url": None, }, ), ] ) return True platform = MockPlatform(async_setup_entry=async_setup_entry) config_entry = MockConfigEntry(entry_id="super-mock-id") entity_platform = MockEntityPlatform( hass, platform_name=config_entry.domain, platform=platform ) assert await entity_platform.async_setup_entry(config_entry) await hass.async_block_till_done() assert len(hass.states.async_entity_ids()) == 1 device = registry.async_get_device({("mqtt", "1234")}) assert device is not None assert device.identifiers == {("mqtt", "1234")} assert device.configuration_url is None
async def test_unload_entry_resets_platform(hass): """Test unloading an entry removes all entities.""" mock_setup_entry = Mock(return_value=mock_coro(True)) loader.set_component(hass, 'test_domain.entry_domain', MockPlatform(async_setup_entry=mock_setup_entry)) component = EntityComponent(_LOGGER, DOMAIN, hass) entry = MockConfigEntry(domain='entry_domain') assert await component.async_setup_entry(entry) assert len(mock_setup_entry.mock_calls) == 1 add_entities = mock_setup_entry.mock_calls[0][1][2] add_entities([MockEntity()]) await hass.async_block_till_done() assert len(hass.states.async_entity_ids()) == 1 assert await component.async_unload_entry(entry) assert len(hass.states.async_entity_ids()) == 0
async def test_setup_entry(hass): """Test setup entry calls async_setup_entry on platform.""" mock_setup_entry = Mock(return_value=mock_coro(True)) loader.set_component( hass, 'test_domain.entry_domain', MockPlatform(async_setup_entry=mock_setup_entry, scan_interval=timedelta(seconds=5))) component = EntityComponent(_LOGGER, DOMAIN, hass) entry = MockConfigEntry(domain='entry_domain') assert await component.async_setup_entry(entry) assert len(mock_setup_entry.mock_calls) == 1 p_hass, p_entry, p_add_entities = mock_setup_entry.mock_calls[0][1] assert p_hass is hass assert p_entry is entry assert component._platforms[entry.entry_id].scan_interval == \ timedelta(seconds=5)
def test_platform_not_ready(hass): """Test that we retry when platform not ready.""" platform1_setup = Mock(side_effect=[PlatformNotReady, PlatformNotReady, None]) loader.set_component(hass, 'test_domain.mod1', MockPlatform(platform1_setup)) component = EntityComponent(_LOGGER, DOMAIN, hass) yield from component.async_setup({ DOMAIN: { 'platform': 'mod1' } }) assert len(platform1_setup.mock_calls) == 1 assert 'test_domain.mod1' not in hass.config.components utcnow = dt_util.utcnow() with patch('homeassistant.util.dt.utcnow', return_value=utcnow): # Should not trigger attempt 2 async_fire_time_changed(hass, utcnow + timedelta(seconds=29)) yield from hass.async_block_till_done() assert len(platform1_setup.mock_calls) == 1 # Should trigger attempt 2 async_fire_time_changed(hass, utcnow + timedelta(seconds=30)) yield from hass.async_block_till_done() assert len(platform1_setup.mock_calls) == 2 assert 'test_domain.mod1' not in hass.config.components # This should not trigger attempt 3 async_fire_time_changed(hass, utcnow + timedelta(seconds=59)) yield from hass.async_block_till_done() assert len(platform1_setup.mock_calls) == 2 # Trigger attempt 3, which succeeds async_fire_time_changed(hass, utcnow + timedelta(seconds=60)) yield from hass.async_block_till_done() assert len(platform1_setup.mock_calls) == 3 assert 'test_domain.mod1' in hass.config.components
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
def test_setup_loads_platforms(self): component_setup = Mock(return_value=True) platform_setup = Mock(return_value=None) loader.set_component( 'test_component', MockModule('test_component', setup=component_setup)) loader.set_component('test_domain.mod2', MockPlatform(platform_setup, ['test_component'])) component = EntityComponent(_LOGGER, DOMAIN, self.hass) assert not component_setup.called assert not platform_setup.called component.setup({DOMAIN: { 'platform': 'mod2', }}) assert component_setup.called assert platform_setup.called
async def test_setup_dependencies_platform(hass): """Test we setup the dependencies of a platform. We're explictely testing that we process dependencies even if a component with the same name has already been loaded. """ mock_integration( hass, MockModule('test_component', dependencies=['test_component2'])) mock_integration(hass, MockModule('test_component2')) mock_entity_platform(hass, 'test_domain.test_component', MockPlatform()) component = EntityComponent(_LOGGER, DOMAIN, hass) await component.async_setup({DOMAIN: { 'platform': 'test_component', }}) assert 'test_component' in hass.config.components assert 'test_component2' in hass.config.components assert 'test_domain.test_component' in hass.config.components
async def test_platform_warn_slow_setup(hass): """Warn we log when platform setup takes a long time.""" platform = MockPlatform() mock_entity_platform(hass, "test_domain.platform", platform) component = EntityComponent(_LOGGER, DOMAIN, hass) with patch.object(hass.loop, "call_later") as mock_call: await component.async_setup({DOMAIN: {"platform": "platform"}}) assert mock_call.called # mock_calls[0] is the warning message for component setup # mock_calls[3] is the warning message for platform setup timeout, logger_method = mock_call.mock_calls[3][1][:2] assert timeout == entity_platform.SLOW_SETUP_WARNING assert logger_method == _LOGGER.warning assert mock_call().cancel.called
async def test_platforms_shutdown_on_stop(opp): """Test that we shutdown platforms on stop.""" platform1_setup = Mock(side_effect=[PlatformNotReady, PlatformNotReady, None]) mock_integration(opp, MockModule("mod1")) mock_entity_platform(opp, "test_domain.mod1", MockPlatform(platform1_setup)) component = EntityComponent(_LOGGER, DOMAIN, opp) await component.async_setup({DOMAIN: {"platform": "mod1"}}) await opp.async_block_till_done() assert len(platform1_setup.mock_calls) == 1 assert "test_domain.mod1" not in opp.config.components with patch.object( component._platforms[DOMAIN], "async_shutdown" ) as mock_async_shutdown: opp.bus.async_fire(EVENT_OPENPEERPOWER_STOP) await opp.async_block_till_done() assert mock_async_shutdown.called
async def test_reset_cancels_retry_setup(hass): """Test that resetting a platform will cancel scheduled a setup retry.""" async_setup_entry = Mock(side_effect=PlatformNotReady) platform = MockPlatform(async_setup_entry=async_setup_entry) config_entry = MockConfigEntry() ent_platform = MockEntityPlatform(hass, platform_name=config_entry.domain, platform=platform) with patch.object(entity_platform, "async_call_later") as mock_call_later: assert not await ent_platform.async_setup_entry(config_entry) assert len(mock_call_later.mock_calls) == 1 assert len(mock_call_later.return_value.mock_calls) == 0 assert ent_platform._async_cancel_retry_setup is not None await ent_platform.async_reset() assert len(mock_call_later.return_value.mock_calls) == 1 assert ent_platform._async_cancel_retry_setup is None
def test_set_scan_interval_via_config(self, mock_track): """Test the setting of the scan interval via configuration.""" def platform_setup(hass, config, add_devices, discovery_info=None): """Test the platform setup.""" add_devices([EntityTest(should_poll=True)]) loader.set_component('test_domain.platform', MockPlatform(platform_setup)) component = EntityComponent(_LOGGER, DOMAIN, self.hass) component.setup({ DOMAIN: { 'platform': 'platform', 'scan_interval': timedelta(seconds=30), } }) assert mock_track.called assert timedelta(seconds=30) == mock_track.call_args[0][2]
async def test_stop_shutdown_cancels_retry_setup_and_interval_listener(hass): """Test that shutdown will cancel scheduled a setup retry and interval listener.""" async_setup_entry = Mock(side_effect=PlatformNotReady) platform = MockPlatform(async_setup_entry=async_setup_entry) config_entry = MockConfigEntry() ent_platform = MockEntityPlatform(hass, platform_name=config_entry.domain, platform=platform) with patch.object(entity_platform, "async_call_later") as mock_call_later: assert not await ent_platform.async_setup_entry(config_entry) assert len(mock_call_later.mock_calls) == 1 assert len(mock_call_later.return_value.mock_calls) == 0 assert ent_platform._async_cancel_retry_setup is not None await ent_platform.async_shutdown() assert len(mock_call_later.return_value.mock_calls) == 1 assert ent_platform._async_unsub_polling is None assert ent_platform._async_cancel_retry_setup is None
def test_platform_warn_slow_setup(hass): """Warn we log when platform setup takes a long time.""" platform = MockPlatform() loader.set_component('test_domain.platform', platform) component = EntityComponent(_LOGGER, DOMAIN, hass) with patch.object(hass.loop, 'call_later', MagicMock()) \ as mock_call: yield from component.async_setup({DOMAIN: { 'platform': 'platform', }}) assert mock_call.called timeout, logger_method = mock_call.mock_calls[0][1][:2] assert timeout == entity_platform.SLOW_SETUP_WARNING assert logger_method == _LOGGER.warning assert mock_call().cancel.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", ]
async def test_setup_entry(hass): """Test setup entry calls async_setup_entry on platform.""" mock_setup_entry = AsyncMock(return_value=True) mock_entity_platform( hass, "test_domain.entry_domain", MockPlatform(async_setup_entry=mock_setup_entry, scan_interval=timedelta(seconds=5)), ) component = EntityComponent(_LOGGER, DOMAIN, hass) entry = MockConfigEntry(domain="entry_domain") assert await component.async_setup_entry(entry) assert len(mock_setup_entry.mock_calls) == 1 p_hass, p_entry, _ = mock_setup_entry.mock_calls[0][1] assert p_hass is hass assert p_entry is entry assert component._platforms[entry.entry_id].scan_interval == timedelta( seconds=5)
async def test_device_info_not_overrides(hass): """Test device info is forwarded correctly.""" registry = await hass.helpers.device_registry.async_get_registry() device = registry.async_get_or_create( config_entry_id='bla', connections={('mac', 'abcd')}, manufacturer='test-manufacturer', model='test-model' ) assert device.manufacturer == 'test-manufacturer' assert device.model == 'test-model' async def async_setup_entry(hass, config_entry, async_add_entities): """Mock setup entry method.""" async_add_entities([ MockEntity(unique_id='qwer', device_info={ 'connections': {('mac', 'abcd')}, }), ]) return True platform = MockPlatform( async_setup_entry=async_setup_entry ) config_entry = MockConfigEntry(entry_id='super-mock-id') entity_platform = MockEntityPlatform( hass, platform_name=config_entry.domain, platform=platform ) assert await entity_platform.async_setup_entry(config_entry) await hass.async_block_till_done() device2 = registry.async_get_device(set(), {('mac', 'abcd')}) assert device2 is not None assert device.id == device2.id assert device2.manufacturer == 'test-manufacturer' assert device2.model == 'test-model'