One device with two entities

In a custom_component, one switch device have two switch, onoff and onoff2. Now the onoff is set as an entities and onoff2 as an attribute of the first one. Is it possible to have one device with two entities defined. First one with onoff switch and the second one as onoff2 switch.

switch:
  - platform: template
    switches:
      my_second_switch:
        value_template: "{{ state_attr('switch.my_first_switch','onoff2') }}"
        turn_on:
          service: script.some_script
        turn_off:
          service_: script.some_other_script

That’s the basics of what you would need to do.
It’s not really possible to help much more based on the limited information.

Thank you, as I’m the developper of the custom_component I would like to code the second onoff (onoff2) as a separated entities directly in my custom_component, switch.py so the user will see two separate switch entities from the same device.

You might be better of on the discord channel, I’m not sure there is much help here for people making their own custom components. Other than pointing people to the documentation.

This suggests though you just call add_entities for each entity you want to add?

Thanks will go to discord. My custom_component is in HA since many years and stable. But it’s the first time I have to include a device with two separated switch. Other devices are thermostats, light, sensor, valve etc which have only one on/off action.

Simple answer is create another class in switch.py for your second switch and in async_add_entities, pass an array with both to it.

You could also have 1 class that handles both switches but create an instance of the class, passing a variable that defines which switch and add these to the array passdd to async_add_entities.

Thanks, do you know where I can find example of two class or one class with two entities

You can have a look at this one.

Thanks that gave me some idea but in this example it is two device type, sensor and panel. On my side it’s one device with two physical output that can be turned on/off separately. When the entities is created the first output si set as the on/off of the device and the second output is added as an attribute. My goal is two create two entities out of one device. Is it possible? This way I’ll have two on/off entities easy to activate without the need of creating template switch for the second output.
My switch.py already support many different device: outlet, valve, power controler which have only one on/off output. But one multi controler come with two physical output that can be activated separately and two input that can be activated separately. So I would like to create two entyties, one for output 1 and one for output 2
Thanks for your help

I think you are misunderstanding. Post link to your code and i will try to give you an example of how to modify it to work.

EDIT: to explain more.

switch.py is creating entities not devices as such. Therefore you can create many switches in this file and tie them to a single device with the device info information being the same.

Thanks, this is my repo. The file that need to be modified is switch.py. At the top you can see all suported devices. The one that I want to add second output is the MC3100ZB Multi-Controller, model 2180 and 2181. At this moment the switch control the first output and the second one is added as an attribute of the first one. I want two separated switch.

OK, much easier to understand with the code.

So, my advice would be to add the following at line 464 (so just before your current entities.append line - but do keep that line)

if device_info["signature"]["model"] in IMPLEMENTED_SED_DEVICE_CONTROL \
  or device_info["signature"]["model"] in IMPLEMENTED_ZB_DEVICE_CONTROL:
    entities.append(Neviweb130Switch(data, device_info, device_name, device_sku, device_firmware, "onoff2"))

and then in your Naviweb130Switch class amend your functions so that this device_type of “onoff2” (or whatever you want to call it), will allow control of this switch.

I would add that using 1 class as you have maybe a very complicated way of doing it (I certainly find hard to read as it is trying to do so many things in 1 class) and it may have been better to create different classes for Naviweb130Switch for each of your major types.

I.e. Naviweb130Switch_Power, Naviweb130Switch_Outlet etc.

You can create a base class for common functions/properties and inherit that into each of these classes ie:

class Naviweb130BaseSwitch(SwitchEntity)
....common properties and functions

class Naviweb130Switch_Power(Naviweb130BaseSwitch)
.....override base properties and functions and add additional ones

class Naviweb130Switch_WifiValve(Naviweb130BaseSwitch)
.....override base properties and functions and add additional ones

and then just add these to your entities list in async_setup_platform. ie

async def async_setup_platform(
    hass,
    config,
    async_add_entities,
    discovery_info=None,
):
    """Set up the Neviweb130 switch."""
    data = hass.data[DOMAIN]

    entities = []
    for device_info in data.neviweb130_client.gateway_data:
        if "signature" in device_info and \
            "model" in device_info["signature"] and \
            device_info["signature"]["model"] in IMPLEMENTED_DEVICE_MODEL:
            device_name = '{} {}'.format(DEFAULT_NAME, device_info["name"])
            device_sku = device_info["sku"]
            device_firmware = "{}.{}.{}".format(device_info["signature"]["softVersion"]["major"],device_info["signature"]["softVersion"]["middle"],device_info["signature"]["softVersion"]["minor"])
            if device_info["signature"]["model"] in IMPLEMENTED_WATER_HEATER_LOAD_MODEL \
              or device_info["signature"]["model"] in IMPLEMENTED_LOAD_DEVICES \
              or device_info["signature"]["model"] in IMPLEMENTED_WIFI_WATER_HEATER_LOAD_MODEL:
                entities.append(Neviweb130SwitchPower(data, device_info, device_name, device_sku, device_firmware))
            elif device_info["signature"]["model"] in IMPLEMENTED_WALL_DEVICES:
                 entities.append(Neviweb130SwitchOutlet(data, device_info, device_name, device_sku, device_firmware))
            
etc
etc

You could then have also created a Naviweb130SwitchAdditional class for your second on/off and had a small class to handle that.

Anyway, hope that helps point you in the right direction.

Thank you. At the beginning there was only one device type but with years that custom_component grow to a point that I need to reformat my code to make it simpler. That’s what I want for 2024.
As I want to keep the device with the output1 and add another to manage output2 I did the following:

if device_info["signature"]["model"] not in IMPLEMENTED_SED_DEVICE_CONTROL and \
                device_info["signature"]["model"] not in IMPLEMENTED_ZB_DEVICE_CONTROL:
 some code
...
else:
    for x in [1,2]:
        device_type = "sensor"
        device_sku = device_info["sku"]
        device_firmware = "{}.{}.{}".format(device_info["signature"]["softVersion"]["major"],device_info["signature"]["softVersion"]["middle"],device_info["signature"]["softVersion"]["minor"])
        if x == 1:
            device_name = '{} {}'.format(DEFAULT_NAME, device_info["name"])
            device_output = 1
            entities.append(Neviweb130Switch(data, device_info, device_name, device_sku, device_output, device_firmware, device_type))
        else:
            device_name = '{} {}'.format(DEFAULT_NAME, device_info["name"]+"_output2")
            device_output = 2
            entities.append(Neviweb130Switch(data, device_info, device_name, device_sku, device_output, device_firmware, device_type))

In the log I see that both device are initialized and updated but, and I think that it is because both devices have the same id nummber I see only the first one in the dev-tools states.
Log:
Setting up neviweb130 switch MC3100ZB: {‘id’: 258171…
Setting up neviweb130 switch MC3100ZB_output2: {‘id’: 258171…

Updating neviweb130 switch MC3100ZB
Updating neviweb130 switch MC3100ZB_output2

so I can’t have two entities for the same device.

I got that message in HA log:
[homeassistant.components.switch] Platform neviweb130 does not generate unique IDs. ID 258171 already exists - ignoring switch.neviweb130_switch_mc3100zb
which is my problem to set the second entities.

Im gonna make a big code cleanup first and see how I can manage to have two entities.

Thank you