Reverse component path (#104087)

* Reverse component path

* Update translations helper

* Fix

* Revert incorrect change of PLATFORM_FORMAT

* Fix use of PLATFORM_FORMAT in tts

* Fix ios
This commit is contained in:
Erik Montnemery 2023-12-05 08:43:58 +01:00 committed by GitHub
parent a9381d2590
commit 84e74e4c74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 45 additions and 45 deletions

View file

@ -287,7 +287,7 @@ class DeviceTrackerPlatform:
) -> None:
"""Set up a legacy platform."""
assert self.type == PLATFORM_TYPE_LEGACY
full_name = f"{DOMAIN}.{self.name}"
full_name = f"{self.name}.{DOMAIN}"
LOGGER.info("Setting up %s", full_name)
with async_start_setup(hass, [full_name]):
try:

View file

@ -52,9 +52,9 @@ def get_service(
discovery_info: DiscoveryInfoType | None = None,
) -> iOSNotificationService | None:
"""Get the iOS notification service."""
if "notify.ios" not in hass.config.components:
if "ios.notify" not in hass.config.components:
# Need this to enable requirements checking in the app.
hass.config.components.add("notify.ios")
hass.config.components.add("ios.notify")
if not ios.devices_with_push(hass):
return None

View file

@ -126,7 +126,7 @@ def async_setup_legacy(
hass.data[NOTIFY_SERVICES].setdefault(integration_name, []).append(
notify_service
)
hass.config.components.add(f"{DOMAIN}.{integration_name}")
hass.config.components.add(f"{integration_name}.{DOMAIN}")
async def async_platform_discovered(
platform: str, info: DiscoveryInfoType | None

View file

@ -545,7 +545,7 @@ class SpeechManager:
self.providers[engine] = provider
self.hass.config.components.add(
PLATFORM_FORMAT.format(domain=engine, platform=DOMAIN)
PLATFORM_FORMAT.format(domain=DOMAIN, platform=engine)
)
@callback

View file

@ -304,7 +304,7 @@ class EntityPlatform:
current_platform.set(self)
logger = self.logger
hass = self.hass
full_name = f"{self.domain}.{self.platform_name}"
full_name = f"{self.platform_name}.{self.domain}"
object_id_language = (
hass.config.language
if hass.config.language in languages.NATIVE_ENTITY_IDS

View file

@ -48,7 +48,7 @@ def component_translation_path(
If component is just a single file, will return None.
"""
parts = component.split(".")
domain = parts[-1]
domain = parts[0]
is_platform = len(parts) == 2
# If it's a component that is just one file, we don't support translations
@ -57,7 +57,7 @@ def component_translation_path(
return None
if is_platform:
filename = f"{parts[0]}.{language}.json"
filename = f"{parts[1]}.{language}.json"
else:
filename = f"{language}.json"
@ -96,7 +96,7 @@ def _merge_resources(
# Build response
resources: dict[str, dict[str, Any]] = {}
for component in components:
domain = component.partition(".")[0]
domain = component.rpartition(".")[-1]
domain_resources = resources.setdefault(domain, {})
@ -154,7 +154,7 @@ async def _async_get_component_strings(
# Determine paths of missing components/platforms
files_to_load = {}
for loaded in components:
domain = loaded.rpartition(".")[-1]
domain = loaded.partition(".")[0]
integration = integrations[domain]
path = component_translation_path(loaded, language, integration)
@ -225,7 +225,7 @@ class _TranslationCache:
languages = [LOCALE_EN] if language == LOCALE_EN else [LOCALE_EN, language]
integrations: dict[str, Integration] = {}
domains = list({loaded.rpartition(".")[-1] for loaded in components})
domains = list({loaded.partition(".")[0] for loaded in components})
ints_or_excs = await async_get_integrations(self.hass, domains)
for domain, int_or_exc in ints_or_excs.items():
if isinstance(int_or_exc, Exception):

View file

@ -538,7 +538,7 @@ def async_get_loaded_integrations(hass: core.HomeAssistant) -> set[str]:
if "." not in component:
integrations.add(component)
continue
domain, _, platform = component.partition(".")
platform, _, domain = component.partition(".")
if domain in BASE_PLATFORMS:
integrations.add(platform)
return integrations
@ -563,7 +563,7 @@ def async_start_setup(
time_taken = dt_util.utcnow() - started
for unique, domain in unique_components.items():
del setup_started[unique]
integration = domain.rpartition(".")[-1]
integration = domain.partition(".")[0]
if integration in setup_time:
setup_time[integration] += time_taken
else:

View file

@ -259,7 +259,7 @@ async def test_connected_device_registered(
assert await entity_platform.async_setup_entry(config_entry)
await hass.async_block_till_done()
full_name = f"{entity_platform.domain}.{config_entry.domain}"
full_name = f"{config_entry.domain}.{entity_platform.domain}"
assert full_name in hass.config.components
assert len(hass.states.async_entity_ids()) == 0 # should be disabled
assert len(entity_registry.entities) == 3

View file

@ -123,7 +123,7 @@ async def test_reading_yaml_config(
assert device.config_picture == config.config_picture
assert device.consider_home == config.consider_home
assert device.icon == config.icon
assert f"{device_tracker.DOMAIN}.test" in hass.config.components
assert f"test.{device_tracker.DOMAIN}" in hass.config.components
@patch("homeassistant.components.device_tracker.const.LOGGER.warning")
@ -603,7 +603,7 @@ async def test_bad_platform(hass: HomeAssistant) -> None:
with assert_setup_component(0, device_tracker.DOMAIN):
assert await async_setup_component(hass, device_tracker.DOMAIN, config)
assert f"{device_tracker.DOMAIN}.bad_platform" not in hass.config.components
assert f"bad_platform.{device_tracker.DOMAIN}" not in hass.config.components
async def test_adding_unknown_device_to_config(

View file

@ -150,7 +150,7 @@ async def test_restore_state(
async def test_setup_component(hass: HomeAssistant, setup: str) -> None:
"""Set up a TTS platform with defaults."""
assert hass.services.has_service(tts.DOMAIN, "clear_cache")
assert f"{tts.DOMAIN}.test" in hass.config.components
assert f"test.{tts.DOMAIN}" in hass.config.components
@pytest.mark.parametrize("init_tts_cache_dir_side_effect", [OSError(2, "No access")])

View file

@ -1286,7 +1286,7 @@ async def test_issue_forecast_deprecated_no_logging(
assert weather_entity.state == ATTR_CONDITION_SUNNY
assert "Setting up weather.test" in caplog.text
assert "Setting up test.weather" in caplog.text
assert (
"custom_components.test_weather.weather::weather.test is using a forecast attribute on an instance of WeatherEntity"
not in caplog.text

View file

@ -215,7 +215,7 @@ async def test_platform_not_ready(hass: HomeAssistant) -> None:
await component.async_setup({DOMAIN: {"platform": "mod1"}})
await hass.async_block_till_done()
assert len(platform1_setup.mock_calls) == 1
assert "test_domain.mod1" not in hass.config.components
assert "mod1.test_domain" not in hass.config.components
# Should not trigger attempt 2
async_fire_time_changed(hass, utcnow + timedelta(seconds=29))
@ -226,7 +226,7 @@ async def test_platform_not_ready(hass: HomeAssistant) -> None:
async_fire_time_changed(hass, utcnow + timedelta(seconds=30))
await hass.async_block_till_done()
assert len(platform1_setup.mock_calls) == 2
assert "test_domain.mod1" not in hass.config.components
assert "mod1.test_domain" not in hass.config.components
# This should not trigger attempt 3
async_fire_time_changed(hass, utcnow + timedelta(seconds=59))
@ -237,7 +237,7 @@ async def test_platform_not_ready(hass: HomeAssistant) -> None:
async_fire_time_changed(hass, utcnow + timedelta(seconds=60))
await hass.async_block_till_done()
assert len(platform1_setup.mock_calls) == 3
assert "test_domain.mod1" in hass.config.components
assert "mod1.test_domain" in hass.config.components
async def test_extract_from_service_fails_if_no_entity_id(hass: HomeAssistant) -> None:
@ -317,7 +317,7 @@ async def test_setup_dependencies_platform(hass: HomeAssistant) -> None:
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
assert "test_component.test_domain" in hass.config.components
async def test_setup_entry(hass: HomeAssistant) -> None:
@ -680,7 +680,7 @@ async def test_platforms_shutdown_on_stop(hass: HomeAssistant) -> None:
await component.async_setup({DOMAIN: {"platform": "mod1"}})
await hass.async_block_till_done()
assert len(platform1_setup.mock_calls) == 1
assert "test_domain.mod1" not in hass.config.components
assert "mod1.test_domain" not in hass.config.components
with patch.object(
component._platforms[DOMAIN], "async_shutdown"

View file

@ -268,7 +268,7 @@ async def test_platform_error_slow_setup(
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.test_domain" not in hass.config.components
assert "test_platform is taking longer than 0 seconds" in caplog.text
# Cleanup lingering (setup_platform) task after test is done
@ -833,7 +833,7 @@ async def test_setup_entry(
assert await entity_platform.async_setup_entry(config_entry)
await hass.async_block_till_done()
full_name = f"{entity_platform.domain}.{config_entry.domain}"
full_name = f"{config_entry.domain}.{entity_platform.domain}"
assert full_name in hass.config.components
assert len(hass.states.async_entity_ids()) == 1
assert len(entity_registry.entities) == 1
@ -856,7 +856,7 @@ async def test_setup_entry_platform_not_ready(
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}"
full_name = f"{config_entry.domain}.{ent_platform.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
@ -877,7 +877,7 @@ async def test_setup_entry_platform_not_ready_with_message(
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}"
full_name = f"{config_entry.domain}.{ent_platform.domain}"
assert full_name not in hass.config.components
assert len(async_setup_entry.mock_calls) == 1
@ -904,7 +904,7 @@ async def test_setup_entry_platform_not_ready_from_exception(
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}"
full_name = f"{config_entry.domain}.{ent_platform.domain}"
assert full_name not in hass.config.components
assert len(async_setup_entry.mock_calls) == 1
@ -1669,7 +1669,7 @@ async def test_setup_entry_with_entities_that_block_forever(
):
assert await platform.async_setup_entry(config_entry)
await hass.async_block_till_done()
full_name = f"{platform.domain}.{config_entry.domain}"
full_name = f"{config_entry.domain}.{platform.domain}"
assert full_name in hass.config.components
assert len(hass.states.async_entity_ids()) == 0
assert len(entity_registry.entities) == 1

View file

@ -53,7 +53,7 @@ async def test_reload_platform(hass: HomeAssistant) -> None:
await hass.async_block_till_done()
assert component_setup.called
assert f"{DOMAIN}.{PLATFORM}" in hass.config.components
assert f"{PLATFORM}.{DOMAIN}" in hass.config.components
assert len(setup_called) == 1
platform = async_get_platform_without_config_entry(hass, PLATFORM, DOMAIN)
@ -93,7 +93,7 @@ async def test_setup_reload_service(hass: HomeAssistant) -> None:
await hass.async_block_till_done()
assert component_setup.called
assert f"{DOMAIN}.{PLATFORM}" in hass.config.components
assert f"{PLATFORM}.{DOMAIN}" in hass.config.components
assert len(setup_called) == 1
await async_setup_reload_service(hass, PLATFORM, [DOMAIN])
@ -134,7 +134,7 @@ async def test_setup_reload_service_when_async_process_component_config_fails(
await hass.async_block_till_done()
assert component_setup.called
assert f"{DOMAIN}.{PLATFORM}" in hass.config.components
assert f"{PLATFORM}.{DOMAIN}" in hass.config.components
assert len(setup_called) == 1
await async_setup_reload_service(hass, PLATFORM, [DOMAIN])
@ -186,7 +186,7 @@ async def test_setup_reload_service_with_platform_that_provides_async_reset_plat
await hass.async_block_till_done()
assert component_setup.called
assert f"{DOMAIN}.{PLATFORM}" in hass.config.components
assert f"{PLATFORM}.{DOMAIN}" in hass.config.components
assert len(setup_called) == 1
await async_setup_reload_service(hass, PLATFORM, [DOMAIN])

View file

@ -508,7 +508,7 @@ async def test_restore_entity_end_to_end(
await hass.async_block_till_done()
assert component_setup.called
assert f"{DOMAIN}.{PLATFORM}" in hass.config.components
assert f"{PLATFORM}.{DOMAIN}" in hass.config.components
assert len(setup_called) == 1
platform = async_get_platform_without_config_entry(hass, PLATFORM, DOMAIN)

View file

@ -56,14 +56,14 @@ async def test_component_translation_path(
)
assert path.normpath(
translation.component_translation_path("switch.test", "en", int_test)
translation.component_translation_path("test.switch", "en", int_test)
) == path.normpath(
hass.config.path("custom_components", "test", "translations", "switch.en.json")
)
assert path.normpath(
translation.component_translation_path(
"switch.test_embedded", "en", int_test_embedded
"test_embedded.switch", "en", int_test_embedded
)
) == path.normpath(
hass.config.path(
@ -255,7 +255,7 @@ async def test_translation_merging(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test we merge translations of two integrations."""
hass.config.components.add("sensor.moon")
hass.config.components.add("moon.sensor")
hass.config.components.add("sensor")
orig_load_translations = translation.load_translations_files
@ -263,7 +263,7 @@ async def test_translation_merging(
def mock_load_translations_files(files):
"""Mock loading."""
result = orig_load_translations(files)
result["sensor.moon"] = {
result["moon.sensor"] = {
"state": {"moon__phase": {"first_quarter": "First Quarter"}}
}
return result
@ -276,13 +276,13 @@ async def test_translation_merging(
assert "component.sensor.state.moon__phase.first_quarter" in translations
hass.config.components.add("sensor.season")
hass.config.components.add("season.sensor")
# Patch in some bad translation data
def mock_load_bad_translations_files(files):
"""Mock loading."""
result = orig_load_translations(files)
result["sensor.season"] = {"state": "bad data"}
result["season.sensor"] = {"state": "bad data"}
return result
with patch(
@ -308,7 +308,7 @@ async def test_translation_merging_loaded_apart(
def mock_load_translations_files(files):
"""Mock loading."""
result = orig_load_translations(files)
result["sensor.moon"] = {
result["moon.sensor"] = {
"state": {"moon__phase": {"first_quarter": "First Quarter"}}
}
return result
@ -323,7 +323,7 @@ async def test_translation_merging_loaded_apart(
assert "component.sensor.state.moon__phase.first_quarter" not in translations
hass.config.components.add("sensor.moon")
hass.config.components.add("moon.sensor")
with patch(
"homeassistant.helpers.translation.load_translations_files",

View file

@ -672,7 +672,7 @@ async def test_async_get_loaded_integrations(hass: HomeAssistant) -> None:
hass.config.components.add("notbase.switch")
hass.config.components.add("myintegration")
hass.config.components.add("device_tracker")
hass.config.components.add("device_tracker.other")
hass.config.components.add("other.device_tracker")
hass.config.components.add("myintegration.light")
assert setup.async_get_loaded_integrations(hass) == {
"other",
@ -729,9 +729,9 @@ async def test_async_start_setup(hass: HomeAssistant) -> None:
async def test_async_start_setup_platforms(hass: HomeAssistant) -> None:
"""Test setup started context manager keeps track of setup times for platforms."""
with setup.async_start_setup(hass, ["sensor.august"]):
with setup.async_start_setup(hass, ["august.sensor"]):
assert isinstance(
hass.data[setup.DATA_SETUP_STARTED]["sensor.august"], datetime.datetime
hass.data[setup.DATA_SETUP_STARTED]["august.sensor"], datetime.datetime
)
assert "august" not in hass.data[setup.DATA_SETUP_STARTED]