esphome / feature-requests

ESPHome Feature Request Tracker
https://esphome.io/
412 stars 26 forks source link

Substitutions per package #1928

Open mariusz-schimke opened 1 year ago

mariusz-schimke commented 1 year ago

Describe the problem you have/What new integration you would like

The idea I have is making it possible to optionally pass substitutions to individual packages like this:

packages:
  config: !include device-config.yaml
    substitutions:
      my_config_param1: my config value 1
      my_config_param2: my config value 2
  control: !include device-control-entities.yaml
  diagnostics: !include device-diagnostics-entities.yaml

Please describe your use case for this integration and alternatives you've tried:

I made use of packages for my use case, which is a gas/water/electricity meter based on pulse counting. I extracted the whole metering logic to a separate package to be able to reuse it for different meters. Also, I used substitutions to be able to specify parameters like name, friendly name, pulse per unit, unit, icon etc.

Now I would like to create a multi-purpose device, where metering will be only one of its features. But since substitutions are global, I will have to use prefixes to distinguish those parameters that are specific to the metering logic, not to confuse them with those that will be used for other features. But then the prefix will seem like a redundant piece of text from the point of view of the single-purpose devices, where the metering package represents the only purpose the devices are used for.

So this

substitutions:
  # general parameters
  name: gas-meter
  friendly_name: "Gas meter"

  # meter-specific parameters
  device_class: gas
  unit_of_measurement: "m³"
  accuracy_decimals: "2"
  pulses_per_unit: "100"
  max_pulse_interval_s: "60"
  medium_icon: "mdi:counter"

  log_level: INFO

would have to be transformed to this to be clear in terms what context it is used in

substitutions:
  # general parameters
  name: my-multifunctional-device
  friendly_name: "My multifunctional device"

  # meter-specific parameters
  meter_device_class: gas
  meter_unit_of_measurement: "m³"
  meter_accuracy_decimals: "2"
  meter_pulses_per_unit: "100"
  meter_max_pulse_interval_s: "60"
  meter_medium_icon: "mdi:counter"

  log_level: INFO

But I would rather want to have it configured like this (or so):

packages:
  config: !include device-config.yaml
  control: !include device-control-entities.yaml
  diagnostics: !include device-diagnostics-entities.yaml
  meter: !include custom-pulse-meter.yaml
    substitutions:
      device_class: gas
      unit_of_measurement: "m³"
      accuracy_decimals: "2"
      pulses_per_unit: "100"
      max_pulse_interval_s: "60"
      medium_icon: "mdi:counter"

Additional context

eduperez commented 1 year ago

I think it would also be interesting to have "substitutions per include":

Instead of and endless specification of bluetooth sensors like this:

sensor:

  - platform: atc_mithermometer
    mac_address: "A4:C1:38:5B:0E:DF"
    temperature:
      name: "ATC1 Temperature"
    humidity:
      name: "ATC1 Humidity"
    battery_level:
      name: "ATC1 Battery"
    battery_voltage:
      name: "ATC1 Voltage"
  - platform: ble_rssi
    mac_address: "A4:C1:38:5B:0E:DF"
    name: "ATC1 RSSI"

  - platform: atc_mithermometer
    mac_address: "A4:C1:38:02:7E:11"
    [...]

It would be nice to be able to define one template:

  - platform: atc_mithermometer
    mac_address: "${MAC}"
    temperature:
      name: "${ID} Temperature"
    humidity:
      name: "${ID} Humidity"
    battery_level:
      name: "${ID} Battery"
    battery_voltage:
      name: "${ID} Voltage"
  - platform: ble_rssi
    mac_address: "${MAC}"
    name: "${ID} RSSI"

And then invoke it with a single line per sensor:

sensor:
    - <<: !include atc_sensor.yaml ATC1 A4:C1:38:5B:0E:DF
    - <<: !include atc_sensor.yaml ATC2 A4:C1:38:02:7E:11
    [...]
ssieb commented 1 year ago

https://github.com/esphome/esphome/pull/3510