External python library in custom sensor

Hey guys,

I wanted to write a custom sensor to receive data, sent from my self constructed remote sensor via 433MHZ. The problem I currently have is that I’m not able to import packages installed with pip3. To import local files I had to add the folder to the path but that doesn’t work for pip packages. Can anyone tell me what I’ve done wrong?

    2019-01-19 13:01:44 ERROR (MainThread) [homeassistant.loader] Error loading custom_components.sensor.rfm69-temperature. Make sure all dependencies are installed
Traceback (most recent call last):
File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/loader.py", line 92, in get_component
    module = importlib.import_module(path)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 673, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/etc/homeassistant/custom_components/sensor/rfm69-temperature.py", line 7, in <module>
    import RFM69
File "/etc/homeassistant/custom_components/sensor/RFM69.py", line 4, in <module>
    import spidev
ImportError: No module named 'spidev'

Thats my sensor class. PS does it work when the update is blocking?

import os
import sys
sys.path.append(os.path.dirname(__file__))
#sys.path.append('/usr/local/lib/python3.5')

from homeassistant.helpers.entity import Entity
import RFM69
from RFM69registers import *
import time
import re
import spidev

KEYS = {
    'v': 'volt',
    't': 'temperature',
    'h': 'humidity'
}

REQUIREMENTS = ['spidev==3.2', 'RPi.GPIO']

def setup_platform(hass, config, add_devices, discovery_info=None):
    """Setup rfm sensor"""
    add_devices([Rfm69TemperatureSensor(22, 100)])


class Rfm69TemperatureSensor(Entity):
    """RFM69 Temperature and humidity sensor"""

    def __init__(self, nodeId, networkId):
        """Initialize the sensor"""
        self.state = {}
        self.listener = RFM69.RFM69(RF69_433MHZ, nodeId, networkId, False)
        self.listener.rcCalibration()
        self.listener.setHighPower(True)

    @property
    def name(self):
        """Get name for this sensor"""
        return 'RFM69 Temperature Sensor'

    @property
    def state(self):
        return self.state

    @property
    def unit_of_measurement(self):
        """Get unit of measurement"""
        return 'C'

    @property
    def device_state_attributes(self):
        return {
            'volt': 'Volt',
            'temperature': '°',
            'humidity': '%'
        }

    def update(self):
        """Wait for update from sensor"""
        self.listener.receiveBegin()
        while not self.listener.receiveDone():
            time.sleep(.1)

        msg = ''.join([chr(letter) for letter in self.listener.DATA])

        for pair in msg.split('&'):
            key, value = pair.split('=')
            self.state[KEYS[key]] = value

        if self.listener.ACKRequested():
            self.listener.sendACK()

Thanks in advance for help :slight_smile:

Best
FragSalat

I’m sry. I just didn’t knew that I installed homeassistant in a python virtual environment and had to use a different pip binary.

/srv/homeassistant/bin/pip3 install spidev

Generally you should switch into the virtual environment, then do the normal pip install, and then exit out of the virtual environment:

source /srv/homeassistant/bin/activate
pip3 install ...
deactivate

BUT, if you add the package to your code’s REQUIREMENTS variable, HA will install it automatically for you.

1 Like

Hi @pnbruckner, are these “magic” variables documented somewhere? I have the same problem with my integration. It installs requirements into deps folder of config, but the integration doesn’t see this library in deps.

I’m going to try to put library into REQUIREMENTS in __init__.py of my custom integration, but even I searched hard, I didn’t found documentation describing this variable.

That information is very old and probably no longer applies. For documentation about building integrations, see

Home Assistant Developer Docs | Home Assistant Developer Docs (home-assistant.io)

See the “core” docs.