ShellyForHass - Roller with toggle button - Longclick

Hi,

I’m using now ShellyForHass for my Shelly1 and Shelly2.5 devices.
My Rollers (covers) have toggle buttons.
According to Shelly settings (at least as far as I could find), there’s no long click for rollers, only for relays. So I made one using AppDaemon. Hopefully you will find it useful as I do.

(I’m using the long click for setting the roller to specific position. Theoretically, using ShellyForHass 0.1.9, an even named ‘shellyforhass.click’ should be available, but it isn’t working for me. No event fires when listening to “shellyforhass.click”)

This is the code:

class shelly_longclick(hass.Hass):

  def initialize (self): 
    self.run_in(self.init, 30) # This solves some race between AD and HA after HA restarts and Shelly sends some info. Not sure what, but reality strikes with some rollers moving by themselves after a restart if not waiting here...
         
  def init (self, kwargs):    
    # get all covers
    self.covers = dict.fromkeys(self.get_state("cover"))
    for cover in self.covers:
        # Setting the position I want for long click
        if cover == "cover.shelly_roller_boys":
            self.covers[cover] = {"position": 34, "longClickEvent": False}
        elif cover == "cover.shelly_roller_porch_big":
            self.covers[cover] = {"position": 27, "longClickEvent": False}
        elif cover == "cover.shelly_roller_porch_small":
            self.covers[cover] = {"position": 32, "longClickEvent": False}
        elif cover == "cover.shelly_roller_salon_big":
            self.covers[cover] = {"position": 24, "longClickEvent": False}
        elif cover == "cover.shelly_roller_stairs":
            self.covers[cover] = {"position": 32, "longClickEvent": False}
        elif cover == "cover.shelly_roller_parents":
            self.covers[cover] = {"position": 30, "longClickEvent": False}
        else:
            self.covers[cover] = {"position": 34, "longClickEvent": False}
        
    self.listen_event(self.shelly_event_fired, "shelly_switch_click")
    
  def shelly_event_fired (self, event_name, data, kwargs):
    if "shelly_roller" not in data["entity_id"]: return   # Here you can change for other entities but rollers
     # The event comes from the "binary_sensor.shelly_roller_XYZ_switch" or "binary_sensor.shelly_roller_XYZ_switch_2"
    _entityId = data["entity_id"].replace("binary_sensor", "cover").replace("_switch_2", "").replace("_switch", "")
    _position = 0
    
    if data["click_cnt"] == 1 and data["state"] == True: # Start of long click
        self.covers[_entityId]["longClickEvent"] = True
        return
        
    if data["click_cnt"] == 1 and data["state"] == False and self.covers[_entityId]["longClickEvent"] == True: # End of long click
        self.covers[_entityId]["longClickEvent"] = False
        self.call_service("cover/set_cover_position", entity_id = _entityId, position = self.covers[_entityId]["position"]) 
    
    return

This works for long click up or down. You can separate the up long click from the down long click by distinguishing between …switch and …switch_2.

Cheers.