wxt9861 / esxi_stats

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

Can't start or stop virtual machine #66

Closed megapearl closed 1 year ago

megapearl commented 2 years ago

Hi,

As I didn't have this problem before with vCenter, I do have it when configured the ESXi host directly. When I start or stop a virtual machine I got this error:

This error originated from a custom integration.

Logger: homeassistant.helpers.script.shed_macos
Source: custom_components/esxi_stats/__init__.py:321
Integration: ESXi Stats (documentation, issues)
First occurred: 10:52:43 (1 occurrences)
Last logged: 10:52:43

Shed MacOS: Error executing script. Unexpected error for call_service at pos 1: VM UUID not found
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 367, in _async_step
    await getattr(self, handler)()
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 570, in _async_call_service_step
    await service_task
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1636, in async_call
    task.result()
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1673, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/home/homeassistant/.homeassistant/custom_components/esxi_stats/__init__.py", line 338, in vm_power
    vm_uuid = async_get_vm_details(vm_name)
  File "/home/homeassistant/.homeassistant/custom_components/esxi_stats/__init__.py", line 321, in async_get_vm_details
    raise ValueError("VM UUID not found")
ValueError: VM UUID not found

and

Logger: homeassistant.components.websocket_api.http.connection
Source: custom_components/esxi_stats/__init__.py:321
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 10:52:43 (1 occurrences)
Last logged: 10:52:43

[547075532208] VM UUID not found
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/components/websocket_api/commands.py", line 192, in handle_call_service
    await hass.services.async_call(
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1636, in async_call
    task.result()
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1673, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/entity_component.py", line 204, in handle_service
    await self.hass.helpers.service.entity_service_call(
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/service.py", line 668, in entity_service_call
    future.result()  # pop exception if have
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/entity.py", line 931, in async_request_call
    await coro
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/service.py", line 705, in _handle_entity_call
    await result
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/components/template/switch.py", line 159, in async_turn_on
    await self._on_script.async_run(context=self._context)
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 1243, in async_run
    await asyncio.shield(run.async_run())
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 349, in async_run
    await self._async_step(log_exceptions=False)
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 367, in _async_step
    await getattr(self, handler)()
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 570, in _async_call_service_step
    await service_task
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1636, in async_call
    task.result()
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1673, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/home/homeassistant/.homeassistant/custom_components/esxi_stats/__init__.py", line 338, in vm_power
    vm_uuid = async_get_vm_details(vm_name)
  File "/home/homeassistant/.homeassistant/custom_components/esxi_stats/__init__.py", line 321, in async_get_vm_details
    raise ValueError("VM UUID not found")
ValueError: VM UUID not found

configuration.yaml snippet:

switch:
      shed_macos:
        friendly_name: Shed MacOS
        value_template: '{{ (is_state("sensor.esxi_vm_macos", "running")) }}'
        icon_template: >-
          {% if is_state("sensor.esxi_vm_macos", "running"), "running" %}
            mdi:server-network
          {% else %}
            mdi:server-network-off
          {% endif %}
        turn_on:
          service: esxi_stats.vm_power
          data:
            host: 'esxi-fileserver.flissinger.local'
            vm: 'MacOS'
            command: 'on'
        turn_off:
          service: esxi_stats.vm_power
          data:
            host: 'esxi-fileserver.flissinger.local'
            vm: 'MacOS'
            command: 'shutdown'

How to troubleshoot?

megapearl commented 2 years ago

I reinstalled your component from the 'master' branch, configured the esxi servers again and problem is gone now.

wxt9861 commented 2 years ago

Can you help me get a bit of info on this. Maybe this is a bug.

You have a vCenter cluster and when the component was connected via vCenter everything is working fine. However, when you configure the component to talk to the host directly you are having issue. Am I getting this correct?

Is the host and VM still part of a vCenter cluster?

megapearl commented 2 years ago

That's right and yes the host and VM are still part of a vCenter cluster.

I changed from vCenter to ESXi directly, because vCenter itself is running as VM in ESXi, so if that one is down I can't control the other VM's via homeassistant. So I removed the component via HACS, and removed everything from configuration.yaml, and started over by reinstalling via HACS en configuring again via Configuration -> Devices & Services -> Add Integration -> ESXi Stats. Maybe there where some files cached locally from the old setup I don't know. I can't reproduce it anymore since I reinstalled all stuff, but if I can test some stuff for you please let me know.

image
wxt9861 commented 2 years ago

Thank you. I will investigate.

megapearl commented 2 years ago

The problem returned, how to debug? Using v0.7.0beta 2 of you component.

[547045256736] VM UUID not found Traceback (most recent call last): File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/components/websocket_api/commands.py", line 193, in handle_call_service await hass.services.async_call( File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1634, in async_call task.result() File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1671, in _execute_service await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)( File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/entity_component.py", line 204, in handle_service await service.entity_service_call( File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/service.py", line 677, in entity_service_call future.result() # pop exception if have File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/entity.py", line 955, in async_request_call await coro File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/service.py", line 714, in _handle_entity_call await result File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/components/template/switch.py", line 159, in async_turn_on await self._on_script.async_run(context=self._context) File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 1275, in async_run await asyncio.shield(run.async_run()) File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 359, in async_run await self._async_step(log_exceptions=False) File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 379, in _async_step await getattr(self, handler)() File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 582, in _async_call_service_step await service_task File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1634, in async_call task.result() File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1671, in _execute_service await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)( File "/home/homeassistant/.homeassistant/custom_components/esxi_stats/__init__.py", line 338, in vm_power vm_uuid = async_get_vm_details(vm_name) File "/home/homeassistant/.homeassistant/custom_components/esxi_stats/__init__.py", line 321, in async_get_vm_details raise ValueError("VM UUID not found") ValueError: VM UUID not found

wxt9861 commented 2 years ago

It looks like using UUID as identifier is not reliable when a host is part of vCenter. I will take a closer look at how vCenter handles unique IDs for VMs so we can handle vCenter differently than standalone hosts. Time is a little tight right now but hope to have something soon.

TheGabeMan commented 2 years ago

Not quite sure if it is a bug or expected vCenter / ESXi behaviour that @megapearl didn't anticipate on.

When "asking" vCenter or ESXi directly for a VM, they are seen as different VMs. For example when VEEAM Image level backup makes backups of a VM through vCenter, it can't match the VM when you try to backup it through ESXi directly.

megapearl commented 2 years ago

I have the same problem 'VM UUID' not found, whenever I configure the component using vCenter or ESXi directly.

megapearl commented 2 years ago

Updated to 0.7.0beta4 and still having this problem, tried to add the ESXi hosts directly and tried the vCenter vm. Is there anything I can check to debug this issue?

wxt9861 commented 2 years ago

That's still outstanding. See release notes.

soend commented 1 year ago

Having the same problem using 0.7.0beta4. I connect directly to the host and host is not part of a vCenter and has never been part of it.

I did have a look at the function where i am getting the error: async_get_vm_details and i see that the vm_name parameter is passed down direclty from service call data. So if the vm name has spaces it will receive the error "VM UUID not found". Other places in code the vm name is lowered and spaces are replaced with underscore. This should also be done here. So the function would look something like this:

def async_get_vm_details(vm_name):
    vm_name_cleaned = vm_name.replace(" ", "_").lower()
    for _entry in hass.data[DOMAIN_DATA]:
        if vm_name_cleaned in hass.data[DOMAIN_DATA][_entry]["vm"]:
            return hass.data[DOMAIN_DATA][_entry]["vm"][vm_name_cleaned]["uuid"]

    raise ValueError("VM UUID not found")

I tested that and it now works for me with this change made.

Other option would be ofc just mention that in the service call vm parameter description that spaces need to be replaced with underscores and it has to be in lowercase.

megapearl commented 1 year ago

Thanks for your solution! I adjusted the code also, and can start and stop VM's again.