Skip to content

Commit

Permalink
Use _attr_* in whois integration (#61250)
Browse files Browse the repository at this point in the history
  • Loading branch information
milanmeu committed Dec 8, 2021
1 parent 40828e2 commit dbe0a80
Showing 1 changed file with 12 additions and 38 deletions.
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

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

0 comments on commit dbe0a80

Please sign in to comment.