diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..cba7e4f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +FROM alpine:3.9 + +RUN apk --no-cache update \ + && apk add --no-cache \ + rsyslog \ + rsyslog-elasticsearch \ + rsyslog-imrelp \ + rsyslog-mmjsonparse \ + rsyslog-mmutf8fix \ + rsyslog-omrelp \ + python3 \ + py3-requests + +RUN \ + echo "" >> /etc/rsyslog.conf && \ + echo "module(load=\"imudp\")" >> /etc/rsyslog.conf && \ + echo "input(type=\"imudp\" port=\"514\" ruleset=\"apple_airport\")" >> /etc/rsyslog.conf && \ + echo "module(load=\"omprog\")" >> /etc/rsyslog.conf && \ + echo "ruleset(name=\"apple_airport\"){" >> /etc/rsyslog.conf && \ + echo "action(type=\"omprog\" binary=\"/opt/apple_airport.py\")" >> /etc/rsyslog.conf && \ + echo "}" >> /etc/rsyslog.conf + +COPY apple_airport.py /opt/apple_airport.py + +RUN chmod +x /opt/apple_airport.py + +CMD ["/usr/sbin/rsyslogd","-n", "-d"] + +EXPOSE 514/udp \ No newline at end of file diff --git a/README.md b/README.md index 643d4a6..f226141 100644 --- a/README.md +++ b/README.md @@ -163,3 +163,8 @@ device_tracker: track_new_devices: false hide_if_away: true ``` + +# Docker +To build docker image run `docker build -f Dockerfile -t ha-airport-rsyslog .` +To run rsyslog in docker run smth like this `docker run --rm -it -p 514:514/udp -e HA_HOST=my_home_assistant_host -e HA_PORT=my_home_assistant_port -e HA_TOKEN=My_Long-Lived_Access_Token --name rsyslog rsyslog` +To create long-lived token use home assistant cotrol panel or read https://developers.home-assistant.io/docs/auth_api/#long-lived-access-token. \ No newline at end of file diff --git a/apple_airport.py b/apple_airport.py index fa2f105..db657b8 100644 --- a/apple_airport.py +++ b/apple_airport.py @@ -1,6 +1,15 @@ #!/usr/bin/python3 from requests import post -headers = {'x-ha-access': 'http_password', 'content-type': 'application/json'} + +import os + +host = os.getenv('HA_HOST', '127.0.0.1') +port = os.getenv('HA_PORT', '8123') +token = os.getenv('HA_TOKEN', 'NO TOKEN') + +endpoint = 'http://' + host + ':' + port +headers = {'Authorization': 'Bearer ' + token, 'content-type': 'application/json'} + while True: try: str = input() @@ -8,16 +17,16 @@ break if 'pppoe: Disconnected.' in str or 'ether: (WAN) link state is Down' in str: postData = '{"state":"off","attributes": {"wan_ip":""}}' - post('http://127.0.0.1:8123/api/states/binary_sensor.Internet',data=postData,headers=headers) + post(endpoint + '/api/states/binary_sensor.Internet',data=postData,headers=headers) elif 'pppoe: Connection established' in str: wan_ip = str[str.find('established')+12:str.find('->')-1] postData = '{"state":"on","attributes": {"wan_ip":"'+wan_ip+'"}}' - post('http://127.0.0.1:8123/api/states/binary_sensor.Internet',data=postData,headers=headers) + post(endpoint + '/api/states/binary_sensor.Internet',data=postData,headers=headers) elif 'Disassociated with station' in str: mac = str[-17:len(str)] postData = '{"mac":"'+mac+'","source_type":"router","consider_home":"3"}' - post('http://127.0.0.1:8123/api/services/device_tracker/see',data=postData,headers=headers) + post(endpoint + '/api/services/device_tracker/see',data=postData,headers=headers) elif 'Installed unicast CCMP key for supplicant' in str: mac = str[-17:len(str)] postData = '{"mac":"'+mac+'","source_type":"router","consider_home":"99:00:00"}' - post('http://127.0.0.1:8123/api/services/device_tracker/see',data=postData,headers=headers) + post(endpoint + '/api/services/device_tracker/see',data=postData,headers=headers)