2014-11-08 17:20:43 -08:00
|
|
|
"""
|
|
|
|
homeassistant.components.switch
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
Component to interface with various switches that can be controlled remotely.
|
|
|
|
"""
|
|
|
|
import logging
|
2014-12-04 21:06:45 -08:00
|
|
|
from datetime import timedelta
|
2014-11-08 17:20:43 -08:00
|
|
|
|
2015-01-09 00:07:58 -08:00
|
|
|
from homeassistant.loader import get_component
|
2014-11-08 17:20:43 -08:00
|
|
|
import homeassistant.util as util
|
2014-12-06 23:57:02 -08:00
|
|
|
from homeassistant.const import (
|
|
|
|
STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF, ATTR_ENTITY_ID)
|
|
|
|
from homeassistant.helpers import (
|
2015-01-19 00:02:25 -08:00
|
|
|
generate_entity_id, extract_entity_ids, platform_devices_from_config)
|
2015-01-10 23:47:23 -08:00
|
|
|
from homeassistant.components import group, discovery, wink
|
2014-11-09 15:12:23 -08:00
|
|
|
|
2014-11-08 17:20:43 -08:00
|
|
|
DOMAIN = 'switch'
|
|
|
|
DEPENDENCIES = []
|
|
|
|
|
2015-01-19 23:47:18 -08:00
|
|
|
GROUP_NAME_ALL_SWITCHES = 'all switches'
|
|
|
|
ENTITY_ID_ALL_SWITCHES = group.ENTITY_ID_FORMAT.format('all_switches')
|
2014-11-08 17:20:43 -08:00
|
|
|
|
|
|
|
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
|
|
|
|
2014-12-15 19:14:31 -08:00
|
|
|
ATTR_TODAY_MWH = "today_mwh"
|
|
|
|
ATTR_CURRENT_POWER_MWH = "current_power_mwh"
|
2014-11-08 17:20:43 -08:00
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
|
|
|
|
2015-01-09 00:07:58 -08:00
|
|
|
# Maps discovered services to their platforms
|
2015-01-10 23:47:23 -08:00
|
|
|
DISCOVERY_PLATFORMS = {
|
|
|
|
discovery.services.BELKIN_WEMO: 'wemo',
|
|
|
|
wink.DISCOVER_SWITCHES: 'wink',
|
2015-01-09 00:07:58 -08:00
|
|
|
}
|
|
|
|
|
2014-11-08 17:20:43 -08:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def is_on(hass, entity_id=None):
|
|
|
|
""" Returns if the switch is on based on the statemachine. """
|
|
|
|
entity_id = entity_id or ENTITY_ID_ALL_SWITCHES
|
|
|
|
|
|
|
|
return hass.states.is_state(entity_id, STATE_ON)
|
|
|
|
|
|
|
|
|
|
|
|
def turn_on(hass, entity_id=None):
|
|
|
|
""" Turns all or specified switch on. """
|
|
|
|
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
|
|
|
|
|
2014-11-30 18:42:52 -08:00
|
|
|
hass.services.call(DOMAIN, SERVICE_TURN_ON, data)
|
2014-11-08 17:20:43 -08:00
|
|
|
|
|
|
|
|
|
|
|
def turn_off(hass, entity_id=None):
|
|
|
|
""" Turns all or specified switch off. """
|
|
|
|
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
|
|
|
|
|
2014-11-30 18:42:52 -08:00
|
|
|
hass.services.call(DOMAIN, SERVICE_TURN_OFF, data)
|
2014-11-08 17:20:43 -08:00
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
|
|
|
""" Track states and offer events for switches. """
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-01-05 23:02:41 -08:00
|
|
|
switches = platform_devices_from_config(
|
|
|
|
config, DOMAIN, hass, ENTITY_ID_FORMAT, logger)
|
2014-11-08 17:20:43 -08:00
|
|
|
|
2014-12-04 21:06:45 -08:00
|
|
|
@util.Throttle(MIN_TIME_BETWEEN_SCANS)
|
|
|
|
def update_states(now):
|
2014-11-08 17:20:43 -08:00
|
|
|
""" Update states of all switches. """
|
2015-01-09 00:07:58 -08:00
|
|
|
if switches:
|
|
|
|
logger.info("Updating switch states")
|
|
|
|
|
|
|
|
for switch in switches.values():
|
2015-02-28 22:33:44 -08:00
|
|
|
switch.update_ha_state(True)
|
2015-01-09 00:07:58 -08:00
|
|
|
|
|
|
|
update_states(None)
|
|
|
|
|
|
|
|
# Track all switches in a group
|
|
|
|
switch_group = group.Group(
|
|
|
|
hass, GROUP_NAME_ALL_SWITCHES, switches.keys(), False)
|
2014-11-08 17:20:43 -08:00
|
|
|
|
2015-01-09 00:07:58 -08:00
|
|
|
def switch_discovered(service, info):
|
|
|
|
""" Called when a switch is discovered. """
|
2015-01-10 23:47:23 -08:00
|
|
|
platform = get_component("{}.{}".format(
|
|
|
|
DOMAIN, DISCOVERY_PLATFORMS[service]))
|
2015-01-09 00:07:58 -08:00
|
|
|
|
2015-01-11 23:22:40 -08:00
|
|
|
discovered = platform.devices_discovered(hass, config, info)
|
2015-01-09 00:07:58 -08:00
|
|
|
|
2015-01-10 23:47:23 -08:00
|
|
|
for switch in discovered:
|
|
|
|
if switch is not None and switch not in switches.values():
|
2015-02-28 22:33:44 -08:00
|
|
|
switch.hass = hass
|
|
|
|
|
2015-01-19 00:02:25 -08:00
|
|
|
switch.entity_id = generate_entity_id(
|
|
|
|
ENTITY_ID_FORMAT, switch.name, switches.keys())
|
2015-01-09 00:07:58 -08:00
|
|
|
|
2015-01-10 23:47:23 -08:00
|
|
|
switches[switch.entity_id] = switch
|
2014-11-08 17:20:43 -08:00
|
|
|
|
2015-02-28 22:33:44 -08:00
|
|
|
switch.update_ha_state()
|
2014-11-08 17:20:43 -08:00
|
|
|
|
2015-01-10 23:47:23 -08:00
|
|
|
switch_group.update_tracked_entity_ids(switches.keys())
|
2015-01-09 00:07:58 -08:00
|
|
|
|
2015-01-10 23:47:23 -08:00
|
|
|
discovery.listen(hass, DISCOVERY_PLATFORMS.keys(), switch_discovered)
|
2014-11-08 17:20:43 -08:00
|
|
|
|
|
|
|
def handle_switch_service(service):
|
|
|
|
""" Handles calls to the switch services. """
|
2015-01-05 23:02:41 -08:00
|
|
|
target_switches = [switches[entity_id] for entity_id
|
|
|
|
in extract_entity_ids(hass, service)
|
|
|
|
if entity_id in switches]
|
2014-11-08 17:20:43 -08:00
|
|
|
|
2015-01-05 23:02:41 -08:00
|
|
|
if not target_switches:
|
|
|
|
target_switches = switches.values()
|
2014-11-08 17:20:43 -08:00
|
|
|
|
2015-01-05 23:02:41 -08:00
|
|
|
for switch in target_switches:
|
2014-11-08 17:20:43 -08:00
|
|
|
if service.service == SERVICE_TURN_ON:
|
|
|
|
switch.turn_on()
|
|
|
|
else:
|
|
|
|
switch.turn_off()
|
|
|
|
|
2015-02-28 22:33:44 -08:00
|
|
|
switch.update_ha_state(True)
|
2014-11-08 17:20:43 -08:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
return True
|