hass_command_line_scripts/davisconditions.py

72 lines
2.8 KiB
Python
Raw Normal View History

2025-01-16 12:04:54 -08:00
#!/usr/home/homeassistant/bin/python3
2025-01-16 12:49:36 -08:00
#
# 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.
2025-01-16 12:04:54 -08:00
import sys
import json
import requests
mappings = {
'temp_in': 'temp_in',
'hum_in': 'hum_in',
'temp': 'temp',
'hum': 'hum',
'dew_point': 'dew_point',
'bar_sea_level': 'barometer',
'wind_speed_avg_last_1_min': 'wind_speed_1min',
'wind_dir_scalar_avg_last_1_min': 'wind_dir_1min',
'wind_speed_hi_last_10_min': 'wind_gust_10min',
'wind_dir_at_hi_speed_last_10_min': 'wind_gust_dir_10min',
'solar_rad': 'solar_rad',
'uv_index': 'uv_index',
}
if __name__ == '__main__':
2025-01-16 12:49:36 -08:00
exit_code = 1
2025-01-16 12:04:54 -08:00
for i in range(4):
try:
vals = dict()
davis_ip = sys.argv[1]
url = f'http://{davis_ip}/v1/current_conditions'
req = requests.get (url, timeout=3)
if req.ok:
j = req.json()
for c in j['data']['conditions']:
vals = vals | c
result = {mappings[k]: vals[k] for k in mappings.keys() if k in vals}
print (json.dumps ({'data': result}))
2025-01-16 12:49:36 -08:00
exit_code = 0
break
2025-01-16 12:04:54 -08:00
except:
pass
2025-01-16 12:49:36 -08:00
sys.exit(exit_code)