From d4c426373056f3ecf7e7df9bf1a5f9a93b8c25e4 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Tue, 27 Jul 2021 01:25:22 +0200 Subject: [PATCH] Adjust typing of _attr_extra_state_attributes (#53529) --- homeassistant/components/airvisual/__init__.py | 6 ++---- homeassistant/components/guardian/__init__.py | 8 +++----- homeassistant/components/netatmo/netatmo_entity_base.py | 4 +--- homeassistant/components/openuv/__init__.py | 5 +---- homeassistant/components/sia/sia_entity_base.py | 2 +- homeassistant/helpers/entity.py | 6 ++++-- 6 files changed, 12 insertions(+), 19 deletions(-) diff --git a/homeassistant/components/airvisual/__init__.py b/homeassistant/components/airvisual/__init__.py index 015c913b815..21d4054f6ee 100644 --- a/homeassistant/components/airvisual/__init__.py +++ b/homeassistant/components/airvisual/__init__.py @@ -1,7 +1,7 @@ """The airvisual component.""" from __future__ import annotations -from collections.abc import Mapping, MutableMapping +from collections.abc import Mapping from datetime import timedelta from math import ceil from typing import Any, Dict, cast @@ -364,9 +364,7 @@ class AirVisualEntity(CoordinatorEntity): """Initialize.""" super().__init__(coordinator) - self._attr_extra_state_attributes: MutableMapping[str, Any] = { - ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION - } + self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION} async def async_added_to_hass(self) -> None: """Register callbacks.""" diff --git a/homeassistant/components/guardian/__init__.py b/homeassistant/components/guardian/__init__.py index 96f5ed36720..9338f9a47a9 100644 --- a/homeassistant/components/guardian/__init__.py +++ b/homeassistant/components/guardian/__init__.py @@ -2,8 +2,8 @@ from __future__ import annotations import asyncio -from collections.abc import Awaitable, MutableMapping -from typing import Any, cast +from collections.abc import Awaitable +from typing import cast from aioguardian import Client @@ -221,9 +221,7 @@ class GuardianEntity(CoordinatorEntity): """Initialize.""" self._attr_device_class = device_class self._attr_device_info = {"manufacturer": "Elexa"} - self._attr_extra_state_attributes: MutableMapping[str, Any] = { - ATTR_ATTRIBUTION: "Data provided by Elexa" - } + self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: "Data provided by Elexa"} self._attr_icon = icon self._attr_name = name self._entry = entry diff --git a/homeassistant/components/netatmo/netatmo_entity_base.py b/homeassistant/components/netatmo/netatmo_entity_base.py index 51fc14f6f8e..f276fb3d947 100644 --- a/homeassistant/components/netatmo/netatmo_entity_base.py +++ b/homeassistant/components/netatmo/netatmo_entity_base.py @@ -30,9 +30,7 @@ class NetatmoBase(Entity): self._model: str = "" self._attr_name = None self._attr_unique_id = None - self._attr_extra_state_attributes: dict = { - ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION - } + self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION} async def async_added_to_hass(self) -> None: """Entity created.""" diff --git a/homeassistant/components/openuv/__init__.py b/homeassistant/components/openuv/__init__.py index 0de97e52cbe..efe6fa89ca8 100644 --- a/homeassistant/components/openuv/__init__.py +++ b/homeassistant/components/openuv/__init__.py @@ -2,7 +2,6 @@ from __future__ import annotations import asyncio -from collections.abc import MutableMapping from typing import Any from pyopenuv import Client @@ -169,9 +168,7 @@ class OpenUvEntity(Entity): def __init__(self, openuv: OpenUV, sensor_type: str) -> None: """Initialize.""" - self._attr_extra_state_attributes: MutableMapping[str, Any] = { - ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION - } + self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION} self._attr_should_poll = False self._attr_unique_id = ( f"{openuv.client.latitude}_{openuv.client.longitude}_{sensor_type}" diff --git a/homeassistant/components/sia/sia_entity_base.py b/homeassistant/components/sia/sia_entity_base.py index 5169702e67b..0a84615d6eb 100644 --- a/homeassistant/components/sia/sia_entity_base.py +++ b/homeassistant/components/sia/sia_entity_base.py @@ -43,7 +43,7 @@ class SIABaseEntity(RestoreEntity): self._cancel_availability_cb: CALLBACK_TYPE | None = None - self._attr_extra_state_attributes: dict[str, Any] = {} + self._attr_extra_state_attributes = {} self._attr_should_poll = False self._attr_name = SIA_NAME_FORMAT.format( self._port, self._account, self._zone, self._attr_device_class diff --git a/homeassistant/helpers/entity.py b/homeassistant/helpers/entity.py index 8c134226f69..6383de15b4a 100644 --- a/homeassistant/helpers/entity.py +++ b/homeassistant/helpers/entity.py @@ -246,7 +246,7 @@ class Entity(ABC): _attr_device_info: DeviceInfo | None = None _attr_entity_picture: str | None = None _attr_entity_registry_enabled_default: bool - _attr_extra_state_attributes: MutableMapping[str, Any] | None = None + _attr_extra_state_attributes: MutableMapping[str, Any] _attr_force_update: bool _attr_icon: str | None _attr_name: str | None @@ -319,7 +319,9 @@ class Entity(ABC): Implemented by platform classes. Convention for attribute names is lowercase snake_case. """ - return self._attr_extra_state_attributes + if hasattr(self, "_attr_extra_state_attributes"): + return self._attr_extra_state_attributes + return None @property def device_info(self) -> DeviceInfo | None: