Skip to content

Commit

Permalink
a few tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
craigbarratt committed Aug 31, 2020
1 parent 094c04c commit deae5c4
Showing 1 changed file with 78 additions and 34 deletions.
112 changes: 78 additions & 34 deletions pyscript_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
{
"data": {
"text/plain": [
"62.6"
"34.47"
]
},
"execution_count": 5,
Expand Down Expand Up @@ -386,7 +386,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"The sun's elevation is 62.6, which is > 30 degrees\n"
"The sun's elevation is 34.47, which is > 30 degrees\n"
]
}
],
Expand Down Expand Up @@ -607,7 +607,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 23,
"metadata": {},
"outputs": [
{
Expand All @@ -631,7 +631,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -647,7 +647,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 25,
"metadata": {},
"outputs": [
{
Expand All @@ -656,7 +656,7 @@
"False"
]
},
"execution_count": 26,
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -674,7 +674,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 26,
"metadata": {
"scrolled": true
},
Expand All @@ -683,16 +683,32 @@
"name": "stdout",
"output_type": "stream",
"text": [
"test_service called with parameters {'param1': 45, 'other_param': [6, 8, 12]}\n"
"test_service2 called with parameters {'param1': 45, 'other_param': [6, 8, 12]}\n"
]
}
],
"source": [
"@service\n",
"def test_service2(**kwargs):\n",
" log.debug(f\"test_service called with parameters {kwargs}\")\n",
" log.debug(f\"test_service2 called with parameters {kwargs}\")\n",
"\n",
"service.call(\"pyscript\", \"test_service2\", param1=45, other_param=[6,8,12])"
"pyscript.test_service2(param1=45, other_param=[6,8,12])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's clean up at the end of this section by deleting the function"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"del test_service2"
]
},
{
Expand Down Expand Up @@ -905,7 +921,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"As discussed in this [Wiki Page](https://github.com/custom-components/pyscript/wiki/Event-based-triggers), here's an example that listens to state change events for a particular component, in this case `sun`. We trigger on the built-in `EVENT_STATE_CHANGED` event, and only call the function when the state variable name starts with `\"sun.\"`. There are typically a pair state updates every 4-5 minutes, so you might need to wait a while to see any output:"
"As discussed in this [Wiki Page](https://github.com/custom-components/pyscript/wiki/Event-based-triggers), here's an example that listens to state change events for a particular component, in this case `sun`. We trigger on the built-in `EVENT_STATE_CHANGED` event, and only call the function when the state variable name starts with `\"sun.\"`. These state updates might be 4-10 minutes apart, and a lot less often at night, so you might need to wait a while to see any output:"
]
},
{
Expand All @@ -917,8 +933,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"entity sun.sun changed from above_horizon to above_horizon and attributes are now {'next_dawn': '2020-08-24T13:05:35+00:00', 'next_dusk': '2020-08-24T03:18:39+00:00', 'next_midnight': '2020-08-24T08:11:10+00:00', 'next_noon': '2020-08-24T20:11:24+00:00', 'next_rising': '2020-08-24T13:32:46+00:00', 'next_setting': '2020-08-24T02:51:24+00:00', 'elevation': 62.38, 'azimuth': 196.92, 'rising': False, 'friendly_name': 'Sun'}\n",
"entity sun.sun changed from above_horizon to above_horizon and attributes are now {'next_dawn': '2020-08-24T13:05:35+00:00', 'next_dusk': '2020-08-24T03:18:39+00:00', 'next_midnight': '2020-08-24T08:11:10+00:00', 'next_noon': '2020-08-24T20:11:24+00:00', 'next_rising': '2020-08-24T13:32:46+00:00', 'next_setting': '2020-08-24T02:51:24+00:00', 'elevation': 62.38, 'azimuth': 196.93, 'rising': False, 'friendly_name': 'Sun'}\n"
"entity sun.sun changed from above_horizon to above_horizon and attributes are now {'next_dawn': '2020-09-01T13:12:49+00:00', 'next_dusk': '2020-09-01T03:06:47+00:00', 'next_midnight': '2020-09-01T08:08:53+00:00', 'next_noon': '2020-08-31T20:15:23+00:00', 'next_rising': '2020-09-01T13:44:36+00:00', 'next_setting': '2020-09-01T02:46:15+00:00', 'elevation': 35.97, 'azimuth': 119.95, 'rising': True, 'friendly_name': 'Sun'}\n"
]
}
],
Expand All @@ -935,7 +950,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"If you are tired of waiting, you could set some random state variable that starts with `\"sun.\"`, although it's not recommended to set state variable in a components address space:"
"If you are tired of waiting, you could set some random state variable that starts with `\"sun.\"`, although it's not recommended to set state variable in another component's address space unless you are sure it won't conflict with a name that component might use:"
]
},
{
Expand Down Expand Up @@ -1225,7 +1240,18 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"As expected, the first service call continues and the second service call is terminated as soon as it calls `task.unique`."
"As expected, the first service call continues and the second service call is terminated as soon as it calls `task.unique`.\n",
"\n",
"Now let's clean up the function before moving on."
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
"del long_running"
]
},
{
Expand All @@ -1240,7 +1266,7 @@
},
{
"cell_type": "code",
"execution_count": 49,
"execution_count": 50,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -1261,7 +1287,7 @@
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": 51,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -1293,15 +1319,15 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 52,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"waiting for sensor2\n",
"result = {'trigger_type': 'time'}\n"
"result = {'trigger_type': 'timeout'}\n"
]
}
],
Expand Down Expand Up @@ -1332,7 +1358,7 @@
},
{
"cell_type": "code",
"execution_count": 52,
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -1344,7 +1370,7 @@
" state_trigger=\"input_boolean.test_sensor2 == 'on' or input_boolean.test_sensor1 == 'off'\",\n",
" timeout=10\n",
" )\n",
" if result[\"trigger_type\"] == \"time\":\n",
" if result[\"trigger_type\"] == \"timeout\":\n",
" log.debug(f\"sensor2 didn't turn on withing 10 seconds\")\n",
" elif result[\"trigger_type\"] == \"state\":\n",
" if result[\"var_name\"] == \"input_boolean.test_sensor2\":\n",
Expand All @@ -1362,7 +1388,7 @@
},
{
"cell_type": "code",
"execution_count": 53,
"execution_count": 54,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -1394,7 +1420,7 @@
},
{
"cell_type": "code",
"execution_count": 54,
"execution_count": 55,
"metadata": {
"scrolled": true
},
Expand Down Expand Up @@ -1429,8 +1455,10 @@
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"execution_count": 56,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
Expand All @@ -1452,6 +1480,22 @@
"input_boolean.test_sensor2 = \"on\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's clean up the function before moving on."
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
"del sensor1_active"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -1468,7 +1512,7 @@
},
{
"cell_type": "code",
"execution_count": 56,
"execution_count": 58,
"metadata": {},
"outputs": [
{
Expand All @@ -1477,7 +1521,7 @@
"['jupyter_0', 'file.scripts']"
]
},
"execution_count": 56,
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1495,7 +1539,7 @@
},
{
"cell_type": "code",
"execution_count": 57,
"execution_count": 59,
"metadata": {},
"outputs": [
{
Expand All @@ -1504,7 +1548,7 @@
"'jupyter_0'"
]
},
"execution_count": 57,
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1522,7 +1566,7 @@
},
{
"cell_type": "code",
"execution_count": 58,
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -1538,7 +1582,7 @@
},
{
"cell_type": "code",
"execution_count": 59,
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -1547,7 +1591,7 @@
},
{
"cell_type": "code",
"execution_count": 60,
"execution_count": 62,
"metadata": {},
"outputs": [
{
Expand All @@ -1556,7 +1600,7 @@
"123"
]
},
"execution_count": 60,
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1574,7 +1618,7 @@
},
{
"cell_type": "code",
"execution_count": 61,
"execution_count": 63,
"metadata": {},
"outputs": [
{
Expand Down

0 comments on commit deae5c4

Please sign in to comment.