Adding a functional button entity #2
Replies: 44 comments 23 replies
-
|
I just found your TODOs: button and event. :) I have a use case for one or both! I'll bang my head against it for awhile but I'd sure like your input. I'm not sure I'm smart enough to do it alone. |
Beta Was this translation helpful? Give feedback.
-
|
Yea, I have not done the button as it is an 'event' and I have not looked at how to do that. Nice you found the I'll have a bit of a look and get back to you. |
Beta Was this translation helpful? Give feedback.
-
|
I ran some experiments, evidently buttons get "pressed" (according to HA) whenever the state changes, so if it becomes Unknown or Unavailable then that will trigger a Button Press automation. :-/ Nothing I can do about that. I've got an event being fired within the entity code, and it's publishing "something" to MQTT, but it's not the correct something. (I shoehorned in the entity stuff but it might give you an idea of what I'm thinking about.) I created an event in my app like this: Currently, the Event doesn't register in HA because the |
Beta Was this translation helpful? Give feedback.
-
|
@AlmostInteractive - I took at your PR. Nice! It makes me very happy to see that somebody can read my code and extend it. Thank you. I took a look at a button I have on my MQTT network. The question that we need to resolve is - how doe the client app model events? All of the entities to date are state entities. Something reports ON and in an hour if nothing is reported it is still ON. So the code tells the MQTT broker to retain this value. This means that if a client (HASS) is restarted it gets the current state. Events, I think, should not be retained as we do not want to be told the button is pressed every time HASS is restarted. So there is the first, not so big issue - do not retain events. In MQTT the events are otherwise pretty well the same as the states. When I press my button it will sent an event value of "single", "double", or "hold" (that I know of). After I release it, it seems to send an event value "" (empty string). So how does the model provide this? If we use properties, as we have to date, then the application would need to do the work of setting a string property to "single" for say 100ms and then to "". It does not feel right. Hmm. I'm wondering if a public event would be the go. That is rather than adding a button property we add a button event on the class and listed to events ... that feel right. Doable but different to all the others :-) So a model may look something like: The bit I do not like is that is exposes a N2H data type I'm still pondering ... does this make any sense? Your thoughts? |
Beta Was this translation helpful? Give feedback.
-
|
Sounds like we are both on the same wavelength. I'll give your code a run and get back to you. |
Beta Was this translation helpful? Give feedback.
-
|
I fixed point 2 above. :) It's now correctly publishing the AdditionalOptions in the entity config! It's publishing on event detection, but I don't think it's publishing the correct information. I can't get it to trigger anything in HA. I'm on the other side of the world so I'll catch you when I wake up. |
Beta Was this translation helpful? Give feedback.
-
|
What I notice is that the publish payload just has "press". Look at the differences between battery_1_charging and test_event here seen in MQTT Explorer: The configuration has: but no "state" property in the payload. The battery_1_charging looks like this: |
Beta Was this translation helpful? Give feedback.
-
|
I do not know but I think you may want to send 2 events. "press" then "" or "None" as the press is a momentary event. FYI - I capture button press events in Node-RED by an event: |
Beta Was this translation helpful? Give feedback.
-
|
I think you want a payload like:
|
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
|
Okay, so it seems to be working with |
Beta Was this translation helpful? Give feedback.
-
|
End of day update: It works. I've got it so the Right now it's hardcoded to send "press" but it shouldn't be too hard to make it a generic payload event with "event_type" being required. It kinda bugs me how I shoehorned in Subscribe/Unsubscribe but I'm not really sure how to change it. |
Beta Was this translation helpful? Give feedback.
-
Great to hear :-)
Is this the subscribe/unsubscribe methods on the model? |
Beta Was this translation helpful? Give feedback.
-
|
I've created a branch in your repo off your working branch. I think the code needs refactoring to make way for easier event implementation. I'm removing base class between attributes and entities and renaming the EntityBase class to StateEntityBase class. The intent is be able to add an EventEntityBase type so there is clear separation ... see how it goes. I may abandon this ... see how it goes :-) |
Beta Was this translation helpful? Give feedback.
-
|
Excellent. That sounds great. :-) |
Beta Was this translation helpful? Give feedback.
-
|
I see, That helps improve my understanding. Thx.
Wow ...that blows a lot of my thinking. I need somebody hands on ... thank you. Big help. Nothing decided in my mind. |
Beta Was this translation helpful? Give feedback.
-
|
Hows about: |
Beta Was this translation helpful? Give feedback.
-
|
Unless I dump JSON into "value", essentially turning it into a Dictionary, there'll be use cases that fall outside the With any number of variable combinations requiring a new configuration and class. I could, but I wouldn't; I'd implement a DictionaryEventArgs on the client side and use that. 😂 I could dump it all into JSON and shove that in "value" but that seems less user friendly. As long as I can get a key-value pair somewhere into the event to be parsed and sent along to HA, I'll be happy. |
Beta Was this translation helpful? Give feedback.
-
|
My last post today, see ya. Further brain storming... |
Beta Was this translation helpful? Give feedback.
-
|
I merged that branch into my main. We can do some refinements (such as the ones you proposed) before we PR it into your code. I checked with HA to see if it could handle a kvp event payload and it worked, as expected. |
Beta Was this translation helpful? Give feedback.
-
|
So I've got this version built into my app and I'm loving it. I hooked up the keyboard Super happy with this! |
Beta Was this translation helpful? Give feedback.
-
|
I wonder if you have used other parts of N2H. I want to get an idea of what bits have been used by somebody other than me. |
Beta Was this translation helpful? Give feedback.
-
|
This is my first time using your library and I'm glad I could contribute something to it. Here's my usage, if you're curious: https://github.com/AlmostInteractive/HAcomms/blob/master/HAcomms/Models/HAcommsModel.cs |
Beta Was this translation helpful? Give feedback.
-
|
Hey @RobSmyth sorry I've been incommunicado. I just moved and was without power. It's a whole thing. Anyway! What do you think about my current branch? It's been working great for me, except for one thing that isn't new to my code and I don't know yet how to get around. Events and Buttons get triggered when the application starts because they get updated. This results in a phantom activation of my Events and also on the Button handler app-side. I have an idea about making a "HaPulseEvent" which will set state and then set it back to "unknown" moments later which I think will help my use case. But anyway, do you feel it's ready to merge into your code base? |
Beta Was this translation helpful? Give feedback.
-
|
Hi! I've been working on automated versioning for N2H, and other projects, so I've not got back to looking at this. But yes, I do want to merge it into N2H. I'm not sure about how to do the event bit, so I propose you raise a PR to a feature branch off master and I can make what changes I like before merging to master. Does that sound good? Having a look at the Events and Buttons triggering on start. |
Beta Was this translation helpful? Give feedback.
-
|
Neat. 👍 I opened the PR. If you figure out the trigger-on-start thing, that would be cool, but I think it's an HA thing. I tested with my S3 Box3 running ESPHome and its buttons triggered at startup too. Doesn't mean there isn't a hack-around though. 😄 |
Beta Was this translation helpful? Give feedback.
-
|
On the triggering on start ... not sure I have the code, can you point me at some lines of how/why they get updated at start? I see |
Beta Was this translation helpful? Give feedback.
-
|
Great! Merged, I'll sort out my versioning and get another early dev release out soon. |
Beta Was this translation helpful? Give feedback.
-
|
Even if not related to your event on start issue ... I'm fixing the retain flag now. Moving from client to each event that supports retain. |
Beta Was this translation helpful? Give feedback.
-
|
I've updated the code the N2H button branch. If you would like to have a look the class Interesting ... I now understand that HASS is looking for the
These events are now merged into the message and sent the same as other messages. I'm now making the event configurable as a pulse as a HASS workaround. WIP. Thinking that it will treat the first event type as the steady (or idle) state and always publish immediately after an event. I feel dirty doing a work around, but it is a configuration option :-). |
Beta Was this translation helpful? Give feedback.





Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey @RobSmyth , I was hoping I might borrow some of your genius. I'd like to make a ButtonEntity but I'm a little lacking in experience. I added the entity description to the
HassTypesGeneratorDefinition.csfile and while it shows up and the button works from HA -> App, two things are still wrong:/{button_name}/configwhich, for some reason, activates a button press in HA (the event description is: "{button name} changed to {date_time of event}")Here's a link to my changes, insufficient as they are. Do you have any thoughts about how to resolve those issues?
https://github.com/AlmostInteractive/Net2HassMqtt/compare/main...AlmostInteractive:Net2HassMqtt:buttonEntity?expand=1
Beta Was this translation helpful? Give feedback.
All reactions