Update HIBP sensor to use API v3 and API Key (#25699)
* Update HIBP sensor to use API v3 and API Key * ran black code formatter * fixed stray , that was invalid in multiple json formatters
This commit is contained in:
parent
8b6ddc22a5
commit
a0494e44eb
2 changed files with 19 additions and 20 deletions
|
@ -1,8 +1,8 @@
|
||||||
{
|
{
|
||||||
"domain": "haveibeenpwned",
|
"domain": "haveibeenpwned",
|
||||||
"name": "Haveibeenpwned",
|
"name": "Haveibeenpwned",
|
||||||
"documentation": "https://www.home-assistant.io/components/haveibeenpwned",
|
"documentation": "https://www.home-assistant.io/components/haveibeenpwned",
|
||||||
"requirements": [],
|
"requirements": [],
|
||||||
"dependencies": [],
|
"dependencies": [],
|
||||||
"codeowners": []
|
"codeowners": []
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import requests
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
from homeassistant.const import CONF_EMAIL, ATTR_ATTRIBUTION
|
from homeassistant.const import CONF_EMAIL, CONF_API_KEY, ATTR_ATTRIBUTION
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.helpers.event import track_point_in_time
|
from homeassistant.helpers.event import track_point_in_time
|
||||||
|
@ -25,17 +25,21 @@ HA_USER_AGENT = "Home Assistant HaveIBeenPwned Sensor Component"
|
||||||
MIN_TIME_BETWEEN_FORCED_UPDATES = timedelta(seconds=5)
|
MIN_TIME_BETWEEN_FORCED_UPDATES = timedelta(seconds=5)
|
||||||
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=15)
|
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=15)
|
||||||
|
|
||||||
URL = "https://haveibeenpwned.com/api/v2/breachedaccount/"
|
URL = "https://haveibeenpwned.com/api/v3/breachedaccount/"
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{vol.Required(CONF_EMAIL): vol.All(cv.ensure_list, [cv.string])}
|
{
|
||||||
|
vol.Required(CONF_EMAIL): vol.All(cv.ensure_list, [cv.string]),
|
||||||
|
vol.Required(CONF_API_KEY): cv.string,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up the HaveIBeenPwned sensor."""
|
"""Set up the HaveIBeenPwned sensor."""
|
||||||
emails = config.get(CONF_EMAIL)
|
emails = config.get(CONF_EMAIL)
|
||||||
data = HaveIBeenPwnedData(emails)
|
api_key = config[CONF_API_KEY]
|
||||||
|
data = HaveIBeenPwnedData(emails, api_key)
|
||||||
|
|
||||||
devices = []
|
devices = []
|
||||||
for email in emails:
|
for email in emails:
|
||||||
|
@ -125,13 +129,14 @@ class HaveIBeenPwnedSensor(Entity):
|
||||||
class HaveIBeenPwnedData:
|
class HaveIBeenPwnedData:
|
||||||
"""Class for handling the data retrieval."""
|
"""Class for handling the data retrieval."""
|
||||||
|
|
||||||
def __init__(self, emails):
|
def __init__(self, emails, api_key):
|
||||||
"""Initialize the data object."""
|
"""Initialize the data object."""
|
||||||
self._email_count = len(emails)
|
self._email_count = len(emails)
|
||||||
self._current_index = 0
|
self._current_index = 0
|
||||||
self.data = {}
|
self.data = {}
|
||||||
self._email = emails[0]
|
self._email = emails[0]
|
||||||
self._emails = emails
|
self._emails = emails
|
||||||
|
self._api_key = api_key
|
||||||
|
|
||||||
def set_next_email(self):
|
def set_next_email(self):
|
||||||
"""Set the next email to be looked up."""
|
"""Set the next email to be looked up."""
|
||||||
|
@ -146,16 +151,10 @@ class HaveIBeenPwnedData:
|
||||||
def update(self, **kwargs):
|
def update(self, **kwargs):
|
||||||
"""Get the latest data for current email from REST service."""
|
"""Get the latest data for current email from REST service."""
|
||||||
try:
|
try:
|
||||||
url = "{}{}".format(URL, self._email)
|
url = "{}{}?truncateResponse=false".format(URL, self._email)
|
||||||
|
header = {USER_AGENT: HA_USER_AGENT, "hibp-api-key": self._api_key}
|
||||||
_LOGGER.debug("Checking for breaches for email: %s", self._email)
|
_LOGGER.debug("Checking for breaches for email: %s", self._email)
|
||||||
|
req = requests.get(url, headers=header, allow_redirects=True, timeout=5)
|
||||||
req = requests.get(
|
|
||||||
url,
|
|
||||||
headers={USER_AGENT: HA_USER_AGENT},
|
|
||||||
allow_redirects=True,
|
|
||||||
timeout=5,
|
|
||||||
)
|
|
||||||
|
|
||||||
except requests.exceptions.RequestException:
|
except requests.exceptions.RequestException:
|
||||||
_LOGGER.error("Failed fetching data for %s", self._email)
|
_LOGGER.error("Failed fetching data for %s", self._email)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue