2014-11-09 01:20:43 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.switch
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
Component to interface with various switches that can be controlled remotely.
|
2015-11-09 12:12:18 +00:00
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation
|
|
|
|
at https://home-assistant.io/components/switch/
|
2014-11-09 01:20:43 +00:00
|
|
|
"""
|
2014-12-05 05:06:45 +00:00
|
|
|
from datetime import timedelta
|
2015-09-27 06:17:04 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2014-11-09 01:20:43 +00:00
|
|
|
|
2015-09-27 06:17:04 +00:00
|
|
|
from homeassistant.config import load_yaml_config_file
|
2015-03-22 02:37:18 +00:00
|
|
|
from homeassistant.helpers.entity_component import EntityComponent
|
2015-06-13 21:56:20 +00:00
|
|
|
from homeassistant.helpers.entity import ToggleEntity
|
2015-03-01 09:35:58 +00:00
|
|
|
|
2014-12-07 07:57:02 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF, ATTR_ENTITY_ID)
|
2015-11-07 14:52:36 +00:00
|
|
|
from homeassistant.components import (
|
|
|
|
group, discovery, wink, isy994, verisure, zwave)
|
2014-11-09 23:12:23 +00:00
|
|
|
|
2014-11-09 01:20:43 +00:00
|
|
|
DOMAIN = 'switch'
|
2015-03-01 09:35:58 +00:00
|
|
|
SCAN_INTERVAL = 30
|
2014-11-09 01:20:43 +00:00
|
|
|
|
2015-01-20 07:47:18 +00:00
|
|
|
GROUP_NAME_ALL_SWITCHES = 'all switches'
|
|
|
|
ENTITY_ID_ALL_SWITCHES = group.ENTITY_ID_FORMAT.format('all_switches')
|
2014-11-09 01:20:43 +00:00
|
|
|
|
|
|
|
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
|
|
|
|
2014-12-16 03:14:31 +00:00
|
|
|
ATTR_TODAY_MWH = "today_mwh"
|
|
|
|
ATTR_CURRENT_POWER_MWH = "current_power_mwh"
|
2015-08-28 22:11:55 +00:00
|
|
|
ATTR_SENSOR_STATE = "sensor_state"
|
2014-11-09 01:20:43 +00:00
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
|
|
|
|
2015-01-09 08:07:58 +00:00
|
|
|
# Maps discovered services to their platforms
|
2015-01-11 07:47:23 +00:00
|
|
|
DISCOVERY_PLATFORMS = {
|
2015-07-20 07:07:01 +00:00
|
|
|
discovery.SERVICE_WEMO: 'wemo',
|
2015-01-11 07:47:23 +00:00
|
|
|
wink.DISCOVER_SWITCHES: 'wink',
|
2015-04-12 21:18:14 +00:00
|
|
|
isy994.DISCOVER_SWITCHES: 'isy994',
|
2015-11-07 14:52:36 +00:00
|
|
|
verisure.DISCOVER_SWITCHES: 'verisure',
|
|
|
|
zwave.DISCOVER_SWITCHES: 'zwave',
|
2015-01-09 08:07:58 +00:00
|
|
|
}
|
|
|
|
|
2015-06-13 21:56:20 +00:00
|
|
|
PROP_TO_ATTR = {
|
|
|
|
'current_power_mwh': ATTR_CURRENT_POWER_MWH,
|
|
|
|
'today_power_mw': ATTR_TODAY_MWH,
|
2015-08-28 22:11:55 +00:00
|
|
|
'sensor_state': ATTR_SENSOR_STATE
|
2015-06-13 21:56:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-09 01:20:43 +00: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-12-01 02:42:52 +00:00
|
|
|
hass.services.call(DOMAIN, SERVICE_TURN_ON, data)
|
2014-11-09 01:20:43 +00: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-12-01 02:42:52 +00:00
|
|
|
hass.services.call(DOMAIN, SERVICE_TURN_OFF, data)
|
2014-11-09 01:20:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
|
|
|
""" Track states and offer events for switches. """
|
2015-03-22 02:37:18 +00:00
|
|
|
component = EntityComponent(
|
2015-03-01 09:35:58 +00:00
|
|
|
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, DISCOVERY_PLATFORMS,
|
|
|
|
GROUP_NAME_ALL_SWITCHES)
|
|
|
|
component.setup(config)
|
2015-01-09 08:07:58 +00:00
|
|
|
|
2014-11-09 01:20:43 +00:00
|
|
|
def handle_switch_service(service):
|
|
|
|
""" Handles calls to the switch services. """
|
2015-03-06 07:17:05 +00:00
|
|
|
target_switches = component.extract_from_service(service)
|
2014-11-09 01:20:43 +00:00
|
|
|
|
2015-01-06 07:02:41 +00:00
|
|
|
for switch in target_switches:
|
2014-11-09 01:20:43 +00:00
|
|
|
if service.service == SERVICE_TURN_ON:
|
|
|
|
switch.turn_on()
|
|
|
|
else:
|
|
|
|
switch.turn_off()
|
|
|
|
|
2015-06-13 21:56:20 +00:00
|
|
|
if switch.should_poll:
|
|
|
|
switch.update_ha_state(True)
|
2014-11-09 01:20:43 +00:00
|
|
|
|
2015-09-27 06:17:04 +00:00
|
|
|
descriptions = load_yaml_config_file(
|
|
|
|
os.path.join(os.path.dirname(__file__), 'services.yaml'))
|
|
|
|
hass.services.register(DOMAIN, SERVICE_TURN_OFF, handle_switch_service,
|
|
|
|
descriptions.get(SERVICE_TURN_OFF))
|
|
|
|
hass.services.register(DOMAIN, SERVICE_TURN_ON, handle_switch_service,
|
|
|
|
descriptions.get(SERVICE_TURN_ON))
|
2014-11-09 01:20:43 +00:00
|
|
|
|
|
|
|
return True
|
2015-06-13 21:56:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SwitchDevice(ToggleEntity):
|
|
|
|
""" Represents a switch within Home Assistant. """
|
|
|
|
# pylint: disable=no-self-use
|
|
|
|
|
|
|
|
@property
|
|
|
|
def current_power_mwh(self):
|
|
|
|
""" Current power usage in mwh. """
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def today_power_mw(self):
|
|
|
|
""" Today total power usage in mw. """
|
|
|
|
return None
|
|
|
|
|
2015-08-29 19:32:46 +00:00
|
|
|
@property
|
2015-08-31 10:07:52 +00:00
|
|
|
def is_standby(self):
|
|
|
|
""" Is the device in standby. """
|
2015-08-29 19:32:46 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def sensor_state(self):
|
|
|
|
""" Is the sensor on or off. """
|
|
|
|
return None
|
|
|
|
|
2015-06-13 21:56:20 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
""" Returns device specific state attributes. """
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state_attributes(self):
|
|
|
|
""" Returns optional state attributes. """
|
|
|
|
data = {}
|
|
|
|
|
|
|
|
for prop, attr in PROP_TO_ATTR.items():
|
|
|
|
value = getattr(self, prop)
|
|
|
|
if value:
|
|
|
|
data[attr] = value
|
|
|
|
|
|
|
|
device_attr = self.device_state_attributes
|
|
|
|
|
|
|
|
if device_attr is not None:
|
|
|
|
data.update(device_attr)
|
|
|
|
|
|
|
|
return data
|