Documenting code

This commit is contained in:
Ethan L. Miller
2025-01-16 12:49:36 -08:00
parent a20d1bb422
commit 91e9f79f6a
3 changed files with 126 additions and 6 deletions

40
davisconditions.py Normal file → Executable file
View File

@ -1,4 +1,36 @@
#!/usr/home/homeassistant/bin/python3
#
# Gather data from the Davis WeatherLink Live and output it
# in JSON format for use in Home Assistant.
# Designed to be run as a command_line integration.
#
# Requires Python3 and the requests package.
# Update the first line to the python3 binary used by HomeAssistant.
#
#==========================================================================
# Copyright 2025 Ethan L. Miller (code@ethanmiller.us)
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import sys
import json
@ -19,9 +51,8 @@ mappings = {
'uv_index': 'uv_index',
}
if __name__ == '__main__':
vals = dict()
exit_code = 1
for i in range(4):
ok = False
try:
vals = dict()
davis_ip = sys.argv[1]
@ -33,7 +64,8 @@ if __name__ == '__main__':
vals = vals | c
result = {mappings[k]: vals[k] for k in mappings.keys() if k in vals}
print (json.dumps ({'data': result}))
ok = True
exit_code = 0
break
except:
pass
if ok: sys.exit(0)
sys.exit(exit_code)