Issues with load_platform

I started to write my own custom component, but I’m having a problem with load_platform:
https://home-assistant.io/developers/component_generic_discovery/

I copied the example files in my custom_components directory:
/custom_components/myflashyhub.py
/custom_components/switch/myflashyhub.py

myflashhub.py:

from homeassistant.components.discovery import load_platform
DOMAIN = ‘myflashyhub’

MFH_GLOBAL = None

def setup(hass, config):
“”“Your controller/hub specific code.”“”

global MFH_GLOBAL
if MFH_GLOBAL is None:
    MFH_GLOBAL = 'Test'
load_platform(hass, 'switch', DOMAIN, {'optional': 'arguments'})
return True

/switch/myflashhub.py:

import homeassistant.components.myflashyhub as myflashyhub

def setup_platform(hass, config, add_devices, discovery_info=None):
“”“Your switch/light specific code.”“”
# You can now use myflashyhub.MFH_GLOBAL
print(myflashyhub.MFH_GLOBAL)

When starting Home Assistant, I get the following error:

INFO:homeassistant.core:Bus:Handling <Event platform_discovered[L]: platform=myflashyhub, discovered=optional=arguments, service=load_platform.switch>
ERROR:homeassistant.loader:Error loading custom_components.switch.myflashyhub. Make sure all dependencies are installed
Traceback (most recent call last):
File “/volume1/@entware/lib/python3.5/site-packages/homeassistant/loader.py”, line 139, in get_component
module = importlib.import_module(path)
File “/volume1/@entware/lib/python3.5/importlib/init.py”, line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “”, line 986, in _gcd_import
File “”, line 969, in _find_and_load
File “”, line 958, in _find_and_load_unlocked
File “”, line 673, in _load_unlocked
File “”, line 665, in exec_module
File “”, line 222, in _call_with_frames_removed
File “/volume1/homeassistant/custom_components/switch/myflashyhub.py”, line 1, in
import homeassistant.components.myflashyhub as myflashyhub
ImportError: No module named ‘homeassistant.components.myflashyhub’
ERROR:homeassistant.loader:Unable to find component switch.myflashyhub
ERROR:homeassistant.bootstrap:Unable to find platform switch.myflashyhub
INFO:homeassistant.core:Bus:Handling <Event call_service[L]: service_data=message=The following components and platforms could not be set up:

  • switch.myflashyhub
    Please check your config, notification_id=invalid_config, title=Invalid config, domain=persistent_notification, service_call_id=30818256-1, service=create>
    INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: old_state=None, new_state=<state persistent_notification.invalid_config=The following components and platforms could not be set up:
  • switch.myflashyhub
    Please check your config; title=Invalid config @ 2016-12-29T23:04:50.376181+01:00>, entity_id=persistent_notification.invalid_config>
    INFO:homeassistant.core:Bus:Handling <Event service_executed[L]: service_call_id=30818256-1>

It cannot find the myflashyhub component.

I can work around this error by copying the component myflashyhub.py into the components directory of Home Assistant.

But then I get another error:

INFO:homeassistant.core:Bus:Handling <Event component_loaded[L]: component=switch>
INFO:homeassistant.core:Bus:Handling <Event platform_discovered[L]: discovered=optional=arguments, platform=myflashyhub, service=load_platform.switch>
INFO:homeassistant.loader:Loaded switch.myflashyhub from custom_components.switch.myflashyhub
INFO:homeassistant.components.switch:Setting up switch.myflashyhub
None
INFO:homeassistant.core:Starting Home Assistant core loop

myflashyhub.MFH_GLOBAL is none. So there is something wrong.

Does this sound familiar?

I’m running Home Assistant on a Synology NAS. Just installed it via python3 and PIP. No docker image.

Thx,

Wouter

That’s my first error when i started HA development. Unfortunately, the sample itself is misleading. You won’t be able to import your classes as a custom component. If you place your code inside the source components folder, it works fine