2019-04-03 17:40:03 +02:00
|
|
|
"""Component to interface with switches that can be controlled remotely."""
|
2014-12-04 21:06:45 -08:00
|
|
|
from datetime import timedelta
|
2015-09-26 23:17:04 -07:00
|
|
|
import logging
|
2014-11-08 17:20:43 -08:00
|
|
|
|
2016-04-12 00:37:42 -04:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2017-07-16 10:14:46 -07:00
|
|
|
from homeassistant.loader import bind_hass
|
2015-03-21 19:37:18 -07:00
|
|
|
from homeassistant.helpers.entity_component import EntityComponent
|
2015-06-13 14:56:20 -07:00
|
|
|
from homeassistant.helpers.entity import ToggleEntity
|
2019-02-05 06:52:19 +01:00
|
|
|
from homeassistant.helpers.config_validation import ( # noqa
|
2019-07-31 12:25:30 -07:00
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
PLATFORM_SCHEMA_BASE,
|
|
|
|
ENTITY_SERVICE_SCHEMA,
|
|
|
|
)
|
2014-12-06 23:57:02 -08:00
|
|
|
from homeassistant.const import (
|
2019-07-31 12:25:30 -07:00
|
|
|
STATE_ON,
|
|
|
|
SERVICE_TURN_ON,
|
|
|
|
SERVICE_TURN_OFF,
|
|
|
|
SERVICE_TOGGLE,
|
|
|
|
)
|
2016-06-11 17:43:13 -07:00
|
|
|
from homeassistant.components import group
|
2014-11-09 15:12:23 -08:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
DOMAIN = "switch"
|
2017-01-06 00:16:12 +01:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=30)
|
2014-11-08 17:20:43 -08:00
|
|
|
|
2019-07-31 12:25:30 -07: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
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
ENTITY_ID_FORMAT = DOMAIN + ".{}"
|
2014-11-08 17:20:43 -08:00
|
|
|
|
2017-03-19 21:02:11 +00:00
|
|
|
ATTR_TODAY_ENERGY_KWH = "today_energy_kwh"
|
2017-05-03 03:32:19 -03:00
|
|
|
ATTR_CURRENT_POWER_W = "current_power_w"
|
2014-11-08 17:20:43 -08:00
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
|
|
|
|
2015-06-13 14:56:20 -07:00
|
|
|
PROP_TO_ATTR = {
|
2019-07-31 12:25:30 -07:00
|
|
|
"current_power_w": ATTR_CURRENT_POWER_W,
|
|
|
|
"today_energy_kwh": ATTR_TODAY_ENERGY_KWH,
|
2015-06-13 14:56:20 -07:00
|
|
|
}
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
DEVICE_CLASS_OUTLET = "outlet"
|
|
|
|
DEVICE_CLASS_SWITCH = "switch"
|
2019-04-17 02:07:14 +02:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
DEVICE_CLASSES = [DEVICE_CLASS_OUTLET, DEVICE_CLASS_SWITCH]
|
2019-04-17 02:07:14 +02:00
|
|
|
|
|
|
|
DEVICE_CLASSES_SCHEMA = vol.All(vol.Lower, vol.In(DEVICE_CLASSES))
|
|
|
|
|
2014-11-08 17:20:43 -08:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2017-07-16 10:14:46 -07:00
|
|
|
@bind_hass
|
2014-11-08 17:20:43 -08:00
|
|
|
def is_on(hass, entity_id=None):
|
2017-02-02 06:44:05 +01:00
|
|
|
"""Return if the switch is on based on the statemachine.
|
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
2014-11-08 17:20:43 -08:00
|
|
|
entity_id = entity_id or ENTITY_ID_ALL_SWITCHES
|
|
|
|
return hass.states.is_state(entity_id, STATE_ON)
|
|
|
|
|
|
|
|
|
2018-02-24 19:24:33 +01:00
|
|
|
async def async_setup(hass, config):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Track states and offer events for switches."""
|
2018-07-06 23:05:34 +02:00
|
|
|
component = hass.data[DOMAIN] = EntityComponent(
|
2019-07-31 12:25:30 -07:00
|
|
|
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_SWITCHES
|
|
|
|
)
|
2018-02-24 19:24:33 +01:00
|
|
|
await component.async_setup(config)
|
2015-01-09 00:07:58 -08:00
|
|
|
|
2018-08-16 09:50:11 +02:00
|
|
|
component.async_register_entity_service(
|
2019-07-31 12:25:30 -07:00
|
|
|
SERVICE_TURN_OFF, ENTITY_SERVICE_SCHEMA, "async_turn_off"
|
2018-08-16 09:50:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
component.async_register_entity_service(
|
2019-07-31 12:25:30 -07:00
|
|
|
SERVICE_TURN_ON, ENTITY_SERVICE_SCHEMA, "async_turn_on"
|
2018-08-16 09:50:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
component.async_register_entity_service(
|
2019-07-31 12:25:30 -07:00
|
|
|
SERVICE_TOGGLE, ENTITY_SERVICE_SCHEMA, "async_toggle"
|
2018-08-16 09:50:11 +02:00
|
|
|
)
|
2014-11-08 17:20:43 -08:00
|
|
|
|
|
|
|
return True
|
2015-06-13 14:56:20 -07:00
|
|
|
|
|
|
|
|
2018-07-06 23:05:34 +02:00
|
|
|
async def async_setup_entry(hass, entry):
|
2018-08-19 22:29:08 +02:00
|
|
|
"""Set up a config entry."""
|
2018-07-06 23:05:34 +02:00
|
|
|
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 14:56:20 -07:00
|
|
|
class SwitchDevice(ToggleEntity):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Representation of a switch."""
|
2015-06-13 14:56:20 -07: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 14:56:20 -07: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 14:56:20 -07:00
|
|
|
return None
|
|
|
|
|
2015-08-29 20:32:46 +01:00
|
|
|
@property
|
2015-08-31 11:07:52 +01:00
|
|
|
def is_standby(self):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Return true if device is in standby."""
|
2015-08-29 20:32:46 +01:00
|
|
|
return None
|
|
|
|
|
2015-06-13 14:56:20 -07:00
|
|
|
@property
|
|
|
|
def state_attributes(self):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Return the optional state attributes."""
|
2015-06-13 14:56:20 -07:00
|
|
|
data = {}
|
|
|
|
|
|
|
|
for prop, attr in PROP_TO_ATTR.items():
|
|
|
|
value = getattr(self, prop)
|
|
|
|
if value:
|
|
|
|
data[attr] = value
|
|
|
|
|
|
|
|
return data
|
2019-04-17 02:07:14 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the class of this device, from component DEVICE_CLASSES."""
|
|
|
|
return None
|