Skip to content

Instantly share code, notes, and snippets.

View tonyroberts's full-sized avatar

Tony Roberts tonyroberts

View GitHub Profile
#
# Blueprint to control a light (or lights) using a Philips Hue SML001
# motion sensor connected to Home Assistant via ZHA
#
# Home Assitant configures the sensor to be an Occupancy Sensor at boot,
# setting the Occupied to Unoccipied time and sensitivity of the sensor.
# I used to use HA timers to do this, but Blueprint is now much more simple :-)
#
# Additionally the sensor can be disabled using a oneof two entities.
# I usually link this to a TV state, as I do not want my lights going on & off
//
// Having multiple Python environments in a single process is a problem.
// The code in this gist is not a complete solution but contains some
// ideas of how the problem might be worked around.
//
// Python extension modules link against python3.dll which redirects
// to the full python3x.dll module, but if multiple Python runtimes
// are loaded then when loading an extension module the python3.dll
// dependency resolves to the first instance of python3.dll that was
// loaded, which may not be the correct one.
@xl_func("str: rtd")
def subscribe(topic):
"""Get a value that has been published on another sheet"""
return SubscriberRTD(topic)
from pyxll import RTD
import pubsub
class SubscriberRTD(RTD):
def __init__(self, topic):
super().__init__(value="Waiting...")
self.__topic = topic
def connect(self):
[PYXLL]
modules =
pubsub_example
from pyxll import xl_func
import pubsub
@xl_func
def publish(topic, value):
"""Publish a value on a topic to be picked up by another sheet"""
pubsub.publish(topic, value)
return f"[Published to {topic}]"
# Use a single global message broker for all topics
_global_message_broker = MessageBroker()
def publish(topic, value):
"""Publishes a value on a topic for a subscriber to receive"""
_global_message_broker.publish(topic, value)
def subscribe(topic, callback):
class MessageBroker:
def __init__(self):
self.__subscribers = {}
def publish(self, topic, message):
for callback in self.__subscribers.get(topic, []):
callback(message)
def subscribe(self, topic, callback):
from pyxll import xl_app, async_call
import win32gui, win32ui
import tempfile
import os
def get_image_size(image):
"""Return the size of an image in pixels as (width, height)."""
# Get the size of the input image in pixels (Excel sizes are in points,
# or 1/72th of an inch.
from pyxll import xl_func, xl_app
import matplotlib.pyplot as plt
import numpy as np
import torch
@xl_func
def nn_Run(net, image_name, scale=1, offset=-0.5, seed=None):
"""Run the neural network with random weights"""