Refactored device components

This commit is contained in:
Paulus Schoutsen committed 2015-03-01 01:35:58 -08:00
1 parent f0c6ac1aa3
commit 89100d14c8
20 files changed
+239 -339

No files matched your search

+9 -47
View File
@@ -6,16 +6,16 @@ Component to interface with various switches that can be controlled remotely.
import logging
from datetime import timedelta
from homeassistant.loader import get_component
import homeassistant.util as util
from homeassistant.helpers.device_component import DeviceComponent
from homeassistant.const import (
STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF, ATTR_ENTITY_ID)
from homeassistant.helpers import (
generate_entity_id, extract_entity_ids, platform_devices_from_config)
from homeassistant.helpers import extract_entity_ids
from homeassistant.components import group, discovery, wink
DOMAIN = 'switch'
DEPENDENCIES = []
SCAN_INTERVAL = 30
GROUP_NAME_ALL_SWITCHES = 'all switches'
ENTITY_ID_ALL_SWITCHES = group.ENTITY_ID_FORMAT.format('all_switches')
@@ -59,47 +59,12 @@ def turn_off(hass, entity_id=None):
def setup(hass, config):
""" Track states and offer events for switches. """
logger = logging.getLogger(__name__)
component = DeviceComponent(
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, DISCOVERY_PLATFORMS,
GROUP_NAME_ALL_SWITCHES)
component.setup(config)
switches = platform_devices_from_config(
config, DOMAIN, hass, ENTITY_ID_FORMAT, logger)
@util.Throttle(MIN_TIME_BETWEEN_SCANS)
def update_states(now):
""" Update states of all switches. """
if switches:
logger.info("Updating switch states")
for switch in switches.values():
switch.update_ha_state(True)
update_states(None)
# Track all switches in a group
switch_group = group.Group(
hass, GROUP_NAME_ALL_SWITCHES, switches.keys(), False)
def switch_discovered(service, info):
""" Called when a switch is discovered. """
platform = get_component("{}.{}".format(
DOMAIN, DISCOVERY_PLATFORMS[service]))
discovered = platform.devices_discovered(hass, config, info)
for switch in discovered:
if switch is not None and switch not in switches.values():
switch.hass = hass
switch.entity_id = generate_entity_id(
ENTITY_ID_FORMAT, switch.name, switches.keys())
switches[switch.entity_id] = switch
switch.update_ha_state()
switch_group.update_tracked_entity_ids(switches.keys())
discovery.listen(hass, DISCOVERY_PLATFORMS.keys(), switch_discovered)
switches = component.devices
def handle_switch_service(service):
""" Handles calls to the switch services. """
@@ -118,9 +83,6 @@ def setup(hass, config):
switch.update_ha_state(True)
# Update state every 30 seconds
hass.track_time_change(update_states, second=[0, 30])
hass.services.register(DOMAIN, SERVICE_TURN_OFF, handle_switch_service)
hass.services.register(DOMAIN, SERVICE_TURN_ON, handle_switch_service)
+4 -13
View File
@@ -3,22 +3,13 @@ from homeassistant.helpers import ToggleDevice
from homeassistant.const import STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME
def get_devices(hass, config):
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
""" Find and return demo switches. """
return get_switches()
def devices_discovered(hass, config, info):
""" Called when a device is discovered. """
return get_switches()
def get_switches():
""" Returns the Wink switches. """
return [
add_devices_callback([
DemoSwitch('Ceiling', STATE_ON),
DemoSwitch('AC', STATE_OFF)
]
])
class DemoSwitch(ToggleDevice):
+5 -4
View File
@@ -7,14 +7,15 @@ from homeassistant.helpers import ToggleDevice
import tellcore.constants as tellcore_constants
def get_devices(hass, config):
""" Find and return Tellstick switches. """
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
""" Find and return tellstick switches. """
try:
import tellcore.telldus as telldus
except ImportError:
logging.getLogger(__name__).exception(
"Failed to import tellcore")
return []
return
core = telldus.TelldusCore()
switches_and_lights = core.devices()
@@ -25,7 +26,7 @@ def get_devices(hass, config):
if not switch.methods(tellcore_constants.TELLSTICK_DIM):
switches.append(TellstickSwitchDevice(switch))
return switches
add_devices_callback(switches)
class TellstickSwitchDevice(ToggleDevice):
+20 -34
View File
@@ -6,50 +6,36 @@ from homeassistant.components.switch import (
ATTR_TODAY_MWH, ATTR_CURRENT_POWER_MWH)
def get_devices(hass, config):
""" Find and return WeMo switches. """
pywemo, _ = get_pywemo()
if pywemo is None:
return []
logging.getLogger(__name__).info("Scanning for WeMo devices")
switches = pywemo.discover_devices()
# Filter out the switches and wrap in WemoSwitch object
return [WemoSwitch(switch) for switch in switches
if isinstance(switch, pywemo.Switch)]
def devices_discovered(hass, config, info):
""" Called when a device is discovered. """
_, discovery = get_pywemo()
if discovery is None:
return []
device = discovery.device_from_description(info)
return [] if device is None else [WemoSwitch(device)]
def get_pywemo():
""" Tries to import PyWemo. """
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
""" Find and return wemo switches. """
try:
# pylint: disable=no-name-in-module, import-error
import homeassistant.external.pywemo.pywemo as pywemo
import homeassistant.external.pywemo.pywemo.discovery as discovery
return pywemo, discovery
except ImportError:
logging.getLogger(__name__).exception((
"Failed to import pywemo. "
"Did you maybe not run `git submodule init` "
"and `git submodule update`?"))
return None, None
return
if discovery_info is not None:
device = discovery.device_from_description(discovery_info)
if device:
add_devices_callback([device])
return
logging.getLogger(__name__).info("Scanning for WeMo devices")
switches = pywemo.discover_devices()
# Filter out the switches and wrap in WemoSwitch object
add_devices_callback(
[WemoSwitch(switch) for switch in switches
if isinstance(switch, pywemo.Switch)])
class WemoSwitch(ToggleDevice):
+11 -20
View File
@@ -8,26 +8,17 @@ from homeassistant.components.wink import WinkToggleDevice
from homeassistant.const import CONF_ACCESS_TOKEN
def get_devices(hass, config):
""" Find and return Wink switches. """
token = config.get(CONF_ACCESS_TOKEN)
def setup_platform(hass, config, add_devices, discovery_info=None):
""" Sets up the Wink platform. """
if discovery_info is None:
token = config.get(CONF_ACCESS_TOKEN)
if token is None:
logging.getLogger(__name__).error(
"Missing wink access_token - "
"get one at https://winkbearertoken.appspot.com/")
return []
if token is None:
logging.getLogger(__name__).error(
"Missing wink access_token - "
"get one at https://winkbearertoken.appspot.com/")
return
pywink.set_bearer_token(token)
pywink.set_bearer_token(token)
return get_switches()
def devices_discovered(hass, config, info):
""" Called when a device is discovered. """
return get_switches()
def get_switches():
""" Returns the Wink switches. """
return [WinkToggleDevice(switch) for switch in pywink.get_switches()]
add_devices(WinkToggleDevice(switch) for switch in pywink.get_switches())