Handle missing attrs in whois results (#65254)

* Handle missing attrs in whois results

- Some attrs are not set depending on where the
  domain is registered

- Fixes #65164

* Set to unknown instead of do not create

* no multi-line lambda
This commit is contained in:
J. Nick Koston 2022-01-30 15:19:04 -06:00 committed by GitHub
parent 473abb1793
commit 62fd31a1e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 82 additions and 8 deletions

View file

@ -80,6 +80,13 @@ def _ensure_timezone(timestamp: datetime | None) -> datetime | None:
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, ...] = (
WhoisSensorEntityDescription(
key="admin",
@ -87,7 +94,7 @@ SENSORS: tuple[WhoisSensorEntityDescription, ...] = (
icon="mdi:account-star",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
value_fn=lambda domain: domain.admin if domain.admin else None,
value_fn=lambda domain: _fetch_attr_if_exists(domain, "admin"),
),
WhoisSensorEntityDescription(
key="creation_date",
@ -123,7 +130,7 @@ SENSORS: tuple[WhoisSensorEntityDescription, ...] = (
icon="mdi:account",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
value_fn=lambda domain: domain.owner if domain.owner else None,
value_fn=lambda domain: _fetch_attr_if_exists(domain, "owner"),
),
WhoisSensorEntityDescription(
key="registrant",
@ -131,7 +138,7 @@ SENSORS: tuple[WhoisSensorEntityDescription, ...] = (
icon="mdi:account-edit",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
value_fn=lambda domain: domain.registrant if domain.registrant else None,
value_fn=lambda domain: _fetch_attr_if_exists(domain, "registrant"),
),
WhoisSensorEntityDescription(
key="registrar",
@ -147,7 +154,7 @@ SENSORS: tuple[WhoisSensorEntityDescription, ...] = (
icon="mdi:store",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
value_fn=lambda domain: domain.reseller if domain.reseller else None,
value_fn=lambda domain: _fetch_attr_if_exists(domain, "reseller"),
),
)
@ -190,7 +197,6 @@ async def async_setup_entry(
)
for description in SENSORS
],
update_before_add=True,
)