hass-core/homeassistant/components/wemo.py

172 lines
5.2 KiB
Python
Raw Normal View History

2014-03-23 12:03:34 -07:00
"""
Component to interface with WeMo devices on the network.
"""
import logging
from datetime import datetime, timedelta
import homeassistant as ha
2014-03-23 12:03:34 -07:00
import homeassistant.util as util
from homeassistant.components import (group, extract_entity_ids,
STATE_ON, STATE_OFF,
2014-03-23 12:03:34 -07:00
SERVICE_TURN_ON, SERVICE_TURN_OFF,
ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME)
DOMAIN = 'wemo'
DEPENDENCIES = []
2014-03-23 12:03:34 -07:00
GROUP_NAME_ALL_WEMOS = 'all_wemos'
ENTITY_ID_ALL_WEMOS = group.ENTITY_ID_FORMAT.format(
GROUP_NAME_ALL_WEMOS)
ENTITY_ID_FORMAT = DOMAIN + '.{}'
ATTR_TODAY_KWH = "today_kwh"
ATTR_CURRENT_POWER = "current_power"
ATTR_TODAY_ON_TIME = "today_on_time"
ATTR_TODAY_STANDBY_TIME = "today_standby_time"
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
def is_on(hass, entity_id=None):
2014-04-14 23:48:00 -07:00
""" Returns if the wemo is on based on the statemachine. """
entity_id = entity_id or ENTITY_ID_ALL_WEMOS
return hass.states.is_state(entity_id, STATE_ON)
2014-04-14 23:48:00 -07:00
def turn_on(hass, entity_id=None):
2014-04-14 23:48:00 -07:00
""" Turns all or specified wemo on. """
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
hass.call_service(DOMAIN, SERVICE_TURN_ON, data)
2014-04-14 23:48:00 -07:00
def turn_off(hass, entity_id=None):
2014-04-14 23:48:00 -07:00
""" Turns all or specified wemo off. """
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
hass.call_service(DOMAIN, SERVICE_TURN_OFF, data)
2014-04-14 23:48:00 -07:00
2014-03-23 12:03:34 -07:00
# pylint: disable=too-many-branches
def setup(hass, config):
2014-03-23 12:03:34 -07:00
""" Track states and offer events for WeMo switches. """
logger = logging.getLogger(__name__)
try:
2014-11-08 13:57:08 -08:00
# Pylint does not play nice if not every folders has an __init__.py
# pylint: disable=no-name-in-module, import-error
import homeassistant.external.pywemo.pywemo as pywemo
2014-03-23 12:03:34 -07:00
except ImportError:
logger.exception((
"Failed to import pywemo. "
"Did you maybe not run `git submodule init` "
"and `git submodule update`?"))
2014-03-23 12:03:34 -07:00
return False
if ha.CONF_HOSTS in config[DOMAIN]:
2014-04-24 22:53:35 -07:00
devices = []
for host in config[DOMAIN][ha.CONF_HOSTS].split(","):
2014-04-24 22:53:35 -07:00
device = pywemo.device_from_host(host)
if device:
devices.append(device)
else:
logger.info("Scanning for WeMo devices")
devices = pywemo.discover_devices()
2014-03-23 12:03:34 -07:00
is_switch = lambda switch: isinstance(switch, pywemo.Switch)
2014-03-23 12:03:34 -07:00
switches = [device for device in devices if is_switch(device)]
2014-03-23 12:03:34 -07:00
if len(switches) == 0:
2014-03-23 12:03:34 -07:00
logger.error("No WeMo switches found")
return False
# Dict mapping serial no to entity IDs
sno_to_ent = {}
# Dict mapping entity IDs to devices
ent_to_dev = {}
def update_wemo_state(device):
2014-03-23 12:03:34 -07:00
""" Update the state of specified WeMo device. """
# We currently only support switches
if not is_switch(device):
2014-03-23 12:03:34 -07:00
return
try:
entity_id = sno_to_ent[device.serialnumber]
except KeyError:
# New device, set it up
entity_id = util.ensure_unique_string(
ENTITY_ID_FORMAT.format(util.slugify(device.name)),
list(ent_to_dev.keys()))
2014-03-23 12:03:34 -07:00
sno_to_ent[device.serialnumber] = entity_id
ent_to_dev[entity_id] = device
state = STATE_ON if device.get_state(True) else STATE_OFF
state_attr = {ATTR_FRIENDLY_NAME: device.name}
if isinstance(device, pywemo.Insight):
2014-03-23 12:03:34 -07:00
pass
# Should work but doesn't..
#state_attr[ATTR_TODAY_KWH] = device.today_kwh
#state_attr[ATTR_CURRENT_POWER] = device.current_power
#state_attr[ATTR_TODAY_ON_TIME] = device.today_on_time
#state_attr[ATTR_TODAY_STANDBY_TIME] = device.today_standby_time
hass.states.set(entity_id, state, state_attr)
2014-03-23 12:03:34 -07:00
# pylint: disable=unused-argument
def update_wemos_state(time, force_reload=False):
2014-03-23 12:03:34 -07:00
""" Update states of all WeMo devices. """
# First time this method gets called, force_reload should be True
if force_reload or \
datetime.now() - update_wemos_state.last_updated > \
MIN_TIME_BETWEEN_SCANS:
2014-03-23 12:03:34 -07:00
logger.info("Updating WeMo status")
update_wemos_state.last_updated = datetime.now()
2014-03-23 12:03:34 -07:00
for device in switches:
update_wemo_state(device)
2014-03-23 12:03:34 -07:00
update_wemos_state(None, True)
2014-03-23 12:03:34 -07:00
2014-10-29 00:47:55 -07:00
# Track all wemos in a group
group.setup_group(hass, GROUP_NAME_ALL_WEMOS, sno_to_ent.values(), False)
2014-03-23 12:03:34 -07:00
def handle_wemo_service(service):
2014-03-23 12:03:34 -07:00
""" Handles calls to the WeMo service. """
devices = [ent_to_dev[entity_id] for entity_id
in extract_entity_ids(hass, service)
if entity_id in ent_to_dev]
2014-03-23 12:03:34 -07:00
if not devices:
2014-03-23 12:03:34 -07:00
devices = ent_to_dev.values()
for device in devices:
if service.service == SERVICE_TURN_ON:
device.on()
else:
device.off()
update_wemo_state(device)
2014-03-23 12:03:34 -07:00
# Update WeMo state every 30 seconds
hass.track_time_change(update_wemos_state, second=[0, 30])
2014-03-23 12:03:34 -07:00
hass.services.register(DOMAIN, SERVICE_TURN_OFF, handle_wemo_service)
2014-03-23 12:03:34 -07:00
hass.services.register(DOMAIN, SERVICE_TURN_ON, handle_wemo_service)
2014-03-23 12:03:34 -07:00
return True