diff --git a/homeassistant/components/usb/__init__.py b/homeassistant/components/usb/__init__.py index 355b60906b3..ba63e59e8f1 100644 --- a/homeassistant/components/usb/__init__.py +++ b/homeassistant/components/usb/__init__.py @@ -6,7 +6,7 @@ import fnmatch import logging import os import sys -from typing import TypedDict +from typing import Any from serial.tools.list_ports import comports from serial.tools.list_ports_common import ListPortInfo @@ -17,8 +17,10 @@ from homeassistant.components import websocket_api from homeassistant.components.websocket_api.connection import ActiveConnection from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT_STOP from homeassistant.core import Event, HomeAssistant, callback +from homeassistant.data_entry_flow import BaseServiceInfo from homeassistant.helpers import discovery_flow, system_info from homeassistant.helpers.debounce import Debouncer +from homeassistant.helpers.frame import report from homeassistant.helpers.typing import ConfigType from homeassistant.loader import async_get_usb @@ -31,7 +33,8 @@ _LOGGER = logging.getLogger(__name__) REQUEST_SCAN_COOLDOWN = 60 # 1 minute cooldown -class UsbServiceInfo(TypedDict): +@dataclasses.dataclass +class UsbServiceInfo(BaseServiceInfo): """Prepared info from usb entries.""" device: str @@ -41,6 +44,25 @@ class UsbServiceInfo(TypedDict): manufacturer: str | None description: str | None + # Used to prevent log flooding. To be removed in 2022.6 + _warning_logged: bool = False + + def __getitem__(self, name: str) -> Any: + """ + Allow property access by name for compatibility reason. + + Deprecated, and will be removed in version 2022.6. + """ + if not self._warning_logged: + report( + f"accessed discovery_info['{name}'] instead of discovery_info.{name}; this will fail in version 2022.6", + exclude_integrations={"usb"}, + error_if_core=False, + level=logging.DEBUG, + ) + self._warning_logged = True + return getattr(self, name) + def human_readable_device_name( device: str,