Skip to content

Instantly share code, notes, and snippets.

@balloob
balloob / demo.js
Created August 1, 2017 15:50
Example using home-assistant-js-websocket with Node
const WebSocket = require('ws');
global.WebSocket = WebSocket;
const HAWS = require("home-assistant-js-websocket");
const getWsUrl = haUrl => `ws://${haUrl}/api/websocket`;
HAWS.createConnection(getWsUrl('localhost:8123')).then(conn => {
HAWS.subscribeEntities(conn, logEntities);
});
@balloob
balloob / example-panel.js
Last active August 31, 2020 13:34
Example Panel for Home Assistant
/*
Example panel.
Put this file in <config>/www/example-panel.js
In configuration.yaml:
panel_custom:
- name: example-panel
# url_path needs to be unique for each panel_custom config
@balloob
balloob / debug-visibility.patch
Created June 27, 2020 06:57
Patch for HA frontend to debug visiblitychange behavior on Android
diff --git a/src/layouts/home-assistant.ts b/src/layouts/home-assistant.ts
index 0dae29df..663d152f 100644
--- a/src/layouts/home-assistant.ts
+++ b/src/layouts/home-assistant.ts
@@ -150,6 +150,7 @@ export class HomeAssistantAppEl extends HassElement {
protected _handleVisibilityChange() {
if (document.hidden) {
+ console.log("home-assistant handle tab hidden");
// If the document is hidden, we will prevent reconnects until we are visible again
@balloob
balloob / ha-deno.ts
Last active May 16, 2020 15:04
Home Assistant Websocket Deno
// Connect to Home Assistant from Deno
// Video: https://twitter.com/balloob/status/1261550082521919488?s=19
//
// Example is built-in as CLI. Try it out:
// deno run --allow-net https://raw-path-to-gist <home assistant url> <long lived access token>
//
// To use in your own code:
// import { createConnection } from https://raw-path-to-gist
// const conn = await createConnection(urlOfHomeAssistant, accessToken)
@balloob
balloob / find.js
Created December 26, 2019 11:02
Icon finder
const path = require("path");
const fs = require("fs");
const ICON_PACKAGE_PATH = path.resolve("./node_modules/@mdi/svg/");
const META_PATH = path.resolve(ICON_PACKAGE_PATH, "meta.json");
const ICON_PATH = path.resolve(ICON_PACKAGE_PATH, "svg");
// Print icons + sizes to create sizes.json
// const getPath = (icon) => {
// const svg = fs.readFileSync(`${ICON_PATH}/${icon}.svg`, {
@balloob
balloob / config_flow.py
Created October 16, 2019 21:04
Config flow to link Home Assistant with Home Assistant
"""Config flow to connect with Home Assistant."""
import logging
import voluptuous as vol
from homeassistant.helpers import config_entry_oauth2_flow
from .const import DOMAIN
@balloob
balloob / config_entry_reloader.py
Created August 21, 2019 18:05
Config Entry Reloader
class EntityRegistryDisabledHandler:
"""Handler to handle when entities related to config entries updating disabled_by."""
RELOAD_AFTER_UPDATE_DELAY = 30
def __init__(self, hass: HomeAssistant) -> None:
"""Initialize the handler."""
self.hass = hass
self.registry: Optional[entity_registry.EntityRegistry] = None
self.changed: Set[str] = set()
@balloob
balloob / example_options_flow.diff
Created August 15, 2019 20:21
Example options flow
diff --git a/homeassistant/components/unifi/config_flow.py b/homeassistant/components/unifi/config_flow.py
index e5a8965df..06751ed6e 100644
--- a/homeassistant/components/unifi/config_flow.py
+++ b/homeassistant/components/unifi/config_flow.py
@@ -2,6 +2,7 @@
import voluptuous as vol
from homeassistant import config_entries
+from homeassistant.core import callback
from homeassistant.const import (
@balloob
balloob / environment.py
Created January 31, 2016 07:16
Detect Python virtual environments venv and virtualenv
def is_virtual():
""" Return if we run in a virtual environtment. """
# Check supports venv && virtualenv
return (getattr(sys, 'base_prefix', sys.prefix) != sys.prefix or
hasattr(sys, 'real_prefix'))
@balloob
balloob / CLA.md
Last active September 27, 2018 09:19 — forked from pjcozzi/CLA.md
CLA for Apache 2.0 license

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I have the right to submit it under the Apache 2.0 license; or

(b) The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the Apache 2.0 license; or