twrecked / hass-virtual

Virtual Components for Home Assistant
GNU General Public License v3.0
157 stars 25 forks source link

[FR] add a "valve" entity #91

Open ildar170975 opened 9 months ago

ildar170975 commented 9 months ago

Please add a virtual "valve" entity introduced in 2024.1.

Tony763 commented 9 months ago

Doc: https://www.home-assistant.io/integrations/valve/ Src: https://github.com/home-assistant/core/tree/dev/homeassistant/components/valve

twrecked commented 1 month ago

Bit late sorry, but I just added valve to 0.9.0b13.

Tony763 commented 1 month ago

Will test it in the morning, thanks :)

Tony763 commented 1 month ago

Setting availability state works correctly, but turning valve ON threw error.

  Test Valve:
  - platform: valve
    initial_value: 'closed'
    open_close_duration: 10
    open_close_tick: 1
    initial_availability: false
024-08-16 19:59:45.744 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [134347817255408] Unexpected exception
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 241, in handle_call_service
response = await hass.services.async_call(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/core.py", line 2763, in async_call
response_data = await coro
^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/core.py", line 2806, in _execute_service
return await target(service_call)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 999, in entity_service_call
single_response = await _handle_entity_call(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 1071, in _handle_entity_call
result = await task
^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/valve/__init__.py", line 264, in async_set_valve_position
await self.hass.async_add_executor_job(self.set_valve_position, position)
File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/virtual/valve.py", line 95, in set_valve_position
self._set_position(position)
File "/config/custom_components/virtual/entity.py", line 199, in _set_position
self._tick()
File "/config/custom_components/virtual/entity.py", line 227, in _tick
async_call_later(self.hass, self._open_close_tick, self._update_position)
File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 1590, in async_call_later
return loop.call_at(loop.time() + delay, _run_async_call_action, hass, job).cancel
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 776, in call_at
self._check_thread()
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 834, in _check_thread
raise RuntimeError(
RuntimeError: Non-thread-safe operation invoked on an event loop other than the current one
twrecked commented 1 month ago

That was done through a service call?

Tony763 commented 1 month ago

Availability trough service call in developer tools. Turning ON by card in lovelance.

twrecked commented 1 month ago

Both those are working here. Odd. I'll take a look.

twrecked commented 1 month ago

Which home assistant version are you using?

Tony763 commented 1 month ago

2024.8.1 in Docker Should be always latest when available, it update automatically by WatchTower

twrecked commented 1 month ago

I'm running exactly the same version. Can I ask which type of lovelace card?

Tony763 commented 1 month ago

Default one:

image

Opening and changing position. It won't finish movement.

Tony763 commented 1 month ago

Running 2024.8.2, watch tower just finished update. Behavior is same.

twrecked commented 1 month ago

I think it's because I don't have asyncio debug enabled. I'm just figuring out how to turn that on.

Tony763 commented 1 month ago

Maybe loop.set_debug() would help. https://docs.python.org/3/library/asyncio-dev.html

twrecked commented 1 month ago

I used the profiler. I reproduced it so I'll fix it.

twrecked commented 1 month ago

Should fixed in b15; turned out I'd misunderstood the async stuff. The code is actually cleaner now.

Tony763 commented 1 month ago

Ah, I looked into PR and think You just forgot to convert these into asynchronous :slightly_smiling_face: B15 fixes it completely for me. Thanks

ildar170975 commented 1 month ago

Guys, thanks a lot for adding valves. Tested in b15. Config:

version: 1
devices:
  Test Valve:
  - platform: valve
    initial_value: 'closed'
    open_close_duration: 5
    open_close_tick: 1

This is my 1st experience with valve entities, so may be I am making obvious mistakes. Tried to use this service call:

      action: valve.set_valve_position
      target:
        entity_id: valve.test_valve
      data:
        position: 33

And the final point is "40", not "33": изображение

Is it expected?


Another question is for this view: изображение I agree that this could be issues for HA frontend - but may be these are simple questions:

  1. Why a "shifted up slide" stands for "open"? For me it looks like "closed" (update 06.09.24: created a separate discussion in Frontend).
  2. What is a meaning of countdown timer "in xxx minute"? (update 06.09.24: it was probably caused by time sync, see a related issue)
ildar170975 commented 1 month ago

BTW, in Docs it is written: изображение

but in fact services are called differently: изображение

and this difference comes from official Docs for valves: изображение

Prepared an issue about this error in Docs for valves - https://github.com/home-assistant/home-assistant.io/issues/34407


Also, similar observation is for "covers" in Docs: изображение they are called differently in fact (and official Docs for covers do conform the actual implementation).