2014-11-09 01:20:43 +00:00
|
|
|
"""
|
|
|
|
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
|
2014-11-09 01:20:43 +00:00
|
|
|
|
2016-04-12 04:37:42 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2017-02-02 05:44:05 +00:00
|
|
|
from homeassistant.core import callback
|
2017-07-16 17:14:46 +00:00
|
|
|
from homeassistant.loader import bind_hass
|
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
|
2016-03-28 01:48:51 +00:00
|
|
|
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA # noqa
|
2016-04-12 04:37:42 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2014-12-07 07:57:02 +00:00
|
|
|
from homeassistant.const import (
|
2016-01-16 15:45:05 +00:00
|
|
|
STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE,
|
|
|
|
ATTR_ENTITY_ID)
|
2016-06-12 00:43:13 +00:00
|
|
|
from homeassistant.components import group
|
2014-11-09 23:12:23 +00:00
|
|
|
|
2014-11-09 01:20:43 +00:00
|
|
|
DOMAIN = 'switch'
|
2017-06-15 22:52:28 +00:00
|
|
|
DEPENDENCIES = ['group']
|
2017-01-05 23:16:12 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=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 + '.{}'
|
|
|
|
|
2017-03-19 21:02:11 +00:00
|
|
|
ATTR_TODAY_ENERGY_KWH = "today_energy_kwh"
|
2017-05-03 06:32:19 +00:00
|
|
|
ATTR_CURRENT_POWER_W = "current_power_w"
|
2014-11-09 01:20:43 +00:00
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
|
|
|
|
2015-06-13 21:56:20 +00:00
|
|
|
PROP_TO_ATTR = {
|
2017-05-03 06:32:19 +00:00
|
|
|
'current_power_w': ATTR_CURRENT_POWER_W,
|
2017-03-19 21:02:11 +00:00
|
|
|
'today_energy_kwh': ATTR_TODAY_ENERGY_KWH,
|
2015-06-13 21:56:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 04:37:42 +00:00
|
|
|
SWITCH_SERVICE_SCHEMA = vol.Schema({
|
|
|
|
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
|
|
|
|
})
|
|
|
|
|
2014-11-09 01:20:43 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2017-07-16 17:14:46 +00:00
|
|
|
@bind_hass
|
2014-11-09 01:20:43 +00:00
|
|
|
def is_on(hass, entity_id=None):
|
2017-02-02 05:44:05 +00:00
|
|
|
"""Return if the switch is on based on the statemachine.
|
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
2014-11-09 01:20:43 +00:00
|
|
|
entity_id = entity_id or ENTITY_ID_ALL_SWITCHES
|
|
|
|
return hass.states.is_state(entity_id, STATE_ON)
|
|
|
|
|
|
|
|
|
2017-07-16 17:14:46 +00:00
|
|
|
@bind_hass
|
2014-11-09 01:20:43 +00:00
|
|
|
def turn_on(hass, entity_id=None):
|
2017-02-02 05:44:05 +00:00
|
|
|
"""Turn all or specified switch on."""
|
2017-02-23 20:57:25 +00:00
|
|
|
hass.add_job(async_turn_on, hass, entity_id)
|
2017-02-02 05:44:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
2017-07-16 17:14:46 +00:00
|
|
|
@bind_hass
|
2017-02-02 05:44:05 +00:00
|
|
|
def async_turn_on(hass, entity_id=None):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn all or specified switch on."""
|
2014-11-09 01:20:43 +00:00
|
|
|
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
|
2017-02-02 05:44:05 +00:00
|
|
|
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data))
|
2014-11-09 01:20:43 +00:00
|
|
|
|
|
|
|
|
2017-07-16 17:14:46 +00:00
|
|
|
@bind_hass
|
2014-11-09 01:20:43 +00:00
|
|
|
def turn_off(hass, entity_id=None):
|
2017-02-02 05:44:05 +00:00
|
|
|
"""Turn all or specified switch off."""
|
2017-02-23 20:57:25 +00:00
|
|
|
hass.add_job(async_turn_off, hass, entity_id)
|
2017-02-02 05:44:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
2017-07-16 17:14:46 +00:00
|
|
|
@bind_hass
|
2017-02-02 05:44:05 +00:00
|
|
|
def async_turn_off(hass, entity_id=None):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn all or specified switch off."""
|
2014-11-09 01:20:43 +00:00
|
|
|
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
|
2017-02-02 05:44:05 +00:00
|
|
|
hass.async_add_job(
|
|
|
|
hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data))
|
2014-11-09 01:20:43 +00:00
|
|
|
|
|
|
|
|
2017-07-16 17:14:46 +00:00
|
|
|
@bind_hass
|
2016-01-16 15:45:05 +00:00
|
|
|
def toggle(hass, entity_id=None):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Toggle all or specified switch."""
|
2016-01-16 15:45:05 +00:00
|
|
|
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
|
|
|
|
hass.services.call(DOMAIN, SERVICE_TOGGLE, data)
|
|
|
|
|
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async def async_setup(hass, config):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Track states and offer events for switches."""
|
2018-07-06 21:05:34 +00:00
|
|
|
component = hass.data[DOMAIN] = EntityComponent(
|
2016-06-12 00:43:13 +00:00
|
|
|
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_SWITCHES)
|
2018-02-24 18:24:33 +00:00
|
|
|
await component.async_setup(config)
|
2015-01-09 08:07:58 +00:00
|
|
|
|
2018-08-16 07:50:11 +00:00
|
|
|
component.async_register_entity_service(
|
|
|
|
SERVICE_TURN_OFF, SWITCH_SERVICE_SCHEMA,
|
|
|
|
'async_turn_off'
|
|
|
|
)
|
|
|
|
|
|
|
|
component.async_register_entity_service(
|
|
|
|
SERVICE_TURN_ON, SWITCH_SERVICE_SCHEMA,
|
|
|
|
'async_turn_on'
|
|
|
|
)
|
|
|
|
|
|
|
|
component.async_register_entity_service(
|
|
|
|
SERVICE_TOGGLE, SWITCH_SERVICE_SCHEMA,
|
|
|
|
'async_toggle'
|
|
|
|
)
|
2014-11-09 01:20:43 +00:00
|
|
|
|
|
|
|
return True
|
2015-06-13 21:56:20 +00:00
|
|
|
|
|
|
|
|
2018-07-06 21:05:34 +00:00
|
|
|
async def async_setup_entry(hass, entry):
|
|
|
|
"""Setup a config entry."""
|
|
|
|
return await hass.data[DOMAIN].async_setup_entry(entry)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass, entry):
|
|
|
|
"""Unload a config entry."""
|
|
|
|
return await hass.data[DOMAIN].async_unload_entry(entry)
|
|
|
|
|
|
|
|
|
2015-06-13 21:56:20 +00:00
|
|
|
class SwitchDevice(ToggleEntity):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Representation of a switch."""
|
2015-06-13 21:56:20 +00:00
|
|
|
|
|
|
|
@property
|
2017-03-19 21:02:11 +00:00
|
|
|
def current_power_w(self):
|
|
|
|
"""Return the current power usage in W."""
|
2015-06-13 21:56:20 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
2017-03-19 21:02:11 +00:00
|
|
|
def today_energy_kwh(self):
|
|
|
|
"""Return the today total energy usage in kWh."""
|
2015-06-13 21:56:20 +00:00
|
|
|
return None
|
|
|
|
|
2015-08-29 19:32:46 +00:00
|
|
|
@property
|
2015-08-31 10:07:52 +00:00
|
|
|
def is_standby(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return true if device is in standby."""
|
2015-08-29 19:32:46 +00:00
|
|
|
return None
|
|
|
|
|
2015-06-13 21:56:20 +00:00
|
|
|
@property
|
|
|
|
def state_attributes(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the optional state attributes."""
|
2015-06-13 21:56:20 +00:00
|
|
|
data = {}
|
|
|
|
|
|
|
|
for prop, attr in PROP_TO_ATTR.items():
|
|
|
|
value = getattr(self, prop)
|
|
|
|
if value:
|
|
|
|
data[attr] = value
|
|
|
|
|
|
|
|
return data
|