{ lib, ... }: let colorbulbs = [ { name = "Living Bulb 1"; id = "34945479BC57"; } { name = "Living Bulb 2"; id = "485519D9A1B2"; } { name = "Living Bulb 3"; id = "485519D9AE95"; } { name = "Living Bulb 4"; id = "485519D94A28"; } { name = "Living Bulb 5"; id = "485519DA6B6A"; } { name = "Living Bulb 6"; id = "485519D9E018"; } { name = "Bedroom Bulb 1"; id = "485519FBDF20"; } { name = "Bedroom Bulb 2"; id = "485519FBE171"; } { name = "Bedroom Bulb 3"; id = "485519FBE377"; } { name = "Bedroom Bulb 4"; id = "485519FC05C5"; } ]; switches = []; proswitches = [ { name = "Livingroom Switch"; id = "shellyplus2pm-e86beae5d5d8"; relay = "0"; } { name = "Kitchen Switch"; id = "shellyplus2pm-e86beae5d5d8"; relay = "1"; } { name = "Bedroom Switch"; id = "shelly1pmminig3-34b7da933fe0"; relay = "0"; } { name = "Hallway Circuit"; id = "shellypro3-c8f09e894448"; relay = "0"; } { name = "Bathroom Circuit"; id = "shellypro3-c8f09e894448"; relay = "1"; } { name = "Kitchen Circuit"; id = "shellypro3-c8f09e894448"; relay = "2"; } { name = "TV Switch"; id = "shelly1pmminig3-34b7da8b64c8"; relay = "0"; } ]; in { services.home-assistant.extraComponents = [ "shelly" ]; services.home-assistant.config = { mqtt = { switch = builtins.concatLists [ (builtins.map (switch: let unique_id = builtins.replaceStrings [" "] ["_"] switch.name; in { name = switch.name; unique_id = unique_id; state_topic = "shellies/shellyswitch25-${switch.id}/relay/${switch.relay}"; command_topic = "shellies/shellyswitch25-${switch.id}/relay/${switch.relay}/command"; payload_on = "on"; payload_off = "off"; } ) switches) (builtins.map (switch: let unique_id = builtins.replaceStrings [" "] ["_"] switch.name; in { name = switch.name; unique_id = unique_id; state_topic = "shellies/${switch.id}/status/switch:${switch.relay}"; value_template = "{{ value_json.output }}"; state_on = true; state_off = false; command_topic = "shellies/${switch.id}/rpc"; payload_on = "{\"id\":${switch.relay}, \"src\":\"homeassistant\", \"method\":\"Switch.Set\", \"params\":{\"id\":${switch.relay}, \"on\":true}}"; payload_off = "{\"id\":${switch.relay}, \"src\":\"homeassistant\", \"method\":\"Switch.Set\", \"params\":{\"id\":${switch.relay}, \"on\":false}}"; availability_topic = "shellies/${switch.id}/online"; payload_available = "true"; payload_not_available = "false"; } ) proswitches) ]; light = builtins.map (bulb: let unique_id = builtins.replaceStrings [" "] ["_"] bulb.name; in { name = bulb.name; unique_id = "${unique_id}"; schema = "template"; state_topic = "shellies/shellycolorbulb-${bulb.id}/color/0/status"; state_template = "{% if value_json.ison %}on{% else %}off{% endif %}"; command_topic = "shellies/shellycolorbulb-${bulb.id}/color/0/set"; command_on_template = '' { "turn": "on", "effect": 0, {%- if red is defined and green is defined and blue is defined -%} "mode": "color", "red": {{ red }}, "green": {{ green }}, "blue": {{ blue }}, {%- endif -%} {%- if brightness is defined -%} "gain": {{brightness | float | multiply(0.3922) | round(0)}}, "brightness": {{brightness | float | multiply(0.3922) | round(0)}}, {%- endif -%} {% if color_temp is defined %} "mode": "white", "temp":{{ (1/(color_temp | float)) | multiply(1000000) | round(0) }}, {% endif %} } ''; command_off_template = '' { "turn": "off" } ''; brightness_template = "{{ value_json.brightness | float | multiply(2.55) | round(0) }}"; color_temp_template = "{{ 1000000 | multiply(1/(value_json.temp | float)) | round(0) }}"; red_template = "{{ value_json.red }}"; green_template = "{{ value_json.green }}"; blue_template = "{{ value_json.blue }}"; max_mireds = 333; min_mireds = 154; }) colorbulbs; }; }; }