{ config, pkgs, ... }:

let
  domain = "home.giugl.io";
  teslamateURL = "https://tesla.giugl.io";
  teslaModel = "Model 3 Long Range";
  teslaName = "Lady Giovanna";
  piperPort = 10200;
  whisperPort = 10300;
in
{
  services.go2rtc = {
    enable = true;
    settings.api.listen = "127.0.0.1:1984";
  };

  # services.wyoming = {
  #   faster-whisper = {
  #     servers."home" = {
  #       enable = true;
  #       uri = "tcp://127.0.0.1:${toString whisperPort}";
  #       model = "large-v3";
  #       device = "cuda";
  #       language = "en";
  #     };
  #   };
  #   piper = {
  #     servers."home" = {
  #       enable = true;
  #       uri = "tcp://127.0.0.1:${toString piperPort}";
  #       voice = "en-us-ryan-medium";
  #       lengthScale = 0.63;
  #     };
  #   };
  # };

  services.home-assistant = {
    enable = true;
    package = pkgs.unstablePkgs.home-assistant;
    config = {
      http = {
        server_host = "127.0.0.1";
        server_port = 8123;
        use_x_forwarded_for = true;
        trusted_proxies = [ "127.0.0.1" ];
      };
      homeassistant = {
        name = "Underwood House";
        unit_system = "metric";
      };
      default_config = { };
      automation = "!include automations.yaml";
      frontend.themes = "!include_dir_merge_named themes";
      binary_sensor = [{
        platform = "template";
        sensors = {
          tesla_park_brake = {
            friendly_name = "Parking Brake";
            icon_template = "mdi:car-brake-parking";
            value_template = "{% if is_state('sensor.tesla_shift_state', 'P') %}\n  ON\n{% else %}\n  OFF\n{% endif %}";
          };
        };
      }];

      sensor = [{
        platform = "template";
        sensors = {
          tesla_est_battery_range_mi = {
            friendly_name = "Estimated Range (mi)";
            unit_of_measurement = "mi";
            icon_template = "mdi:gauge";
            availability_template = "{{ has_value('sensor.tesla_est_battery_range_km') }}";
            value_template = "{% if has_value('sensor.tesla_est_battery_range_km') %}\n  {{ (states('sensor.tesla_est_battery_range_km') | float / 1.609344) | round(2) }}\n{% endif %}\n";
          };
          tesla_rated_battery_range_mi = {
            friendly_name = "Rated Range (mi)";
            unit_of_measurement = "mi";
            icon_template = "mdi:gauge";
            availability_template = "{{ has_value('sensor.tesla_rated_battery_range_km') }}";
            value_template = "{% if has_value('sensor.tesla_rated_battery_range_km') %}\n  {{ (states('sensor.tesla_rated_battery_range_km') | float / 1.609344) | round(2) }}\n{% endif %}\n";
          };
          tesla_ideal_battery_range_mi = {
            friendly_name = "Ideal Range (mi)";
            unit_of_measurement = "mi";
            icon_template = "mdi:gauge";
            availability_template = "{{ has_value('sensor.tesla_ideal_battery_range_km') }}";
            value_template = "{% if has_value('sensor.tesla_ideal_battery_range_km') %}\n  {{ (states('sensor.tesla_ideal_battery_range_km') | float / 1.609344) | round(2) }}\n{% endif %}\n";
          };
          tesla_odometer_mi = {
            friendly_name = "Odometer (mi)";
            unit_of_measurement = "mi";
            icon_template = "mdi:counter";
            availability_template = "{{ has_value('sensor.tesla_odometer') }}";
            value_template = "{% if has_value('sensor.tesla_odometer') %}\n  {{ (states('sensor.tesla_odometer') | float / 1.609344) | round(2) }}\n{% endif %}\n";
          };
          tesla_speed_mph = {
            friendly_name = "Speed (MPH)";
            unit_of_measurement = "mph";
            icon_template = "mdi:speedometer";
            availability_template = "{{ has_value('sensor.tesla_speed') }}";
            value_template = "{% if has_value('sensor.tesla_speed') %}\n  {{ (states('sensor.tesla_speed') | float / 1.609344) | round(2) }}\n{% endif %}\n";
          };
          tesla_elevation_ft = {
            friendly_name = "Elevation (ft)";
            unit_of_measurement = "ft";
            icon_template = "mdi:image-filter-hdr";
            availability_template = "{{ has_value('sensor.tesla_elevation') }}";
            value_template = "{% if has_value('sensor.tesla_elevation') %}\n  {{ (states('sensor.tesla_elevation') | float * 3.2808) | round(2) }}\n{% endif %}\n";
          };
          tesla_tpms_pressure_fl_psi = {
            friendly_name = "Front Left Tire Pressure (psi)";
            unit_of_measurement = "psi";
            icon_template = "mdi:car-tire-alert";
            availability_template = "{{ has_value('sensor.tesla_tpms_fl') }}";
            value_template = "{% if has_value('sensor.tesla_tpms_fl') %}\n  {{ (states('sensor.tesla_tpms_fl') | float * 14.50377) | round(2) }}\n{% endif %}\n";
          };
          tesla_tpms_pressure_fr_psi = {
            friendly_name = "Front Right Tire Pressure (psi)";
            unit_of_measurement = "psi";
            icon_template = "mdi:car-tire-alert";
            availability_template = "{{ has_value('sensor.tesla_tpms_fr') }}";
            value_template = "{% if has_value('sensor.tesla_tpms_fr') %}\n  {{ (states('sensor.tesla_tpms_fr') | float * 14.50377) | round(2) }}\n{% endif %}\n";
          };
          tesla_tpms_pressure_rl_psi = {
            friendly_name = "Rear Left Tire Pressure (psi)";
            unit_of_measurement = "psi";
            icon_template = "mdi:car-tire-alert";
            availability_template = "{{ has_value('sensor.tesla_tpms_rl') }}";
            value_template = "{% if has_value('sensor.tesla_tpms_rl') %}\n  {{ (states('sensor.tesla_tpms_rl') | float * 14.50377) | round(2) }}\n{% endif %}\n";
          };
          tesla_tpms_pressure_rr_psi = {
            friendly_name = "Rear Right Tire Pressure (psi)";
            unit_of_measurement = "psi";
            icon_template = "mdi:car-tire-alert";
            availability_template = "{{ has_value('sensor.tesla_tpms_rr') }}";
            value_template = "{% if has_value('sensor.tesla_tpms_rr') %}\n  {{ (states('sensor.tesla_tpms_rr') | float * 14.50377) | round(2) }}\n{% endif %}\n";
          };
          tesla_active_route_distance_to_arrival_km = {
            friendly_name = "Active route distance to arrival (km)";
            unit_of_measurement = "km";
            icon_template = "mdi:map-marker-distance";
            availability_template = "{{ has_value('sensor.tesla_active_route_distance_to_arrival_mi') }}";
            value_template = "{% if has_value('sensor.tesla_active_route_distance_to_arrival_mi') %}\n  {{ (states('sensor.tesla_active_route_distance_to_arrival_mi') | float * 1.609344) | round(2) }}\n{% endif %}";
          };
        };
      }];

      mqtt = [
        {
          sensor = {
            name = "Display Name";
            object_id = "tesla_display_name";
            unique_id = "teslamate_1_display_name";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
              name = teslaName;
            };
            state_topic = "teslamate/cars/1/display_name";
            icon = "mdi:car";
          };
        }
        {
          device_tracker = {
            name = "Location";
            object_id = "tesla_location";
            unique_id = "teslamate_1_location";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            json_attributes_topic = "teslamate/cars/1/location";
            icon = "mdi:crosshairs-gps";
          };
        }
        {
          device_tracker = {
            name = "Active route location";
            object_id = "tesla_active_route_location";
            unique_id = "teslamate_1_active_route_location";
            availability = [{
              topic = "teslamate/cars/1/active_route";
              value_template = "{{ 'offline' if value_json.error else 'online' }}";
            }];
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            json_attributes_topic = "teslamate/cars/1/active_route";
            json_attributes_template = "{% if not value_json.error and value_json.location %}\n  {{ value_json.location | tojson }}\n{% else %}\n  {}\n{% endif %}\n";
            icon = "mdi:crosshairs-gps";
          };
        }
        {
          sensor = {
            name = "State";
            object_id = "tesla_state";
            unique_id = "teslamate_1_state";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/state";
            icon = "mdi:car-connected";
          };
        }
        {
          sensor = {
            name = "Since";
            object_id = "tesla_since";
            unique_id = "teslamate_1_since";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/since";
            device_class = "timestamp";
            icon = "mdi:clock-outline";
          };
        }
        {
          sensor = {
            name = "Version";
            object_id = "tesla_version";
            unique_id = "teslamate_1_version";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/version";
            icon = "mdi:alphabetical";
          };
        }
        {
          sensor = {
            name = "Update Version";
            object_id = "tesla_update_version";
            unique_id = "teslamate_1_update_version";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/update_version";
            icon = "mdi:alphabetical";
          };
        }
        {
          sensor = {
            name = "Model";
            object_id = "tesla_model";
            unique_id = "teslamate_1_model";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/model";
          };
        }
        {
          sensor = {
            name = "Trim Badging";
            object_id = "tesla_trim_badging";
            unique_id = "teslamate_1_trim_badging";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/trim_badging";
            icon = "mdi:shield-star-outline";
          };
        }
        {
          sensor = {
            name = "Exterior Color";
            object_id = "tesla_exterior_color";
            unique_id = "teslamate_1_exterior_color";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/exterior_color";
            icon = "mdi:palette";
          };
        }
        {
          sensor = {
            name = "Wheel Type";
            object_id = "tesla_wheel_type";
            unique_id = "teslamate_1_wheel_type";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/wheel_type";
          };
        }
        {
          sensor = {
            name = "Spoiler Type";
            object_id = "tesla_spoiler_type";
            unique_id = "teslamate_1_spoiler_type";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/spoiler_type";
            icon = "mdi:car-sports";
          };
        }
        {
          sensor = {
            name = "Geofence";
            object_id = "tesla_geofence";
            unique_id = "teslamate_1_geofence";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/geofence";
            icon = "mdi:earth";
          };
        }
        {
          sensor = {
            name = "Shift State";
            object_id = "tesla_shift_state";
            unique_id = "teslamate_1_shift_state";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/shift_state";
            icon = "mdi:car-shift-pattern";
          };
        }
        {
          sensor = {
            name = "Power";
            object_id = "tesla_power";
            unique_id = "teslamate_1_power";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/power";
            device_class = "power";
            unit_of_measurement = "kW";
            icon = "mdi:flash";
          };
        }
        {
          sensor = {
            name = "Speed";
            object_id = "tesla_speed";
            unique_id = "teslamate_1_speed";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/speed";
            unit_of_measurement = "km/h";
            icon = "mdi:speedometer";
          };
        }
        {
          sensor = {
            name = "Heading";
            object_id = "tesla_heading";
            unique_id = "teslamate_1_heading";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/heading";
            unit_of_measurement = "°";
            icon = "mdi:compass";
          };
        }
        {
          sensor = {
            name = "Elevation";
            object_id = "tesla_elevation";
            unique_id = "teslamate_1_elevation";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/elevation";
            unit_of_measurement = "m";
            icon = "mdi:image-filter-hdr";
          };
        }
        {
          sensor = {
            name = "Inside Temp";
            object_id = "tesla_inside_temp";
            unique_id = "teslamate_1_inside_temp";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/inside_temp";
            device_class = "temperature";
            unit_of_measurement = "°C";
            icon = "mdi:thermometer-lines";
          };
        }
        {
          sensor = {
            name = "Outside Temp";
            object_id = "tesla_outside_temp";
            unique_id = "teslamate_1_outside_temp";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/outside_temp";
            device_class = "temperature";
            unit_of_measurement = "°C";
            icon = "mdi:thermometer-lines";
          };
        }
        {
          sensor = {
            name = "Odometer";
            object_id = "tesla_odometer";
            unique_id = "teslamate_1_odometer";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/odometer";
            unit_of_measurement = "km";
            icon = "mdi:counter";
          };
        }
        {
          sensor = {
            name = "Est Battery Range";
            object_id = "tesla_est_battery_range_km";
            unique_id = "teslamate_1_est_battery_range_km";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/est_battery_range_km";
            unit_of_measurement = "km";
            icon = "mdi:gauge";
          };
        }
        {
          sensor = {
            name = "Rated Battery Range";
            object_id = "tesla_rated_battery_range_km";
            unique_id = "teslamate_1_rated_battery_range_km";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/rated_battery_range_km";
            unit_of_measurement = "km";
            icon = "mdi:gauge";
          };
        }
        {
          sensor = {
            name = "Ideal Battery Range";
            object_id = "tesla_ideal_battery_range_km";
            unique_id = "teslamate_1_ideal_battery_range_km";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/ideal_battery_range_km";
            unit_of_measurement = "km";
            icon = "mdi:gauge";
          };
        }
        {
          sensor = {
            name = "Battery Level";
            object_id = "tesla_battery_level";
            unique_id = "teslamate_1_battery_level";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/battery_level";
            device_class = "battery";
            unit_of_measurement = "%";
            icon = "mdi:battery-80";
          };
        }
        {
          sensor = {
            name = "Usable Battery Level";
            object_id = "tesla_usable_battery_level";
            unique_id = "teslamate_1_usable_battery_level";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/usable_battery_level";
            unit_of_measurement = "%";
            icon = "mdi:battery-80";
          };
        }
        {
          sensor = {
            name = "Charge Energy Added";
            object_id = "tesla_charge_energy_added";
            unique_id = "teslamate_1_charge_energy_added";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/charge_energy_added";
            device_class = "energy";
            unit_of_measurement = "kWh";
            icon = "mdi:battery-charging";
          };
        }
        {
          sensor = {
            name = "Charge Limit Soc";
            object_id = "tesla_charge_limit_soc";
            unique_id = "teslamate_1_charge_limit_soc";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/charge_limit_soc";
            unit_of_measurement = "%";
            icon = "mdi:battery-charging-100";
          };
        }
        {
          sensor = {
            name = "Charger Actual Current";
            object_id = "tesla_charger_actual_current";
            unique_id = "teslamate_1_charger_actual_current";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/charger_actual_current";
            device_class = "current";
            unit_of_measurement = "A";
            icon = "mdi:lightning-bolt";
          };
        }
        {
          sensor = {
            name = "Charger Phases";
            object_id = "tesla_charger_phases";
            unique_id = "teslamate_1_charger_phases";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/charger_phases";
            icon = "mdi:sine-wave";
          };
        }
        {
          sensor = {
            name = "Charger Power";
            object_id = "tesla_charger_power";
            unique_id = "teslamate_1_charger_power";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/charger_power";
            device_class = "power";
            unit_of_measurement = "kW";
            icon = "mdi:lightning-bolt";
          };
        }
        {
          sensor = {
            name = "Charger Voltage";
            object_id = "tesla_charger_voltage";
            unique_id = "teslamate_1_charger_voltage";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/charger_voltage";
            device_class = "voltage";
            unit_of_measurement = "V";
            icon = "mdi:lightning-bolt";
          };
        }
        {
          sensor = {
            name = "Scheduled Charging Start Time";
            object_id = "tesla_scheduled_charging_start_time";
            unique_id = "teslamate_1_scheduled_charging_start_time";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/scheduled_charging_start_time";
            device_class = "timestamp";
            icon = "mdi:clock-outline";
          };
        }
        {
          sensor = {
            name = "Time To Full Charge";
            object_id = "tesla_time_to_full_charge";
            unique_id = "teslamate_1_time_to_full_charge";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/time_to_full_charge";
            unit_of_measurement = "h";
            icon = "mdi:clock-outline";
          };
        }
        {
          sensor = {
            name = "TPMS Pressure Front Left";
            object_id = "tesla_tpms_pressure_fl";
            unique_id = "teslamate_1_tpms_pressure_fl";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/tpms_pressure_fl";
            unit_of_measurement = "bar";
            icon = "mdi:car-tire-alert";
          };
        }
        {
          sensor = {
            name = "TPMS Pressure Front Right";
            object_id = "tesla_tpms_pressure_fr";
            unique_id = "teslamate_1_tpms_pressure_fr";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/tpms_pressure_fr";
            unit_of_measurement = "bar";
            icon = "mdi:car-tire-alert";
          };
        }
        {
          sensor = {
            name = "TPMS Pressure Rear Left";
            object_id = "tesla_tpms_pressure_rl";
            unique_id = "teslamate_1_tpms_pressure_rl";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/tpms_pressure_rl";
            unit_of_measurement = "bar";
            icon = "mdi:car-tire-alert";
          };
        }
        {
          sensor = {
            name = "TPMS Pressure Rear Right";
            object_id = "tesla_tpms_pressure_rr";
            unique_id = "teslamate_1_tpms_pressure_rr";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/tpms_pressure_rr";
            unit_of_measurement = "bar";
            icon = "mdi:car-tire-alert";
          };
        }
        {
          sensor = {
            name = "Active route destination";
            object_id = "tesla_active_route_destination";
            unique_id = "teslamate_1_active_route_destination";
            availability = [{
              topic = "teslamate/cars/1/active_route";
              value_template = "{{ 'offline' if value_json.error else 'online' }}";
            }];
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/active_route";
            value_template = "{% if not value_json.error and value_json.destination %}\n  {{ value_json.destination }}\n{% endif %}\n";
            icon = "mdi:map-marker";
          };
        }
        {
          sensor = {
            name = "Active route energy at arrival";
            object_id = "tesla_active_route_energy_at_arrival";
            unique_id = "teslamate_1_active_route_energy_at_arrival";
            availability = [{
              topic = "teslamate/cars/1/active_route";
              value_template = "{{ 'offline' if value_json.error else 'online' }}";
            }];
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/active_route";
            value_template = "{% if not value_json.error and value_json.energy_at_arrival %}\n  {{ value_json.energy_at_arrival }}\n{% endif %}\n";
            unit_of_measurement = "%";
            icon = "mdi:battery-80";
          };
        }
        {
          sensor = {
            name = "Active route distance to arrival (mi)";
            object_id = "tesla_active_route_distance_to_arrival_mi";
            unique_id = "teslamate_1_active_route_distance_to_arrival_mi";
            availability = [{
              topic = "teslamate/cars/1/active_route";
              value_template = "{{ 'offline' if value_json.error else 'online' }}";
            }];
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/active_route";
            value_template = "{% if not value_json.error and value_json.miles_to_arrival %}\n  {{ value_json.miles_to_arrival }}\n{% endif %}\n";
            unit_of_measurement = "mi";
            icon = "mdi:map-marker-distance";
          };
        }
        {
          sensor = {
            name = "Active route minutes to arrival";
            object_id = "tesla_active_route_minutes_to_arrival";
            unique_id = "teslamate_1_active_route_minutes_to_arrival";
            availability = [{
              topic = "teslamate/cars/1/active_route";
              value_template = "{{ 'offline' if value_json.error else 'online' }}";
            }];
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/active_route";
            value_template = "{% if not value_json.error and value_json.minutes_to_arrival %}\n  {{ value_json.minutes_to_arrival }}\n{% endif %}\n";
            unit_of_measurement = "min";
            icon = "mdi:clock-outline";
          };
        }
        {
          sensor = {
            name = "Active route traffic minutes delay";
            object_id = "tesla_active_route_traffic_minutes_delay";
            unique_id = "teslamate_1_active_route_traffic_minutes_delay";
            availability = [{
              topic = "teslamate/cars/1/active_route";
              value_template = "{{ 'offline' if value_json.error else 'online' }}";
            }];
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/active_route";
            value_template = "{% if not value_json.error and value_json.traffic_minutes_delay %}\n  {{ value_json.traffic_minutes_delay }}\n{% endif %}\n";
            unit_of_measurement = "min";
            icon = "mdi:clock-alert-outline";
          };
        }
        {
          binary_sensor = {
            name = "Healthy";
            object_id = "tesla_healthy";
            unique_id = "teslamate_1_healthy";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/healthy";
            payload_on = "true";
            payload_off = "false";
            icon = "mdi:heart-pulse";
          };
        }
        {
          binary_sensor = {
            name = "Update Available";
            object_id = "tesla_update_available";
            unique_id = "teslamate_1_update_available";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/update_available";
            payload_on = "true";
            payload_off = "false";
            icon = "mdi:alarm";
          };
        }
        {
          binary_sensor = {
            name = "Locked";
            object_id = "tesla_locked";
            unique_id = "teslamate_1_locked";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            device_class = "lock";
            state_topic = "teslamate/cars/1/locked";
            payload_on = "false";
            payload_off = "true";
          };
        }
        {
          binary_sensor = {
            name = "Sentry Mode";
            object_id = "tesla_sentry_mode";
            unique_id = "teslamate_1_sentry_mode";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/sentry_mode";
            payload_on = "true";
            payload_off = "false";
            icon = "mdi:cctv";
          };
        }
        {
          binary_sensor = {
            name = "Windows Open";
            object_id = "tesla_windows_open";
            unique_id = "teslamate_1_windows_open";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            device_class = "window";
            state_topic = "teslamate/cars/1/windows_open";
            payload_on = "true";
            payload_off = "false";
            icon = "mdi:car-door";
          };
        }
        {
          binary_sensor = {
            name = "Doors Open";
            object_id = "tesla_doors_open";
            unique_id = "teslamate_1_doors_open";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            device_class = "door";
            state_topic = "teslamate/cars/1/doors_open";
            payload_on = "true";
            payload_off = "false";
            icon = "mdi:car-door";
          };
        }
        {
          binary_sensor = {
            name = "Trunk Open";
            object_id = "tesla_trunk_open";
            unique_id = "teslamate_1_trunk_open";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            device_class = "opening";
            state_topic = "teslamate/cars/1/trunk_open";
            payload_on = "true";
            payload_off = "false";
            icon = "mdi:car-side";
          };
        }
        {
          binary_sensor = {
            name = "Frunk Open";
            object_id = "tesla_frunk_open";
            unique_id = "teslamate_1_frunk_open";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            device_class = "opening";
            state_topic = "teslamate/cars/1/frunk_open";
            payload_on = "true";
            payload_off = "false";
            icon = "mdi:car-side";
          };
        }
        {
          binary_sensor = {
            name = "Is User Present";
            object_id = "tesla_is_user_present";
            unique_id = "teslamate_1_is_user_present";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            device_class = "presence";
            state_topic = "teslamate/cars/1/is_user_present";
            payload_on = "true";
            payload_off = "false";
            icon = "mdi:human-greeting";
          };
        }
        {
          binary_sensor = {
            name = "Is Climate On";
            object_id = "tesla_is_climate_on";
            unique_id = "teslamate_1_is_climate_on";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/is_climate_on";
            payload_on = "true";
            payload_off = "false";
            icon = "mdi:fan";
          };
        }
        {
          binary_sensor = {
            name = "Is Preconditioning";
            object_id = "tesla_is_preconditioning";
            unique_id = "teslamate_1_is_preconditioning";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            state_topic = "teslamate/cars/1/is_preconditioning";
            payload_on = "true";
            payload_off = "false";
            icon = "mdi:fan";
          };
        }
        {
          binary_sensor = {
            name = "Plugged In";
            object_id = "tesla_plugged_in";
            unique_id = "teslamate_1_plugged_in";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            device_class = "plug";
            state_topic = "teslamate/cars/1/plugged_in";
            payload_on = "true";
            payload_off = "false";
            icon = "mdi:ev-station";
          };
        }
        {
          binary_sensor = {
            name = "Charge Port Door OPEN";
            object_id = "tesla_charge_port_door_open";
            unique_id = "teslamate_1_charge_port_door_open";
            device = {
              identifiers = [ "teslamate_car_1" ];
              configuration_url = teslamateURL;
              manufacturer = "Tesla";
              model = teslaModel;
            };
            device_class = "opening";
            state_topic = "teslamate/cars/1/charge_port_door_open";
            payload_on = "true";
            payload_off = "false";
            icon = "mdi:ev-plug-tesla";
          };
        }
      ];
    };
    extraComponents = [
      "otbr"
      "litterrobot"
      "apple_tv"
      "homekit"
      "homekit_controller"
      "spotify"
      "hue"
      "sonos"
      "tplink"
      "ollama"
      "wyoming"
      "whisper"
      "piper"
      "isal"
      "radarr"
      "sonarr"
      "mqtt"
      "mqtt_eventstream"
      "mqtt_json"
      "mqtt_room"
      "mqtt_statestream"
      "github"
      "webostv"
      "reolink"
      "onvif"
    ];
    # extraPackages = python3Packages: with python3Packages; [
    #   pkgs.openai-whisper
    #   openai-whisper
    #   pkgs.piper-tts
    #   pkgs.wyoming-piper
    # ];
  };

  services.mosquitto = {
    enable = true;
    listeners = [
      {
        address = "127.0.0.1";
        acl = [ "pattern readwrite #" ];
        omitPasswordAuth = true;
        settings.allow_anonymous = true;
      }
    ];
  };

  architect.vhost.${domain} = with config.architect.networks; {
    dnsInterfaces = [ "tailscale" "lan" ];

    locations."/" = {
      port = config.services.home-assistant.config.http.server_port;
      allowWAN = true;
      allowLan = true;
      proxyWebsockets = true;
      allow = [
        tailscale.net
      ];
    };
  };
}