18
18
from homeassistant .helpers .entity import Entity
19
19
20
20
from .providers import PROVIDERS
21
+ from .const import OUTFILE , CONF_NOTIFY , CONF_EXCLUDE , CONF_EXCLUDE_CLIENTS , CONF_PROVIDER , CONF_LOG_LOCATION , STARTUP
21
22
22
23
_LOGGER = logging .getLogger (__name__ )
23
24
24
- CONF_NOTIFY = "enable_notification"
25
- CONF_EXCLUDE = "exclude"
26
- CONF_PROVIDER = "provider"
27
- CONF_LOG_LOCATION = "log_location"
28
-
29
25
ATTR_HOSTNAME = "hostname"
30
26
ATTR_COUNTRY = "country"
31
27
ATTR_REGION = "region"
38
34
SCAN_INTERVAL = timedelta (minutes = 1 )
39
35
40
36
PLATFORM_NAME = "authenticated"
41
-
42
- LOGFILE = "home-assistant.log"
43
- OUTFILE = ".ip_authenticated.yaml"
44
-
45
37
PLATFORM_SCHEMA = PLATFORM_SCHEMA .extend (
46
38
{
47
39
vol .Optional (CONF_PROVIDER , default = "ipapi" ): vol .In (
50
42
vol .Optional (CONF_LOG_LOCATION , default = "" ): cv .string ,
51
43
vol .Optional (CONF_NOTIFY , default = True ): cv .boolean ,
52
44
vol .Optional (CONF_EXCLUDE , default = []): vol .All (cv .ensure_list , [cv .string ]),
45
+ vol .Optional (CONF_EXCLUDE_CLIENTS , default = []): vol .All (cv .ensure_list , [cv .string ]),
53
46
}
54
47
)
55
48
@@ -60,17 +53,21 @@ def humanize_time(timestring):
60
53
61
54
62
55
def setup_platform (hass , config , add_devices , discovery_info = None ):
56
+ # Print startup message
57
+ _LOGGER .info (STARTUP )
58
+
63
59
"""Create the sensor"""
64
60
notify = config .get (CONF_NOTIFY )
65
61
exclude = config .get (CONF_EXCLUDE )
62
+ exclude_clients = config .get (CONF_EXCLUDE_CLIENTS )
66
63
hass .data [PLATFORM_NAME ] = {}
67
64
68
- if not load_authentications (hass .config .path (".storage/auth" ), exclude ):
65
+ if not load_authentications (hass .config .path (".storage/auth" ), exclude , exclude_clients ):
69
66
return False
70
67
71
68
out = str (hass .config .path (OUTFILE ))
72
69
73
- sensor = AuthenticatedSensor (hass , notify , out , exclude , config [CONF_PROVIDER ])
70
+ sensor = AuthenticatedSensor (hass , notify , out , exclude , exclude_clients , config [CONF_PROVIDER ])
74
71
sensor .initial_run ()
75
72
76
73
add_devices ([sensor ], True )
@@ -79,21 +76,22 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
79
76
class AuthenticatedSensor (Entity ):
80
77
"""Representation of a Sensor."""
81
78
82
- def __init__ (self , hass , notify , out , exclude , provider ):
79
+ def __init__ (self , hass , notify , out , exclude , exclude_clients , provider ):
83
80
"""Initialize the sensor."""
84
81
self .hass = hass
85
82
self ._state = None
86
83
self .provider = provider
87
84
self .stored = {}
88
85
self .last_ip = None
89
86
self .exclude = exclude
87
+ self .exclude_clients = exclude_clients
90
88
self .notify = notify
91
89
self .out = out
92
90
93
91
def initial_run (self ):
94
92
"""Run this at startup to initialize the platform data."""
95
93
users , tokens = load_authentications (
96
- self .hass .config .path (".storage/auth" ), self .exclude
94
+ self .hass .config .path (".storage/auth" ), self .exclude , self . exclude_clients
97
95
)
98
96
99
97
if os .path .isfile (self .out ):
@@ -155,7 +153,7 @@ def update(self):
155
153
"""Method to update sensor value"""
156
154
updated = False
157
155
users , tokens = load_authentications (
158
- self .hass .config .path (".storage/auth" ), self .exclude
156
+ self .hass .config .path (".storage/auth" ), self .exclude , self . exclude_clients
159
157
)
160
158
_LOGGER .debug ("Users %s" , users )
161
159
_LOGGER .debug ("Access %s" , tokens )
@@ -296,7 +294,7 @@ def get_hostname(ip_address):
296
294
return hostname
297
295
298
296
299
- def load_authentications (authfile , exclude ):
297
+ def load_authentications (authfile , exclude , exclude_clients ):
300
298
"""Load info from auth file."""
301
299
if not os .path .exists (authfile ):
302
300
_LOGGER .critical ("File is missing %s" , authfile )
@@ -318,6 +316,8 @@ def load_authentications(authfile, exclude):
318
316
excludeaddress , False
319
317
):
320
318
raise Exception ("IP in excluded address configuration" )
319
+ if token ["client_id" ] in exclude_clients :
320
+ raise Exception ("Client in excluded clients configuration" )
321
321
if token .get ("last_used_at" ) is None :
322
322
continue
323
323
if token ["last_used_ip" ] in tokens_cleaned :
@@ -397,6 +397,10 @@ def notify(self, hass):
397
397
country = "**Country:** {}" .format (self .country )
398
398
else :
399
399
country = ""
400
+ if self .hostname is not None :
401
+ hostname = "**Hostname:** {}" .format (self .hostname )
402
+ else :
403
+ hostname = ""
400
404
if self .region is not None :
401
405
region = "**Region:** {}" .format (self .region )
402
406
else :
@@ -420,6 +424,7 @@ def notify(self, hass):
420
424
self .ip_address ,
421
425
self .username ,
422
426
country ,
427
+ hostname ,
423
428
region ,
424
429
city ,
425
430
last_used_at .replace ("T" , " " ),
0 commit comments