400 Client Error in call_service using broadlink send

I’m trying to send an IR packet to a Broadlink RM Mini3 in an appdaemon app.
Worth mentioning that I succeeded to do it via the Services tab in the Developer Tools. So, this works perfectly:
image

However, inside an appdaemon app using:

self.call_service("broadlink/send", data={"host": "192.168.88.146", "packet": "JgCwAFceDh4ODw4PDx0dDw0QDhANEA0QDg4QDg8PDRAPDg4QDg8dDw0eDw8OAAq2Vh4OHg0QDg8OHh0PDg4PEA4PDRANEA4PDhAODw8ODhAODh0QDR8NEA0ACrdWHg8dDg8OEA0eHQ8PDg4QDRANEA4PDhANEA0QDhAODw4PHQ8OHg0QDQAKtlYfDh4NEA0QDh4cEA0QDg8OEA0QDRANEA4PDhAODw8ODhAdDg8dDhAOAA0FAAAAAAAAAAA=="})

this error arises:

2019-10-30 00:15:54.899006 WARNING AppDaemon: ------------------------------------------------------------
2019-10-30 00:15:54.899375 WARNING AppDaemon: Unexpected error in worker for App control_canales:
2019-10-30 00:15:54.899855 WARNING AppDaemon: Worker Ags: {'name': 'control_canales', 'id': UUID('81f7ec31-7881-4dea-90b1-63130c28c8d8'), 'type': 'attr', 'function': <bound method Canales.toogle_tv of <canales.Canales object at 0x7f6770331e10>>, 'attribute': 'state', 'entity': 'input_boolean.power_telesalon', 'new_state': 'on', 'old_state': 'off', 'kwargs': {'handle': UUID('78b3a082-c054-4532-903e-7a42cc8055f6')}}
2019-10-30 00:15:54.900071 WARNING AppDaemon: ------------------------------------------------------------
2019-10-30 00:15:54.901224 WARNING AppDaemon: Traceback (most recent call last):
  File "/home/homeassistant/appdaemon/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 595, in worker
    self.sanitize_state_kwargs(app, args["kwargs"]))
  File "/home/homeassistant/appdaemon/apps/canales.py", line 21, in toogle_tv
    self.call_service("broadlink/send", data={"host": "192.168.88.146", "packet": "JgCwAFceDh4ODw4PDx0dDw0QDhANEA0QDg4QDg8PDRAPDg4QDg8dDw0eDw8OAAq2Vh4OHg0QDg8OHh0PDg4PEA4PDRANEA4PDhAODw8ODhAODh0QDR8NEA0ACrdWHg8dDg8OEA0eHQ8PDg4QDRANEA4PDhANEA0QDhAODw4PHQ8OHg0QDQAKtlYfDh4NEA0QDh4cEA0QDg8OEA0QDRANEA4PDhAODw8ODhAdDg8dDhAOAA0FAAAAAAAAAAA=="})
  File "/home/homeassistant/appdaemon/lib/python3.6/site-packages/appdaemon/plugins/hass/hassapi.py", line 22, in func_wrapper
    return func(*args, **kwargs)
  File "/home/homeassistant/appdaemon/lib/python3.6/site-packages/appdaemon/plugins/hass/hassapi.py", line 522, in call_service
    r.raise_for_status()
  File "/home/homeassistant/appdaemon/lib/python3.6/site-packages/requests/models.py", line 940, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: http://192.168.88.8:8123/api/services/broadlink/send

2019-10-30 00:15:54.901565 WARNING AppDaemon: ------------------------------------------------------------

I’ve tried many different ways to put the host and packet arguments but all of them result in an “invalid syntax” error. For instance:

2019-10-30 22:33:31.642974 WARNING AppDaemon: ------------------------------------------------------------
2019-10-30 22:33:31.643405 WARNING AppDaemon: Unexpected error loading module: /home/homeassistant/appdaemon/apps/canales.py:
2019-10-30 22:33:31.643643 WARNING AppDaemon: ------------------------------------------------------------
2019-10-30 22:33:31.644459 WARNING AppDaemon: Traceback (most recent call last):
  File "/home/homeassistant/appdaemon/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 2026, in check_app_updates
    self.read_app(mod["name"], mod["reload"])
  File "/home/homeassistant/appdaemon/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 1795, in read_app
    importlib.reload(self.modules[module_name])
  File "/usr/lib/python3.6/importlib/__init__.py", line 166, in reload
    _bootstrap._exec(spec, module)
  File "<frozen importlib._bootstrap>", line 618, in _exec
  File "<frozen importlib._bootstrap_external>", line 674, in exec_module
  File "<frozen importlib._bootstrap_external>", line 781, in get_code
  File "<frozen importlib._bootstrap_external>", line 741, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/homeassistant/appdaemon/apps/canales.py", line 21
    self.call_service("broadlink/send", host= "192.168.88.146", packet= "JgCwAFceDh4ODw4PDx0dDw0QDhANEA0QDg4QDg8PDRAPDg4QDg8dDw0eDw8OAAq2Vh4OHg0QDg8OHh0PDg4PEA4PDRANEA4PDhAODw8ODhAODh0QDR8NEA0ACrdWHg8dDg8OEA0eHQ8PDg4QDRANEA4PDhANEA0QDhAODw4PHQ8OHg0QDQAKtlYfDh4NEA0QDh4cEA0QDg8OEA0QDRANEA4PDhAODw8ODhAdDg8dDhAOAA0FAAAAAAAAAAA=="}) # Power button
                                                                                                                                                                                                                                                                                                                                       ^
SyntaxError: invalid syntax

2019-10-30 22:33:31.644880 WARNING AppDaemon: ------------------------------------------------------------

Any hint would be appreciated. Thanks!

PS: This is my system health:

arch	x86_64
dev	false
docker	false
hassio	false
os_name	Linux
python_version	3.6.8
timezone	Europe/Madrid
version	0.99.2
virtualenv	true

Hmm I don’t see anything wrong.
Did you try putting the data in a python dict and and pass the dict to self.call_service like this?

data = {
    "host": "192.168.88.146",
    "packet": "JgCwAFceDh4ODw4PDx0dDw0QDhANEA0QDg4QDg8PDRAPDg4QDg8dDw0eDw8OAAq2Vh4OHg0QDg8OHh0PDg4PEA4PDRANEA4PDhAODw8ODhAODh0QDR8NEA0ACrdWHg8dDg8OEA0eHQ8PDg4QDRANEA4PDhANEA0QDhAODw4PHQ8OHg0QDQAKtlYfDh4NEA0QDh4cEA0QDg8OEA0QDRANEA4PDhAODw8ODhAdDg8dDhAOAA0FAAAAAAAAAAA=="
}
self.call_service("broadlink/send", data=data)

Or maybe try converting the dict to JSON and pass this to self.call_service something like this:

data = {
    "host": "192.168.88.146",
    "packet": "JgCwAFceDh4ODw4PDx0dDw0QDhANEA0QDg4QDg8PDRAPDg4QDg8dDw0eDw8OAAq2Vh4OHg0QDg8OHh0PDg4PEA4PDRANEA4PDhAODw8ODhAODh0QDR8NEA0ACrdWHg8dDg8OEA0eHQ8PDg4QDRANEA4PDhANEA0QDhAODw4PHQ8OHg0QDQAKtlYfDh4NEA0QDh4cEA0QDg8OEA0QDRANEA4PDhAODw8ODhAdDg8dDhAOAA0FAAAAAAAAAAA=="
}
json_data = json.dumps(data)
self.call_service("broadlink/send", data=json_data)
1 Like

First of all, big thanks for trying to help.
Concerning first strategy, no luck:

2019-11-01 19:36:10.666180 WARNING AppDaemon: ------------------------------------------------------------
2019-11-01 19:36:10.666580 WARNING AppDaemon: Unexpected error in worker for App control_canales:
2019-11-01 19:36:10.666853 WARNING AppDaemon: Worker Ags: {'name': 'control_canales', 'id': UUID('1658b6eb-840b-46ac-b415-0f6eea3e0ff0'), 'type': 'attr', 'function': <bound method Canales.toogle_tv of <canales.Canales object at 0x7f6775936eb8>>, 'attribute': 'state', 'entity': 'input_boolean.power_telesalon', 'new_state': 'on', 'old_state': 'off', 'kwargs': {'handle': UUID('034aaf60-2c93-451f-b1ad-c2dbedadf9e5')}}
2019-11-01 19:36:10.667050 WARNING AppDaemon: ------------------------------------------------------------
2019-11-01 19:36:10.668150 WARNING AppDaemon: Traceback (most recent call last):
  File "/home/homeassistant/appdaemon/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 595, in worker
    self.sanitize_state_kwargs(app, args["kwargs"]))
  File "/home/homeassistant/appdaemon/apps/canales.py", line 25, in toogle_tv
    self.call_service("broadlink/send", data=data)
  File "/home/homeassistant/appdaemon/lib/python3.6/site-packages/appdaemon/plugins/hass/hassapi.py", line 22, in func_wrapper
    return func(*args, **kwargs)
  File "/home/homeassistant/appdaemon/lib/python3.6/site-packages/appdaemon/plugins/hass/hassapi.py", line 522, in call_service
    r.raise_for_status()
  File "/home/homeassistant/appdaemon/lib/python3.6/site-packages/requests/models.py", line 940, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: http://192.168.88.8:8123/api/services/broadlink/send

2019-11-01 19:36:10.668461 WARNING AppDaemon: ------------------------------------------------------------

Concerning the conversion from dict to JSON, same thing:

2019-11-01 19:43:30.770136 WARNING AppDaemon: ------------------------------------------------------------
2019-11-01 19:43:30.770647 WARNING AppDaemon: Unexpected error in worker for App control_canales:
2019-11-01 19:43:30.771368 WARNING AppDaemon: Worker Ags: {'name': 'control_canales', 'id': UUID('36cf2d5c-6237-46d9-a159-557f3edf0f12'), 'type': 'attr', 'function': <bound method Canales.toogle_tv of <canales.Canales object at 0x7f67703d3908>>, 'attribute': 'state', 'entity': 'input_boolean.power_telesalon', 'new_state': 'on', 'old_state': 'off', 'kwargs': {'handle': UUID('82adcfaa-2736-4c44-8688-f357a6477409')}}
2019-11-01 19:43:30.771710 WARNING AppDaemon: ------------------------------------------------------------
2019-11-01 19:43:30.772484 WARNING AppDaemon: Traceback (most recent call last):
  File "/home/homeassistant/appdaemon/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 595, in worker
    self.sanitize_state_kwargs(app, args["kwargs"]))
  File "/home/homeassistant/appdaemon/apps/canales.py", line 27, in toogle_tv
    self.call_service("broadlink/send", data=json_data)
  File "/home/homeassistant/appdaemon/lib/python3.6/site-packages/appdaemon/plugins/hass/hassapi.py", line 22, in func_wrapper
    return func(*args, **kwargs)
  File "/home/homeassistant/appdaemon/lib/python3.6/site-packages/appdaemon/plugins/hass/hassapi.py", line 522, in call_service
    r.raise_for_status()
  File "/home/homeassistant/appdaemon/lib/python3.6/site-packages/requests/models.py", line 940, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: http://192.168.88.8:8123/api/services/broadlink/send

2019-11-01 19:43:30.772888 WARNING AppDaemon: ------------------------------------------------------------

Here is what I have in configuration.yaml (just in case):

homeassistant:
  # Name of the location where Home Assistant is running
  name: Casa
  # Location required to calculate the time the sun rises and sets
  latitude: !secret lat_home
  longitude: !secret lon_home
  # Impacts weather/sunrise data (altitude above sea level in meters)
  elevation: 15
  # metric for Metric, imperial for Imperial
  unit_system: metric
  time_zone: Europe/Madrid
  whitelist_external_dirs:
    - /home/homeassistant/media
    - /home/homeassistant/.homeassistant

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

api:

http:

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
  
recorder: !include includes/recorder.yaml
  
cloud:

mqtt: !include includes/mqtt.yaml

# Sensors
sensor: !include_dir_merge_list includes/sensors
binary_sensor: !include_dir_merge_list includes/binary_sensors
input_datetime: !include_dir_merge_named includes/input_datetime
light: !include_dir_merge_list includes/lights
input_boolean: !include_dir_merge_named includes/input_booleans
input_select: !include_dir_merge_named includes/input_selects
input_number: !include_dir_merge_named includes/input_numbers
switch: !include_dir_merge_list includes/switches

# Chromecasts
cast:
  media_player:
  - host: !secret ip_chromecastvideo
  - host: !secret ip_chromecastaudio
  - host: !secret ip_googlehomemini

zwave:
  usb_path: /dev/zwave

notify: !include includes/notify.yaml

telegram_bot:
  - platform: polling
    api_key: !secret api_telegram
    allowed_chat_ids:
      - !secret id_telegram_group
      - !secret id_telegram_dad
      - !secret id_telegram_mom
    
mikrotik:
  host: !secret router_host
  username: !secret router_user
  password: !secret router_pass
  login_method: token

And this is where broadlink is .homeassistant/includes/switches/broadlink.yaml :

- platform: broadlink
  host: 192.168.88.146
  mac: '34:EA:34:41:25:FD'
  type: rm_mini
  timeout: 15

Funny thing is that the service call from Developer Tools works nicely.

Thanks again. I really appreciate your help.

Can you please also show your appdaemon.yaml file? Do other service calls work and just this one not?

1 Like

In your original attempt, the error message was this…

The AppDaemon code you’re using looks correct. Your error message, however, tells us that we received an error message from Home Assistant for this request.

If you make the request while viewing the Home Assistant log (or by immediately going to /devloper-tools/logs in Home Assistant and hitting “refresh”) you, hopefully, will get more information about what happened.

1 Like

Oh, sorry, yes, the appdaemon.yaml file:

log:
  accessfile: /home/homeassistant/.homeassistant/appdaccess.log
  errorfile: /home/homeassistant/.homeassistant/appderror.log
  logfile: /home/homeassistant/.homeassistant/appdlog.log
  log_generations: 3
  log_size: 1000000
appdaemon:
  api_port: 5001
  threads: 10
  plugins:
    HASS:
      type: hass
      ha_url: http://192.168.88.8:8123
      token: !secret token
      #cert_verify: False
      #namespace: default
    MQTT:
      type: mqtt
      namespace: mqtt
      client_host: 192.168.88.8
      client_user: !secret usermqtt
      client_password: !secret passmqtt
      client_topics:
        - zigbee2mqtt/#

I have an app that is using a telegram notification and this call service works perfectly:

import appdaemon.plugins.hass.hassapi as hass
#
# App to monitor the doorbell and notify
#

class MonitorDoorbell(hass.Hass):

  def initialize(self):
    self.listen_state(self.detect_doorbell, 'switch.doorbell')
    self.log("Hello from doorbell monitor app")

  def detect_doorbell(self, entity, attribute, old, new, kwargs):
    if new == "on":
      self.log("monitor_door.py: Doorbell ringing")
      self.call_service("notify/telegram_dad",message = "Doorbell rang!")
      self.run_in(self.doorbell_off, 5)
      
  def doorbell_off(self, kwargs):
    self.turn_off("switch.doorbell")

Thanks again for your help. I’m afraid it will be something stupid from my side and I’m wasting your time.

Thanks for the hint! Unfortunately, no error shows up in the homeassistant log; double checked in the Logs tab in Developers section of the UI and also in the command line:

homeassistant@hass-nuc:~/.homeassistant$ tail -f home-assistant.log

Thanks anyway!

The log may be buried by your log level settings.

Before you issue the broadlink service call, call_service logger/set_level, level=“debug”

wait a beat, then make your broadlink call.

wait a bit, and then, set the log level back to whatever default you prefer.

While it’s generally not advised to use time.sleep() in an AppDaemon app, for this quick test case, it’ll be fine. I’d wait at least 1 second in both cases to be sure the commands have finished entirely. Then examine your logs. You’ll have a lot more data, thanks to the debug log level, but, hopefully, something in all of that will give an indication as to what is going wrong.

2 Likes

I just found a post on reddit, where a user had to add square brackets around the packet to make it work in a service call, maybe this could be related to your problem

1 Like

AH! I see the issue now!

data={} is NOT correct for this service call.

host= “192.168.88.146”, packet=“adsfasdfasdfadsf” is correct. The reason OP got an “invalid syntax” was because they included a “}” at the end of the packet string that should not be there (probably a copy/paste error).

So, in conclusion, try this:

self.call_service(
  'broadlink/send', 
  host="192.168.88.146",
  packet= "JgCwAFceDh4ODw4PDx0dDw0QDhANEA0QDg4QDg8PDRAPDg4QDg8dDw0eDw8OAAq2Vh4OHg0QDg8OHh0PDg4PEA4PDRANEA4PDhAODw8ODhAODh0QDR8NEA0ACrdWHg8dDg8OEA0eHQ8PDg4QDRANEA4PDhANEA0QDhAODw4PHQ8OHg0QDQAKtlYfDh4NEA0QDh4cEA0QDg8OEA0QDRANEA4PDhAODw8ODhAdDg8dDhAOAA0FAAAAAAAAAAA==")
2 Likes

There should be an icon for :triple face palm: Sorry so much for wasting your time.

It just works and everything happened as @swiftlyfalling has said.
I tried many different combinations, but when I did the one without data={} the last curly bracket was still there resulting in an “invalid syntax”.

@Burningstone , told’ya!

Thanks again to you both!

2 Likes