n8henrie / fauxmo

Emulated Belkin WeMo devices that work with the Amazon Echo
https://n8henrie.com/2016/02/fauxmo-home-automation-with-the-amazon-echo-raspberry-pi-and-homeassistant/
Other
377 stars 78 forks source link

Unexpected keyword argument "initial_state" #122

Closed imsunburnt closed 9 months ago

imsunburnt commented 9 months ago

My Issue

In config.json when I include "initial_state": "off" I get the following ERROR:

(fauxmo-server) pi@pi0-2:~ $ fauxmo -c .fauxmo/config.json -vvv
2024-02-07 18:30:08 fauxmo:45       INFO     Fauxmo v0.7.0
2024-02-07 18:30:08 fauxmo:46       DEBUG    3.9.2 (default, Mar 12 2021, 04:06:34)
[GCC 10.2.1 20210110]
2024-02-07 18:30:08 fauxmo:29       DEBUG    Attempting to get IP address automatically
2024-02-07 18:30:08 fauxmo:44       DEBUG    Using IP address: 192.168.15.63
2024-02-07 18:30:08 fauxmo:122      DEBUG    plugin_vars: {}
2024-02-07 18:30:08 fauxmo:128      DEBUG    device config: {'port': 53000, 'on_cmd': 'http://pi3-4:3001/x10_command?command=echo%20rf%20L7%20on%20|%20nc%20-vN%20127.0.0.1%201099', 'off_cmd': 'http://pi3-4:3001/x10_command?command=echo%20rf%20L7%20off%20|%20nc%20-vN%20127.0.0.1%201099', 'method': 'GET', 'initial_state': 'off', 'use_fake_state': True, 'name': 'test light'}
2024-02-07 18:30:08 fauxmo:135      ERROR    Error in plugin <class 'fauxmo.plugins.simplehttpplugin.SimpleHTTPPlugin'>
Traceback (most recent call last):
  File "/home/pi/.venv/fauxmo-server/fauxmo-server/bin/fauxmo", line 8, in <module>
    sys.exit(cli())
  File "/home/pi/.venv/fauxmo-server/fauxmo-server/lib/python3.9/site-packages/fauxmo/cli.py", line 45, in cli
    main(config_path_str=args.config, verbosity=verbosity)
  File "/home/pi/.venv/fauxmo-server/fauxmo-server/lib/python3.9/site-packages/fauxmo/fauxmo.py", line 133, in main
    plugin = PluginClass(**plugin_vars, **device)
TypeError: __init__() got an unexpected keyword argument 'initial_state'

==============================================
My config.json:
(fauxmo-server) pi@pi0-2:~ $ cat .fauxmo/config.json
{
    "FAUXMO": {
        "ip_address": "auto"
    },
    "PLUGINS": {
        "SimpleHTTPPlugin": {
            "DEVICES": [
                {
                    "port": 53000,
                    "on_cmd": "http://pi3-4:3001/x10_command...",
                    "off_cmd": "http://pi3-4:3001/x10_command...",
                    "method": "GET",
                    "initial_state": "off",
                    "use_fake_state": true,
                    "name": "test light"
                }
          ]
        }
    }
}

WHYT

My original problem occurred when upgrading from v0.5.0 to v0.7.0 and re-using the config.json file that does not contain a value for "initial_state" (since it's v0.5.0) but does have "use_fake_state":

(fauxmo-server) pi@pi0-2:~ $ cat .fauxmo/config.json
{
    "FAUXMO": {
        "ip_address": "auto"
    },
    "PLUGINS": {
        "SimpleHTTPPlugin": {
            "DEVICES": [
                {
                    "port": 53000,
                    "on_cmd": "http://pi3-4:3001/x10_command?command=echo%20rf%20L7%20on%20|%20nc%20-vN%20127.0.0.1%201099",
                    "off_cmd": "http://pi3-4:3001/x10_command?command=echo%20rf%20L7%20off%20|%20nc%20-vN%20127.0.0.1%201099",
                    "method": "GET",
                    "use_fake_state": true,
                    "name": "test light"
                }
          ]
        }
    }
}

When I run fauxmo from the venv and I command Alexa to discover devices I get: AttributeError: 'SimpleHTTPPlugin' object has no attribute '_latest_action'

Traceback (most recent call last):
  File "/usr/lib/python3.9/asyncio/selector_events.py", line 870, in _read_ready__data_received
    self._protocol.data_received(data)
  File "/home/pi/.venv/fauxmo-server/fauxmo-server/lib/python3.9/site-packages/fauxmo/protocols.py", line 70, in data_received
    self.handle_action(msg)
  File "/home/pi/.venv/fauxmo-server/fauxmo-server/lib/python3.9/site-packages/fauxmo/protocols.py", line 155, in handle_action
    state = self.plugin.get_state().casefold()
  File "/home/pi/.venv/fauxmo-server/fauxmo-server/lib/python3.9/site-packages/fauxmo/plugins/simplehttpplugin.py", line 192, in get_state
    return super().get_state()
  File "/home/pi/.venv/fauxmo-server/fauxmo-server/lib/python3.9/site-packages/fauxmo/plugins/__init__.py", line 98, in get_state
    return self.latest_action
  File "/home/pi/.venv/fauxmo-server/fauxmo-server/lib/python3.9/site-packages/fauxmo/plugins/__init__.py", line 64, in __getattribute__
    return object.__getattribute__(self, name)
  File "/home/pi/.venv/fauxmo-server/fauxmo-server/lib/python3.9/site-packages/fauxmo/plugins/__init__.py", line 111, in latest_action
    return self._latest_action
  File "/home/pi/.venv/fauxmo-server/fauxmo-server/lib/python3.9/site-packages/fauxmo/plugins/__init__.py", line 64, in __getattribute__
    return object.__getattribute__(self, name)
AttributeError: 'SimpleHTTPPlugin' object has no attribute '_latest_action'

After looking at the code plugins/__init__.py it looks like I need to define "initial_state" in config.json, else the attribute _latest_action will not get defined. When I add a value for "initial_state" in config.json then I get the error that I wrote this issue for.

Am I right for including "initial_state" in config.json or have I misunderstood how to use it?

NOTE: I added "initial_state" to plugins/simplehttpplugin.py and the error went away.

    def __init__(
        self,
        *,
        ...
        initial_state: str = "off",
        ...
    ) -> None:
        ...
        self.use_fake_state = use_fake_state
        self.initial_state = initial_state

        super().__init__(name=name, port=port, initial_state=initial_state)

Please make sure you've taken these steps before submitting a new issue:

n8henrie commented 9 months ago

Wow, quite an oversight on my part -- and the tests I added should have included something for this.

Thanks for pointing it out, and for a complete / thorough issue.

n8henrie commented 9 months ago

I did add it to the mqttplugin (which was initially where the need for "fake_state" stuff came up): https://github.com/n8henrie/fauxmo-plugins/commit/485e310c99cd91dee6e23634f43250c7d062ecde and just didn't follow through here. I don't think this is needed for the HomeAssistant plugin, as hass does state reporting pretty reliably (and so I never gave it afake_state option), but will add to commandline and simplehttp plugins.

n8henrie commented 9 months ago

I did add a test that might have caught this, but this code path is never taken because none of the test configs include initial_state. SMH.

Appropriately failing tests added in 04ede8081dd16227733d0da0101f377d0c08a6f1

ChazTuna commented 8 months ago

I read this and get the same issue with the commandlineplugin.py. Is there a fix coming for that too? Do I need to add "initial_state": "off", to my config file? Thanks.