Simplify whois value_fn (#65265)

This commit is contained in:
J. Nick Koston 2022-01-30 22:19:52 -06:00 committed by Paulus Schoutsen
parent 7e350b8347
commit 8bdee9cb1c

View File

@ -80,13 +80,6 @@ def _ensure_timezone(timestamp: datetime | None) -> datetime | None:
return timestamp return timestamp
def _fetch_attr_if_exists(domain: Domain, attr: str) -> str | None:
"""Fetch an attribute if it exists and is truthy or return None."""
if hasattr(domain, attr) and (value := getattr(domain, attr)):
return cast(str, value)
return None
SENSORS: tuple[WhoisSensorEntityDescription, ...] = ( SENSORS: tuple[WhoisSensorEntityDescription, ...] = (
WhoisSensorEntityDescription( WhoisSensorEntityDescription(
key="admin", key="admin",
@ -94,7 +87,7 @@ SENSORS: tuple[WhoisSensorEntityDescription, ...] = (
icon="mdi:account-star", icon="mdi:account-star",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
value_fn=lambda domain: _fetch_attr_if_exists(domain, "admin"), value_fn=lambda domain: getattr(domain, "admin", None),
), ),
WhoisSensorEntityDescription( WhoisSensorEntityDescription(
key="creation_date", key="creation_date",
@ -130,7 +123,7 @@ SENSORS: tuple[WhoisSensorEntityDescription, ...] = (
icon="mdi:account", icon="mdi:account",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
value_fn=lambda domain: _fetch_attr_if_exists(domain, "owner"), value_fn=lambda domain: getattr(domain, "owner", None),
), ),
WhoisSensorEntityDescription( WhoisSensorEntityDescription(
key="registrant", key="registrant",
@ -138,7 +131,7 @@ SENSORS: tuple[WhoisSensorEntityDescription, ...] = (
icon="mdi:account-edit", icon="mdi:account-edit",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
value_fn=lambda domain: _fetch_attr_if_exists(domain, "registrant"), value_fn=lambda domain: getattr(domain, "registrant", None),
), ),
WhoisSensorEntityDescription( WhoisSensorEntityDescription(
key="registrar", key="registrar",
@ -154,7 +147,7 @@ SENSORS: tuple[WhoisSensorEntityDescription, ...] = (
icon="mdi:store", icon="mdi:store",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
value_fn=lambda domain: _fetch_attr_if_exists(domain, "reseller"), value_fn=lambda domain: getattr(domain, "reseller", None),
), ),
) )