Use _attr_* in whois integration (#61250)

pull/61254/head
Milan Meulemans 2021-12-08 19:58:46 +01:00 committed by GitHub
parent 40828e221e
commit dbe0a801c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 38 deletions

View File

@ -49,45 +49,19 @@ 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."""
@ -95,7 +69,7 @@ class WhoisSensor(SensorEntity):
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:
@ -105,12 +79,12 @@ class WhoisSensor(SensorEntity):
"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 = {}
@ -137,5 +111,5 @@ class WhoisSensor(SensorEntity):
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