From 0f2e39adee496ce45b416c832611d75534b97cca Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 29 Nov 2021 16:11:36 +0100 Subject: [PATCH] Add get method to DhcpServiceInfo (#60527) Co-authored-by: epenet <epenet@users.noreply.github.com> --- homeassistant/components/dhcp/__init__.py | 24 +++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/dhcp/__init__.py b/homeassistant/components/dhcp/__init__.py index 65f1759b54e..47a202aa2dd 100644 --- a/homeassistant/components/dhcp/__init__.py +++ b/homeassistant/components/dhcp/__init__.py @@ -46,6 +46,8 @@ from homeassistant.loader import async_get_dhcp from homeassistant.util.async_ import run_callback_threadsafe from homeassistant.util.network import is_invalid, is_link_local, is_loopback +from .const import DOMAIN + FILTER = "udp and (port 67 or 68)" REQUESTED_ADDR = "requested_addr" MESSAGE_TYPE = "message-type" @@ -71,20 +73,38 @@ class DhcpServiceInfo(BaseServiceInfo): def __getitem__(self, name: str) -> Any: """ - Allow property access by name for compatibility reason. + Enable method 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={"dhcp"}, + exclude_integrations={DOMAIN}, error_if_core=False, level=logging.DEBUG, ) self._warning_logged = True return getattr(self, name) + def get(self, name: str, default: Any = None) -> Any: + """ + Enable method for compatibility reason. + + Deprecated, and will be removed in version 2022.6. + """ + if not self._warning_logged: + report( + f"accessed discovery_info.get('{name}') instead of discovery_info.{name}; this will fail in version 2022.6", + exclude_integrations={DOMAIN}, + error_if_core=False, + level=logging.DEBUG, + ) + self._warning_logged = True + if hasattr(self, name): + return getattr(self, name) + return default + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the dhcp component."""