Skip to content

Instantly share code, notes, and snippets.

@jtbandes
Last active November 3, 2025 01:32
Show Gist options
  • Save jtbandes/39da3dfda0b16521319c63b277ee082a to your computer and use it in GitHub Desktop.
Save jtbandes/39da3dfda0b16521319c63b277ee082a to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"kernelspec": {
"name": "python",
"display_name": "Python (Pyodide)",
"language": "python"
},
"language_info": {
"codemirror_mode": {
"name": "python",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8"
}
},
"nbformat_minor": 5,
"nbformat": 4,
"cells": [
{
"id": "538579b5-3c03-4231-9b53-8c79339003e8",
"cell_type": "markdown",
"source": "# hue-zigbee-encoding demo\n\nUse `HueLightUpdateMessage` to represent a Hue light Zigbee message. All attributes are optional (they accept a value of `None`). You can apply a subset of attributes to change only those settings of the lights.\n\nSee [the README](https://github.com/jtbandes/hue-zigbee-encoding?tab=readme-ov-file#hue-zigbee-encoding) for more detailed documentation.",
"metadata": {}
},
{
"id": "2ad3ccfe-d8bf-406b-99b0-9bc91cac266b",
"cell_type": "code",
"source": "%pip install hue-zigbee-encoding\nfrom hue_zigbee_encoding import *",
"metadata": {
"trusted": true
},
"outputs": [],
"execution_count": 1
},
{
"id": "81c940a6-f884-4bb5-bfe5-9a0c55d1cebf",
"cell_type": "markdown",
"source": "Use the `HueLightUpdateMessage()` constructor to choose attribute values yourself:",
"metadata": {}
},
{
"id": "8966ebdc-510f-4d99-8044-903f27d4a743",
"cell_type": "code",
"source": "msg = HueLightUpdateMessage(\n is_on=True,\n brightness=127,\n)",
"metadata": {
"trusted": true
},
"outputs": [],
"execution_count": 2
},
{
"id": "7a038c37-0d65-4d28-a3c2-8e597b5e5003",
"cell_type": "markdown",
"source": "Or use `HueLightUpdateMessage.from_bytes` to parse a byte string:",
"metadata": {}
},
{
"id": "53242ae2-ff11-45c8-96ad-393036a6f402",
"cell_type": "code",
"source": "msg = HueLightUpdateMessage.from_bytes(b\"\\x03\\x00\\x01\\x7f\")",
"metadata": {
"trusted": true
},
"outputs": [],
"execution_count": 3
},
{
"id": "abfe7305-2a80-4cfe-91b4-36ccfc1fd2ab",
"cell_type": "markdown",
"source": "## Converting to bytes or numbers",
"metadata": {}
},
{
"id": "3c56f68a-f98f-4068-9e1d-56fae47a4839",
"cell_type": "markdown",
"source": "Use `to_bytes` to convert to a byte string. Then use `hex` to convert to a printable string, or use `list` to a list of decimal integers:",
"metadata": {}
},
{
"id": "4c55b233-64fe-471c-8efb-5c162404de95",
"cell_type": "code",
"source": "msg.to_bytes()",
"metadata": {
"trusted": true
},
"outputs": [
{
"execution_count": 4,
"output_type": "execute_result",
"data": {
"text/plain": "b'\\x03\\x00\\x01\\x7f'"
},
"metadata": {}
}
],
"execution_count": 4
},
{
"id": "bf719653-5004-426e-8513-e5a058e85dd9",
"cell_type": "code",
"source": "msg.to_bytes().hex()",
"metadata": {
"trusted": true
},
"outputs": [
{
"execution_count": 5,
"output_type": "execute_result",
"data": {
"text/plain": "'0300017f'"
},
"metadata": {}
}
],
"execution_count": 5
},
{
"id": "6befe4bb-5064-4bc7-bb96-3ac361b117c7",
"cell_type": "code",
"source": "list(msg.to_bytes())",
"metadata": {
"trusted": true
},
"outputs": [
{
"execution_count": 6,
"output_type": "execute_result",
"data": {
"text/plain": "[3, 0, 1, 127]"
},
"metadata": {}
}
],
"execution_count": 6
},
{
"id": "0dd9096d-3a0f-4648-9241-d08078cd49c6",
"cell_type": "markdown",
"source": "## Complete demo\n\nHere are all of the available attributes. In practice, you wouldn't actually want to use all the attributes at the same time (e.g. `color_temp` and `color_xy` are redundant).",
"metadata": {}
},
{
"id": "0cc24a87-dc7f-486f-adf5-f320991765ae",
"cell_type": "code",
"source": "msg = HueLightUpdateMessage(\n is_on=True,\n brightness=127,\n color_temp=HueLightColorMired.from_kelvin(6500),\n color_xy=HueLightColorXY(x=0.185, y=0.063),\n transition_time=2,\n effect=HueLightEffect.COSMOS,\n effect_speed=127,\n gradient=HueLightGradient(\n style=HueLightGradientStyle.SCATTERED,\n colors=[\n HueLightColorXY(x=0.424, y=0.285),\n HueLightColorXY(x=0.229, y=0.279),\n ],\n ),\n gradient_params=HueLightGradientParams(scale=0.0, offset=1.0),\n)\n\nmsg.to_bytes().hex()",
"metadata": {
"trusted": true
},
"outputs": [
{
"execution_count": 7,
"output_type": "execute_result",
"data": {
"text/plain": "'ff01017f99005b2f201002000f0a200200003b4958fc64567f0008'"
},
"metadata": {}
}
],
"execution_count": 7
}
]
}
hue-zigbee-encoding==0.1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment