diff --git a/homeassistant/components/whois/sensor.py b/homeassistant/components/whois/sensor.py index 5d5e595fa50..3742ed90131 100644 --- a/homeassistant/components/whois/sensor.py +++ b/homeassistant/components/whois/sensor.py @@ -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