2019-04-03 15:40:03 +00:00
|
|
|
"""Support for switches using GC100."""
|
2017-10-30 07:40:14 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.components.switch import PLATFORM_SCHEMA
|
|
|
|
from homeassistant.const import DEVICE_DEFAULT_NAME
|
2017-10-30 07:40:14 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.helpers.entity import ToggleEntity
|
2019-03-21 05:56:46 +00:00
|
|
|
|
|
|
|
from . import CONF_PORTS, DATA_GC100
|
2017-10-30 07:40:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
_SWITCH_SCHEMA = vol.Schema({cv.string: cv.string})
|
2017-10-30 07:40:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Required(CONF_PORTS): vol.All(cv.ensure_list, [_SWITCH_SCHEMA])}
|
|
|
|
)
|
2017-10-30 07:40:14 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-10-30 07:40:14 +00:00
|
|
|
"""Set up the GC100 devices."""
|
|
|
|
switches = []
|
|
|
|
ports = config.get(CONF_PORTS)
|
|
|
|
for port in ports:
|
|
|
|
for port_addr, port_name in port.items():
|
2019-07-31 19:25:30 +00:00
|
|
|
switches.append(GC100Switch(port_name, port_addr, hass.data[DATA_GC100]))
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(switches, True)
|
2017-10-30 07:40:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GC100Switch(ToggleEntity):
|
|
|
|
"""Represent a switch/relay from GC100."""
|
|
|
|
|
|
|
|
def __init__(self, name, port_addr, gc100):
|
|
|
|
"""Initialize the GC100 switch."""
|
|
|
|
self._name = name or DEVICE_DEFAULT_NAME
|
|
|
|
self._port_addr = port_addr
|
|
|
|
self._gc100 = gc100
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the switch."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return the state of the entity."""
|
|
|
|
return self._state
|
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def turn_on(self, **kwargs):
|
2017-10-30 07:40:14 +00:00
|
|
|
"""Turn the device on."""
|
|
|
|
self._gc100.write_switch(self._port_addr, 1, self.set_state)
|
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def turn_off(self, **kwargs):
|
2017-10-30 07:40:14 +00:00
|
|
|
"""Turn the device off."""
|
|
|
|
self._gc100.write_switch(self._port_addr, 0, self.set_state)
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update the sensor state."""
|
|
|
|
self._gc100.read_sensor(self._port_addr, self.set_state)
|
|
|
|
|
|
|
|
def set_state(self, state):
|
|
|
|
"""Set the current state."""
|
|
|
|
self._state = state == 1
|
|
|
|
self.schedule_update_ha_state()
|