wxt9861 / esxi_stats

ESXi component for Home Assistant
MIT License
145 stars 40 forks source link

Added VM's proper name and the host it runs on #87

Closed ecoen66 closed 7 months ago

ecoen66 commented 8 months ago

Added additional attributes: VM's proper name (for calling esxi_stats.vm_power), and the host name of the VMware host it is running on (enables searching for running VMs by host, allowing to shutdown the VMs prior to shutting down a host server by IMC or PDU).

ecoen66 commented 8 months ago

Here's the script I use to shutdown all running VMs on a host:

alias: Toggle VM host
mode: single
description: Shutdown all running VMs on a host
fields:
  sensor_entity:
    description: The sensor.esxi_vmhost to work with
    example: sensor.esxi_vmhost_esxi222_home_lan
variables:
  host_name: |
    {{ state_attr(sensor_entity,'name') }}
  vm_names: |
    {{ states | selectattr('attributes.host_name', 'search', host_name)
      | map(attribute='attributes.vm_name') | list }}
sequence:
  - repeat:
      for_each: "{{ vm_names }}"
      sequence:
        - service: esxi_stats.vm_power
          data_template:
            host: 192.168.1.220
            vm: "{{ repeat.item }}"
            command: shutdown
  - wait_template: |
      {{ states | selectattr('attributes.host_name', 'search', host_name)
        | map(attribute='attributes.vm_name') | list | count < 1}}
wxt9861 commented 8 months ago

Hi @ecoen66 Thank you for your contribution.

A few things:

  1. Can you please change this to go into service_calls branch? 0.7 beta releases are based on that branch.
  2. 0.7 beta already returns host_name attribute
ecoen66 commented 8 months ago

Thanks. I removed my host_name, but kept vm_name as the proper, actionable name for the VM.

ecoen66 commented 8 months ago

Unfortunately, having merged my code with service_calls branch? 0.7, my integration to vCenter no longer works at all. I now get the dreaded cannot access local variable 'host_power_policy' where it is not associated with a value

ecoen66 commented 8 months ago

Fixed the "host_power_policy" issue.

ecoen66 commented 8 months ago

More issues. Now cannot call service: esxi_stats.vm_power... VM UUID not found

I'm not digging this branch of code...

ecoen66 commented 8 months ago

Ok, that was a PITA. I didn't realize that you had changed the service: esxi_stats.vm_power to use the mangled VM name rather than the proper name as you had in the master branch. Sorry for publishing so many updates here while debugging.

ecoen66 commented 8 months ago

My script now reads:

alias: Toggle VM host
mode: single
description: Shutdown all running VMs on a host
fields:
  sensor_entity:
    description: The sensor.esxi_vmhost to work with
    example: sensor.esxi_vmhost_esxi222_home_lan
variables:
  host_name: |
    {{ state_attr(sensor_entity,'name') }}
  vm_names: |
    {{ states | selectattr('attributes.host_name', 'search', host_name)
      | rejectattr('attributes.state', '==', 'off') | map(attribute='attributes.name') | list }}
sequence:
  - repeat:
      for_each: "{{ vm_names }}"
      sequence:
        - service: esxi_stats.vm_power
          data_template:
            host: 192.168.1.220
            vm: "{{ repeat.item }}"
            command: shutdown
  - wait_template: |
      {{ states | selectattr('attributes.host_name', 'search', host_name)
        | rejectattr('attributes.state', '==', 'off') | map(attribute='attributes.name') | list | count < 1}}
wxt9861 commented 8 months ago

The latest example script you provided does not use the new attribute you created "vm_name", but instead uses the existing attribute "name". Is that a typo?

ecoen66 commented 8 months ago

No, it's not a typo. That change in the script was due to the fact that you had changed the service: esxi_stats.vm_power to use the mangled VM name rather than the proper name as you had in the master branch. I do still use the new vm_name attribute in the Lovelace UI.