Skip to content

Commit

Permalink
add fan switch, optimize loops, cleanup
Browse files Browse the repository at this point in the history
- Add the "Inovelli Fan Controller (VZM35-SN)"
- Optimize looping over multiple entities
- Remove entity_id from fields/parameters
  - Use `target: entity_id: <entity>` instead
- Remove "Off" option from "Color" and "Duration"
  - Use "Effect" instead
  - "Off" values are still accepted, just not selectable from the fields
- Change "Brightness level" default from 40 to 100
- Sort the "Effect" to group similar effects together
- Add VSCode settings
  • Loading branch information
zanix committed Oct 13, 2023
1 parent 3375e30 commit fb12e4e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 83 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"*.yaml": "home-assistant"
}
}
146 changes: 63 additions & 83 deletions script/inovelli_blue_led_zigbee2mqtt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ blueprint:
GitHub: https://github.com/zanix/home-assistant-blueprints
ℹ️ Version: 2023.3.1
ℹ️ Version: 2023.10.1
domain: script
source_url: https://github.com/zanix/home-assistant-blueprints/blob/master/script/inovelli_blue_led_zigbee2mqtt.yaml

Expand All @@ -30,19 +30,12 @@ fields:
manufacturer: Inovelli
entity:
integration: mqtt
domain: light
entity_id:
name: Entities
description: (Depricated, use Target) Inovelli devices to set effects.
example: light.living_room
selector:
entity:
domain: light
integration: mqtt
multiple: true
domain:
- light
- fan
led:
name: LED
description: Choose which LED to control. Default is All.
description: Choose which LED to control. Default is All. (Led 1 is at the bottom)
default: All
selector:
select:
Expand All @@ -63,7 +56,6 @@ fields:
select:
custom_value: true
options:
- "Off"
- Red
- Orange
- Yellow
Expand All @@ -78,7 +70,7 @@ fields:
level:
name: Brightness level
description: Value from 0 (off) to 100 (100% brightness).
default: 40
default: 100
example: '40'
selector:
number:
Expand All @@ -96,22 +88,22 @@ fields:
- 'Clear'
- 'Solid'
- 'Aurora'
- 'Slow Chase'
- 'Chase'
- 'Fast Blink'
- 'Fast Chase'
- 'Fast Falling'
- 'Fast Rising'
- 'Fast Siren'
- 'Slow Blink'
- 'Medium Blink'
- 'Fast Blink'
- 'Slow Falling'
- 'Medium Falling'
- 'Medium Rising'
- 'Fast Falling'
- 'Open/Close'
- 'Pulse'
- 'Slow Blink'
- 'Slow Chase'
- 'Slow Falling'
- 'Slow Rising'
- 'Medium Rising'
- 'Fast Rising'
- 'Slow Siren'
- 'Fast Siren'
- 'Small to Big'
duration:
name: Duration
Expand All @@ -121,7 +113,6 @@ fields:
select:
custom_value: true
options:
- 'Off'
- 1 Second
- 2 Seconds
- 3 Seconds
Expand Down Expand Up @@ -182,10 +173,12 @@ fields:
enable_debug:
name: Enable debug output?
selector:
boolean:
constant:
value: true
label: Enabled
command_path_map:
name: Command Path Map
description: >-
description: |
Map of Entity IDs with a MQTT command path map when HA and Z2M do not match up.
Enter a new row for each mapping: light.entity_name: "zigbee2mqtt/path/to/device/set"'.
You can also use a template but you need to use YAML mode to do so.
Expand All @@ -196,12 +189,19 @@ variables:
# Set to true to create a "persistent_notification" with debugging information.
debug: '{{ iif(enable_debug is defined, enable_debug, false) }}'

# Domains and models to filter in the target entities.
domains:
- light
- fan
models:
- Inovelli 2-in-1 switch + dimmer (VZM31-SN)
- Inovelli Fan Controller (VZM35-SN)

# Create a list of provided targets (areas, devices, entities)
target: '{{ target|default([])|map(lower) }}'
area: '{{ target.area_id|default([])|lower }}'
device: '{{ target.device_id|default([])|lower }}'
entity: '{{ target.entity_id|default([])|lower }}'
entity_id: '{{ entity_id|default([])|lower }}'
command_path_map: '{{ command_path_map|default([]) }}'
entity_list: >
{% set switch = namespace(entities=[]) %}
Expand All @@ -224,7 +224,7 @@ variables:
{# Detect switches #}
{% for area in areas.areas %}
{% for ent in area_entities(area) %}
{% if is_device_attr(ent, 'model', 'Inovelli 2-in-1 switch + dimmer (VZM31-SN)') and ent.split('.')[0] == 'light' %}
{% if device_attr(ent, 'model') in models and ent.split('.')[0] in domains %}
{% set switch.entities = switch.entities + [ent|string|trim] %}
{% endif %}
{% endfor %}
Expand All @@ -248,7 +248,7 @@ variables:
{# Detect switches #}
{% for device in devices.devices %}
{% for ent in device_entities(device) %}
{% if is_device_attr(ent, 'model', 'Inovelli 2-in-1 switch + dimmer (VZM31-SN)') and ent.split('.')[0] == 'light' %}
{% if device_attr(ent, 'model') in models and ent.split('.')[0] in domains %}
{% set switch.entities = switch.entities + [ent|string|trim] %}
{% endif %}
{% endfor %}
Expand All @@ -271,33 +271,13 @@ variables:
{% endif %}
{# Detect switches #}
{% for ent in entities.entities %}
{% if is_device_attr(ent, 'model', 'Inovelli 2-in-1 switch + dimmer (VZM31-SN)') and ent.split('.')[0] == 'light' %}
{% if device_attr(ent, 'model') in models and ent.split('.')[0] in domains %}
{% set switch.entities = switch.entities + [ent|string|trim] %}
{% endif %}
{% endfor %}
{% endif %}
{# Entity ID (backward compatibility) #}
{% set entities = namespace(entities=[]) %}
{% if entity_id %}
{# Convert to a list #}
{% if ',' in entity_id %}
{% set entitynum = entity_id.split(',') | count %}
{% for i in range(0, entitynum) %}
{% set entities.entities = entities.entities + [entity_id.split(',')[i]|string|trim ] %}
{% endfor %}
{% elif entity_id[0]|count == 1 %} {# if the first item in the list has only a single character, it can't be a valid entity #}
{% set entities.entities = entities.entities + [entity_id|string|trim] %}
{% else %}
{% set entities.entities = entity_id %}
{% endif %}
{# Detect switches #}
{% for ent in entities.entities %}
{% if is_device_attr(ent, 'model', 'Inovelli 2-in-1 switch + dimmer (VZM31-SN)') and ent.split('.')[0] == 'light' %}
{% set switch.entities = switch.entities + [ent|string|trim] %}
{% endif %}
{% endfor %}
{% endif %}
{# Output #}
{{ switch.entities|unique|list|lower }}
# Convert LED number to value
Expand All @@ -318,7 +298,7 @@ variables:
{{ led|int(default=-1) }}
{% endif %}
level: '{{ level|default(40) }}'
level: '{{ level|default(100) }}'

# Convert Color to value
colors:
Expand Down Expand Up @@ -444,8 +424,8 @@ variables:
{% endif %}
sequence:
# Set enable_debug = true above to provide output troubleshooting information.
- if:
- alias: "Debug: Send notification"
if:
- condition: template
value_template: '{{ debug == true }}'
then:
Expand All @@ -463,12 +443,35 @@ sequence:
duration: {{ duration }} ({{ duration_value }})
command_path_map: {{ command_path_map }}
# Do not continue if we don't have at least one entity
- condition: template
- alias: "Check for an empty entity list"
condition: template
value_template: |
{{ entity_list|count > 0 }}
# Loop through entity_list
- variables:
payload: |-
{% if led_value == -1 %}
{% set payload_data = {
"led_effect": {
"effect": effect_value,
"color": color_value,
"level": level,
"duration": duration_value,
}
} %}
{% else %}
{% set payload_data = {
"individual_led_effect": {
"led": led_value,
"effect": effect_value,
"color": color_value,
"level": level,
"duration": duration_value,
}
} %}
{% endif %}
{{ payload_data | tojson }}
- repeat:
for_each: '{{ entity_list }}'
sequence:
Expand All @@ -480,32 +483,9 @@ sequence:
{% else %}
zigbee2mqtt/{{ device_attr(repeat.item, "name") }}/set
{% endif %}
# Construct the payload
payload: |-
{% if led_value == -1 %}
{% set payload_data = {
"led_effect": {
"effect": effect_value,
"color": color_value,
"level": level,
"duration": duration_value,
}
} %}
{% else %}
{% set payload_data = {
"individual_led_effect": {
"led": led_value,
"effect": effect_value,
"color": color_value,
"level": level,
"duration": duration_value,
}
} %}
{% endif %}
{{ payload_data|tojson }}
# Set enable_debug = true above to provide output troubleshooting information
- if:
- alias: "Debug: Send notification"
if:
- condition: template
value_template: '{{ debug == true }}'
then:
Expand All @@ -519,8 +499,8 @@ sequence:
topic: {{ command_path }}
payload: {{ payload }}
# Send payload to the switch
- service: mqtt.publish
- alias: "Send payload to the switch"
service: mqtt.publish
data:
topic: '{{ command_path }}'
payload_template: "{{ payload }}"
Expand Down

0 comments on commit fb12e4e

Please sign in to comment.