Skip to content

Commit e0484ba

Browse files
Alsatian67MartinHjelmarejoostlek
authored
Improve dev error message for YAML platform setup missing method (home-assistant#155505)
Co-authored-by: Martin Hjelmare <[email protected]> Co-authored-by: Joost Lekkerkerker <[email protected]>
1 parent 62f758f commit e0484ba

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

homeassistant/helpers/entity_platform.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,18 @@ async def async_setup(
330330
self.domain,
331331
)
332332
learn_more_url = None
333-
if self.platform and "custom_components" not in self.platform.__file__: # type: ignore[attr-defined]
334-
learn_more_url = (
335-
f"https://www.home-assistant.io/integrations/{self.platform_name}/"
336-
)
333+
if self.platform:
334+
if "custom_components" in self.platform.__file__: # type: ignore[attr-defined]
335+
self.logger.warning(
336+
(
337+
"The %s platform module for the %s custom integration does not implement"
338+
" async_setup_platform or setup_platform."
339+
),
340+
self.platform_name,
341+
self.domain,
342+
)
343+
else:
344+
learn_more_url = f"https://www.home-assistant.io/integrations/{self.platform_name}/"
337345
platform_key = f"platform: {self.platform_name}"
338346
yaml_example = f"```yaml\n{self.domain}:\n - {platform_key}\n```"
339347
async_create_issue(

tests/helpers/test_entity_platform.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from collections.abc import Iterable
55
from datetime import timedelta
66
import logging
7+
import types
78
from typing import Any
89
from unittest.mock import ANY, AsyncMock, Mock, patch
910

@@ -1611,6 +1612,32 @@ async def test_platform_with_no_setup(
16111612
}
16121613

16131614

1615+
async def test_platform_with_no_setup_custom_component_hint(
1616+
hass: HomeAssistant,
1617+
caplog: pytest.LogCaptureFixture,
1618+
issue_registry: ir.IssueRegistry,
1619+
) -> None:
1620+
"""Test setting up a custom integration platform without setup logs extra warning."""
1621+
platform_mod = types.ModuleType("custom_components.mock_integration.mock_platform")
1622+
platform_mod.__file__ = (
1623+
"/config/custom_components/mock_integration/mock_platform.py"
1624+
)
1625+
1626+
entity_platform = MockEntityPlatform(
1627+
hass,
1628+
domain="mock-integration",
1629+
platform_name="mock-platform",
1630+
platform=platform_mod,
1631+
)
1632+
1633+
await entity_platform.async_setup(None)
1634+
1635+
assert (
1636+
"The mock-platform platform module for the mock-integration custom integration "
1637+
"does not implement async_setup_platform or setup_platform." in caplog.text
1638+
)
1639+
1640+
16141641
async def test_platforms_sharing_services(hass: HomeAssistant) -> None:
16151642
"""Test platforms share services."""
16161643
entity_platform1 = MockEntityPlatform(

0 commit comments

Comments
 (0)