Skip to content
This repository was archived by the owner on Aug 27, 2022. It is now read-only.

Commit 10f88ba

Browse files
authored
Allow ignoring specific client_ids (#61)
1 parent d20ea05 commit 10f88ba

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

custom_components/authenticated/sensor.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
CONF_NOTIFY = "enable_notification"
2525
CONF_EXCLUDE = "exclude"
26+
CONF_EXCLUDE_CLIENTS = "exclude_clients"
2627
CONF_PROVIDER = "provider"
2728
CONF_LOG_LOCATION = "log_location"
2829

@@ -50,6 +51,7 @@
5051
vol.Optional(CONF_LOG_LOCATION, default=""): cv.string,
5152
vol.Optional(CONF_NOTIFY, default=True): cv.boolean,
5253
vol.Optional(CONF_EXCLUDE, default=[]): vol.All(cv.ensure_list, [cv.string]),
54+
vol.Optional(CONF_EXCLUDE_CLIENTS, default=[]): vol.All(cv.ensure_list, [cv.string]),
5355
}
5456
)
5557

@@ -63,14 +65,15 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
6365
"""Create the sensor"""
6466
notify = config.get(CONF_NOTIFY)
6567
exclude = config.get(CONF_EXCLUDE)
68+
exclude_clients = config.get(CONF_EXCLUDE_CLIENTS)
6669
hass.data[PLATFORM_NAME] = {}
6770

68-
if not load_authentications(hass.config.path(".storage/auth"), exclude):
71+
if not load_authentications(hass.config.path(".storage/auth"), exclude, exclude_clients):
6972
return False
7073

7174
out = str(hass.config.path(OUTFILE))
7275

73-
sensor = AuthenticatedSensor(hass, notify, out, exclude, config[CONF_PROVIDER])
76+
sensor = AuthenticatedSensor(hass, notify, out, exclude, exclude_clients, config[CONF_PROVIDER])
7477
sensor.initial_run()
7578

7679
add_devices([sensor], True)
@@ -79,21 +82,22 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
7982
class AuthenticatedSensor(Entity):
8083
"""Representation of a Sensor."""
8184

82-
def __init__(self, hass, notify, out, exclude, provider):
85+
def __init__(self, hass, notify, out, exclude, exclude_clients, provider):
8386
"""Initialize the sensor."""
8487
self.hass = hass
8588
self._state = None
8689
self.provider = provider
8790
self.stored = {}
8891
self.last_ip = None
8992
self.exclude = exclude
93+
self.exclude_clients = exclude_clients
9094
self.notify = notify
9195
self.out = out
9296

9397
def initial_run(self):
9498
"""Run this at startup to initialize the platform data."""
9599
users, tokens = load_authentications(
96-
self.hass.config.path(".storage/auth"), self.exclude
100+
self.hass.config.path(".storage/auth"), self.exclude, self.exclude_clients
97101
)
98102

99103
if os.path.isfile(self.out):
@@ -155,7 +159,7 @@ def update(self):
155159
"""Method to update sensor value"""
156160
updated = False
157161
users, tokens = load_authentications(
158-
self.hass.config.path(".storage/auth"), self.exclude
162+
self.hass.config.path(".storage/auth"), self.exclude, self.exclude_clients
159163
)
160164
for access in tokens:
161165
try:
@@ -294,7 +298,7 @@ def get_hostname(ip_address):
294298
return hostname
295299

296300

297-
def load_authentications(authfile, exclude):
301+
def load_authentications(authfile, exclude, exclude_clients):
298302
"""Load info from auth file."""
299303
if not os.path.exists(authfile):
300304
_LOGGER.critical("File is missing %s", authfile)
@@ -316,6 +320,8 @@ def load_authentications(authfile, exclude):
316320
excludeaddress, False
317321
):
318322
raise Exception("IP in excluded address configuration")
323+
if token["client_id"] in exclude_clients:
324+
raise Exception("Client in excluded clients configuration")
319325
if token.get("last_used_at") is None:
320326
continue
321327
if token["last_used_ip"] in tokens_cleaned:

0 commit comments

Comments
 (0)