homeassistant:
  name: Maple House
  latitude: 43.6532
  longitude: -79.3832
  elevation: 76
  unit_system: metric
  currency: CAD
  country: CA
  time_zone: America/Toronto
  external_url: "https://home.example.net"
  internal_url: "http://192.168.1.20:8123"
  packages: !include_dir_named packages

default_config:

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

frontend:
  themes: !include_dir_merge_named themes

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

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

history:

logbook:
  exclude:
    entities:
      - sensor.date
      - sensor.time

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
      availability_topic: "home/basement/status"
      payload_available: "online"
      payload_not_available: "offline"

    - 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

  switch:
    - name: "Garage Workbench"
      unique_id: garage_workbench
      command_topic: "home/garage/workbench/set"
      state_topic: "home/garage/workbench/state"
      payload_on: "ON"
      payload_off: "OFF"
      retain: true

template:
  - sensor:
      - name: "Average Indoor Temperature"
        unique_id: average_indoor_temperature
        unit_of_measurement: "°C"
        device_class: temperature
        state_class: measurement
        state: >-
          {% set temperatures = [
            states('sensor.living_room_temperature') | float(none),
            states('sensor.bedroom_temperature') | float(none),
            states('sensor.basement_temperature') | float(none)
          ] | reject('eq', none) | list %}
          {{ (temperatures | average) | round(1) if temperatures else 'unknown' }}

  - binary_sensor:
      - name: "Any Exterior Door Open"
        unique_id: any_exterior_door_open
        device_class: door
        state: >-
          {{ is_state('binary_sensor.front_door', 'on')
             or is_state('binary_sensor.back_door', 'on')
             or is_state('binary_sensor.garage_entry_door', 'on') }}

input_boolean:
  guest_mode:
    name: Guest Mode
    icon: mdi:account-multiple

  vacation_mode:
    name: Vacation Mode
    icon: mdi:airplane

input_number:
  night_temperature:
    name: Night Temperature
    min: 16
    max: 22
    step: 0.5
    unit_of_measurement: "°C"
    icon: mdi:thermometer

input_datetime:
  weekday_wake_time:
    name: Weekday Wake Time
    has_date: false
    has_time: true

timer:
  garage_light:
    name: Garage Light Timer
    duration: "00:15:00"

automation:
  - id: exterior_lights_at_sunset
    alias: "Exterior lights at sunset"
    description: "Turn on exterior lights shortly before sunset."
    mode: single
    triggers:
      - trigger: sun
        event: sunset
        offset: "-00:20:00"
    conditions:
      - condition: state
        entity_id: input_boolean.vacation_mode
        state: "off"
    actions:
      - action: light.turn_on
        target:
          entity_id:
            - light.front_porch
            - light.driveway
        data:
          brightness_pct: 65
          transition: 5

  - id: exterior_lights_off_at_night
    alias: "Exterior lights off at night"
    mode: single
    triggers:
      - trigger: time
        at: "23:30:00"
    actions:
      - action: light.turn_off
        target:
          entity_id:
            - light.front_porch
            - light.driveway
        data:
          transition: 5

  - id: garage_motion_lighting
    alias: "Garage motion lighting"
    mode: restart
    triggers:
      - trigger: state
        entity_id: binary_sensor.garage_motion
        to: "on"
    conditions:
      - condition: numeric_state
        entity_id: sensor.garage_illuminance
        below: 40
    actions:
      - action: light.turn_on
        target:
          entity_id: light.garage
      - action: timer.start
        target:
          entity_id: timer.garage_light

  - id: garage_light_timer_finished
    alias: "Garage light timer finished"
    mode: restart
    triggers:
      - trigger: event
        event_type: timer.finished
        event_data:
          entity_id: timer.garage_light
    conditions:
      - condition: state
        entity_id: binary_sensor.garage_motion
        state: "off"
    actions:
      - action: light.turn_off
        target:
          entity_id: light.garage

  - id: leak_alert
    alias: "Water leak alert"
    mode: parallel
    max: 5
    triggers:
      - trigger: state
        entity_id:
          - binary_sensor.kitchen_sink_leak
          - binary_sensor.laundry_leak
          - binary_sensor.water_heater_leak
        to: "on"
    actions:
      - action: notify.mobile_app_primary_phone
        data:
          title: "Water leak detected"
          message: >-
            {{ trigger.to_state.name }} detected moisture at
            {{ now().strftime('%H:%M') }}.
          data:
            priority: high
            ttl: 0
      - action: switch.turn_off
        target:
          entity_id: switch.main_water_valve

  - id: night_climate_setback
    alias: "Night climate setback"
    mode: single
    triggers:
      - trigger: time
        at: "22:30:00"
    conditions:
      - condition: state
        entity_id: input_boolean.guest_mode
        state: "off"
    actions:
      - action: climate.set_temperature
        target:
          entity_id: climate.main_floor
        data:
          temperature: "{{ states('input_number.night_temperature') | float(18.5) }}"

script:
  good_night:
    alias: "Good night"
    icon: mdi:weather-night
    mode: single
    sequence:
      - action: light.turn_off
        target:
          area_id:
            - kitchen
            - living_room
            - basement
      - action: lock.lock
        target:
          entity_id:
            - lock.front_door
            - lock.back_door
      - action: alarm_control_panel.alarm_arm_home
        target:
          entity_id: alarm_control_panel.home
      - action: climate.set_temperature
        target:
          entity_id: climate.main_floor
        data:
          temperature: "{{ states('input_number.night_temperature') | float(18.5) }}"

scene:
  - id: movie_night
    name: Movie Night
    entities:
      light.living_room_ceiling:
        state: "off"
      light.living_room_lamp:
        state: "on"
        brightness: 64
        color_temp_kelvin: 2700
      media_player.living_room_tv:
        state: "on"

notify:
  - platform: group
    name: household
    services:
      - service: mobile_app_primary_phone
      - service: mobile_app_secondary_phone

sensor: !include sensors.yaml
binary_sensor: !include binary_sensors.yaml
group: !include groups.yaml
rest_command: !include rest_commands.yaml