From cb990823cde402eb7e2e21a87eae404ee912ee7c Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Wed, 25 Feb 2026 16:15:28 +0100 Subject: [PATCH] Improve platforms pylint plugin (#164067) --- .../nintendo_parental_controls/__init__.py | 6 +- .../components/portainer/__init__.py | 2 +- homeassistant/components/togrill/__init__.py | 2 +- homeassistant/components/zinvolt/__init__.py | 2 +- .../plugins/hass_enforce_sorted_platforms.py | 2 +- tests/pylint/test_enforce_sorted_platforms.py | 80 +++++++++++++++++++ 6 files changed, 87 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/nintendo_parental_controls/__init__.py b/homeassistant/components/nintendo_parental_controls/__init__.py index c1aa2458931..6efe2828718 100644 --- a/homeassistant/components/nintendo_parental_controls/__init__.py +++ b/homeassistant/components/nintendo_parental_controls/__init__.py @@ -20,11 +20,11 @@ from .coordinator import NintendoParentalControlsConfigEntry, NintendoUpdateCoor from .services import async_setup_services _PLATFORMS: list[Platform] = [ - Platform.SENSOR, - Platform.TIME, - Platform.SWITCH, Platform.NUMBER, Platform.SELECT, + Platform.SENSOR, + Platform.SWITCH, + Platform.TIME, ] PLATFORM_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) diff --git a/homeassistant/components/portainer/__init__.py b/homeassistant/components/portainer/__init__.py index d74c35dcdb9..9d6f3524605 100644 --- a/homeassistant/components/portainer/__init__.py +++ b/homeassistant/components/portainer/__init__.py @@ -29,9 +29,9 @@ from .services import async_setup_services _PLATFORMS: list[Platform] = [ Platform.BINARY_SENSOR, + Platform.BUTTON, Platform.SENSOR, Platform.SWITCH, - Platform.BUTTON, ] CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) diff --git a/homeassistant/components/togrill/__init__.py b/homeassistant/components/togrill/__init__.py index f7e6568575e..280a23ba538 100644 --- a/homeassistant/components/togrill/__init__.py +++ b/homeassistant/components/togrill/__init__.py @@ -10,9 +10,9 @@ from .coordinator import DeviceNotFound, ToGrillConfigEntry, ToGrillCoordinator _PLATFORMS: list[Platform] = [ Platform.EVENT, + Platform.NUMBER, Platform.SELECT, Platform.SENSOR, - Platform.NUMBER, ] diff --git a/homeassistant/components/zinvolt/__init__.py b/homeassistant/components/zinvolt/__init__.py index adee797b00a..ad85d27ce8b 100644 --- a/homeassistant/components/zinvolt/__init__.py +++ b/homeassistant/components/zinvolt/__init__.py @@ -16,8 +16,8 @@ from .coordinator import ZinvoltConfigEntry, ZinvoltDeviceCoordinator _PLATFORMS: list[Platform] = [ Platform.BINARY_SENSOR, - Platform.SENSOR, Platform.NUMBER, + Platform.SENSOR, ] diff --git a/pylint/plugins/hass_enforce_sorted_platforms.py b/pylint/plugins/hass_enforce_sorted_platforms.py index aa6a6c16efa..5ae26a179c9 100644 --- a/pylint/plugins/hass_enforce_sorted_platforms.py +++ b/pylint/plugins/hass_enforce_sorted_platforms.py @@ -36,7 +36,7 @@ class HassEnforceSortedPlatformsChecker(BaseChecker): """Check for sorted PLATFORMS const.""" if ( isinstance(target, nodes.AssignName) - and target.name == "PLATFORMS" + and target.name in {"PLATFORMS", "_PLATFORMS"} and isinstance(node.value, nodes.List) ): platforms = [v.as_string() for v in node.value.elts] diff --git a/tests/pylint/test_enforce_sorted_platforms.py b/tests/pylint/test_enforce_sorted_platforms.py index d1e6d500cc3..ad62ddf38e3 100644 --- a/tests/pylint/test_enforce_sorted_platforms.py +++ b/tests/pylint/test_enforce_sorted_platforms.py @@ -40,6 +40,30 @@ from . import assert_adds_messages, assert_no_messages """, id="typed_multiple_platform", ), + pytest.param( + """ + _PLATFORMS = [Platform.SENSOR] + """, + id="private_one_platform", + ), + pytest.param( + """ + _PLATFORMS = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.SENSOR] + """, + id="private_multiple_platforms", + ), + pytest.param( + """ + _PLATFORMS: list[str] = [Platform.SENSOR] + """, + id="private_typed_one_platform", + ), + pytest.param( + """ + _PLATFORMS: list[str] = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.SENSOR] + """, + id="private_typed_multiple_platforms", + ), ], ) def test_enforce_sorted_platforms( @@ -110,3 +134,59 @@ def test_enforce_sorted_platforms_bad_typed( ), ): enforce_sorted_platforms_checker.visit_annassign(assign_node) + + +def test_enforce_sorted_private_platforms_bad( + linter: UnittestLinter, + enforce_sorted_platforms_checker: BaseChecker, +) -> None: + """Bad test case for private _PLATFORMS.""" + assign_node = astroid.extract_node( + """ + _PLATFORMS = [Platform.SENSOR, Platform.BINARY_SENSOR, Platform.BUTTON] + """, + "homeassistant.components.pylint_test", + ) + + with assert_adds_messages( + linter, + MessageTest( + msg_id="hass-enforce-sorted-platforms", + line=2, + node=assign_node, + args=None, + confidence=UNDEFINED, + col_offset=0, + end_line=2, + end_col_offset=71, + ), + ): + enforce_sorted_platforms_checker.visit_assign(assign_node) + + +def test_enforce_sorted_private_platforms_bad_typed( + linter: UnittestLinter, + enforce_sorted_platforms_checker: BaseChecker, +) -> None: + """Bad typed test case for private _PLATFORMS.""" + assign_node = astroid.extract_node( + """ + _PLATFORMS: list[str] = [Platform.SENSOR, Platform.BINARY_SENSOR, Platform.BUTTON] + """, + "homeassistant.components.pylint_test", + ) + + with assert_adds_messages( + linter, + MessageTest( + msg_id="hass-enforce-sorted-platforms", + line=2, + node=assign_node, + args=None, + confidence=UNDEFINED, + col_offset=0, + end_line=2, + end_col_offset=82, + ), + ): + enforce_sorted_platforms_checker.visit_annassign(assign_node)