1818from homeassistant .helpers .entity import Entity
1919
2020from .providers import PROVIDERS
21+ from .const import OUTFILE , CONF_NOTIFY , CONF_EXCLUDE , CONF_EXCLUDE_CLIENTS , CONF_PROVIDER , CONF_LOG_LOCATION , STARTUP
2122
2223_LOGGER = logging .getLogger (__name__ )
2324
24- CONF_NOTIFY = "enable_notification"
25- CONF_EXCLUDE = "exclude"
26- CONF_PROVIDER = "provider"
27- CONF_LOG_LOCATION = "log_location"
28-
2925ATTR_HOSTNAME = "hostname"
3026ATTR_COUNTRY = "country"
3127ATTR_REGION = "region"
3834SCAN_INTERVAL = timedelta (minutes = 1 )
3935
4036PLATFORM_NAME = "authenticated"
41-
42- LOGFILE = "home-assistant.log"
43- OUTFILE = ".ip_authenticated.yaml"
44-
4537PLATFORM_SCHEMA = PLATFORM_SCHEMA .extend (
4638 {
4739 vol .Optional (CONF_PROVIDER , default = "ipapi" ): vol .In (
5042 vol .Optional (CONF_LOG_LOCATION , default = "" ): cv .string ,
5143 vol .Optional (CONF_NOTIFY , default = True ): cv .boolean ,
5244 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 ]),
5346 }
5447)
5548
@@ -60,17 +53,21 @@ def humanize_time(timestring):
6053
6154
6255def setup_platform (hass , config , add_devices , discovery_info = None ):
56+ # Print startup message
57+ _LOGGER .info (STARTUP )
58+
6359 """Create the sensor"""
6460 notify = config .get (CONF_NOTIFY )
6561 exclude = config .get (CONF_EXCLUDE )
62+ exclude_clients = config .get (CONF_EXCLUDE_CLIENTS )
6663 hass .data [PLATFORM_NAME ] = {}
6764
68- if not load_authentications (hass .config .path (".storage/auth" ), exclude ):
65+ if not load_authentications (hass .config .path (".storage/auth" ), exclude , exclude_clients ):
6966 return False
7067
7168 out = str (hass .config .path (OUTFILE ))
7269
73- sensor = AuthenticatedSensor (hass , notify , out , exclude , config [CONF_PROVIDER ])
70+ sensor = AuthenticatedSensor (hass , notify , out , exclude , exclude_clients , config [CONF_PROVIDER ])
7471 sensor .initial_run ()
7572
7673 add_devices ([sensor ], True )
@@ -79,21 +76,22 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
7976class AuthenticatedSensor (Entity ):
8077 """Representation of a Sensor."""
8178
82- def __init__ (self , hass , notify , out , exclude , provider ):
79+ def __init__ (self , hass , notify , out , exclude , exclude_clients , provider ):
8380 """Initialize the sensor."""
8481 self .hass = hass
8582 self ._state = None
8683 self .provider = provider
8784 self .stored = {}
8885 self .last_ip = None
8986 self .exclude = exclude
87+ self .exclude_clients = exclude_clients
9088 self .notify = notify
9189 self .out = out
9290
9391 def initial_run (self ):
9492 """Run this at startup to initialize the platform data."""
9593 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
9795 )
9896
9997 if os .path .isfile (self .out ):
@@ -155,7 +153,7 @@ def update(self):
155153 """Method to update sensor value"""
156154 updated = False
157155 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
159157 )
160158 _LOGGER .debug ("Users %s" , users )
161159 _LOGGER .debug ("Access %s" , tokens )
@@ -296,7 +294,7 @@ def get_hostname(ip_address):
296294 return hostname
297295
298296
299- def load_authentications (authfile , exclude ):
297+ def load_authentications (authfile , exclude , exclude_clients ):
300298 """Load info from auth file."""
301299 if not os .path .exists (authfile ):
302300 _LOGGER .critical ("File is missing %s" , authfile )
@@ -318,6 +316,8 @@ def load_authentications(authfile, exclude):
318316 excludeaddress , False
319317 ):
320318 raise Exception ("IP in excluded address configuration" )
319+ if token ["client_id" ] in exclude_clients :
320+ raise Exception ("Client in excluded clients configuration" )
321321 if token .get ("last_used_at" ) is None :
322322 continue
323323 if token ["last_used_ip" ] in tokens_cleaned :
@@ -397,6 +397,10 @@ def notify(self, hass):
397397 country = "**Country:** {}" .format (self .country )
398398 else :
399399 country = ""
400+ if self .hostname is not None :
401+ hostname = "**Hostname:** {}" .format (self .hostname )
402+ else :
403+ hostname = ""
400404 if self .region is not None :
401405 region = "**Region:** {}" .format (self .region )
402406 else :
@@ -420,6 +424,7 @@ def notify(self, hass):
420424 self .ip_address ,
421425 self .username ,
422426 country ,
427+ hostname ,
423428 region ,
424429 city ,
425430 last_used_at .replace ("T" , " " ),
0 commit comments