homeassistant:
  name: Maple House
  latitude: !secret home_latitude
  longitude: !secret home_longitude
  elevation: 112
  unit_system: metric
  currency: CAD
  country: CA
  time_zone: America/Toronto
  internal_url: "http://homeassistant.local:8123"
  external_url: !secret external_url
  allowlist_external_dirs:
    - /config/www
    - /media

default_config:

frontend:
  themes: !include_dir_merge_named themes

http:
  use_x_forwarded_for: true
  trusted_proxies:
    - 172.18.0.0/16
  ip_ban_enabled: true
  login_attempts_threshold: 5

logger:
  default: warning
  logs:
    homeassistant.components.automation: info
    homeassistant.components.mqtt: info

recorder:
  db_url: !secret recorder_db_url
  purge_keep_days: 14
  auto_purge: true
  commit_interval: 5
  exclude:
    domains:
      - camera
      - update
    entity_globs:
      - sensor.*_rssi
      - sensor.*_linkquality

history:
  include:
    domains:
      - climate
      - light
      - sensor
      - switch

mqtt:
  sensor:
    - name: "Basement Temperature"
      unique_id: basement_temperature
      state_topic: "home/basement/climate"
      value_template: "{{ value_json.temperature }}"
      unit_of_measurement: "°C"
      device_class: temperature
      state_class: measurement
      expire_after: 600
    - name: "Basement Humidity"
      unique_id: basement_humidity
      state_topic: "home/basement/climate"
      value_template: "{{ value_json.humidity }}"
      unit_of_measurement: "%"
      device_class: humidity
      state_class: measurement
      expire_after: 600

template:
  - binary_sensor:
      - name: "Someone Home"
        unique_id: someone_home
        state: >
          {{ is_state('person.alex', 'home')
             or is_state('person.sam', 'home') }}
  - sensor:
      - name: "Average Indoor Temperature"
        unique_id: average_indoor_temperature
        unit_of_measurement: "°C"
        device_class: temperature
        state_class: measurement
        state: >
          {% set sensors = [
            states('sensor.living_room_temperature'),
            states('sensor.bedroom_temperature'),
            states('sensor.basement_temperature')
          ] | map('float', none) | reject('eq', none) | list %}
          {{ (sensors | average) | round(1) if sensors else 'unknown' }}

input_boolean:
  guest_mode:
    name: Guest Mode
    icon: mdi:account-multiple
  vacation_mode:
    name: Vacation Mode
    icon: mdi:airplane

input_number:
  evening_brightness:
    name: Evening Brightness
    min: 10
    max: 100
    step: 5
    unit_of_measurement: "%"
    icon: mdi:brightness-6

input_datetime:
  quiet_hours_start:
    name: Quiet Hours Start
    has_date: false
    has_time: true
  quiet_hours_end:
    name: Quiet Hours End
    has_date: false
    has_time: true

automation:
  - id: porch_light_at_sunset
    alias: "Exterior: Turn on porch light at sunset"
    mode: single
    trigger:
      - platform: sun
        event: sunset
        offset: "-00:15:00"
    condition:
      - condition: state
        entity_id: input_boolean.vacation_mode
        state: "off"
    action:
      - service: light.turn_on
        target:
          entity_id: light.front_porch
        data:
          brightness_pct: 65
          color_temp_kelvin: 2700

  - id: porch_light_off_at_night
    alias: "Exterior: Turn off porch light at night"
    mode: single
    trigger:
      - platform: time
        at: "23:30:00"
    action:
      - service: light.turn_off
        target:
          entity_id: light.front_porch

  - id: hallway_motion_lighting
    alias: "Hallway: Motion lighting after dark"
    mode: restart
    trigger:
      - platform: state
        entity_id: binary_sensor.hallway_motion
        to: "on"
    condition:
      - condition: sun
        after: sunset
        before: sunrise
    action:
      - service: light.turn_on
        target:
          entity_id: light.hallway
        data:
          brightness_pct: >
            {{ 20 if now().hour >= 23 or now().hour < 6 else
               states('input_number.evening_brightness') | int(60) }}
          transition: 1
      - wait_for_trigger:
          - platform: state
            entity_id: binary_sensor.hallway_motion
            to: "off"
            for: "00:02:00"
        timeout: "00:10:00"
      - service: light.turn_off
        target:
          entity_id: light.hallway
        data:
          transition: 3

  - id: leak_detected_alert
    alias: "Safety: Water leak detected"
    mode: parallel
    max: 5
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.kitchen_sink_leak
          - binary_sensor.water_heater_leak
          - binary_sensor.laundry_room_leak
        to: "on"
    action:
      - service: notify.mobile_app_alex_phone
        data:
          title: "Water leak detected"
          message: >
            {{ trigger.to_state.name }} detected water at
            {{ now().strftime('%H:%M') }}.
          data:
            tag: water-leak
            priority: high
      - service: switch.turn_off
        target:
          entity_id: switch.main_water_valve

  - id: low_temperature_warning
    alias: "Safety: Low indoor temperature warning"
    mode: single
    trigger:
      - platform: numeric_state
        entity_id: sensor.average_indoor_temperature
        below: 12
        for: "00:10:00"
    action:
      - service: notify.mobile_app_alex_phone
        data:
          title: "Low indoor temperature"
          message: >
            The average indoor temperature is
            {{ states('sensor.average_indoor_temperature') }} °C.

  - id: away_mode
    alias: "Presence: Set house to away"
    mode: restart
    trigger:
      - platform: state
        entity_id: binary_sensor.someone_home
        to: "off"
        for: "00:10:00"
    condition:
      - condition: state
        entity_id: input_boolean.guest_mode
        state: "off"
    action:
      - service: climate.set_temperature
        target:
          entity_id: climate.main_floor
        data:
          temperature: 17
      - service: light.turn_off
        target:
          area_id:
            - living_room
            - kitchen
            - basement
      - service: lock.lock
        target:
          entity_id:
            - lock.front_door
            - lock.back_door

  - id: return_home
    alias: "Presence: Prepare house on arrival"
    mode: restart
    trigger:
      - platform: state
        entity_id: binary_sensor.someone_home
        to: "on"
    action:
      - service: climate.set_temperature
        target:
          entity_id: climate.main_floor
        data:
          temperature: 21
      - condition: sun
        after: sunset
        before: sunrise
      - service: light.turn_on
        target:
          entity_id:
            - light.entryway
            - light.living_room
        data:
          brightness_pct: 55
          transition: 2

script:
  good_night:
    alias: Good Night
    icon: mdi:weather-night
    mode: single
    sequence:
      - service: light.turn_off
        target:
          area_id:
            - living_room
            - kitchen
            - basement
            - exterior
      - service: lock.lock
        target:
          entity_id:
            - lock.front_door
            - lock.back_door
      - service: climate.set_temperature
        target:
          entity_id: climate.main_floor
        data:
          temperature: 18
      - service: alarm_control_panel.alarm_arm_night
        target:
          entity_id: alarm_control_panel.house

scene:
  - id: movie_time
    name: Movie Time
    icon: mdi:movie-open
    entities:
      light.living_room:
        state: "on"
        brightness: 40
        color_temp_kelvin: 2400
      light.tv_backlight:
        state: "on"
        brightness: 100
        rgb_color:
          - 25
          - 50
          - 120
      media_player.living_room_tv:
        state: "on"

notify:
  - platform: group
    name: household
    services:
      - service: mobile_app_alex_phone
      - service: mobile_app_sam_phone

sensor:
  - platform: time_date
    display_options:
      - time
      - date

tts:
  - platform: google_translate
    language: en
    cache: true
    cache_dir: /config/tts
    time_memory: 300

media_source:

energy:

lovelace:
  mode: storage

automation ui: !include automations.yaml
script ui: !include scripts.yaml
scene ui: !include scenes.yaml