hass_command_line_scripts/davisconditions.py

129 lines
4.8 KiB
Python
Raw Permalink Normal View History

2025-01-18 11:44:18 -08:00
#!/usr/bin/env python3
2025-01-16 12:49:36 -08:00
#
2025-01-18 11:44:18 -08:00
# Description:
#
# Gathers data from the Davis WeatherLink Live and outputs it
2025-01-16 12:49:36 -08:00
# in JSON format for use in Home Assistant.
# Designed to be run as a command_line integration.
#
2025-01-18 11:44:18 -08:00
# Requirements:
# - Python 3.9+
# - requests package
# - Update the first line to the python3 binary used by HomeAssistant.
#
# Usage/installation:
#
# Takes a single argument: the 8-digit code identifying the station.
# This is part of the URL you'd use to view the information on the web,
# and is listed on the web page. For example,
# https://waterdata.usgs.gov/monitoring-location/14339000/
# is for measurement station 14339000.
# The web page title is:
# Rogue River at Dodge Bridge, Near Eagle Point, OR - 14339000
# This means that the command line would be:
# /path/to/binary/riverconditions.py 14339000
#
#
# Update the first line of this script to be the same python3 executable as
# your Home Assistant instance uses.
#
# To use the integration, add the following to your configuration.yaml file
# (without the comments, obviously!)
# ------------------------
# command_line:
# - sensor:
# name: "River conditions"
# unique_id: river_conditions
# command: '/home/homeassistant/bin/riverconditions.py 14339000'
# scan_interval: 1800
# json_attributes:
# - data
# value_template: 'Rogue River conditions at Dodge Bridge'
# ------------------------
# You can use any value you want for value_template.
# Scan interval should be relatively long, since the values aren't updated
# frequently. Minimum interval should be 600 seconds (every 10 minutes).
#
# Next, add one or more sensors corresponding to the conditions you want to
# track in your system. For example,
# ------------------------
# template:
# - sensors:
# river_height:
# friendly_name: "River height"
# device_class: distance
# value_template: "{{ state_attr('sensor.river_conditions', 'data')['height'] | round(1) }}"
# river_flow:
# friendly_name: "River flow"
# device_class: volume_flow_rate
# unit_of_measurement: "cfs"
# value_template: "{{ state_attr('sensor.river_conditions', 'data')['flow'] | round(0) }}"
# ------------------------
# Note that height and flow are both contained within the 'data' attribute of the river_conditions
# sensor populated by the command.
#
#
2025-01-16 12:49:36 -08:00
#
#==========================================================================
# 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)