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
|
|
|
|
|
|
|
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 (
|
|
|
|
extract_entity_ids, platform_devices_from_config)
|
|
|
|
from homeassistant.components import group
|
2014-11-09 15:12:23 -08:00
|
|
|
|
2014-11-08 17:20:43 -08:00
|
|
|
DOMAIN = 'switch'
|
|
|
|
DEPENDENCIES = []
|
|
|
|
|
|
|
|
GROUP_NAME_ALL_SWITCHES = 'all_switches'
|
|
|
|
ENTITY_ID_ALL_SWITCHES = group.ENTITY_ID_FORMAT.format(
|
|
|
|
GROUP_NAME_ALL_SWITCHES)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
_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-06 23:57:02 -08:00
|
|
|
if not switches:
|
2014-11-08 17:20:43 -08:00
|
|
|
return False
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
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. """
|
|
|
|
|
2014-12-04 21:06:45 -08:00
|
|
|
logger.info("Updating switch states")
|
2014-11-08 17:20:43 -08:00
|
|
|
|
2015-01-05 23:02:41 -08:00
|
|
|
for switch in switches.values():
|
2014-12-04 21:06:45 -08:00
|
|
|
switch.update_ha_state(hass)
|
2014-11-08 17:20:43 -08:00
|
|
|
|
2014-12-04 21:06:45 -08:00
|
|
|
update_states(None)
|
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()
|
|
|
|
|
|
|
|
switch.update_ha_state(hass)
|
|
|
|
|
2014-11-25 00:20:36 -08:00
|
|
|
# Track all switches in a group
|
2015-01-08 20:02:34 -08:00
|
|
|
group.Group(hass, GROUP_NAME_ALL_SWITCHES, switches.keys(), False)
|
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
|