Skip to content

Commit

Permalink
Update HIBP sensor to use API v3 and API Key (#25699)
Browse files Browse the repository at this point in the history
* Update HIBP sensor to use API v3 and API Key

* ran black code formatter

* fixed stray , that was invalid in multiple json formatters
  • Loading branch information
aetaric authored and balloob committed Aug 9, 2019
1 parent 8b6ddc2 commit a0494e4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
12 changes: 6 additions & 6 deletions homeassistant/components/haveibeenpwned/manifest.json
@@ -1,8 +1,8 @@
{
"domain": "haveibeenpwned",
"name": "Haveibeenpwned",
"documentation": "https://www.home-assistant.io/components/haveibeenpwned",
"requirements": [],
"dependencies": [],
"codeowners": []
"domain": "haveibeenpwned",
"name": "Haveibeenpwned",
"documentation": "https://www.home-assistant.io/components/haveibeenpwned",
"requirements": [],
"dependencies": [],
"codeowners": []
}
27 changes: 13 additions & 14 deletions homeassistant/components/haveibeenpwned/sensor.py
Expand Up @@ -7,7 +7,7 @@
import voluptuous as vol

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
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.event import track_point_in_time
Expand All @@ -25,17 +25,21 @@
MIN_TIME_BETWEEN_FORCED_UPDATES = timedelta(seconds=5)
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(
{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):
"""Set up the HaveIBeenPwned sensor."""
emails = config.get(CONF_EMAIL)
data = HaveIBeenPwnedData(emails)
api_key = config[CONF_API_KEY]
data = HaveIBeenPwnedData(emails, api_key)

devices = []
for email in emails:
Expand Down Expand Up @@ -125,13 +129,14 @@ def update(self):
class HaveIBeenPwnedData:
"""Class for handling the data retrieval."""

def __init__(self, emails):
def __init__(self, emails, api_key):
"""Initialize the data object."""
self._email_count = len(emails)
self._current_index = 0
self.data = {}
self._email = emails[0]
self._emails = emails
self._api_key = api_key

def set_next_email(self):
"""Set the next email to be looked up."""
Expand All @@ -146,16 +151,10 @@ def update_no_throttle(self):
def update(self, **kwargs):
"""Get the latest data for current email from REST service."""
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)

req = requests.get(
url,
headers={USER_AGENT: HA_USER_AGENT},
allow_redirects=True,
timeout=5,
)
req = requests.get(url, headers=header, allow_redirects=True, timeout=5)

except requests.exceptions.RequestException:
_LOGGER.error("Failed fetching data for %s", self._email)
Expand Down

0 comments on commit a0494e4

Please sign in to comment.