'Platform not found' for simple custom component in 0.94.2

Results:

root@plutonium:/usr/src/app# cd /config
root@plutonium:/config# python -m site
sys.path = [
‘/config’,
‘/usr/local/lib/python37.zip’,
‘/usr/local/lib/python3.7’,
‘/usr/local/lib/python3.7/lib-dynload’,
‘/usr/local/lib/python3.7/site-packages’,
]
USER_BASE: ‘/root/.local’ (doesn’t exist)
USER_SITE: ‘/root/.local/lib/python3.7/site-packages’ (doesn’t exist)
ENABLE_USER_SITE: True
root@plutonium:/config# python3 -m site
sys.path = [
‘/config’,
‘/usr/local/lib/python37.zip’,
‘/usr/local/lib/python3.7’,
‘/usr/local/lib/python3.7/lib-dynload’,
‘/usr/local/lib/python3.7/site-packages’,
]
USER_BASE: ‘/root/.local’ (doesn’t exist)
USER_SITE: ‘/root/.local/lib/python3.7/site-packages’ (doesn’t exist)
ENABLE_USER_SITE: True
root@plutonium:/config#

The lines with (doesn't exist) are verbatim from the console and not me adding commentary.

Ok, I think this might work:

cd /usr/src/app
python
import sys
sys.path.insert(0, '/config/custom_components')
import whatever.climate

EDIT: Actually, to be closer to how HA does it:

cd /usr/src/app
python
import sys
sys.path.insert(0, '/config')
import custom_components.whatever.climate
1 Like

OK, that sequence of commands caused the relative import to reveal itself as a problem:

In my climate.py, line 26 is the first use of the relative import:
Screenshot%20from%202019-06-14%2012-48-15

Before we finally bury Old Nelly, can you explain those python commands?

See Intra-package References.

Basically dot means the current package. So in this case, for the original climate.py, in its original location, . means homeassistant.components.mqtt (or homeassistant/components/mqtt/__init__.py.) And .discovery means homeassistant.components.mqtt.discovery (or homeassistant/components/mqtt/discovery.py.)

What I’ve done in the past, rather than change the import statements, or copying all the files, I simply created indirect references. E.g., in this case I would create /config/custom_components/whatever/__init__.py which would contain:

from homeassistant.components.mqtt import *

and /config/custom_components/whatever/const.py that would contain:

from homeassistant.components.mqtt.const import *

etc.

Or, although I haven’t tried this, you might be able to just make symlinks:

cd /config/custom_components/whatever
ln -s /usr/src/app/homeassistant/components/mqtt/__init__.py __init__.py
...

Either of these last two options allows you to copy and modify only what you want, while effectively including the other files as-is. When you update HA you automatically get any new versions – which can be a good thing, or a bad thing. :wink:

Thanks for the link. What I should’ve said was what is the purpose of these commands:
import sys
sys.path.insert(0, '/config')

I found this link explaining sys and my guess is the two commands are being used to configure python’s environment?

I like the suggestions you’ve made for incorporating the from statements into __init__.py.

I finished creating a customized version of 0.94’s MQTT climate.py. It took longer than expected because the code changed since 0.89 (for the better, but it created more work for me). The few tests I’ve done confirm it’s working as desired. FWIW, I have no __init__.py in the directory (just climate.py and manifest.json) and Home Assistant seems to have no problem with that arrangement.

I’m hoping this is the last time I will need to create a customized version of MQTT’s climate component. pvizeli is redesigning the entire climate component (for all platforms). I know at least one of the things it will do (that one of my modifications does now) is to make a distinction between an HVAC system’s operating mode (auto, heat, cool, off) and its operating state (heating, cooling, drying, idle).

Oh, sorry, I misunderstood what you were asking.

Yeah, basically the import mechanism will use sys.path to search for modules & packages. You’ll notice that when I had you run that site package it output what sys.path would be (although I had you do it originally from the wrong directory. Should have been in /usr/src/apps/homeassisant, which I figured out later.) You can add paths to sys.path (which is just a Python list of path strings) to make it look in additional places. In fact, that’s what HA’s loader does:

I just suggested you do the same thing “manually.” :slight_smile:

1 Like