mirror of
https://github.com/home-assistant/core.git
synced 2025-08-17 11:31:39 +02:00
Fix and clean haveibeenpwned (#17306)
* Move first forced data fetching and update to async_added_to_hass. * Clean up code.
This commit is contained in:
committed by
Pascal Vizeli
parent
707b7c202d
commit
e5c3a4be80
@@ -42,24 +42,18 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
|
|
||||||
devices = []
|
devices = []
|
||||||
for email in emails:
|
for email in emails:
|
||||||
devices.append(HaveIBeenPwnedSensor(data, hass, email))
|
devices.append(HaveIBeenPwnedSensor(data, email))
|
||||||
|
|
||||||
add_entities(devices)
|
add_entities(devices)
|
||||||
|
|
||||||
# To make sure we get initial data for the sensors ignoring the normal
|
|
||||||
# throttle of 15 minutes but using an update throttle of 5 seconds
|
|
||||||
for sensor in devices:
|
|
||||||
sensor.update_nothrottle()
|
|
||||||
|
|
||||||
|
|
||||||
class HaveIBeenPwnedSensor(Entity):
|
class HaveIBeenPwnedSensor(Entity):
|
||||||
"""Implementation of a HaveIBeenPwned sensor."""
|
"""Implementation of a HaveIBeenPwned sensor."""
|
||||||
|
|
||||||
def __init__(self, data, hass, email):
|
def __init__(self, data, email):
|
||||||
"""Initialize the HaveIBeenPwned sensor."""
|
"""Initialize the HaveIBeenPwned sensor."""
|
||||||
self._state = None
|
self._state = None
|
||||||
self._data = data
|
self._data = data
|
||||||
self._hass = hass
|
|
||||||
self._email = email
|
self._email = email
|
||||||
self._unit_of_measurement = "Breaches"
|
self._unit_of_measurement = "Breaches"
|
||||||
|
|
||||||
@@ -95,6 +89,12 @@ class HaveIBeenPwnedSensor(Entity):
|
|||||||
|
|
||||||
return val
|
return val
|
||||||
|
|
||||||
|
async def async_added_to_hass(self):
|
||||||
|
"""Get initial data."""
|
||||||
|
# To make sure we get initial data for the sensors ignoring the normal
|
||||||
|
# throttle of 15 minutes but using an update throttle of 5 seconds
|
||||||
|
self.hass.async_add_executor_job(self.update_nothrottle)
|
||||||
|
|
||||||
def update_nothrottle(self, dummy=None):
|
def update_nothrottle(self, dummy=None):
|
||||||
"""Update sensor without throttle."""
|
"""Update sensor without throttle."""
|
||||||
self._data.update_no_throttle()
|
self._data.update_no_throttle()
|
||||||
@@ -106,11 +106,10 @@ class HaveIBeenPwnedSensor(Entity):
|
|||||||
# normal using update
|
# normal using update
|
||||||
if self._email not in self._data.data:
|
if self._email not in self._data.data:
|
||||||
track_point_in_time(
|
track_point_in_time(
|
||||||
self._hass, self.update_nothrottle,
|
self.hass, self.update_nothrottle,
|
||||||
dt_util.now() + MIN_TIME_BETWEEN_FORCED_UPDATES)
|
dt_util.now() + MIN_TIME_BETWEEN_FORCED_UPDATES)
|
||||||
return
|
return
|
||||||
|
|
||||||
if self._email in self._data.data:
|
|
||||||
self._state = len(self._data.data[self._email])
|
self._state = len(self._data.data[self._email])
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user