No module named cec

Hi
I’m new to home assistant and I have a problem.
I followed this tutorial: https://www.youtube.com/watch?v=tCGlQSsQ-Mc on a raspberry pi 3 model b
but when I add a hdmi_cec component to my config I get this error:

17-04-20 00:42:17 ERROR (MainThread) [homeassistant.setup] Error during setup of component hdmi_cec
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/setup.py", line 192, in _async_setup_component
    None, component.setup, hass, processed_config)
  File "/usr/lib/python3.4/asyncio/futures.py", line 388, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.4/asyncio/tasks.py", line 286, in _wakeup
    value = future.result()
  File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
    raise self._exception
  File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/hdmi_cec.py", line 186, in setup
    adapter = CecAdapter(name=display_name, activate_source=False)
  File "/home/homeassistant/.homeassistant/deps/pycec/cec.py", line 20, in __init__
    import cec
ImportError: No module named 'cec'

this is what I added to my config:

hdmi_cec:
  devices:
    TV: 1.0.0.0
    Pi: 1.4.0.0

I did some research myself and found this: HDMI_CEC configuration error
but that didn’t help me.

The test command works: echo scan | cec-client -s -d 1

opening a connection to the CEC adapter...
requesting CEC bus information ...
CEC bus information
===================
device #1: Recorder 1
address:       1.4.0.0
active source: no
vendor:        Pulse Eight
osd string:    CECTester
CEC version:   1.4
power status:  on
language:      eng


device #5: Audio
address:       1.0.0.0
active source: no
vendor:        Onkyo
osd string:    TX-SR444
CEC version:   1.4
power status:  on
language:      ???


currently active source: unknown (-1)

I fixed it

How did you fix it? I’m struggling with the same problem but can’t figure out why I can’t make it working.

I did all kinds of things, I don’t know which one solved it in the end.
I added the user home assistant as root:

sudo adduser <username> sudo

I followed these instructions as homeassistant, while not in the virtual environment and while in the virtual environment:

sudo apt-get update
sudo apt-get install cmake libudev-dev libxrandr-dev python-dev swig
cd
git clone https://github.com/Pulse-Eight/platform.git
mkdir platform/build
cd platform/build
cmake ..
make
sudo make install
cd
git clone https://github.com/Pulse-Eight/libcec.git
mkdir libcec/build
cd libcec/build
cmake -DRPI_INCLUDE_DIR=/opt/vc/include -DRPI_LIB_DIR=/opt/vc/lib ..
make -j4
sudo make install
sudo ldconfig

using the ‘ls’ command in the virtual environment I saw the symlink was broken, so I removed it and created a new one.

I don’t remember in what order I did all these things exactly, I also did a few reboots.
I hope you can fix it!

2 Likes

Thanks @rob1998 for your instructions.

I’m not running HA in virtual environment myself, in case that would matter (in case someone else struggling the same as I did), anyway I simply followed everything in the order you gave, and everything started to work perfectly! The only exception was that I didn’t add HA as root (ie I ignored the very first step).

Your help much appreciated!

1 Like

Thanks! This is exactly what I was looking for! It works for me.

This is still an issue, just tried to use the hdmi_cec integration in docker and i get the same error

Ran into that issue while trying to setup devcontainers (Docker) for Home Assistant with CEC support. The pyCec library doesn’t pull libcec (which isn’t even on Pypi), and you need to build it yourself.

Most importantly, though, you need to have SWIG installed, otherwise CMake will just silently forget to compile the Python bindings (cec.py and _cec.so) for you

sudo apt-get install swig

git clone --recursive https://github.com/Pulse-Eight/libcec.git \
    && mkdir libcec/build \
    && mkdir libcec/src/platform/build

cd libcec/src/platform/build
cmake ..
make -j$(nproc)
sudo make install

cd libcec/build
cmake .. -DHAVE_PYTHON=1 -DHAVE_LINUX_API=1
make -j$(nproc)
sudo make install

Then you need to copy or link cec.py and _cec.so to Home Assistant’s venv.

ln -s /usr/local/lib/python3.12/dist-packages/cec.py ${VIRTUAL_ENV:?}/lib/python3.12/site-packages/cec.py
ln -s /usr/local/lib/python3.12/dist-packages/_cec.so ${VIRTUAL_ENV:?}/lib/python3.12/site-packages/_cec.so

And also you need to refresh the LD cache for libcec.so.6 to show up.

sudo ldconfig