7
7
import voluptuous as vol
8
8
9
9
from homeassistant .components .sensor import PLATFORM_SCHEMA
10
- from homeassistant .const import CONF_EMAIL , ATTR_ATTRIBUTION
10
+ from homeassistant .const import CONF_EMAIL , CONF_API_KEY , ATTR_ATTRIBUTION
11
11
import homeassistant .helpers .config_validation as cv
12
12
from homeassistant .helpers .entity import Entity
13
13
from homeassistant .helpers .event import track_point_in_time
25
25
MIN_TIME_BETWEEN_FORCED_UPDATES = timedelta (seconds = 5 )
26
26
MIN_TIME_BETWEEN_UPDATES = timedelta (minutes = 15 )
27
27
28
- URL = "https://haveibeenpwned.com/api/v2 /breachedaccount/"
28
+ URL = "https://haveibeenpwned.com/api/v3 /breachedaccount/"
29
29
30
30
PLATFORM_SCHEMA = PLATFORM_SCHEMA .extend (
31
- {vol .Required (CONF_EMAIL ): vol .All (cv .ensure_list , [cv .string ])}
31
+ {
32
+ vol .Required (CONF_EMAIL ): vol .All (cv .ensure_list , [cv .string ]),
33
+ vol .Required (CONF_API_KEY ): cv .string ,
34
+ }
32
35
)
33
36
34
37
35
38
def setup_platform (hass , config , add_entities , discovery_info = None ):
36
39
"""Set up the HaveIBeenPwned sensor."""
37
40
emails = config .get (CONF_EMAIL )
38
- data = HaveIBeenPwnedData (emails )
41
+ api_key = config [CONF_API_KEY ]
42
+ data = HaveIBeenPwnedData (emails , api_key )
39
43
40
44
devices = []
41
45
for email in emails :
@@ -125,13 +129,14 @@ def update(self):
125
129
class HaveIBeenPwnedData :
126
130
"""Class for handling the data retrieval."""
127
131
128
- def __init__ (self , emails ):
132
+ def __init__ (self , emails , api_key ):
129
133
"""Initialize the data object."""
130
134
self ._email_count = len (emails )
131
135
self ._current_index = 0
132
136
self .data = {}
133
137
self ._email = emails [0 ]
134
138
self ._emails = emails
139
+ self ._api_key = api_key
135
140
136
141
def set_next_email (self ):
137
142
"""Set the next email to be looked up."""
@@ -146,16 +151,10 @@ def update_no_throttle(self):
146
151
def update (self , ** kwargs ):
147
152
"""Get the latest data for current email from REST service."""
148
153
try :
149
- url = "{}{}" .format (URL , self ._email )
150
-
154
+ url = "{}{}?truncateResponse=false " .format (URL , self ._email )
155
+ header = { USER_AGENT : HA_USER_AGENT , "hibp-api-key" : self . _api_key }
151
156
_LOGGER .debug ("Checking for breaches for email: %s" , self ._email )
152
-
153
- req = requests .get (
154
- url ,
155
- headers = {USER_AGENT : HA_USER_AGENT },
156
- allow_redirects = True ,
157
- timeout = 5 ,
158
- )
157
+ req = requests .get (url , headers = header , allow_redirects = True , timeout = 5 )
159
158
160
159
except requests .exceptions .RequestException :
161
160
_LOGGER .error ("Failed fetching data for %s" , self ._email )
0 commit comments