home assistant example

This commit is contained in:
ben 2023-10-12 15:30:46 +02:00
parent a740fbbf36
commit 321561da75
5 changed files with 151 additions and 24 deletions

View File

@ -18,7 +18,11 @@ sudo apt update
sudo apt install blink1
```
## Usage
## ![](hass.svg) Home Assistant
For adding the blink(1) as a `light` entity in Home Assistant, see [`home-assistant/blink1.yaml`](home-assistant/blink1.yaml).
## Config
Configure which port it listens to in `/etc/default/blink1-tiny-server`:
@ -27,27 +31,6 @@ $ cat /etc/default/blink1-tiny-server
BLINK1_TINY_SERVER_PORT=8011
```
Example:
```shell
$ curl -sX GET http://localhost:8011/blink1/fadeToRGB?rgb=ff0ff
```
Nginx config:
```nginx
location /blink1/ {
# For html page:
#rewrite '^/blink1(/.*)$' $1 break;
#sub_filter_once off;
#sub_filter '"/' '"./';
add_header Content-Type 'application/json' always;
proxy_http_version 1.1;
proxy_pass http://localhost:8011;
}
```
Standard systemd service:
```shell
@ -66,7 +49,44 @@ Oct 03 23:48:31 mathom.s21.sudo.is systemd[1]: Started blink(1) tiny http server
Oct 03 23:48:31 mathom.s21.sudo.is blink1-tiny-server[1644193]: blink1-tiny-server version v2.3.0-linux-x86_64: running on http://localhost:8011/ (html help enabeld)
```
## HTTP API
Very simply HTTP api:
```shell
$ curl -sX GET http://localhost:8011/blink1/fadeToRGB?rgb=ff0ff
$ curl -sX GET https://blink1-host.example.com/blink1/ | jq .
{
"version": "v2.3.0-linux-x86_64",
"uri": "/blink1/",
"millis": 0,
"time": 0,
"rgb": "#ff0ff",
"ledn": 0,
"bright": 0,
"count": 0,
"status": "blink1 status"
}
```
## nginx
Nginx config:
```nginx
location /blink1/ {
# For html page:
#rewrite '^/blink1(/.*)$' $1 break;
#sub_filter_once off;
#sub_filter '"/' '"./';
add_header Content-Type 'application/json' always;
proxy_http_version 1.1;
proxy_pass http://localhost:8011;
}
```
## `--help`
The `--help` output from `blink1-tiny-server` is informative:

View File

@ -3,8 +3,8 @@ Description=blink(1) tiny http server
After=network.target
[Service]
User=nobody
Group=nobody
#User=nobody
#Group=nogroup
EnvironmentFile=/etc/default/blink1-tiny-server
ExecStart=/usr/local/bin/blink1-tiny-server -p ${BLINK1_TINY_SERVER_PORT}

BIN
hass.png Executable file

Binary file not shown.

After

(image error) Size: 8.4 KiB

3
hass.svg Executable file
View File

@ -0,0 +1,3 @@
<svg width="30" height="30" viewBox="0 0 240 240" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M229.39 109.153L130.61 10.3725C124.78 4.5425 115.23 4.5425 109.4 10.3725L10.61 109.153C4.78 114.983 0 126.512 0 134.762V224.762C0 233.012 6.75 239.762 15 239.762H107.27L66.64 199.132C64.55 199.852 62.32 200.262 60 200.262C48.7 200.262 39.5 191.062 39.5 179.762C39.5 168.462 48.7 159.262 60 159.262C71.3 159.262 80.5 168.462 80.5 179.762C80.5 182.092 80.09 184.322 79.37 186.412L111 218.042V102.162C104.2 98.8225 99.5 91.8425 99.5 83.7725C99.5 72.4725 108.7 63.2725 120 63.2725C131.3 63.2725 140.5 72.4725 140.5 83.7725C140.5 91.8425 135.8 98.8225 129 102.162V183.432L160.46 151.972C159.84 150.012 159.5 147.932 159.5 145.772C159.5 134.472 168.7 125.272 180 125.272C191.3 125.272 200.5 134.472 200.5 145.772C200.5 157.072 191.3 166.272 180 166.272C177.5 166.272 175.12 165.802 172.91 164.982L129 208.892V239.772H225C233.25 239.772 240 233.022 240 224.772V134.772C240 126.522 235.23 115.002 229.39 109.162V109.153Z" fill="#F2F4F9"/>
</svg>

After

(image error) Size: 1.0 KiB

104
home-assistant/blink1.yaml Normal file
View File

@ -0,0 +1,104 @@
---
template:
- binary_sensor:
- name: "blink1_on"
device_class: light
state: >-
{{ state_attr('sensor.blink1', 'rgb') != "#000000" }}
sensor:
- platform: rest
resource: http://localhost:8011/blink1
name: blink1
json_attributes:
- rgb
- bright
value_template: "{{ value_json.rgb }}"
rest_command:
blink1_turn_on:
url: http://localhost:8011/blink1/on?bright=250
#url: http://localhost:8011/blink1/fadeToRGB?rgb=ff0ff
method: GET
content_type: "application/json"
blink1_turn_off:
url: http://localhost:8011/blink1/off
method: GET
content_type: "application/json"
blink1_turn_magenta:
url: http://localhost:8011/blink1/fadeToRGB?rgb=ff00ff
method: GET
content_type: "application/json"
blink1_set_color:
url: "http://localhost:8011/blink1/fadeToRGB?rgb={{ rgb }}"
method: GET
light:
- platform: template
lights:
blink1:
friendly_name: blink1
value_template: >-
{{ state_attr('sensor.blink1', 'rgb') != "#000000" }}
# color_template: >-
# # {{ state_attr('sensor.blink1', 'rgb') }}
#
turn_on:
- service: rest_command.blink1_turn_on
- delay:
milliseconds: 500
- service: homeassistant.update_entity
target:
entity_id: sensor.blink1
turn_off:
- service: rest_command.blink1_turn_off
- delay:
milliseconds: 500
- service: homeassistant.update_entity
target:
entity_id: sensor.blink1
set_color:
- service: rest_command.blink1_turn_off
- service: rest_command.blink1_set_color
data:
# https://github.com/velijv/home-assistant-color-helpers#rgb-to-hex
# https://community.home-assistant.io/t/advanced-light-template-help/175654
# https://community.home-assistant.io/t/using-hsv-hsb-to-set-colored-lights/15472
rgb: >-
{%- set h2 = h / 360 -%}
{%- set s2 = s / 100 -%}
{%- set v = 100 -%}
{%- set i = (h2 * 6 ) | round(2,'floor') | int-%}
{%- set f = h2 * 6 - i -%}
{%- set p = v * (1 - s2) -%}
{%- set q = v * (1 - f * s2) -%}
{%- set t = v * (1 - (1 - f) * s2) -%}
{%- if i % 6 == 0 -%}
{%- set r = v | int -%}
{%- set g = t | int -%}
{%- set b = p | int -%}
{%- elif i % 6 == 1 -%}
{%- set r = q | int -%}
{%- set g = v | int -%}
{%- set b = p | int -%}
{%- elif i % 6 == 2 -%}
{%- set r = p | int -%}n
{%- set g = v | int -%}
{%- set b = t | int -%}
{%- elif i % 6 == 3 -%}
{%- set r = p | int -%}
{%- set g = q | int -%}
{%- set b = v | int -%}
{%- elif i % 6 == 4 -%}
{%- set r = t | int -%}
{%- set g = p | int -%}
{%- set b = v | int -%}
{%- elif i % 6 == 5 -%}
{%- set r = v | int -%}
{%- set g = p | int -%}
{%- set b = q | int -%}
{%- endif -%}
{{ '%02x%02x%02x' | format(r, g, b) }}