From bc1c24efb1cbd0a0eeb956e06cbb236e8363aa66 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 6 Feb 2026 15:10:30 +0100 Subject: [PATCH] Improve (r)split performance (#162418) --- homeassistant/components/bthome/device_trigger.py | 2 +- homeassistant/components/google_assistant_sdk/helpers.py | 2 +- homeassistant/components/guardian/config_flow.py | 2 +- homeassistant/components/homeassistant/repairs.py | 2 +- homeassistant/components/kraken/sensor.py | 4 ++-- homeassistant/components/nmap_tracker/__init__.py | 2 +- homeassistant/components/sonos/media_browser.py | 2 +- homeassistant/components/syncthing/sensor.py | 2 +- homeassistant/components/tesla_fleet/entity.py | 2 +- homeassistant/components/teslemetry/entity.py | 2 +- homeassistant/components/tessie/entity.py | 2 +- homeassistant/components/unifiprotect/select.py | 2 +- homeassistant/components/unifiprotect/services.py | 2 +- homeassistant/components/vizio/config_flow.py | 4 ++-- homeassistant/components/zwave_js/repairs.py | 4 ++-- homeassistant/helpers/trigger_template_entity.py | 6 ++++-- tests/components/portainer/test_button.py | 2 +- tests/components/sonos/conftest.py | 2 +- 18 files changed, 24 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/bthome/device_trigger.py b/homeassistant/components/bthome/device_trigger.py index b9e01051419..f8a95c1f6f5 100644 --- a/homeassistant/components/bthome/device_trigger.py +++ b/homeassistant/components/bthome/device_trigger.py @@ -81,7 +81,7 @@ def get_event_types_by_event_class(event_class: str) -> set[str]: but if there is only one button then it will be button without a number postfix. """ - return EVENT_TYPES_BY_EVENT_CLASS.get(event_class.split("_")[0], set()) + return EVENT_TYPES_BY_EVENT_CLASS.get(event_class.split("_", maxsplit=1)[0], set()) async def async_validate_trigger_config( diff --git a/homeassistant/components/google_assistant_sdk/helpers.py b/homeassistant/components/google_assistant_sdk/helpers.py index 93d29e5edb5..a3ced9fd68b 100644 --- a/homeassistant/components/google_assistant_sdk/helpers.py +++ b/homeassistant/components/google_assistant_sdk/helpers.py @@ -142,7 +142,7 @@ def best_matching_language_code( # Use the assist language if supported if assist_language in SUPPORTED_LANGUAGE_CODES: return assist_language - language = assist_language.split("-")[0] + language = assist_language.split("-", maxsplit=1)[0] # Use the agent language if assist and agent start with the same language part if agent_language is not None and agent_language.startswith(language): diff --git a/homeassistant/components/guardian/config_flow.py b/homeassistant/components/guardian/config_flow.py index 55e4893e31b..81a036dd83c 100644 --- a/homeassistant/components/guardian/config_flow.py +++ b/homeassistant/components/guardian/config_flow.py @@ -31,7 +31,7 @@ UNIQUE_ID = "guardian_{0}" @callback def async_get_pin_from_discovery_hostname(hostname: str) -> str: """Get the device's 4-digit PIN from its zeroconf-discovered hostname.""" - return hostname.split(".")[0].split("-")[1] + return hostname.split(".", maxsplit=1)[0].split("-")[1] @callback diff --git a/homeassistant/components/homeassistant/repairs.py b/homeassistant/components/homeassistant/repairs.py index 9dd732e3ce8..cff123da17a 100644 --- a/homeassistant/components/homeassistant/repairs.py +++ b/homeassistant/components/homeassistant/repairs.py @@ -55,7 +55,7 @@ async def async_create_fix_flow( ) -> RepairsFlow: """Create flow.""" - if issue_id.split(".")[0] == "integration_not_found": + if issue_id.split(".", maxsplit=1)[0] == "integration_not_found": assert data return IntegrationNotFoundFlow(data) return ConfirmRepairFlow() diff --git a/homeassistant/components/kraken/sensor.py b/homeassistant/components/kraken/sensor.py index 2a6b36e1729..8d5f9ab65af 100644 --- a/homeassistant/components/kraken/sensor.py +++ b/homeassistant/components/kraken/sensor.py @@ -225,7 +225,7 @@ class KrakenSensor( self._device_name = create_device_name(tracked_asset_pair) self._attr_unique_id = "_".join( [ - tracked_asset_pair.split("/")[0], + tracked_asset_pair.split("/", maxsplit=1)[0], tracked_asset_pair.split("/")[1], description.key, ] @@ -291,4 +291,4 @@ class KrakenSensor( def create_device_name(tracked_asset_pair: str) -> str: """Create the device name for a given tracked asset pair.""" - return f"{tracked_asset_pair.split('/')[0]} {tracked_asset_pair.split('/')[1]}" + return f"{tracked_asset_pair.split('/', maxsplit=1)[0]} {tracked_asset_pair.split('/')[1]}" diff --git a/homeassistant/components/nmap_tracker/__init__.py b/homeassistant/components/nmap_tracker/__init__.py index 8bd534d5716..fda6ec08b45 100644 --- a/homeassistant/components/nmap_tracker/__init__.py +++ b/homeassistant/components/nmap_tracker/__init__.py @@ -46,7 +46,7 @@ MAX_SCAN_ATTEMPTS: Final = 16 def short_hostname(hostname: str) -> str: """Return the first part of the hostname.""" - return hostname.split(".")[0] + return hostname.split(".", maxsplit=1)[0] def human_readable_name(hostname: str, vendor: str, mac_address: str) -> str: diff --git a/homeassistant/components/sonos/media_browser.py b/homeassistant/components/sonos/media_browser.py index 6abe5432371..16250477749 100644 --- a/homeassistant/components/sonos/media_browser.py +++ b/homeassistant/components/sonos/media_browser.py @@ -112,7 +112,7 @@ def _get_title(id_string: str) -> str: # Format is S://server/share/folder # If just S: this will be in the mappings; otherwise use the last folder in path. title = LIBRARY_TITLES_MAPPING.get( - id_string, urllib.parse.unquote(id_string.split("/")[-1]) + id_string, urllib.parse.unquote(id_string.rsplit("/", maxsplit=1)[-1]) ) else: parts = id_string.split("/") diff --git a/homeassistant/components/syncthing/sensor.py b/homeassistant/components/syncthing/sensor.py index 5e33ffbb601..d57da2b30ca 100644 --- a/homeassistant/components/syncthing/sensor.py +++ b/homeassistant/components/syncthing/sensor.py @@ -106,7 +106,7 @@ class FolderSensor(SensorEntity): self._state: dict[str, Any] | None = None self._unsub_timer: CALLBACK_TYPE | None = None - self._short_server_id = server_id.split("-")[0] + self._short_server_id = server_id.split("-", maxsplit=1)[0] self._attr_name = f"{self._short_server_id} {folder_id} {folder_label}" self._attr_unique_id = f"{self._short_server_id}-{folder_id}" self._attr_device_info = DeviceInfo( diff --git a/homeassistant/components/tesla_fleet/entity.py b/homeassistant/components/tesla_fleet/entity.py index 363ae487e84..833d6988cc5 100644 --- a/homeassistant/components/tesla_fleet/entity.py +++ b/homeassistant/components/tesla_fleet/entity.py @@ -206,7 +206,7 @@ class TeslaFleetWallConnectorEntity( manufacturer="Tesla", name="Wall Connector", via_device=(DOMAIN, str(data.id)), - serial_number=din.split("-")[-1], + serial_number=din.rsplit("-", maxsplit=1)[-1], model=model, ) diff --git a/homeassistant/components/teslemetry/entity.py b/homeassistant/components/teslemetry/entity.py index 762678736a5..ce874565160 100644 --- a/homeassistant/components/teslemetry/entity.py +++ b/homeassistant/components/teslemetry/entity.py @@ -222,7 +222,7 @@ class TeslemetryWallConnectorEntity(TeslemetryPollingEntity): configuration_url="https://teslemetry.com/console", name="Wall Connector", via_device=(DOMAIN, str(data.id)), - serial_number=din.split("-")[-1], + serial_number=din.rsplit("-", maxsplit=1)[-1], model=model, ) diff --git a/homeassistant/components/tessie/entity.py b/homeassistant/components/tessie/entity.py index fb49d02f42e..8892f6c4b0d 100644 --- a/homeassistant/components/tessie/entity.py +++ b/homeassistant/components/tessie/entity.py @@ -153,7 +153,7 @@ class TessieWallConnectorEntity(TessieBaseEntity): manufacturer="Tesla", name="Wall Connector", via_device=(DOMAIN, str(data.id)), - serial_number=din.split("-")[-1], + serial_number=din.rsplit("-", maxsplit=1)[-1], ) assert data.live_coordinator super().__init__(data.live_coordinator, key) diff --git a/homeassistant/components/unifiprotect/select.py b/homeassistant/components/unifiprotect/select.py index 817859c996b..bcfd67ca215 100644 --- a/homeassistant/components/unifiprotect/select.py +++ b/homeassistant/components/unifiprotect/select.py @@ -176,7 +176,7 @@ async def _set_paired_camera(obj: Light | Sensor | Doorlock, camera_id: str) -> async def _set_doorbell_message(obj: Camera, message: str) -> None: if message.startswith(DoorbellMessageType.CUSTOM_MESSAGE.value): - message = message.split(":")[-1] + message = message.rsplit(":", maxsplit=1)[-1] await obj.set_lcd_text(DoorbellMessageType.CUSTOM_MESSAGE, text=message) elif message == TYPE_EMPTY_VALUE: await obj.set_lcd_text(None) diff --git a/homeassistant/components/unifiprotect/services.py b/homeassistant/components/unifiprotect/services.py index 0acb98e5aa5..9c651488d1e 100644 --- a/homeassistant/components/unifiprotect/services.py +++ b/homeassistant/components/unifiprotect/services.py @@ -202,7 +202,7 @@ async def remove_privacy_zone(call: ServiceCall) -> None: @callback def _async_unique_id_to_mac(unique_id: str) -> str: """Extract the MAC address from the registry entry unique id.""" - return unique_id.split("_")[0] + return unique_id.split("_", maxsplit=1)[0] async def set_chime_paired_doorbells(call: ServiceCall) -> None: diff --git a/homeassistant/components/vizio/config_flow.py b/homeassistant/components/vizio/config_flow.py index 572f093dfd3..fa01b75de5e 100644 --- a/homeassistant/components/vizio/config_flow.py +++ b/homeassistant/components/vizio/config_flow.py @@ -96,9 +96,9 @@ def _get_pairing_schema(input_dict: dict[str, Any] | None = None) -> vol.Schema: def _host_is_same(host1: str, host2: str) -> bool: """Check if host1 and host2 are the same.""" - host1 = host1.split(":")[0] + host1 = host1.split(":", maxsplit=1)[0] host1 = host1 if is_ip_address(host1) else socket.gethostbyname(host1) - host2 = host2.split(":")[0] + host2 = host2.split(":", maxsplit=1)[0] host2 = host2 if is_ip_address(host2) else socket.gethostbyname(host2) return host1 == host2 diff --git a/homeassistant/components/zwave_js/repairs.py b/homeassistant/components/zwave_js/repairs.py index fe35b9406de..114c8fc88e5 100644 --- a/homeassistant/components/zwave_js/repairs.py +++ b/homeassistant/components/zwave_js/repairs.py @@ -117,10 +117,10 @@ async def async_create_fix_flow( ) -> RepairsFlow: """Create flow.""" - if issue_id.split(".")[0] == "device_config_file_changed": + if issue_id.split(".", maxsplit=1)[0] == "device_config_file_changed": assert data return DeviceConfigFileChangedFlow(data) - if issue_id.split(".")[0] == "migrate_unique_id": + if issue_id.split(".", maxsplit=1)[0] == "migrate_unique_id": assert data return MigrateUniqueIDFlow(data) return ConfirmRepairFlow() diff --git a/homeassistant/helpers/trigger_template_entity.py b/homeassistant/helpers/trigger_template_entity.py index 64c4825a2c2..dc8f52763c3 100644 --- a/homeassistant/helpers/trigger_template_entity.py +++ b/homeassistant/helpers/trigger_template_entity.py @@ -90,7 +90,7 @@ def log_triggered_template_error( elif attribute: target = f" {CONF_ATTRIBUTES}.{attribute}" - logging.getLogger(f"{__package__}.{entity_id.split('.')[0]}").error( + logging.getLogger(f"{__package__}.{entity_id.split('.', maxsplit=1)[0]}").error( "Error rendering%s template for %s: %s", target, entity_id, @@ -138,7 +138,9 @@ class ValueTemplate(Template): ).strip() except jinja2.TemplateError as ex: message = f"Error parsing value for {entity_id}: {ex} (value: {variables['value']}, template: {self.template})" - logger = logging.getLogger(f"{__package__}.{entity_id.split('.')[0]}") + logger = logging.getLogger( + f"{__package__}.{entity_id.split('.', maxsplit=1)[0]}" + ) logger.debug(message) return error_value diff --git a/tests/components/portainer/test_button.py b/tests/components/portainer/test_button.py index a4fd9732117..0b093c5577d 100644 --- a/tests/components/portainer/test_button.py +++ b/tests/components/portainer/test_button.py @@ -91,7 +91,7 @@ async def test_buttons_containers_exceptions( """Test that Portainer buttons, but this time when they will do boom for sure.""" await setup_integration(hass, mock_config_entry) - action = client_method.split("_")[0] + action = client_method.split("_", maxsplit=1)[0] entity_id = f"button.practical_morse_{action}_container" method_mock = getattr(mock_portainer_client, client_method) diff --git a/tests/components/sonos/conftest.py b/tests/components/sonos/conftest.py index aff3bf671bc..3a9447c61c8 100644 --- a/tests/components/sonos/conftest.py +++ b/tests/components/sonos/conftest.py @@ -295,7 +295,7 @@ class SoCoMockFactory: # Generate a different MAC for the non-default speakers. # otherwise new devices will not be created. if ip_address != "192.168.42.2": - last_octet = ip_address.split(".")[-1] + last_octet = ip_address.rsplit(".", maxsplit=1)[-1] my_speaker_info["mac_address"] = f"00-00-00-00-00-{last_octet.zfill(2)}" mock_soco.get_speaker_info = Mock(return_value=my_speaker_info) mock_soco.add_to_queue = Mock(return_value=10)