From 4085c4f6d86fbbe47840341eb552d2e44018231e Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 9 Jun 2023 10:56:04 +0200 Subject: [PATCH] Tweak typing of Entity.platform (#88321) * Tweak typing of Entity.platform * Fix mypy errors * Fix update test * Improve comments --- homeassistant/components/rflink/__init__.py | 1 - homeassistant/components/update/__init__.py | 3 --- homeassistant/helpers/entity.py | 10 ++++++---- tests/components/update/test_init.py | 13 +++++-------- 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/rflink/__init__.py b/homeassistant/components/rflink/__init__.py index b563275297f..8df2d7ec343 100644 --- a/homeassistant/components/rflink/__init__.py +++ b/homeassistant/components/rflink/__init__.py @@ -323,7 +323,6 @@ class RflinkDevice(Entity): Contains the common logic for Rflink entities. """ - platform = None _state: bool | None = None _available = True _attr_should_poll = False diff --git a/homeassistant/components/update/__init__.py b/homeassistant/components/update/__init__.py index 3c8a30d5b4f..e0244034865 100644 --- a/homeassistant/components/update/__init__.py +++ b/homeassistant/components/update/__init__.py @@ -236,9 +236,6 @@ class UpdateEntity(RestoreEntity): Update entities return the brand icon based on the integration domain by default. """ - if self.platform is None: - return None - return ( f"https://brands.home-assistant.io/_/{self.platform.platform_name}/icon.png" ) diff --git a/homeassistant/helpers/entity.py b/homeassistant/helpers/entity.py index ef526f956cd..cdb20833a3d 100644 --- a/homeassistant/helpers/entity.py +++ b/homeassistant/helpers/entity.py @@ -235,13 +235,15 @@ class Entity(ABC): # this class. These may be used to customize the behavior of the entity. entity_id: str = None # type: ignore[assignment] - # Owning hass instance. Will be set by EntityPlatform + # Owning hass instance. Set by EntityPlatform by calling add_to_platform_start # While not purely typed, it makes typehinting more useful for us # and removes the need for constant None checks or asserts. hass: HomeAssistant = None # type: ignore[assignment] - # Owning platform instance. Will be set by EntityPlatform - platform: EntityPlatform | None = None + # Owning platform instance. Set by EntityPlatform by calling add_to_platform_start + # While not purely typed, it makes typehinting more useful for us + # and removes the need for constant None checks or asserts. + platform: EntityPlatform = None # type: ignore[assignment] # Entity description instance for this Entity entity_description: EntityDescription @@ -840,7 +842,7 @@ class Entity(ABC): self._call_on_remove_callbacks() self.hass = None # type: ignore[assignment] - self.platform = None + self.platform = None # type: ignore[assignment] self.parallel_updates = None async def add_to_platform_finish(self) -> None: diff --git a/tests/components/update/test_init.py b/tests/components/update/test_init.py index d0546b6a2ef..a7780f54f70 100644 --- a/tests/components/update/test_init.py +++ b/tests/components/update/test_init.py @@ -49,6 +49,7 @@ async def test_update(hass: HomeAssistant) -> None: """Test getting data from the mocked update entity.""" update = MockUpdateEntity() update.hass = hass + update.platform = MockEntityPlatform(hass) update._attr_installed_version = "1.0.0" update._attr_latest_version = "1.0.1" @@ -57,7 +58,10 @@ async def test_update(hass: HomeAssistant) -> None: update._attr_title = "Title" assert update.entity_category is EntityCategory.DIAGNOSTIC - assert update.entity_picture is None + assert ( + update.entity_picture + == "https://brands.home-assistant.io/_/test_platform/icon.png" + ) assert update.installed_version == "1.0.0" assert update.latest_version == "1.0.1" assert update.release_summary == "Summary" @@ -76,13 +80,6 @@ async def test_update(hass: HomeAssistant) -> None: ATTR_TITLE: "Title", } - # Test with platform - update.platform = MockEntityPlatform(hass) - assert ( - update.entity_picture - == "https://brands.home-assistant.io/_/test_platform/icon.png" - ) - # Test no update available update._attr_installed_version = "1.0.0" update._attr_latest_version = "1.0.0"