Skip to content
This repository was archived by the owner on Aug 27, 2022. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions custom_components/sensor/authenticated.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
ATTR_COUNTRY = 'country'
ATTR_REGION = 'region'
ATTR_CITY = 'city'
ATTR_LAST_AUTHENTICATE_TIME = 'last_authenticated_time'
ATTR_PREVIOUS_AUTHENTICATE_TIME = 'previous_authenticated_time'

SCAN_INTERVAL = timedelta(seconds=30)

Expand Down Expand Up @@ -55,6 +57,8 @@ def __init__(self, hass, notify, log, out):
self._country = None
self._region = None
self._city = None
self._last_authenticated_time = None
self._previous_authenticated_time = None
self._notify = notify
self._log = log
self._out = out
Expand All @@ -79,7 +83,7 @@ def new_ip(self, ip_address, access_time):
geo = 'none'
else:
geo = geo
if 'reserved' in geo:
if 'reserved' in geo or geo['error']:
_LOGGER.debug('The IP is reserved, no GEO info available...')
geo_country = 'none'
geo_region = 'none'
Expand All @@ -96,11 +100,12 @@ def new_ip(self, ip_address, access_time):

def update_ip(self, ip_address, access_time):
"""If we know this IP"""
_LOGGER.debug('Found known IP %s, updating access_timestamp.', ip_address)
_LOGGER.debug('Found known IP %s, updating timestamps.', ip_address)
with open(self._out) as f:
doc = yaml.load(f)
f.close()

doc[ip_address]['previous_authenticated_time'] = doc[ip_address]['last_authenticated']
doc[ip_address]['last_authenticated'] = access_time

with open(self._out, 'w') as f:
Expand All @@ -115,6 +120,7 @@ def write_file(self, ip_address, access_time, country='none', region='none', cit

doc[ip_address] = dict(
last_authenticated=access_time,
previous_authenticated_time='none',
country=country,
region=region,
city=city
Expand Down Expand Up @@ -152,6 +158,8 @@ def update(self):
self._country = geo_info[ip_address]['country']
self._region = geo_info[ip_address]['region']
self._city = geo_info[ip_address]['city']
self._last_authenticated_time = geo_info[ip_address]['last_authenticated']
self._previous_authenticated_time = geo_info[ip_address]['previous_authenticated_time']

@property
def name(self):
Expand All @@ -175,4 +183,6 @@ def device_state_attributes(self):
ATTR_COUNTRY: self._country,
ATTR_REGION: self._region,
ATTR_CITY: self._city,
ATTR_LAST_AUTHENTICATE_TIME: self._last_authenticated_time,
ATTR_PREVIOUS_AUTHENTICATE_TIME: self._previous_authenticated_time,
}