# Worx Landroid (M500 WR141E) package
# https://github.com/Barma-lej/halandroid
# Based on Landroid Bridge @MTrab virtualzone
# https://github.com/MTrab/landroid_cloud

# Cloud #############################################################
landroid_cloud:
  email: !secret landroid_mail
  password: !secret landroid_pass

# Switch ############################################################
switch:
  - platform: template
    switches:
      landroid_mowing:
        value_template: "{{ is_state_attr('sensor.landroid_hans_dieter_status', 'status_id', 7) }}"
        turn_on:
          service: script.landroid_start
        turn_off:
          service: script.landroid_stop

# Sensor ############################################################
# sensor.landroid_hans_dieter_battery
# sensor.landroid_hans_dieter_error
# sensor.landroid_hans_dieter_status

sensor:
  - platform: template
    sensors:

# Info ############
      landroid_ip:
        friendly_name: "IP Address"
        value_template: !secret landroid_ip
        icon_template: "mdi:ip-network"

      landroid_sn:
        friendly_name: "Landroid SN"
        value_template: !secret landroid_sn
        icon_template: "mdi:numeric"

      landroid_mac:
        friendly_name: "Landroid MAC"
        value_template: !secret landroid_mac
        icon_template: "mdi:barcode"

# Battery #########
#      landroid_batvoltage:
#        friendly_name: "Landroid BatVoltage"
#        value_template: "{{ state_attr('sensor.landroid_hans_dieter_battery', 'battery_voltage') }}"
#        icon_template: "mdi:battery-charging-100"
#        unit_of_measurement: "V"

#      landroid_battemp:
#        friendly_name: "Landroid BatTemp"
#        value_template: "{{ state_attr('sensor.landroid_hans_dieter_battery', 'battery_temperature') }}"
#        unit_of_measurement: "°C"
#        device_class: "temperature"

#      landroid_batchargecycles:
#        friendly_name: "Landroid batChargeCycles"
#        value_template: "{{ state_attr('sensor.landroid_hans_dieter_battery', 'charge_cycles') }}"
#        icon_template: "mdi:power-plug"

#      landroid_batcharging:
#        friendly_name: "Landroid BatCharging"
#        value_template: "{{ state_attr('sensor.landroid_hans_dieter_battery', 'charging') }}"

      landroid_bat:
        friendly_name: "Battery level"
        value_template: >-
          {% if is_state_attr('sensor.landroid_hans_dieter_battery', 'charging', 1) -%}Lädt
          {%- else -%}Entlädt
          {%- endif %}
        icon_template: >-
          {% if is_state_attr('sensor.landroid_hans_dieter_battery', 'charging', 1) -%}
            {%- if states('sensor.landroid_hans_dieter_battery')|float > 99 -%}mdi:battery-charging-100
            {%- elif states('sensor.landroid_hans_dieter_battery')|float > 89 -%}mdi:battery-charging-90
            {%- elif states('sensor.landroid_hans_dieter_battery')|float > 79 -%}mdi:battery-charging-80
            {%- elif states('sensor.landroid_hans_dieter_battery')|float > 69 -%}mdi:battery-charging-70
            {%- elif states('sensor.landroid_hans_dieter_battery')|float > 59 -%}mdi:battery-charging-60
            {%- elif states('sensor.landroid_hans_dieter_battery')|float > 49 -%}mdi:battery-charging-50
            {%- elif states('sensor.landroid_hans_dieter_battery')|float > 39 -%}mdi:battery-charging-40
            {%- elif states('sensor.landroid_hans_dieter_battery')|float > 29 -%}mdi:battery-charging-30
            {%- elif states('sensor.landroid_hans_dieter_battery')|float > 19 -%}mdi:battery-charging-20
            {%- elif states('sensor.landroid_hans_dieter_battery')|float > 9 -%}mdi:battery-charging-10
            {%- else -%}mdi:battery-charging-outline
            {%- endif %}
          {%- else -%}
            {%- if states('sensor.landroid_hans_dieter_battery')|float > 99 -%}mdi:battery
            {%- elif states('sensor.landroid_hans_dieter_battery')|float > 89 -%}mdi:battery-90
            {%- elif states('sensor.landroid_hans_dieter_battery')|float > 79 -%}mdi:battery-80
            {%- elif states('sensor.landroid_hans_dieter_battery')|float > 69 -%}mdi:battery-70
            {%- elif states('sensor.landroid_hans_dieter_battery')|float > 59 -%}mdi:battery-60
            {%- elif states('sensor.landroid_hans_dieter_battery')|float > 49 -%}mdi:battery-50
            {%- elif states('sensor.landroid_hans_dieter_battery')|float > 39 -%}mdi:battery-40
            {%- elif states('sensor.landroid_hans_dieter_battery')|float > 29 -%}mdi:battery-30
            {%- elif states('sensor.landroid_hans_dieter_battery')|float > 19 -%}mdi:battery-20
            {%- elif states('sensor.landroid_hans_dieter_battery')|float > 9 -%}mdi:battery-10
            {%- else -%}mdi:battery-outline
            {%- endif %}
          {%- endif %}


# Status ##########
      landroid_bladetime:
        friendly_name: "Landroid BladeTime"
        value_template: "{{ state_attr('sensor.landroid_hans_dieter_status', 'blade_time') }}"
        icon_template: "mdi:fan"
        unit_of_measurement: "min"

      landroid_bladetime_h:
        friendly_name: "Landroid BladeTime"
        value_template: >
          {% set t = states('sensor.landroid_bladetime') | int %}
          {% if t == 0 %}
            Unavailable
          {% elif t > 60 %}
            {{ t // 1440 }} Tg. {{ (t % 1440) // 60 }} Std. {{ t % 60 }} Min.
          {% endif %}
        icon_template: "mdi:fan"
        unit_of_measurement: ""

      landroid_totaltime:
        friendly_name: "Landroid TotalTime"
        value_template: "{{ state_attr('sensor.landroid_hans_dieter_status', 'work_time') }}"
        icon_template: "mdi:bus-clock"
        unit_of_measurement: "min"

      landroid_totaltime_h:
        friendly_name: "Landroid TotalTime"
        value_template: >
          {% set t = states('sensor.landroid_totaltime') | int %}
          {% if t == 0 %}
            Unavailable
          {% elif t > 60 %}
            {{ t // 1440 }} Tg. {{ (t % 1440) // 60 }} Std. {{ t % 60 }} Min.
          {% endif %}
        icon_template: "mdi:bus-clock"
        unit_of_measurement: ""

      landroid_dist:
        friendly_name: "Landroid Dist"
        value_template: "{{ state_attr('sensor.landroid_hans_dieter_status', 'distance') }}"
        icon_template: "mdi:map-marker-distance"
        unit_of_measurement: "m"

      landroid_dist_km:
        friendly_name: "Landroid Dist"
        value_template: "{{ (states('sensor.landroid_dist') | float) / 1000 }}"
        icon_template: "mdi:map-marker-distance"
        unit_of_measurement: "km"

      landroid_lastupdate:
        friendly_name: "Last update"
        value_template: "{{ as_timestamp(strptime( state_attr('sensor.landroid_hans_dieter_status', 'last_update'), '%H:%M:%S %d/%m/%Y')) | timestamp_custom('%d.%m.%Y %H:%M:%S') }}"
        icon_template: "mdi:clock"

# -55 or higher: 4 bars
# -56 to -66: 3 bars
# -67 to -77: 2 bars
# -78 to -88: 1 bar
# -89 or lower: 0 bars
      landroid_wifi:
        friendly_name: "Wifi quality"
        value_template: "{{ state_attr('sensor.landroid_hans_dieter_status', 'rssi') }}"
        icon_template: >-
          {% if state_attr('sensor.landroid_hans_dieter_status', 'rssi')|float > -56 -%}mdi:wifi-strength-4
          {% elif state_attr('sensor.landroid_hans_dieter_status', 'rssi')|float > -67 -%}mdi:wifi-strength-3
          {% elif state_attr('sensor.landroid_hans_dieter_status', 'rssi')|float > -78 -%}mdi:wifi-strength-2
          {% elif state_attr('sensor.landroid_hans_dieter_status', 'rssi')|float > -89 -%}mdi:wifi-strength-1
          {%- else -%}mdi:wifi-strength-outline
          {%- endif %}
        unit_of_measurement: "dBm"

      landroid_yaw:
        friendly_name: "Landroid Yaw"
        value_template: "{{ state_attr('sensor.landroid_hans_dieter_status', 'yaw') }}"
        icon_template: "mdi:axis-z-rotate-clockwise"
        unit_of_measurement: "°"

      landroid_roll:
        friendly_name: "Landroid Roll"
        value_template: "{{ state_attr('sensor.landroid_hans_dieter_status', 'roll') }}"
        icon_template: "mdi:axis-x-rotate-clockwise"
        unit_of_measurement: "°"

      landroid_pitch:
        friendly_name: "Landroid Pitch"
        value_template: "{{ state_attr('sensor.landroid_hans_dieter_status', 'pitch') }}"
        icon_template: "mdi:seat-flat-angled"
        unit_of_measurement: "°"

# Input Boolean #####################################################
input_boolean:
  landroid_raindelay_settings:
    name: Raindelay Settings
    icon: "mdi:file-hidden"
    initial: false
  landroid_timext_settings:
    name: TimeExt Settings
    icon: "mdi:file-hidden"
    initial: false
  landroid_sched_settings:
    name: Scheduler Settings
    icon: "mdi:file-hidden"
    initial: false
  landroid_info_toggle:
    name: Info toggle
    icon: "mdi:file-hidden"
    initial: false

# Automations #######################################################
automation:
#  - id: "landroid_status_notify"
#    alias: "Landroid Status Notification"
#    initial_state: true
#    trigger:
#    - platform: state
#      entity_id: sensor.landroid_hans_dieter_status
#    condition:
#    - condition: template
#      value_template: "{{ trigger.to_state.state != trigger.from_state.state }}"
#    action:
#    - service: persistent_notification.create
#      data_template:
#        title: Landroid Status
#        message: "{{ trigger.from_state.state }} -> {{ trigger.to_state.state }} - {{ states('sensor.date_time') }}"
#
  - id: "landroid_error_notify"
    alias: "Landroid Error Notification"
    initial_state: true
    trigger:
    - platform: state
      entity_id: sensor.landroid_hans_dieter_error
    condition:
    - condition: template
      value_template: "{{ trigger.to_state.state != trigger.from_state.state }}"
    action:
    - service: persistent_notification.create
      data_template:
        title: Landroid Status
        message: "{{ trigger.from_state.state }} -> {{ trigger.to_state.state }} - {{ states('sensor.date_time') }}"

# Scripts ###########################################################
script:
# landroid_cloud.start
# landroid_cloud.stop
# landroid_cloud.pause

# Starts the mower
  landroid_start:
    alias: "Start the mower"
    sequence:
      service: landroid_cloud.start
      data:
        id: 473913

# Stops the mower and sends it home
  landroid_stop:
    alias: "Stop the mower"
    sequence:
      service: landroid_cloud.stop
      data:
        id: 473913

# Pause the mower
  landroid_pause:
    alias: "Pause the mower"
    sequence:
      service: landroid_cloud.pause
      data:
        id: 473913

# Customize #########################################################
homeassistant:
  customize:
# Sensor ##########
    sensor.landroid_hans_dieter_battery:
      friendly_name: "Akkuladung"
    sensor.landroid_hans_dieter_error:
      friendly_name: "Fehler"
    sensor.landroid_hans_dieter_status:
      friendly_name: "Status"

    sensor.landroid_ip:
      friendly_name: "IP-Adresse"
    sensor.landroid_sn:
      friendly_name: "Seriennummer"
    sensor.landroid_mac:
      friendly_name: "MAC-Adresse"

#    sensor.landroid_batvoltage:
#      friendly_name: "Akkuspannung"
#    sensor.landroid_battemp:
#      friendly_name: "Akkutemperatur"
#    sensor.landroid_batchargecycles:
#      friendly_name: "Akkuladungen"
#    sensor.landroid_batcharging:
#      friendly_name: "Akkustatus"
    sensor.landroid_bat:
      friendly_name: "Akkuladung"

    sensor.landroid_bladetime:
      friendly_name: "Klingenarbeitszeit"
    sensor.landroid_bladetime_h:
      friendly_name: "Klingenarbeitszeit"
    sensor.landroid_totaltime:
      friendly_name: "Arbeitszeit"
    sensor.landroid_totaltime_h:
      friendly_name: "Arbeitszeit"
    sensor.landroid_dist:
      friendly_name: "Ges. Distanz"
    sensor.landroid_dist_km:
      friendly_name: "Ges. Distanz"
    sensor.landroid_lastupdate:
      friendly_name: "Aktualisiert seit"
      icon: "mdi:update"
    sensor.landroid_wifi:
      friendly_name: "WLAN Qualität"
    sensor.landroid_pitch:
      friendly_name: "Nicken/Тангаж"
    sensor.landroid_roll:
      friendly_name: "Rollen/Крен"
    sensor.landroid_yaw:
      friendly_name: "Gieren/Рысканье"

# Input Boolean ###
    input_boolean.landroid_raindelay_settings:
      friendly_name: "Regenverzögerung"
    input_boolean.landroid_timext_settings:
      friendly_name: "Zeiterhöhung"
    input_boolean.landroid_sched_settings:
      friendly_name: "Mähplan"
    input_boolean.landroid_info_toggle:
      friendly_name: "Information"

# Automation ######
    automation.landroid_status_notification:
      friendly_name: "Landroid Status Benachrichtigung"
      icon: "mdi:bell"
    automation.landroid_error_notification:
      friendly_name: "Landroid Fehler Benachrichtigung"
      icon: "mdi:bell"

# Scripts #########
    script.landroid_start:
      friendly_name: "Mähen"
      icon: "mdi:play"
    script.landroid_pause:
      friendly_name: "Mähen pausieren"
      icon: "mdi:home"
    script.landroid_stop:
      friendly_name: "Mähen beenden"
      icon: "mdi:home"
