home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
72.97k stars 30.52k forks source link

nmap_tracker not setting up #9318

Closed Regularj closed 7 years ago

Regularj commented 7 years ago

Home Assistant release (hass --version): 0.55.0

Python release (python3 --version): Python 3.6.0

Component/platform: device_tracker.nmap_tracker

Description of problem: The device tracker won't be set up by HASS

Expected: Expected behavior would be: starting up and discovering devices.

Problem-relevant configuration.yaml entries and steps to reproduce:

device_tracker:
  - platform: nmap_tracker
    hosts: 192.168.0.1/24
    consider_home: 360
    home_interval: 1
    track_new_devices: yes
    exclude:
      - 192.168.0.1
      - 192.168.0.157
      - 192.168.0.120

Traceback (if applicable):

pi@hassbian:~ $ cat /home/homeassistant/.homeassistant/home-assistant.log | grep nmap_tracker
2017-08-28 22:49:32 INFO (MainThread) [homeassistant.loader] Loaded device_tracker.nmap_tracker from homeassistant.components.device_tracker.nmap_tracker
2017-08-28 22:51:01 INFO (MainThread) [homeassistant.components.device_tracker] Setting up device_tracker.nmap_tracker
2017-08-28 22:51:01 INFO (Thread-9) [homeassistant.components.device_tracker.nmap_tracker] Scanning...
2017-08-28 22:51:01 ERROR (MainThread) [homeassistant.components.device_tracker] Error setting up platform nmap_tracker
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/device_tracker/nmap_tracker.py", line 44, in get_scanner
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/device_tracker/nmap_tracker.py", line 79, in __init__
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/device_tracker/nmap_tracker.py", line 104, in _update_info
    from nmap import PortScanner, PortScannerError
ImportError: cannot import name 'PortScanner'

Additional info: The file known_devices.yaml is empty. But there are devices in my network.

I installed nmap and arp via apt-get as well as via pip3. My arp binary lies under /usr/sbin:

pi@hassbian:~ $ which arp
/usr/sbin/arp

I tried several solutions from here including:

  1. changing the “cmd” from “arp” to “/usr/sbin/arp”
  2. adding Environment=PATH="$VIRTUAL_ENV/bin:/usr/sbin:$PATH" to my etc/systemd/system/home-assistant@homeassistant.service file
Danielhiversen commented 7 years ago

Is the last part of the traceback log missing?

Regularj commented 7 years ago

Yes, you are right. Very sorry, I edited my first comment. These lines were missing:

    from nmap import PortScanner, PortScannerError
ImportError: cannot import name 'PortScanner'
Regularj commented 7 years ago

No ideas?

Regularj commented 7 years ago

Still present in HASS version 0.55.0.

Regularj commented 7 years ago

After upgrading to Python 3.6 - still the same error with a little longer log:

2017-10-12 23:11:57 ERROR (MainThread) [homeassistant.components.device_tracker] Error setting up platform nmap_tracker
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/components/device_tracker/__init__.py", line 153, in async_setup_platform
    platform.get_scanner, hass, {DOMAIN: p_config})
  File "/usr/local/lib/python3.6/asyncio/futures.py", line 331, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/local/lib/python3.6/asyncio/tasks.py", line 244, in _wakeup
    future.result()
  File "/usr/local/lib/python3.6/asyncio/futures.py", line 244, in result
    raise self._exception
  File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/components/device_tracker/nmap_tracker.py", line 44, in get_scanner
    scanner = NmapDeviceScanner(config[DOMAIN])
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/components/device_tracker/nmap_tracker.py", line 79, in __init__
    self.success_init = self._update_info()
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/components/device_tracker/nmap_tracker.py", line 104, in _update_info
    from nmap import PortScanner, PortScannerError
ImportError: cannot import name 'PortScanner'

How is it possible that either no one can or no one wants to solve this stuff?

MartinHjelmare commented 7 years ago

The import error suggests there's a problem with the installation of the python-nmap requirement. You said you've installed this manually with pip3. Can you see the package from within your home assistant virtual environment?

pip3 show python-nmap
Regularj commented 7 years ago

Yes I can see it:

pi@hassbian:~ $ source /srv/homeassistant/bin/activate
(homeassistant) pi@hassbian:~ $ pip3 show python-nmap
Name: python-nmap
Version: 0.6.1
Summary: This is a python class to use nmap and access scan results from python3
Home-page: http://xael.org/pages/python-nmap-en.html
Author: Alexandre Norman
Author-email: norman@xael.org
License: gpl-3.0.txt
Location: /srv/homeassistant/lib/python3.6/site-packages
Requires:

After "requires" it says nothing. I didn't leave it blank by mistake.

Regularj commented 7 years ago

Furthermore: I can execute the device_tracker's command manually:

(homeassistant) homeassistant@hassbian:/home/pi $ arp -n

Which gives me a list with all devices, their IP-adresses as well as their MAC-adresses.. Isn't this what the nmap device_tracker should do? Wouldn't that mean something ist just missing/not installed?

But what the hell did I miss?

(homeassistant) homeassistant@hassbian:/home/pi $ pip3 freeze
aiohttp==2.2.5
aiohttp-cors==0.5.3
**arp==1.0**
asn1crypto==0.22.0
astral==1.4
async-timeout==1.4.0
bitstring==3.1.5
boto3==1.4.7
botocore==1.7.16
certifi==2017.7.27.1
cffi==1.10.0
chardet==3.0.4
click==6.7
colorlog==3.0.1
construct==2.8.12
cookies==2.2.1
cryptography==1.9
distro==1.0.4
docutils==0.14
enum-compat==0.0.2
enum34==1.1.6
future==0.16.0
google-api-python-client==1.6.4
gTTS-token==1.1.1
hassbian==0.1
homeassistant==0.55.0
httmock==1.2.6
httplib2==0.10.3
idna==2.5
Jinja2==2.9.6
jmespath==0.9.3
jsonrpc-async==0.6
jsonrpc-base==1.0
jsonrpc-websocket==0.5
keyring==8.7
keyrings.alt==1.3
libnacl==1.5.2
limitlessled==1.0.8
MarkupSafe==1.0
multidict==3.2.0
mutagen==1.38
net-tools==0.1.2
netdisco==1.2.2
netifaces==0.10.6
oauth2client==4.0.0
paho-mqtt==1.3.0
pretty-cron==1.0.2
psutil==5.3.1
pyasn1==0.3.7
pyasn1-modules==0.1.4
pycparser==2.18
pycrypto==2.6.1
pyicloud==0.9.1
pyiss==1.0.1
python-dateutil==2.6.1
python-forecastio==1.3.5
**python-libnmap==0.7.0**
python-mirobo==0.1.1
**python-nmap==0.6.1**
python-telegram-bot==8.0
pytz==2017.2
PyXiaomiGateway==0.5.1
PyYAML==3.12
requests==2.14.2
responses==0.5.1
rsa==3.4.2
s3transfer==0.1.11
samsungctl==0.6.0
six==1.11.0
SQLAlchemy==1.1.14
typing==3.6.2
tzlocal==1.4
uritemplate==3.0.0
voluptuous==0.10.5
wakeonlan==0.2.2
yarl==0.13.0
yeelight==0.3.3
zeroconf==0.19.1
MartinHjelmare commented 7 years ago

You have python-libnmap installed which is an old dependency not used anymore by the nmap tracker. I'm not sure of that matters but you could try uninstalling that library. Do you know when it was installed? It's possible that there's another library that depends on it.

Regularj commented 7 years ago

I installed it just today - some post suggested it. But it did nothing. I uninstalled it now - still the same error.

MartinHjelmare commented 7 years ago

You can try opening a python3 prompt from within your homeassistant virtual environment with the homeassistant user, and try to do the import that fails.

I'd also check the site-packages directory of the environment and make sure you can find the source for python-nmap and that there are no old directories that could conflict with the import search path.

Regularj commented 7 years ago

Thanks. In the python3-prompt I can't import the "PortScanner"

(homeassistant) homeassistant@hassbian:/home/pi $ python3
Python 3.6.0 (default, Oct 12 2017, 00:43:06)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import PortScanner
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'PortScanner'

I also tried installing portscanner via pip3 (might be a stupid thing to do? Sorry, I am no python-expert)

(homeassistant) homeassistant@hassbian: $ pip3 install portscanner
Collecting portscanner
  Could not find a version that satisfies the requirement portscanner (from versions: )
No matching distribution found for portscanner

Also: in the site-package-directory there's no python-nmap - just the python_nmap-0.6.1-py3.6.egg-info.

MartinHjelmare commented 7 years ago

From the python prompt you should do:

from nmap import PortScanner

The source should be in a directory called nmap. From the regular command line, you can do:

ls -l /srv/homeassistant/lib/python3.6/site-packages/*nmap*

to find all related directories. This should normally give two hits: nmap and python_nmap-0.6.1-py3.6.egg-info.

Edit: Don't install any other python packages. It's only the python package python-nmap that should be used.

Regularj commented 7 years ago

Same output when importing from nmap. :-(

>>> from nmap import PortScanner
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'PortScanner'

I do have nmap as a directory:

(homeassistant) homeassistant@hassbian:~ $ ls -l /srv/homeassistant/lib/python3.6/site-packages/*nmap*
/srv/homeassistant/lib/python3.6/site-packages/nmap:
total 24
drwxr-xr-x 2 homeassistant homeassistant  4096 Oct 14 14:41 __pycache__
-rw-r--r-- 1 homeassistant homeassistant 12722 Jul 29  2016 test_nmap.py
-rw-r--r-- 1 homeassistant homeassistant   171 Jul 29  2016 test.py

/srv/homeassistant/lib/python3.6/site-packages/python_nmap-0.6.1-py3.6.egg-info:
total 32
-rw-r--r-- 1 homeassistant homeassistant     1 Oct 12 20:56 dependency_links.txt
-rw-r--r-- 1 homeassistant homeassistant   298 Oct 12 20:56 installed-files.txt
-rw-r--r-- 1 homeassistant homeassistant 14770 Oct 12 20:56 PKG-INFO
-rw-r--r-- 1 homeassistant homeassistant   322 Oct 12 20:56 SOURCES.txt
-rw-r--r-- 1 homeassistant homeassistant     5 Oct 12 20:56 top_level.txt
MartinHjelmare commented 7 years ago

Good! You're missing two python modules in the nmap directory: __init__.py and nmap.py.

Try uninstalling python-nmap and reinstalling it. From your homeassistant virtualenvironment as the homeassistant user do:

pip3 uninstall python-nmap
pip3 install python-nmap==0.6.1

After uninstalling python-nmap, check that no nmap directories are left in site-packages, ie the two directories that you found above. If they are gone, you can continue with installing, ie the second command.

Edit: sorry I forgot one = above in the install command. Updated now.

Regularj commented 7 years ago

Thank you, it finally works! python-nmap needed to be reinstalled.

Now I am struggling with the known_devices.yaml not being created/filled. But this is another topic I don't want to spam this issue with that..

Thanks and cheers

MartinHjelmare commented 7 years ago

:+1: