Add get method to ZeroconfServiceInfo (#60528)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2021-11-29 17:37:55 +01:00 committed by GitHub
parent 7b81185d2a
commit 37430e7c9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 2 deletions

View File

@ -120,20 +120,38 @@ class ZeroconfServiceInfo(BaseServiceInfo):
def __getitem__(self, name: str) -> Any: 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. Deprecated, and will be removed in version 2022.6.
""" """
if not self._warning_logged: if not self._warning_logged:
report( report(
f"accessed discovery_info['{name}'] instead of discovery_info.{name}; this will fail in version 2022.6", f"accessed discovery_info['{name}'] instead of discovery_info.{name}; this will fail in version 2022.6",
exclude_integrations={"zeroconf"}, exclude_integrations={DOMAIN},
error_if_core=False, error_if_core=False,
level=logging.DEBUG, level=logging.DEBUG,
) )
self._warning_logged = True self._warning_logged = True
return getattr(self, name) 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
@bind_hass @bind_hass
async def async_get_instance(hass: HomeAssistant) -> HaZeroconf: async def async_get_instance(hass: HomeAssistant) -> HaZeroconf:

View File

@ -1030,3 +1030,37 @@ async def test_no_name(hass, mock_async_zeroconf):
register_call = mock_async_zeroconf.async_register_service.mock_calls[-1] register_call = mock_async_zeroconf.async_register_service.mock_calls[-1]
info = register_call.args[0] info = register_call.args[0]
assert info.name == "Home._home-assistant._tcp.local." assert info.name == "Home._home-assistant._tcp.local."
async def test_service_info_compatibility(hass, caplog):
"""Test compatibility with old-style dict.
To be removed in 2022.6
"""
discovery_info = zeroconf.ZeroconfServiceInfo(
host="mock_host",
port=None,
hostname="mock_hostname",
type="mock_type",
name="mock_name",
properties={},
)
# Ensure first call get logged
assert discovery_info["host"] == "mock_host"
assert discovery_info.get("host") == "mock_host"
assert discovery_info.get("host", "fallback_host") == "mock_host"
assert discovery_info.get("invalid_key", "fallback_host") == "fallback_host"
assert "Detected code that accessed discovery_info['host']" in caplog.text
assert "Detected code that accessed discovery_info.get('host')" not in caplog.text
# Ensure second call doesn't get logged
caplog.clear()
assert discovery_info["host"] == "mock_host"
assert discovery_info.get("host") == "mock_host"
assert "Detected code that accessed discovery_info['host']" not in caplog.text
assert "Detected code that accessed discovery_info.get('host')" not in caplog.text
discovery_info._warning_logged = False
assert discovery_info.get("host") == "mock_host"
assert "Detected code that accessed discovery_info.get('host')" in caplog.text