{ pkgs, lib, ... }:
let

  # https://www.zigbee2mqtt.io/devices/E1757.html
  allDevices = {
    "office_fyrtur_1" = { id = "0x680ae2fffe64fa40"; };
    "office_fyrtur_2" = { id = "0x680ae2fffe91d234"; };
    "bedroom_fyrtur_1" = { id = "0x680ae2fffe6e9f41"; };
    "broken_fyrtur_1" = { id = "0x680ae2fffe8f6411"; };
  };

  # -t "zigbee2mqtt/fyrtur1/set"   -m '{"position":100}'
  # -t "zigbee2mqtt/fyrtur1/set"   -m '{"position":15}'
in
{

  services.zigbee2mqttConfiguration = lib.mapAttrs'
    (name:
      { id, ... }: {
        name = id;
        value = {
          retain = false;
          friendly_name = name;
          transition = 0.1;
        };
      })
    allDevices;

  services.homeAssistantConfig = {

    sensor = lib.flatten (lib.mapAttrsToList
      (name:
        { ... }: [
          {
            name = "battery_${name}";
            platform = "mqtt";
            state_topic = "zigbee2mqtt/${name}";
            availability_topic = "zigbee2mqtt/bridge/state";
            unit_of_measurement = "%";
            icon = "mdi:battery-10";
            value_template = "{{ value_json.battery }}";
          }
          {
            name = "link_${name}";
            platform = "mqtt";
            state_topic = "zigbee2mqtt/${name}";
            availability_topic = "zigbee2mqtt/bridge/state";
            icon = "mdi:signal";
            unit_of_measurement = "lqi";
            value_template = "{{ value_json.linkquality }}";
          }
        ])
      allDevices);

  };

}