Can Copy be used to create a superclass and used for inheritence?

My understanding, from the docs, of Copy is that it is used to create a 2nd object that will work with the same information in the original class. For example (from the docs):

# Example configuration entry
binary_sensor:
  - platform: copy
    source_id: source_binary_sensor
    name: "Copy of source_binary_sensor"

This creates a 2nd binary sensor that will be on and off at the same time as the original. It’s an active mirror.

I’m going to be using 4 binary sensors that all work alike, but each one connects to a different switch. So can I do this:

binary_sensor:
  #
  #Toggle switch group first, with master switch first in that group.
  #
  - platform: gpio
    id: "SensorSuperClass"
    name: "Sensor Super Class"
      number: ${DummyGPIOPinNumber}
    filters:
      delayed_on: 10ms
    device_class: power

Then, for another sensor, do this:

binary_sensor:
  - platform: copy
    source_id: SensorSuperClass
    id: Switch01
    pin:
      number: ${Sensor01GPIOPin}
      mode:
        input: true
        pulldown: true

By overriding the pin number in SensorSuperClass, I’m using the original as a superclass to make it easy to create the 4 Binary Sensor components that are all the same, except for their ids, names, and GPIO pin numbers. (In my setup, each sensor has a lot more info than what I’ve included here - I don’t think the extra details are needed.)

If I do this, will setting the pin number in a copy override the original pin setting in the source class? Is there a problem with using this technique to create a superclass and copy it multiple times to make similar objects where each one has distinct information?

I get that if I do this, anything that happens in the superclass is copied to the subclasses, but the superclass would not be used for anything other than as a model or template for the subclasses.

For what you want, yaml anchors are the solution.

1 Like

There’s also the packages-as-templates approach.

Probably not the best fit in this case though I think.

Packages looks like it would work great for what I’m doing. (I’m still reading up on anchors, since I have questions about them.)

I see packages as separate files. I take it there’s no way to create a package in the current file and to use that and include it in components?

I think yaml anchors would more be the approach to reuse yaml from within the existing file. I seem to recall hitting limitations with yaml anchors though. Maybe to do with merging lists..

Packages you store reused code in an external file. Here’s an example.

I’ve only used both occasionally so not really advanced on the topic. I think Clyde is pretty advanced on these things so anchors may be a good fit. Not sure.

Copy sensors are different again and not really for what you are doing.

I’m having trouble finding good examples for YAML anchors, especially ones that are more than one line or one element long.

I found a few examples, that, putting them together, gave me a clear idea of how to use anchors for more than a value in one line. (I can’t believe it was so hard to find something good and clear about this!) I found a good online YAML interpreter (don’t know why I had not looked for one before!) and combined some examples to find what works.

There’s one issue I’d still like to solve. I have a Binary Sensor in my config:

  - platform: gpio
    id: "Switch01"
    name: "Switch 1"
    web_server:
      sorting_group_id: sorting_group_switch1
      sorting_weight: 10
    pin:
      number: ${Switch1Pin}
      mode:
        input: true
        pulldown: true
    filters:
      delayed_on: 10ms
    device_class: power
    on_press:
      - logger.log: "DEBUG PRESS 1:     ==> Switch pressed"
      - logger.log:
          format: "DEBUG PRESS 2: Delay value: %.0f"
          args: 'id(TestDelay).state'
    on_release:
      - logger.log:
          format: "DEBUG RELEASE 1: Delay value: %.0f"
          args: 'id(TestDelay).state'
      - logger.log: "DEBUG RELEASE 2:    <== Switch released"

(My logging debugging statements are still in there.)

In the section pin, I use a substitution for the GPIO pin, so I can set pin numbers up at the top of the file and not have to hunt for that info if I change it later. That substitution would change from sensor to sensor, obviously, so the 2nd sensor would use Switch2Pin. That’s the one item that would change from component to component.

Since I have multiple lines under pin, what I don’t see is how I can use an anchor and alias and use << to merge data, but how can I replace just that one value in that one section? That’s the only item that changes from one switch or sensor to the others.


Edited to add: I was focused only on the functionality. There are several places where the sensor number is used - in the substitution where the GPIO pin is specified, but also in the fields for id, name, and sorting_group_id. There are also the debugging statements in the log output. Is there some way to handle that in YAML with anchors and a variable or substitution?