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

Commit ded3122

Browse files
committed
Merge branch 'master' of github.com:custom-components/authenticated
2 parents 09b6a21 + cf3400f commit ded3122

File tree

4 files changed

+111
-16
lines changed

4 files changed

+111
-16
lines changed

.github/workflows/release.yaml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# This GitHub action workflow is meant to be copyable to any repo that have the same structure.
2+
# - Your integration exist under custom_components/{INTEGRATION_NAME}/[integration files]
3+
# - You are using GitHub releases to publish new versions
4+
# - You have a INTEGRATION_VERSION constant in custom_components/{INTEGRATION_NAME}/const.py
5+
6+
name: Release Workflow
7+
8+
on:
9+
release:
10+
types: [published]
11+
12+
jobs:
13+
release:
14+
name: Release
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: 📥 Checkout the repository
18+
uses: actions/checkout@v2
19+
20+
- name: 🔢 Get release version
21+
id: version
22+
uses: home-assistant/actions/helpers/version@master
23+
24+
- name: ℹ️ Get integration information
25+
id: information
26+
run: |
27+
name=$(find custom_components/ -type d -maxdepth 1 | tail -n 1 | cut -d "/" -f2)
28+
echo "::set-output name=name::$name"
29+
30+
- name: 🖊️ Set version number
31+
run: |
32+
sed -i '/INTEGRATION_VERSION = /c\INTEGRATION_VERSION = "${{ steps.version.outputs.version }}"' \
33+
"${{ github.workspace }}/custom_components/${{ steps.information.outputs.name }}/const.py"
34+
jq '.version = "${{ steps.version.outputs.version }}"' \
35+
"${{ github.workspace }}/custom_components/${{ steps.information.outputs.name }}/manifest.json" > tmp \
36+
&& mv -f tmp "${{ github.workspace }}/custom_components/${{ steps.information.outputs.name }}/manifest.json"
37+
38+
- name: 👀 Validate data
39+
run: |
40+
if ! grep -q 'INTEGRATION_VERSION = "${{ steps.version.outputs.version }}"' ${{ github.workspace }}/custom_components/${{ steps.information.outputs.name }}/const.py; then
41+
echo "The version in custom_components/${{ steps.information.outputs.name }}/const.py was not correct"
42+
cat ${{ github.workspace }}/custom_components/${{ steps.information.outputs.name }}/const.py | grep INTEGRATION_VERSION
43+
exit 1
44+
fi
45+
manifestversion=$(jq -r '.version' ${{ github.workspace }}/custom_components/${{ steps.information.outputs.name }}/manifest.json)
46+
if [ "$manifestversion" != "${{ steps.version.outputs.version }}" ]; then
47+
echo "The version in custom_components/${{ steps.information.outputs.name }}/manifest.json was not correct"
48+
echo "$manifestversion"
49+
exit 1
50+
fi
51+
52+
- name: 📦 Create zip file for the integration
53+
run: |
54+
cd "${{ github.workspace }}/custom_components/${{ steps.information.outputs.name }}"
55+
zip ${{ steps.information.outputs.name }}.zip -r ./
56+
57+
- name: 📤 Upload the zip file as a release asset
58+
uses: actions/upload-release-asset@v1
59+
env:
60+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
61+
with:
62+
upload_url: ${{ github.event.release.upload_url }}
63+
asset_path: "${{ github.workspace }}/custom_components/${{ steps.information.outputs.name }}/${{ steps.information.outputs.name }}.zip"
64+
asset_name: ${{ steps.information.outputs.name }}.zip
65+
asset_content_type: application/zip
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""Constants for authenticated."""
2+
3+
DOMAIN = "authenticated"
4+
INTEGRATION_VERSION = "main"
5+
ISSUE_URL = "https://github.com/custom-components/authenticated/issues"
6+
7+
STARTUP = f"""
8+
-------------------------------------------------------------------
9+
{DOMAIN}
10+
Version: {INTEGRATION_VERSION}
11+
This is a custom component
12+
If you have any issues with this you need to open an issue here:
13+
https://github.com/custom-components/authenticated/issues
14+
-------------------------------------------------------------------
15+
"""
16+
17+
18+
CONF_NOTIFY = "enable_notification"
19+
CONF_EXCLUDE = "exclude"
20+
CONF_EXCLUDE_CLIENTS = "exclude_clients"
21+
CONF_PROVIDER = "provider"
22+
CONF_LOG_LOCATION = "log_location"
23+
24+
OUTFILE = ".ip_authenticated.yaml"
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"domain": "authenticated",
33
"name": "Authenticated",
4+
"version": "v0.0.0",
45
"documentation": "https://github.com/custom-components/authenticated",
56
"dependencies": [],
67
"codeowners": ["@ludeeus"],
78
"requirements": []
8-
}
9+
}

custom_components/authenticated/sensor.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,10 @@
1818
from homeassistant.helpers.entity import Entity
1919

2020
from .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-
2925
ATTR_HOSTNAME = "hostname"
3026
ATTR_COUNTRY = "country"
3127
ATTR_REGION = "region"
@@ -38,10 +34,6 @@
3834
SCAN_INTERVAL = timedelta(minutes=1)
3935

4036
PLATFORM_NAME = "authenticated"
41-
42-
LOGFILE = "home-assistant.log"
43-
OUTFILE = ".ip_authenticated.yaml"
44-
4537
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
4638
{
4739
vol.Optional(CONF_PROVIDER, default="ipapi"): vol.In(
@@ -50,6 +42,7 @@
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

6255
def 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):
7976
class 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

Comments
 (0)