Move WeMo discovery into a common component
This commit is contained in:
1 parent
4dad40fffb
commit
afe564fb3f
8 files changed
+147
-102
No files matched your search
@@ -18,7 +18,7 @@ from homeassistant.const import (
|
||||
STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE,
|
||||
ATTR_ENTITY_ID)
|
||||
from homeassistant.components import (
|
||||
group, discovery, wink, isy994, verisure, zwave, tellduslive, mysensors)
|
||||
group, wemo, wink, isy994, verisure, zwave, tellduslive, mysensors)
|
||||
|
||||
DOMAIN = 'switch'
|
||||
SCAN_INTERVAL = 30
|
||||
@@ -35,7 +35,7 @@ MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
||||
|
||||
# Maps discovered services to their platforms
|
||||
DISCOVERY_PLATFORMS = {
|
||||
discovery.SERVICE_WEMO: 'wemo',
|
||||
wemo.DISCOVER_SWITCHES: 'wemo',
|
||||
wink.DISCOVER_SWITCHES: 'wink',
|
||||
isy994.DISCOVER_SWITCHES: 'isy994',
|
||||
verisure.DISCOVER_SWITCHES: 'verisure',
|
||||
|
||||
@@ -9,14 +9,13 @@ https://home-assistant.io/components/switch.wemo/
|
||||
import logging
|
||||
|
||||
from homeassistant.components.switch import SwitchDevice
|
||||
from homeassistant.const import (
|
||||
EVENT_HOMEASSISTANT_STOP, STATE_OFF, STATE_ON, STATE_STANDBY)
|
||||
from homeassistant.const import STATE_OFF, STATE_ON, STATE_STANDBY
|
||||
from homeassistant.loader import get_component
|
||||
|
||||
DEPENDENCIES = ['wemo']
|
||||
|
||||
REQUIREMENTS = ['pywemo==0.3.12']
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
_WEMO_SUBSCRIPTION_REGISTRY = None
|
||||
|
||||
ATTR_SENSOR_STATE = "sensor_state"
|
||||
ATTR_SWITCH_MODE = "switch_mode"
|
||||
|
||||
@@ -24,73 +23,33 @@ MAKER_SWITCH_MOMENTARY = "momentary"
|
||||
MAKER_SWITCH_TOGGLE = "toggle"
|
||||
|
||||
|
||||
def _find_manual_wemos(pywemo, static_config):
|
||||
for address in static_config:
|
||||
port = pywemo.ouimeaux_device.probe_wemo(address)
|
||||
if not port:
|
||||
_LOGGER.warning('Unable to probe wemo at %s', address)
|
||||
continue
|
||||
_LOGGER.info('Adding static wemo at %s:%i', address, port)
|
||||
url = 'http://%s:%i/setup.xml' % (address, port)
|
||||
yield pywemo.discovery.device_from_description(url, None)
|
||||
|
||||
|
||||
# pylint: disable=unused-argument, too-many-function-args
|
||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||
""" Find and return WeMo switches. """
|
||||
import pywemo
|
||||
"""Register discovered WeMo switches."""
|
||||
import pywemo.discovery as discovery
|
||||
|
||||
global _WEMO_SUBSCRIPTION_REGISTRY
|
||||
if _WEMO_SUBSCRIPTION_REGISTRY is None:
|
||||
_WEMO_SUBSCRIPTION_REGISTRY = pywemo.SubscriptionRegistry()
|
||||
_WEMO_SUBSCRIPTION_REGISTRY.start()
|
||||
|
||||
def stop_wemo(event):
|
||||
""" Shutdown Wemo subscriptions and subscription thread on exit"""
|
||||
_LOGGER.info("Shutting down subscriptions.")
|
||||
_WEMO_SUBSCRIPTION_REGISTRY.stop()
|
||||
|
||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_wemo)
|
||||
|
||||
if discovery_info is not None:
|
||||
location = discovery_info[2]
|
||||
mac = discovery_info[3]
|
||||
device = discovery.device_from_description(location, mac)
|
||||
|
||||
if device and isinstance(device, pywemo.Switch):
|
||||
if device:
|
||||
add_devices_callback([WemoSwitch(device)])
|
||||
|
||||
return
|
||||
|
||||
_LOGGER.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)])
|
||||
|
||||
# Add manually-defined wemo devices
|
||||
if discovery_info is None and 'static' in config:
|
||||
add_devices_callback(
|
||||
[WemoSwitch(wemo)
|
||||
for wemo in _find_manual_wemos(pywemo, config['static'])])
|
||||
|
||||
|
||||
class WemoSwitch(SwitchDevice):
|
||||
""" Represents a WeMo switch. """
|
||||
def __init__(self, wemo):
|
||||
self.wemo = wemo
|
||||
"""Represents a WeMo switch."""
|
||||
def __init__(self, device):
|
||||
self.wemo = device
|
||||
self.insight_params = None
|
||||
self.maker_params = None
|
||||
|
||||
_WEMO_SUBSCRIPTION_REGISTRY.register(wemo)
|
||||
_WEMO_SUBSCRIPTION_REGISTRY.on(
|
||||
wemo, None, self._update_callback)
|
||||
wemo = get_component('wemo')
|
||||
wemo.SUBSCRIPTION_REGISTRY.register(self.wemo)
|
||||
wemo.SUBSCRIPTION_REGISTRY.on(self.wemo, None, self._update_callback)
|
||||
|
||||
def _update_callback(self, _device, _params):
|
||||
""" Called by the wemo device callback to update state. """
|
||||
"""Called by the wemo device callback to update state."""
|
||||
_LOGGER.info(
|
||||
'Subscription update for %s',
|
||||
_device)
|
||||
@@ -98,17 +57,17 @@ class WemoSwitch(SwitchDevice):
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
""" No polling needed with subscriptions """
|
||||
"""No polling needed with subscriptions"""
|
||||
return False
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
""" Returns the id of this WeMo switch """
|
||||
"""Returns the id of this WeMo switch"""
|
||||
return "{}.{}".format(self.__class__, self.wemo.serialnumber)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
""" Returns the name of the switch if any. """
|
||||
"""Returns the name of the switch if any."""
|
||||
return self.wemo.name
|
||||
|
||||
@property
|
||||
@@ -133,7 +92,7 @@ class WemoSwitch(SwitchDevice):
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
""" Returns the state. """
|
||||
"""Returns the state."""
|
||||
is_on = self.is_on
|
||||
if not is_on:
|
||||
return STATE_OFF
|
||||
@@ -143,19 +102,19 @@ class WemoSwitch(SwitchDevice):
|
||||
|
||||
@property
|
||||
def current_power_mwh(self):
|
||||
""" Current power usage in mwh. """
|
||||
"""Current power usage in mwh."""
|
||||
if self.insight_params:
|
||||
return self.insight_params['currentpower']
|
||||
|
||||
@property
|
||||
def today_power_mw(self):
|
||||
""" Today total power usage in mw. """
|
||||
"""Today total power usage in mw."""
|
||||
if self.insight_params:
|
||||
return self.insight_params['todaymw']
|
||||
|
||||
@property
|
||||
def is_standby(self):
|
||||
""" Is the device on - or in standby. """
|
||||
"""Is the device on - or in standby."""
|
||||
if self.insight_params:
|
||||
standby_state = self.insight_params['state']
|
||||
# Standby is actually '8' but seems more defensive
|
||||
@@ -167,12 +126,12 @@ class WemoSwitch(SwitchDevice):
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
""" True if switch is on. """
|
||||
"""True if switch is on."""
|
||||
return self.wemo.get_state()
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
""" True if switch is available. """
|
||||
"""True if switch is available."""
|
||||
if (self.wemo.model_name == 'Insight' and
|
||||
self.insight_params is None):
|
||||
return False
|
||||
@@ -183,15 +142,15 @@ class WemoSwitch(SwitchDevice):
|
||||
return True
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
""" Turns the switch on. """
|
||||
"""Turns the switch on."""
|
||||
self.wemo.on()
|
||||
|
||||
def turn_off(self):
|
||||
""" Turns the switch off. """
|
||||
"""Turns the switch off."""
|
||||
self.wemo.off()
|
||||
|
||||
def update(self):
|
||||
""" Update WeMo state. """
|
||||
"""Update WeMo state."""
|
||||
try:
|
||||
self.wemo.get_state(True)
|
||||
if self.wemo.model_name == 'Insight':
|
||||
|
||||
Reference in new issue
Block a user