numat / tripplite

Python USB HID interface to Tripplite UPS battery backups.
GNU General Public License v2.0
31 stars 12 forks source link

module 'hid' has no attribute 'device' #1

Closed jorythompson closed 4 years ago

jorythompson commented 4 years ago

HI there, I am trying to get this to work on a Raspberry Pi... I have followed the instructions to add tripplite to the pi (all done as root) and when I run it, I get the following error:

sudo tripplite
Traceback (most recent call last):
  File "/usr/local/bin/tripplite", line 8, in <module>
    sys.exit(command_line())
  File "/usr/local/lib/python3.7/dist-packages/tripplite/__init__.py", line 20, in command_line
    with Battery(args.product_id) as battery:
  File "/usr/local/lib/python3.7/dist-packages/tripplite/driver.py", line 86, in __init__
    self.device = hid.device()
AttributeError: module 'hid' has no attribute 'device'
patrickfuller commented 4 years ago

Weird, I cannot reproduce.

~
▶ pip install hidapi
Collecting hidapi
  Downloading https://files.pythonhosted.org/packages/7c/a0/d5ca6f191c8860a4769ba19448d2b2d6b3e2ca2c30aa61bb96a3f6bd25ba/hidapi-0.9.0.post2.tar.gz (55kB)
     |████████████████████████████████| 61kB 2.3MB/s
Requirement already satisfied: setuptools>=19.0 in /usr/local/Caskroom/miniconda/base/lib/python3.7/site-packages (from hidapi) (41.4.0)
Building wheels for collected packages: hidapi
  Building wheel for hidapi (setup.py) ... done
  Created wheel for hidapi: filename=hidapi-0.9.0.post2-cp37-cp37m-macosx_10_9_x86_64.whl size=43581 sha256=327a8c80800da3150b2d874157116d5ead6189448ce3fd0e5fd7416441358019
  Stored in directory: /Users/pat/Library/Caches/pip/wheels/8b/a6/ef/94b510502abcce6fa03b322eab8d71c4aa21b634e91bc325ab
Successfully built hidapi
Installing collected packages: hidapi
Successfully installed hidapi-0.9.0.post2

~
▶ ipython
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.10.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import hid

In [2]: hid.device
Out[2]: hid.device

In [3]: hid.device?
Init signature: hid.device(self, /, *args, **kwargs)
Docstring:      <no docstring>
File:           /usr/local/Caskroom/miniconda/base/lib/python3.7/site-packages/hid.cpython-37m-darwin.so
Type:           type
Subclasses:

What version do you have (sudo pip freeze | grep hid)? Also, if you open a REPL (sudo python), type import hid; hid.device, does it error?

jorythompson commented 4 years ago
sudo pip freeze | grep hid
hid==1.0.4
hidapi==0.9.0.post2

For further clarification:

cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
patrickfuller commented 4 years ago

I think this is the problem:

hid==1.0.4
hidapi==0.9.0.post2

You have two HID libraries that both respond to import hid. Try fixing by pip uninstall hid and run again.

jorythompson commented 4 years ago

I am unable to remove hid for some reason...

$ sudo pip freeze | grep hid
hid==1.0.4
$ sudo pip uninstall hdi
WARNING: Skipping hdi as it is not installed.
$ pip --version
pip 20.0.2 from /usr/local/lib/python3.7/dist-packages/pip (python 3.7)

Any suggestions?

patrickfuller commented 4 years ago

Typo - hid, not hdi

jorythompson commented 4 years ago

OMG I must be going blind! Good catch (but still no joy):

$ sudo pip freeze | grep hid
hidapi==0.9.0.post2
$ sudo tripplite
Traceback (most recent call last):
  File "/usr/local/bin/tripplite", line 8, in <module>
    sys.exit(command_line())
  File "/usr/local/lib/python3.7/dist-packages/tripplite/__init__.py", line 20, in command_line
    with Battery(args.product_id) as battery:
  File "/usr/local/lib/python3.7/dist-packages/tripplite/driver.py", line 91, in __enter__
    self.open()
  File "/usr/local/lib/python3.7/dist-packages/tripplite/driver.py", line 100, in open
    self.device.open(vendor_id, self.product_id)
  File "hid.pyx", line 66, in hid.device.open
OSError: open failed

I tried removing the USB cable from the UPS (so it sees the UPS at least!):

$ sudo tripplite
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/tripplite/driver.py", line 112, in _get_product_id
    return next(d['product_id'] for d in hid.enumerate()
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/tripplite", line 8, in <module>
    sys.exit(command_line())
  File "/usr/local/lib/python3.7/dist-packages/tripplite/__init__.py", line 20, in command_line
    with Battery(args.product_id) as battery:
  File "/usr/local/lib/python3.7/dist-packages/tripplite/driver.py", line 87, in __init__
    self.product_id = product_id or self._get_product_id()
  File "/usr/local/lib/python3.7/dist-packages/tripplite/driver.py", line 115, in _get_product_id
    raise IOError("Could not find any connected TrippLite devices.")
OSError: Could not find any connected TrippLite devices.

I had tried installing nut to no avail, so I just removed nut and nut-client. Now when I run tripplite I get a little different error:

$ sudo tripplite
Traceback (most recent call last):
  File "/usr/local/bin/tripplite", line 8, in <module>
    sys.exit(command_line())
  File "/usr/local/lib/python3.7/dist-packages/tripplite/__init__.py", line 21, in command_line
    print(json.dumps(battery.get(), indent=4, sort_keys=True))
  File "/usr/local/lib/python3.7/dist-packages/tripplite/driver.py", line 126, in get
    output[category][subcategory] = self._read(options)
  File "/usr/local/lib/python3.7/dist-packages/tripplite/driver.py", line 137, in _read
    options['bytes'] + 1)
  File "hid.pyx", line 193, in hid.device.get_feature_report
OSError: read error
patrickfuller commented 4 years ago

That's usually a permissions error.

Check your dependencies to be sure with apt install gcc libusb-1.0-0-dev libudev-dev. If this fixes it, then it's something with dependencies.

Then, run this as root user: su root, /path/to/tripplite. If that works, then it's an error in permissions config.

This SO post may help in your debugging. lsusb is a good tool for this.

jorythompson commented 4 years ago

Looks like the libraries are already installed:

$ su root
Password:
$ root# apt install gcc libusb-1.0-0-dev libudev-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
gcc is already the newest version (4:8.3.0-1+rpi2).
libudev-dev is already the newest version (241-7~deb10u3+rpi1).
libusb-1.0-0-dev is already the newest version (2:1.0.22-2).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

root# /usr/local/bin/tripplite
Traceback (most recent call last):
  File "/usr/local/bin/tripplite", line 8, in <module>
    sys.exit(command_line())
  File "/usr/local/lib/python3.7/dist-packages/tripplite/__init__.py", line 21, in command_line
    print(json.dumps(battery.get(), indent=4, sort_keys=True))
  File "/usr/local/lib/python3.7/dist-packages/tripplite/driver.py", line 126, in get
    output[category][subcategory] = self._read(options)
  File "/usr/local/lib/python3.7/dist-packages/tripplite/driver.py", line 137, in _read
    options['bytes'] + 1)
  File "hid.pyx", line 193, in hid.device.get_feature_report
OSError: read error

Here is the output from lsusb:

$ lsusb
Bus 001 Device 005: ID 0781:5575 SanDisk Corp. Cruzer Glide
Bus 001 Device 004: ID 09ae:3016 Tripp Lite
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. SMC9514 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
patrickfuller commented 4 years ago

Hm, maybe try rebooting the pi and the battery? Outside of that, I unfortunately can't be of much more help.

jorythompson commented 4 years ago

I had tried rebooting the pi earlier (after I removed nut). Just tried to power off the UPS (no joy)

patrickfuller commented 4 years ago

All that's really left is to open a python terminal and go through the commands line by line. It's not much code but it will take some real debugging time.

From this github post, it looks like the TrippLite you're trying to read from is unfortunately known to have issues.

jorythompson commented 4 years ago

I unistalled tripplite and re-installed nut following these directions and now nut is working fine. I guess there was a conflict between nut and tripplite... Thanks for your help!

SasaKuruppuarachchi commented 2 years ago

The reason is that you have installed the wrong hid library

pip uninstall hid
pip uninstall hidapi
pip install hidapi

should work.

spommere commented 2 days ago

A bit late to the party but the main issue for me was that hidapi did provide a hid() function, but not the right one. I got the error when trying to read from an AIOC:

On top of that, one could not just install or reinstall hid via "apt-get install python3-hid" because it would install hidapi instead.

One had to use a virtual env:

! /bin/bash

set -x rm -rf myenv hid* python3 -m venv myenv . myenv/bin/activate myenv/bin/pip install --target=pwd hid myenv/bin/pip list myenv/bin/python pwd/aioc.py