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://homeassistant.local:8123"

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

recorder:
  db_url: !secret database_url
  purge_keep_days: 14
  auto_purge: true
  commit_interval: 5
  exclude:
    entity_globs:
      - sensor.*_rssi
      - sensor.*_linkquality

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

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

  binary_sensor:
    - name: "Garage Side Door"
      unique_id: garage_side_door
      state_topic: "home/garage/side-door"
      payload_on: "OPEN"
      payload_off: "CLOSED"
      device_class: door

  switch:
    - name: "Patio String Lights"
      unique_id: patio_string_lights
      command_topic: "home/patio/lights/set"
      state_topic: "home/patio/lights/state"
      payload_on: "ON"
      payload_off: "OFF"
      optimistic: false
      retain: true

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

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: 23
    step: 0.5
    unit_of_measurement: "°C"
    icon: mdi:thermometer

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

group:
  family:
    name: Family
    entities:
      - person.alex
      - person.sam

scene: !include scenes.yaml
script: !include scripts.yaml

automation:
  - id: entry_lights_after_sunset
    alias: "Entry lights after sunset"
    description: "Turn on the entry lights when the front door opens after dark."
    mode: restart
    trigger:
      - platform: state
        entity_id: binary_sensor.front_door
        from: "off"
        to: "on"
    condition:
      - condition: sun
        after: sunset
        after_offset: "-00:20:00"
      - condition: state
        entity_id: light.entry
        state: "off"
    action:
      - service: light.turn_on
        target:
          entity_id: light.entry
        data:
          brightness_pct: 70
          transition: 1
      - delay: "00:05:00"
      - condition: state
        entity_id: binary_sensor.front_door
        state: "off"
      - service: light.turn_off
        target:
          entity_id: light.entry
        data:
          transition: 3

  - id: lower_temperature_at_night
    alias: "Lower temperature at night"
    mode: single
    trigger:
      - platform: time
        at: input_datetime.quiet_hours_start
    condition:
      - condition: state
        entity_id: input_boolean.guest_mode
        state: "off"
      - condition: state
        entity_id: input_boolean.vacation_mode
        state: "off"
    action:
      - service: climate.set_temperature
        target:
          entity_id: climate.main_floor
        data:
          temperature: "{{ states('input_number.night_temperature') | float(18.5) }}"

  - id: notify_water_leak
    alias: "Notify on water leak"
    mode: parallel
    max: 5
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.kitchen_sink_leak
          - binary_sensor.laundry_room_leak
          - binary_sensor.water_heater_leak
        from: "off"
        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: vacation_presence_simulation
    alias: "Vacation presence simulation"
    mode: single
    trigger:
      - platform: sun
        event: sunset
        offset: "00:15:00"
    condition:
      - condition: state
        entity_id: input_boolean.vacation_mode
        state: "on"
    action:
      - delay:
          minutes: "{{ range(5, 35) | random }}"
      - service: light.turn_on
        target:
          entity_id:
            - light.living_room
            - light.kitchen
        data:
          brightness_pct: "{{ range(35, 75) | random }}"
      - delay:
          minutes: "{{ range(90, 180) | random }}"
      - service: light.turn_off
        target:
          entity_id:
            - light.living_room
            - light.kitchen