reworded fastapi code

This commit is contained in:
2026-01-20 10:22:04 -05:00
parent cf4635922e
commit 258f918794
8 changed files with 339 additions and 106 deletions

View File

@@ -0,0 +1,107 @@
automation:
- id: update_home_location_from_gps
alias: Update Home Location from GPS
description: Updates the Home zone location based on GPS coordinates from Modbus
trigger:
- platform: state
entity_id:
- sensor.gps_latitude
- sensor.gps_longitude
condition:
- condition: template
value_template: >-
{% set lat = states('sensor.gps_latitude')|float(0) %}
{% set lon = states('sensor.gps_longitude')|float(0) %}
{% set fix = states('sensor.gps_fix')|int(0) %}
{{ lat != 0 and lon != 0 and fix > 0 }}
action:
- service: homeassistant.set_location
data:
latitude: "{{ states('sensor.gps_latitude') }}"
longitude: "{{ states('sensor.gps_longitude') }}"
- id: update_home_location_on_startup
alias: Update Home Location on Startup
description: Sets home location from last known GPS coordinates on HA restart
trigger:
- platform: homeassistant
event: start
condition:
- condition: template
value_template: >-
{% set lat = states('sensor.gps_latitude')|float(0) %}
{% set lon = states('sensor.gps_longitude')|float(0) %}
{{ lat != 0 and lon != 0 }}
action:
- delay:
seconds: 10
- service: homeassistant.set_location
data:
latitude: "{{ states('sensor.gps_latitude') }}"
longitude: "{{ states('sensor.gps_longitude') }}"
- id: refresh_weather_hourly
alias: Refresh Weather Hourly
description: Forces weather to refresh hourly with current GPS location
trigger:
- platform: time_pattern
hours: "/1"
condition:
- condition: state
entity_id: binary_sensor.gps_fix_available
state: "on"
action:
- service: homeassistant.update_entity
target:
entity_id: weather.home
template:
- sensor:
- name: GPS Location
unique_id: gps_location
state: >-
{% set lat = states('sensor.gps_latitude')|float(0) %}
{% set lon = states('sensor.gps_longitude')|float(0) %}
{% if lat != 0 and lon != 0 %}
{{ lat }}, {{ lon }}
{% else %}
unavailable
{% endif %}
attributes:
latitude: "{{ states('sensor.gps_latitude') }}"
longitude: "{{ states('sensor.gps_longitude') }}"
speed: "{{ states('sensor.gps_speed') }}"
course: "{{ states('sensor.gps_course') }}"
altitude: "{{ states('sensor.gps_altitude') }}"
satellites: "{{ states('sensor.gps_satellites') }}"
fix: "{{ states('sensor.gps_fix') }}"
last_updated: "{{ now().isoformat() }}"
# Weather sensors based on current GPS location
- name: Current Weather Temperature
unique_id: current_weather_temperature
unit_of_measurement: "°F"
device_class: temperature
state: "{{ state_attr('weather.home', 'temperature') }}"
- name: Current Weather Humidity
unique_id: current_weather_humidity
unit_of_measurement: "%"
device_class: humidity
state: "{{ state_attr('weather.home', 'humidity') }}"
- name: Current Weather Condition
unique_id: current_weather_condition
state: "{{ states('weather.home') }}"
- name: Current Weather Wind Speed
unique_id: current_weather_wind_speed
unit_of_measurement: "mph"
device_class: wind_speed
state: "{{ state_attr('weather.home', 'wind_speed') }}"
- binary_sensor:
- name: GPS Fix Available
unique_id: gps_fix_available
device_class: connectivity
state: "{{ states('sensor.gps_fix')|int(0) > 0 }}"

View File

@@ -0,0 +1,45 @@
{
pkgs,
inputs,
...
}:
{
networking.firewall.allowedTCPPorts = [
8069
];
systemd.services.contact-api = {
description = "Contact Database API with Frontend";
after = [
"postgresql.service"
"network.target"
];
requires = [ "postgresql.service" ];
wantedBy = [ "multi-user.target" ];
environment = {
PYTHONPATH = "${inputs.self}";
POSTGRES_DB = "richie";
POSTGRES_HOST = "/run/postgresql";
POSTGRES_USER = "richie";
FRONTEND_DIR = "/home/richie/dotfiles/frontend/dist/";
};
serviceConfig = {
Type = "simple";
ExecStart = "${pkgs.my_python}/bin/fastapi run ${inputs.self}/python/api/contact_api.py --port 8069";
Restart = "on-failure";
RestartSec = "5s";
StandardOutput = "journal";
StandardError = "journal";
# Security hardening
NoNewPrivileges = true;
ProtectSystem = "strict";
ProtectHome = "read-only";
PrivateTmp = true;
ReadOnlyPaths = [
"${inputs.self}"
"/home/richie/dotfiles/frontend/dist/"
];
};
};
}