[AppDaemon] How to call a service (Example)

Hi,

I have a button that calls a service like:

show_name: true
show_icon: true
type: button
tap_action:
  action: call-service
  service: vacuum.send_command
  data:
    command: clean_V2
    params:
      act: start
      content:
        type: customArea
        total: 0
        donotClean: 1
        count: 1
        value: 304,-1037,304,-1037
  target:
    area_id: wohnzimmer
    device_id: 6c93d4b0afb03cb1e4e4b3888af14da3
    entity_id: vacuum.deebot_t8_aivi
entity: vacuum.deebot_t8_aivi
name: Move to VorzimmertĂĽre

I really tried hard to call exactly the same service from AppDaemon but I failed.

I do noticed the: call_service method but I still have no plan how to use it.
I would have expected something like:

        self.staubsauger = self.get_entity("vacuum.deebot_t8_aivi")
        self.staubsauger.call_service("vacuum.send_command", command="clean_V2", params={act: start, content: {type: customArea, total: 0, dontClean: 1, count: 1, value: [304,-1037,304,-1037]}})

But this does not work and I am not really knowing what I am doing.

I hope someone can help me out here.

Just saw this and I am sure you got your answer on the discord chat!

1 Like

Yes I got my answer. Thanks a lot for helping me.

Here is my implementation of the solution:

    # ----------------------------------------------------------------------------------
    def initialize(self):

        # create staubsauger object
        self.staubsauger = self.get_entity("vacuum.deebot_t8_aivi")

    # ----------------------------------------------------------------------------------
    def device_move_to_coordinates(self, s_pos):
        self.staubsauger.call_service(
            service="send_command",
            command="clean_V2",
            params={
                "act": "start",
                "content": {
                    "type": "customArea",
                    "total": 0,
                    "donotClean": 1,
                    "count": 1, 
                    "value": f"{s_pos},{s_pos}",    # needs two points that are overlapping: x,y,x,y
                },
            },
        ) 
		
    # ----------------------------------------------------------------------------------
    def device_clean_rooms(self, s_room_numbers):
        self.staubsauger.call_service(
            service="send_command",
            command="spot_area",
            params={"rooms": f"{s_room_numbers}", "cleanings": 1},
        ) 

    # ----------------------------------------------------------------------------------
    def device_clean_area(self, s_area_coordinates):
        self.staubsauger.call_service(
            service="send_command",
            command="custom_area",
            params={"coordinates": f"{s_area_coordinates}"},
        )

I’m going to contribute a simpler example - turning a switch on or off.

In Home Assistant → Developer Tools → Services I can turn a switch off like this - on is similarly trivial.

service: switch.turn_off
data: {}
target:
  entity_id: switch.athom_plug_1

To do this in an app I use this code. As a beginner I’m not 100% sure this is the best way to do things, but it works. Note that above the service is “switch.turn_off” but in the code you simply use “turn_off”. It would be interesting if someone could provide clarity.

from appdaemon.plugins.hass import hassapi as hass

class ChangeStateTest(hass.Hass):

    def initialize(self):
        self.log("Initialising / reloading class ChangeStateTest")

        athome_plug_entity = self.get_entity("switch.athom_plug_1")
        athome_plug_state = athome_plug_entity.get_state(attribute="state")
        self.log(f'athom plug state is {athome_plug_state} type {type(athome_plug_state)}')

        if (athome_plug_state == 'off'):
            self.log("Plug is off. Turning on")
            athome_plug_entity.call_service('turn_on') 
            self.log("Plug turned on")

        if (athome_plug_state == 'on'):
            self.log("Plug is off. Turning off")
            athome_plug_entity.call_service('turn_off')
            self.log("Plug turned off")