gluap / pyduofern-hacs

Repository managing hacs-integration for pyduofern
MIT License
27 stars 8 forks source link

Unknown error / "local variable 'sets' referenced before assignment" when running "duofern.update_device_state" #23

Closed bcutter closed 1 year ago

bcutter commented 1 year ago

I accidentally discovered my script running duofern.update_device_state daily failed always when executing that service (used since v0.4.0)

grafik

Running the action inside the script or the service directly from dev tools gives:

grafik

grafik

When running

service: duofern.update_device_state
data: {}

this is in HA log:

Dieser Fehler wurde von einer benutzerdefinierten Integration verursacht

Logger: homeassistant.helpers.script.websocket_api_script
Source: custom_components/duofern/__init__.py:99
Integration: Duofern control (documentation, issues)
First occurred: 17:08:34 (6 occurrences)
Last logged: 17:18:03

websocket_api script: Error executing script. Unexpected error for call_service at pos 1: local variable 'sets' referenced before assignment
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 451, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 684, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1745, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1788, in _execute_service
    await self._hass.async_add_executor_job(
  File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/duofern/__init__.py", line 99, in update_device_state
    hass.data[DOMAIN]['stick'].command(module_id, 'getStatus')
  File "/usr/local/lib/python3.10/site-packages/pyduofern/duofern_stick.py", line 571, in command
    self.duofern_parser.set(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/pyduofern/duofern.py", line 770, in set
    logger.debug(sets.keys())  # join(" ", sort keys sets)
UnboundLocalError: local variable 'sets' referenced before assignment

Same for executing by automation/script:

Logger: homeassistant.components.automation.cover_up_alle_rollladen_offnen
Source: custom_components/duofern/__init__.py:99
Integration: Automatisierung (documentation, issues)
First occurred: 14. Januar 2023 um 08:37:05 (9 occurrences)
Last logged: 08:54:18

Cover UP_Alle Rollläden öffnen: Choose at step 1: choice 2: Error executing script. Unexpected error for call_service at pos 4: local variable 'sets' referenced before assignment
Cover UP_Alle Rollläden öffnen: Error executing script. Unexpected error for choose at pos 1: local variable 'sets' referenced before assignment
While executing automation automation.cover_up_alle_rollladen_offnen
Cover UP_Alle Rollläden öffnen: Choose at step 1: choice 1: Error executing script. Unexpected error for call_service at pos 4: local variable 'sets' referenced before assignment
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 451, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 684, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1745, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1788, in _execute_service
    await self._hass.async_add_executor_job(
  File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/duofern/__init__.py", line 99, in update_device_state
    hass.data[DOMAIN]['stick'].command(module_id, 'getStatus')
  File "/usr/local/lib/python3.10/site-packages/pyduofern/duofern_stick.py", line 571, in command
    self.duofern_parser.set(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/pyduofern/duofern.py", line 770, in set
    logger.debug(sets.keys())  # join(" ", sort keys sets)
UnboundLocalError: local variable 'sets' referenced before assignment

Enabled debug logging and ran the service again, here's the part from the debug log:

2023-01-15 17:23:18.101 ERROR (MainThread) [homeassistant.helpers.script.websocket_api_script] websocket_api script: Error executing script. Unexpected error for call_service at pos 1: local variable 'sets' referenced before assignment
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 451, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 684, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1745, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1788, in _execute_service
    await self._hass.async_add_executor_job(
  File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/duofern/__init__.py", line 99, in update_device_state
    hass.data[DOMAIN]['stick'].command(module_id, 'getStatus')
  File "/usr/local/lib/python3.10/site-packages/pyduofern/duofern_stick.py", line 571, in command
    self.duofern_parser.set(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/pyduofern/duofern.py", line 770, in set
    logger.debug(sets.keys())  # join(" ", sort keys sets)
UnboundLocalError: local variable 'sets' referenced before assignment
2023-01-15 17:23:18.108 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [546571336144] Error handling message: Unknown error (unknown_error) from 192.168.0.54 (Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/decorators.py", line 27, in _handle_async_response
    await func(hass, connection, msg)
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 646, in handle_execute_script
    await script_obj.async_run(msg.get("variables"), context=context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1524, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 409, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 453, in _async_step
    self._handle_exception(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 476, in _handle_exception
    raise exception
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 451, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 684, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1745, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1788, in _execute_service
    await self._hass.async_add_executor_job(
  File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/duofern/__init__.py", line 99, in update_device_state
    hass.data[DOMAIN]['stick'].command(module_id, 'getStatus')
  File "/usr/local/lib/python3.10/site-packages/pyduofern/duofern_stick.py", line 571, in command
    self.duofern_parser.set(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/pyduofern/duofern.py", line 770, in set
    logger.debug(sets.keys())  # join(" ", sort keys sets)
UnboundLocalError: local variable 'sets' referenced before assignment
gluap commented 1 year ago

I believe this was fixed with the transition to the by-device update state service.