custom-components / pyscript

Pyscript adds rich Python scripting to HASS
Apache License 2.0
874 stars 46 forks source link

Creating a Home Assistant timer from pyscript does not seem to work? #461

Open redlefloh opened 1 year ago

redlefloh commented 1 year ago

Hi not a super critical issue, since I can always create timers as helpers in the HA UI, but just out of curiosity, is there a way to actually create a timer directly from pyscript or is this even a bug? The intuitive: state.set('timer.my_demo_timer', duration="0:00:02", editable=True, restore=True, friendly_name="My Demo Timer") kinda works and sort of creates a timer entity, but it does not actually work as a timer?

I run a test and created two timers, one with the state.set() call above and one with the HA UI:

Screenshot 2023-04-13 at 00 25 03

But as you can see, the first one is not really a timer, i.e. it does not have the Start button?

Screenshot 2023-04-13 at 00 25 09

the second one has:

Screenshot 2023-04-13 at 00 25 24

Now the entity seems to be available as timer in pyscript and the entity even auto.expands as timer in jupyter:

Screenshot 2023-04-13 at 00 22 45 Screenshot 2023-04-13 at 00 23 00

But if I call for example: timer.my_demo_timer.start(2) the timer does not actually start?

here is the Notebook:

Screenshot 2023-04-13 at 00 21 15

This seems to be a bug unless I am not doing things correctly?

And for copmpleteness, here is a file "Test timer in pyscript.ipynb" in case someone wants to replicate. The only thing to do is to create the "My Demo Timer 2" as a helper in in HA and then load the notebook and run every cell in order.

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "7ef17833",
   "metadata": {},
   "outputs": [],
   "source": [
    "from homeassistant.const import EVENT_CALL_SERVICE\n",
    "\n",
    "state.set('timer.my_demo_timer', duration=\"0:00:02\", editable=True, \n",
    "           restore=True, friendly_name=\"My Demo Timer\")\n",
    "\n",
    "# this timer was created with the state_set() call above\n",
    "@state_trigger('timer.my_demo_timer')\n",
    "def monitor_state_trigger(**kwargs):\n",
    "    log.warning(f\"Got STATE TRIGGER with kwargs={kwargs}\")\n",
    "\n",
    "# this timer was created as a helper\n",
    "@state_trigger('timer.my_demo_timer_2')\n",
    "def monitor_state_trigger(**kwargs):\n",
    "    log.warning(f\"Got STATE TRIGGER with kwargs={kwargs}\")\n",
    "\n",
    "# let's monitor the events\n",
    "@event_trigger(EVENT_CALL_SERVICE)\n",
    "def monitor_service_calls(**kwargs):\n",
    "    log.warning(f\"got EVENT_CALL_SERVICE with kwargs={kwargs}\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "4dcaa18e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'duration': '0:00:02', 'editable': True, 'restore': True, 'friendly_name': 'My Demo Timer'}"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state.getattr('timer.my_demo_timer')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "ffc6207e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'duration': '0:00:02', 'editable': True, 'restore': True, 'friendly_name': 'My Demo Timer 2'}"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state.getattr('timer.my_demo_timer_2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "42c666be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "got EVENT_CALL_SERVICE with kwargs={'trigger_type': 'event', 'event_type': 'call_service', 'context': <homeassistant.core.Context object at 0x7f85f87e00>, 'domain': 'timer', 'service': 'start', 'service_data': {'entity_id': 'timer.my_demo_timer', 'duration': 2}}\n"
     ]
    }
   ],
   "source": [
    "timer.my_demo_timer.start(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "a4586966",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "got EVENT_CALL_SERVICE with kwargs={'trigger_type': 'event', 'event_type': 'call_service', 'context': <homeassistant.core.Context object at 0x7f85e3f880>, 'domain': 'timer', 'service': 'start', 'service_data': {'entity_id': 'timer.my_demo_timer_2', 'duration': 2}}\n",
      "Got STATE TRIGGER with kwargs={'trigger_type': 'state', 'var_name': 'timer.my_demo_timer_2', 'value': 'active', 'old_value': 'idle', 'context': <homeassistant.core.Context object at 0x7f85e3f880>}\n",
      "Got STATE TRIGGER with kwargs={'trigger_type': 'state', 'var_name': 'timer.my_demo_timer_2', 'value': 'idle', 'old_value': 'active', 'context': <homeassistant.core.Context object at 0x7f85e3f880>}\n"
     ]
    }
   ],
   "source": [
    "timer.my_demo_timer_2.start(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55e02ac0",
   "metadata": {},
   "outputs": [],
   "source": [
    "timer.my_demo_timer."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "hass pyscript",
   "language": "python",
   "name": "pyscript"
  },
  "language_info": {
   "codemirror_mode": "",
   "file_extension": ".py",
   "mimetype": "",
   "name": "python",
   "nbconvert_exporter": "",
   "version": "1.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}