Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use _attr_* in whois integration #61250

Merged
merged 1 commit into from Dec 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 12 additions & 38 deletions homeassistant/components/whois/sensor.py
Expand Up @@ -49,53 +49,27 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class WhoisSensor(SensorEntity):
"""Implementation of a WHOIS sensor."""

_attr_icon = "mdi:calendar-clock"
_attr_native_unit_of_measurement = TIME_DAYS

def __init__(self, name, domain):
"""Initialize the sensor."""
self.whois = whois.whois

self._name = name
self._domain = domain
self._attr_name = name

self._state = None
self._attributes = None

@property
def name(self):
"""Return the name of the sensor."""
return self._name

@property
def icon(self):
"""Return the icon to represent this sensor."""
return "mdi:calendar-clock"

@property
def native_unit_of_measurement(self):
"""Return the unit of measurement to present the value in."""
return TIME_DAYS

@property
def native_value(self):
"""Return the expiration days for hostname."""
return self._state

@property
def extra_state_attributes(self):
"""Get the more info attributes."""
return self._attributes

def _empty_state_and_attributes(self):
def _empty_value_and_attributes(self):
"""Empty the state and attributes on an error."""
self._state = None
self._attributes = None
self._attr_native_value = None
self._attr_extra_state_attributes = None
epenet marked this conversation as resolved.
Show resolved Hide resolved

def update(self):
"""Get the current WHOIS data for the domain."""
try:
response = self.whois(self._domain)
except whois.BaseException as ex: # pylint: disable=broad-except
_LOGGER.error("Exception %s occurred during WHOIS lookup", ex)
self._empty_state_and_attributes()
self._empty_value_and_attributes()
return

if response:
Expand All @@ -105,12 +79,12 @@ def update(self):
"Did find: %s",
", ".join(response.keys()),
)
self._empty_state_and_attributes()
self._empty_value_and_attributes()
return

if not response["expiration_date"]:
_LOGGER.error("Whois response contains empty expiration_date")
self._empty_state_and_attributes()
self._empty_value_and_attributes()
return

attrs = {}
Expand All @@ -137,5 +111,5 @@ def update(self):

time_delta = expiration_date - expiration_date.now()

self._attributes = attrs
self._state = time_delta.days
self._attr_extra_state_attributes = attrs
self._attr_native_value = time_delta.days