AppDaemon 2 to 3 migration troubles

Hi All,

Some time ago I built a simple app to take a power load from a smart switch and determine if a pedestal fan is actually on or off; then override the HA switch accordingly. Use case - If I turn the fan on by the remote, then it state is updated so dependant automations etc are done correctly.

Sadly a rebuild has meant I’ve lost the AppDaemon V2 add-on I’ve held onto for dear life and It’s finally time to migrate to V3. Ive gone through some migration steps in docs but I’ve hit an error I just can’t seem to figure out how to deal with.

Spoiler alert, my python knowledge is near zero, it was amazing I built it at all with the help of the community.

I’m getting the following error:

2018-09-01 17:56:10.747305 INFO AppDaemon: Initializing app loungefansync using class FanSync from module fansync
2018-09-01 17:56:10.749424 INFO AppDaemon: Initializing app bedfansync using class FanSync from module fansync
2018-09-01 17:56:10.755179 INFO AppDaemon: App initialization complete
2018-09-01 17:59:44.590093 WARNING AppDaemon: ------------------------------------------------------------
2018-09-01 17:59:44.592154 WARNING AppDaemon: Unexpected error in worker for App bedfansync:
2018-09-01 17:59:44.594240 WARNING AppDaemon: Worker Ags: {‘name’: ‘bedfansync’, ‘id’: UUID(‘26be04d3-e550-48fb-8d75-a824d639393a’), ‘type’: ‘attr’, ‘function’: <bound method FanSync.change_switch of <fansync.FanSync object at 0x74ab02b0>>, ‘attribute’: ‘state’, ‘entity’: ‘switch.plug_158d0001031484’, ‘new_state’: ‘on’, ‘old_state’: ‘on’, ‘kwargs’: {‘handle’: UUID(‘be63e87d-c98f-4e59-ae51-b7b0e1660895’)}}
2018-09-01 17:59:44.595722 WARNING AppDaemon: ------------------------------------------------------------
2018-09-01 17:59:44.607339 WARNING AppDaemon: Traceback (most recent call last):
File “/usr/lib/python3.6/site-packages/appdaemon/appdaemon.py”, line 590, in worker
self.sanitize_state_kwargs(app, args[“kwargs”]))
File “/config/appdaemon/apps/fansync.py”, line 31, in change_switch
state = self.get_state(self.args[“plug”], “load_power”)
TypeError: get_state() takes from 1 to 2 positional arguments but 3 were given
2018-09-01 17:59:44.608958 WARNING AppDaemon: ------------------------------------------------------------
2018-09-01 18:00:36.395872 WARNING AppDaemon: ------------------------------------------------------------
2018-09-01 18:00:36.397008 WARNING AppDaemon: Unexpected error in worker for App loungefansync:
2018-09-01 18:00:36.400195 WARNING AppDaemon: Worker Ags: {‘name’: ‘loungefansync’, ‘id’: UUID(‘b0023a87-6565-4642-ba8b-0a50ca659466’), ‘type’: ‘attr’, ‘function’: <bound method FanSync.change_switch of <fansync.FanSync object at 0x74ab0210>>, ‘attribute’: ‘state’, ‘entity’: ‘switch.plug_158d0001a65815’, ‘new_state’: ‘on’, ‘old_state’: ‘on’, ‘kwargs’: {‘handle’: UUID(‘deb24b36-fbfc-4f8a-9e58-65618f8976dc’)}}
2018-09-01 18:00:36.401215 WARNING AppDaemon: ------------------------------------------------------------
2018-09-01 18:00:36.403740 WARNING AppDaemon: Traceback (most recent call last):
File “/usr/lib/python3.6/site-packages/appdaemon/appdaemon.py”, line 590, in worker
self.sanitize_state_kwargs(app, args[“kwargs”]))
File “/config/appdaemon/apps/fansync.py”, line 31, in change_switch
state = self.get_state(self.args[“plug”], “load_power”)
TypeError: get_state() takes from 1 to 2 positional arguments but 3 were given

My files:
apps.yaml

loungefansync:
  module: fansync
  class: FanSync
  plug: switch.plug_158d0001a65815
  device: fan.living_room_fan

bedfansync:
  module: fansync
  class: FanSync
  plug: switch.plug_158d0001031484
  device: fan.bedroom_fan

fansync.py

import appdaemon.plugins.hass.hassapi as hass

#
# Sync remotes and switches so the switches have the correct
# status (on/off) when triggered by the remote from outside HA.

#
# Args:
# plug: entity ID of smart plug with ‘load_power’
# device: entity ID of device using power

class FanSync(hass.Hass):
 
  def initialize(self):
    self.listen_state(self.change_switch, self.args["plug"])

  def change_switch(self, entity, attribute, old, new, kwargs):
    state = self.get_state(self.args["plug"], "load_power")
    self.log(state) #dont use print(state)
    print(state)
    if state < 10: 
      self.set_state(self.args["device"], state = "off")
    else:
      self.set_state(self.args["device"], state = "on")    

Hoping its an easy thing

Thank you so much,

Linton

The way to indicate attributes changed from v2 to v3. I think this will work in v3

state = self.get_state(self.args[“plug”], attribute=“load_power”)
1 Like

@gpbenton That worked thank you!
Lol after an awkward little moment with quotation mark types

1 Like