Skip to content

Commit 297dac3

Browse files
committed
Allow fetching of tibber rates in the inverter script
1 parent d825d08 commit 297dac3

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

inverter

+24-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
55
source "${SCRIPT_DIR}"/.venv/bin/activate
66
cd "${SCRIPT_DIR}"/source || exit 1
77

8-
read -r -d '' setup_code <<'EOF'
8+
read -r -d '' inverter_setup_code <<'EOF'
99
from goodwe.et import OperationMode
1010
from inverter import Inverter
1111
@@ -14,20 +14,21 @@ inverter.log.info("Inverter is manually controlled by a user")
1414
EOF
1515

1616

17-
if [[ ! ${1} =~ ^(--status|--mode)$ ]]; then
17+
if [[ ! ${1} =~ ^(--status|--mode|--tibber)$ ]]; then
1818
echo "This script allows you to interact with the inverter."
1919
echo ""
2020
echo "You have to supply one of the following arguments:"
2121
echo " --status Get the current state of charge and operation mode of the inverter"
2222
echo " --mode {GENERAL,ECO_CHARGE} Set the operation mode of the inverter"
23+
echo " --tibber Fetches, displays the tibber rates and writes them to the database"
2324
echo ""
24-
echo "It uses the hostname defined in the .env file"
25+
echo "It uses the all the variables defined in the .env file"
2526
exit 1
2627
fi
2728

2829
if [[ ${1} == "--status" ]]; then
2930
python3 - << EOF
30-
${setup_code}
31+
${inverter_setup_code}
3132
try:
3233
mode = inverter.get_operation_mode().name
3334
soc = inverter.get_state_of_charge()
@@ -36,7 +37,7 @@ except Exception as e:
3637
inverter.log.exception(e)
3738
exit(1)
3839
EOF
39-
exit $?
40+
exit $?
4041
fi
4142

4243
if [[ ${1} == "--mode" ]]; then
@@ -46,7 +47,7 @@ if [[ ${1} == "--mode" ]]; then
4647
fi
4748

4849
python3 - << EOF
49-
${setup_code}
50+
${inverter_setup_code}
5051
try:
5152
last_mode = inverter.get_operation_mode().name
5253
inverter.set_operation_mode(OperationMode.${2})
@@ -55,5 +56,21 @@ except Exception as e:
5556
inverter.log.exception(e)
5657
exit(1)
5758
EOF
58-
exit $?
59+
exit $?
60+
fi
61+
62+
if [[ ${1} == "--tibber" ]]; then
63+
python3 - << EOF
64+
from tibber_api_handler import TibberAPIHandler
65+
try:
66+
tibber_api_handler = TibberAPIHandler()
67+
api_result = tibber_api_handler._fetch_upcoming_prices_from_api()
68+
all_energy_rates = tibber_api_handler._extract_energy_rates_from_api_response(api_result)
69+
tibber_api_handler._remove_energy_rates_from_the_past(all_energy_rates)
70+
tibber_api_handler.write_energy_rates_to_database(all_energy_rates)
71+
except Exception as e:
72+
tibber_api_handler.log.exception(e)
73+
exit(1)
74+
EOF
75+
exit $?
5976
fi

0 commit comments

Comments
 (0)