AlexxIT / PythonScriptsPro

Advanced version of python scripts for Home Assistant without limits
https://github.com/AlexxIT/Blog
117 stars 17 forks source link

Stopped working after last update #28

Open alanmilinovic opened 8 months ago

alanmilinovic commented 8 months ago

Traceback (most recent call last): File "/config/custom_components/python_script/sensor.py", line 65, in update exec(self.code) File "", line 3, in AttributeError: 'PythonSensor' object has no attribute 'config'

alanmilinovic commented 8 months ago

This is how my ssh_command.py looks like.

from paramiko import SSHClient, AutoAddPolicy

host = self.config.get("host")
port = self.config.get("port")
username = self.config.get("user")
password = self.config.get("pass")
command = self.config.get("cmd")

client = SSHClient()
client.set_missing_host_key_policy(AutoAddPolicy())
client.connect(host, port, username, password)
stdin, stdout, stderr = client.exec_command(command)
resp = stdout.read()
stderr.read()
client.close()

self.state = f"\n{ resp.decode()[:250] }"
self._attr_extra_state_attributes = { "response": resp.decode() }

So far all worked well, not sure what changed in last update, tried to revert to previous version and same error.

alanmilinovic commented 8 months ago

This is my sensor.

  - platform: python_script
    name: 1NUC Model
    unique_id: "1nuc_model"
    icon: mdi:desktop-tower
    scan_interval: 63072000 #2 Years in seconds
    file: scripts/remote_ssh_command.py
    host: !secret 1nuc_host
    port: !secret 1nuc_port
    user: "user"
    pass: !secret password
    cmd: >
      sudo dmidecode -s system-product-name
alanmilinovic commented 8 months ago

I found the issue is in "self.config.get" function. The question is what has changed and how to get attribute value defined in sensor? Tried with data attribute, but it doesn't work, I guess data is only working when called as a service.

AlexxIT commented 8 months ago

Config removed from first release (two months ago). I can return it, no problem. In next version when I'll have time. Or you can make PR https://github.com/AlexxIT/PythonScriptsPro/blob/edd9032670dd960ac54ff1dbd21b64dbe4cd8ef4/custom_components/python_script/sensor.py#L31

alanmilinovic commented 8 months ago

Config removed from first release (two months ago). I can return it, no problem. In next version when I'll have time. Or you can make PR

https://github.com/AlexxIT/PythonScriptsPro/blob/edd9032670dd960ac54ff1dbd21b64dbe4cd8ef4/custom_components/python_script/sensor.py#L31

Great, I manually return it and will wait for you to apply PR. Thanks