mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 20:27:08 +00:00
Use _attr_* in whois integration (#61250)
This commit is contained in:
parent
40828e221e
commit
dbe0a801c6
@ -49,45 +49,19 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
class WhoisSensor(SensorEntity):
|
class WhoisSensor(SensorEntity):
|
||||||
"""Implementation of a WHOIS sensor."""
|
"""Implementation of a WHOIS sensor."""
|
||||||
|
|
||||||
|
_attr_icon = "mdi:calendar-clock"
|
||||||
|
_attr_native_unit_of_measurement = TIME_DAYS
|
||||||
|
|
||||||
def __init__(self, name, domain):
|
def __init__(self, name, domain):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self.whois = whois.whois
|
self.whois = whois.whois
|
||||||
|
|
||||||
self._name = name
|
|
||||||
self._domain = domain
|
self._domain = domain
|
||||||
|
self._attr_name = name
|
||||||
|
|
||||||
self._state = None
|
def _empty_value_and_attributes(self):
|
||||||
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):
|
|
||||||
"""Empty the state and attributes on an error."""
|
"""Empty the state and attributes on an error."""
|
||||||
self._state = None
|
self._attr_native_value = None
|
||||||
self._attributes = None
|
self._attr_extra_state_attributes = None
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the current WHOIS data for the domain."""
|
"""Get the current WHOIS data for the domain."""
|
||||||
@ -95,7 +69,7 @@ class WhoisSensor(SensorEntity):
|
|||||||
response = self.whois(self._domain)
|
response = self.whois(self._domain)
|
||||||
except whois.BaseException as ex: # pylint: disable=broad-except
|
except whois.BaseException as ex: # pylint: disable=broad-except
|
||||||
_LOGGER.error("Exception %s occurred during WHOIS lookup", ex)
|
_LOGGER.error("Exception %s occurred during WHOIS lookup", ex)
|
||||||
self._empty_state_and_attributes()
|
self._empty_value_and_attributes()
|
||||||
return
|
return
|
||||||
|
|
||||||
if response:
|
if response:
|
||||||
@ -105,12 +79,12 @@ class WhoisSensor(SensorEntity):
|
|||||||
"Did find: %s",
|
"Did find: %s",
|
||||||
", ".join(response.keys()),
|
", ".join(response.keys()),
|
||||||
)
|
)
|
||||||
self._empty_state_and_attributes()
|
self._empty_value_and_attributes()
|
||||||
return
|
return
|
||||||
|
|
||||||
if not response["expiration_date"]:
|
if not response["expiration_date"]:
|
||||||
_LOGGER.error("Whois response contains empty expiration_date")
|
_LOGGER.error("Whois response contains empty expiration_date")
|
||||||
self._empty_state_and_attributes()
|
self._empty_value_and_attributes()
|
||||||
return
|
return
|
||||||
|
|
||||||
attrs = {}
|
attrs = {}
|
||||||
@ -137,5 +111,5 @@ class WhoisSensor(SensorEntity):
|
|||||||
|
|
||||||
time_delta = expiration_date - expiration_date.now()
|
time_delta = expiration_date - expiration_date.now()
|
||||||
|
|
||||||
self._attributes = attrs
|
self._attr_extra_state_attributes = attrs
|
||||||
self._state = time_delta.days
|
self._attr_native_value = time_delta.days
|
||||||
|
Loading…
x
Reference in New Issue
Block a user