Skip to content

Commit

Permalink
Add a load_platform mechanism (home-assistant#2012)
Browse files Browse the repository at this point in the history
* discovery.load_platform method

* rm grep
  • Loading branch information
kellerza authored and balloob committed May 10, 2016
1 parent 1d0bc1e commit ec9544b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,5 @@ venv
# vimmy stuff
*.swp
*.swo

ctags.tmp
28 changes: 28 additions & 0 deletions homeassistant/components/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

SCAN_INTERVAL = 300 # seconds

LOAD_PLATFORM = 'load_platform'

SERVICE_WEMO = 'belkin_wemo'
SERVICE_HUE = 'philips_hue'
SERVICE_CAST = 'google_cast'
Expand Down Expand Up @@ -73,6 +75,32 @@ def discover(hass, service, discovered=None, component=None, hass_config=None):
hass.bus.fire(EVENT_PLATFORM_DISCOVERED, data)


def load_platform(hass, component, platform, info=None, hass_config=None):
"""Helper method for generic platform loading.
This method allows a platform to be loaded dynamically without it being
known at runtime (in the DISCOVERY_PLATFORMS list of the component).
Advantages of using this method:
- Any component & platforms combination can be dynamically added
- A component (i.e. light) does not have to import every component
that can dynamically add a platform (e.g. wemo, wink, insteon_hub)
- Custom user components can take advantage of discovery/loading
Target components will be loaded and an EVENT_PLATFORM_DISCOVERED will be
fired to load the platform. The event will contain:
{ ATTR_SERVICE = LOAD_PLATFORM + '.' + <<component>>
ATTR_DISCOVERED = {LOAD_PLATFORM: <<platform>>} }
* dev note: This listener can be found in entity_component.py
"""
if info is None:
info = {LOAD_PLATFORM: platform}
else:
info[LOAD_PLATFORM] = platform
discover(hass, LOAD_PLATFORM + '.' + component, info, component,
hass_config)


def setup(hass, config):
"""Start a discovery service."""
logger = logging.getLogger(__name__)
Expand Down
12 changes: 12 additions & 0 deletions homeassistant/helpers/entity_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,24 @@ def setup(self, config):
self._setup_platform(p_type, p_config)

if self.discovery_platforms:
# Discovery listener for all items in discovery_platforms array
# passed from a component's setup method (e.g. light/__init__.py)
discovery.listen(
self.hass, self.discovery_platforms.keys(),
lambda service, info:
self._setup_platform(self.discovery_platforms[service], {},
info))

# Generic discovery listener for loading platform dynamically
# Refer to: homeassistant.components.discovery.load_platform()
def load_platform_callback(service, info):
"""Callback to load a platform."""
platform = info.pop(discovery.LOAD_PLATFORM)
self._setup_platform(platform, {}, info if info else None)
discovery.listen(
self.hass, discovery.LOAD_PLATFORM + '.' + self.domain,
load_platform_callback)

def extract_from_service(self, service):
"""Extract all known entities from a service call.
Expand Down

0 comments on commit ec9544b

Please sign in to comment.