How to extend a function for an Entity

Hello folks,
I’m studying some option. I came across that for instance it is possible to overwrite a class in a manner to get more functions. So I saw the ToggleEntity for a switch and I’d like to add one more function to that.

The function consists to make a pulse on an output, without having to involve the entire system to trigger a switch.turn_on, delay, switch.turn_off cycle.

That’s frequently necessary for those devices which needing just a short pulse to initiate the action. Some of them might be register themselves as cover, because they have limit switches, whereas other won’t have any feedback, so it would be merely relied to a counter to keep track of their current state.

So I was writing a little function, which just call turn_on and stay a bit idle and then will call turn_off, perhaps without living the function. But I didn’t find the method to register such new function…

    @property
    def pulser(self, **kwargs):
        """Trigger the cover."""
         asus_gpio.write_output(self._port, self._invert_logic ^ 1)
        sleep(self._relay_time)
        self._state = False
         asus_gpio.write_output(self._port, self._invert_logic)
        return asus_gpio.read_input(port)

Definitely on my way to make asus GPIO working :stuck_out_tongue:

Question: how the new function will be enlisted ?

Sorry, I don’t have a good answer for you.

Can you point me to where you found that it is possible to extend stock Home Assistant classes? Is this an approach supported by the Home Assistant project, or is it more of a hack?

I’m not sure how much free time you have available, but might consider making a pull request to the Home Assistant project to include your pulse operation in stock HA. This could be useful for others as well.

https://developers.home-assistant.io/docs/en/entity_index.html

Indeed I couldn’t find a mention. I expect that we may extend any class functionality as per python permits.

For an instance the current intent is taken from the ToggleEntity class, which is a modified class of Switch. So as many can put a different behavior in a python class I just need more detail how this may be added to my intent. Fully conscious that i may fail, but I’m experimenting.

Maybe I am misunderstanding what you are trying to accomplish, but are you using configuration.yaml? Have you considered creating a script that performs a pulse? That script could be triggered by a toggle switch. That’s likely the simplest way to achieve your goal, and would only require a dozen or so short lines of yaml.

For a more involved approach, you could create a custom component (call it ‘pulse-switch’ or something) by copying the source code of the switch component and modifying it to do a pulse whenever it is toggled. That won’t give all switches a pulse feature - only pulse-switches. However, it might be easier than modifying and maintaining a separate fork of Home Assistant.

If you’re dead set on modifying the HA source, then you’ll need to modify the ToggleEntity function: toggle and async_toggle to perform a asynchronous pulse operation or create a new pulse function and add a new service for it in the switch component. I wouldn’t recommend doing this for a number of reasons.

More on custom components:

You can also expose generic python scripts as services. That might be an easy way to get what you want.

I did it

pulse_gate:
  sequence:
    - service: switch.turn_on
      entity_id: switch.gate
        milliseconds: 600
    - service: switch.turn_off
      entity_id: switch.gate

It’s my impression that it takes a long trip by using the HA infrastucture, like to move few step East by going to West :smiley:
For some aspect it’s the correctest formula, which won’t hold the system waiting the event to complete. But the period is definitely unaccepted, below 1 second slice.

I’m considering the fact that my development is just for my own use, entirely responsible for any event. Furthermore I’m not expecting to publish a solution if it won’t follow the HA development rules.
As that said, I just like to replicate the RPi_GPIO (that Asus got started as such) and experiment my own little function as leaning something from HA.

In the second option I’m considering how these are working, I’ll study later, looks that will work by giving the above snippet to process. Also another aspect, using appdaemon as a part of the game. But about the latter, I’m thinking why there should be a fork on HA, when HA can do same pythonic solutions.