desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Return the SCS ID.'
@property def scs_id(self):
return self._scs_id
'No polling needed.'
@property def should_poll(self):
return False
'Return the name of the device if any.'
@property def name(self):
return self._name
'Return true if switch is on.'
@property def is_on(self):
return self._toggled
'Turn the device on.'
def turn_on(self, **kwargs):
from scsgate.tasks import ToggleStatusTask scsgate.SCSGATE.append_task(ToggleStatusTask(target=self._scs_id, toggled=True)) self._toggled = True self.schedule_update_ha_state()
'Turn the device off.'
def turn_off(self, **kwargs):
from scsgate.tasks import ToggleStatusTask scsgate.SCSGATE.append_task(ToggleStatusTask(target=self._scs_id, toggled=False)) self._toggled = False self.schedule_update_ha_state()
'Handle a SCSGate message related with this switch.'
def process_event(self, message):
if (self._toggled == message.toggled): self._logger.info('Switch %s, ignoring message %s because state already active', self._scs_id, message) return self._toggled = message.toggled self.schedule_update_ha_state() command = 'off' if self._toggled: comm...
'Initialize the scenario.'
def __init__(self, scs_id, name, logger, hass):
self._name = name self._scs_id = scs_id self._logger = logger self._hass = hass
'Return the SCS ID.'
@property def scs_id(self):
return self._scs_id
'Return the name of the device if any.'
@property def name(self):
return self._name
'Handle a SCSGate message related with this switch.'
def process_event(self, message):
from scsgate.messages import StateMessage, ScenarioTriggeredMessage if isinstance(message, StateMessage): scenario_id = message.bytes[4] elif isinstance(message, ScenarioTriggeredMessage): scenario_id = message.scenario else: self._logger.warn('Scenario switch: received ...
'Initialize the switch.'
def __init__(self, name, slave, coil):
self._name = name self._slave = (int(slave) if slave else None) self._coil = int(coil) self._is_on = None
'Return true if switch is on.'
@property def is_on(self):
return self._is_on
'Return the name of the switch.'
@property def name(self):
return self._name
'Set switch on.'
def turn_on(self, **kwargs):
modbus.HUB.write_coil(self._slave, self._coil, True)
'Set switch off.'
def turn_off(self, **kwargs):
modbus.HUB.write_coil(self._slave, self._coil, False)
'Update the state of the switch.'
def update(self):
result = modbus.HUB.read_coils(self._slave, self._coil, 1) try: self._is_on = bool(result.bits[0]) except AttributeError: _LOGGER.error('No response from modbus slave %s coil %s', self._slave, self._coil)
'Init of the Acer projector.'
def __init__(self, serial_port, name, timeout, write_timeout, **kwargs):
import serial self.ser = serial.Serial(port=serial_port, timeout=timeout, write_timeout=write_timeout, **kwargs) self._serial_port = serial_port self._name = name self._state = False self._available = False self._attributes = {LAMP_HOURS: STATE_UNKNOWN, INPUT_SOURCE: STATE_UNKNOWN, ECO_MODE:...
'Write to the projector and read the return.'
def _write_read(self, msg):
import serial ret = '' try: if (not self.ser.is_open): self.ser.open() msg = msg.encode('utf-8') self.ser.write(msg) ret = self.ser.read_until(size=20).decode('utf-8') except serial.SerialException: _LOGGER.error('Problem comunicating with %s'...
'Write msg, obtain awnser and format output.'
def _write_read_format(self, msg):
awns = self._write_read(msg) match = re.search('\\r(.+)\\r', awns) if match: return match.group(1) return STATE_UNKNOWN
'Return if projector is available.'
@property def available(self):
return self._available
'Return name of the projector.'
@property def name(self):
return self._name
'Return if the projector is turned on.'
@property def is_on(self):
return self._state
'Return state attributes.'
@property def state_attributes(self):
return self._attributes
'Get the latest state from the projector.'
def update(self):
msg = CMD_DICT[LAMP] awns = self._write_read_format(msg) if (awns == 'Lamp 1'): self._state = True self._available = True elif (awns == 'Lamp 0'): self._state = False self._available = True else: self._available = False for key in self._attributes: ...
'Turn the projector on.'
def turn_on(self):
msg = CMD_DICT[STATE_ON] self._write_read(msg) self._state = STATE_ON
'Turn the projector off.'
def turn_off(self):
msg = CMD_DICT[STATE_OFF] self._write_read(msg) self._state = STATE_OFF
'Initialize the device.'
def __init__(self, node, name):
self.node = node self.node.deviceName = name self._state = False
'Return the the name of the node.'
@property def name(self):
return self.node.deviceName
'Return the ID of this Insteon node.'
@property def unique_id(self):
return 'insteon_local_{}'.format(self.node.device_id)
'Get the updated status of the switch.'
@util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS) def update(self):
resp = self.node.status(0) while (('error' in resp) and (resp['error'] is True)): resp = self.node.status(0) if ('cmd2' in resp): self._state = (int(resp['cmd2'], 16) > 0)
'Return the boolean response if the node is on.'
@property def is_on(self):
return self._state
'Turn device on.'
def turn_on(self, **kwargs):
self.node.on() self._state = True
'Turn device off.'
def turn_off(self, **kwargs):
self.node.off() self._state = False
'Initialize the switch.'
def __init__(self, name, hikvision_cam):
self._name = name self._hikvision_cam = hikvision_cam self._state = STATE_OFF
'Poll for status regularly.'
@property def should_poll(self):
return True
'Return the name of the device if any.'
@property def name(self):
return self._name
'Return the state of the device if any.'
@property def state(self):
return self._state
'Return true if device is on.'
@property def is_on(self):
return (self._state == STATE_ON)
'Turn the device on.'
def turn_on(self, **kwargs):
_LOGGING.info('Turning on Motion Detection ') self._hikvision_cam.enable_motion_detection()
'Turn the device off.'
def turn_off(self, **kwargs):
_LOGGING.info('Turning off Motion Detection ') self._hikvision_cam.disable_motion_detection()
'Update Motion Detection state.'
def update(self):
enabled = self._hikvision_cam.is_motion_detection_enabled() _LOGGING.info('enabled: %s', enabled) self._state = (STATE_ON if enabled else STATE_OFF)
'Initialize the EnOcean switch device.'
def __init__(self, dev_id, devname):
enocean.EnOceanDevice.__init__(self) self.dev_id = dev_id self._devname = devname self._light = None self._on_state = False self._on_state2 = False self.stype = 'switch'
'Return whether the switch is on or off.'
@property def is_on(self):
return self._on_state
'Return the device name.'
@property def name(self):
return self._devname
'Turn on the switch.'
def turn_on(self, **kwargs):
optional = [3] optional.extend(self.dev_id) optional.extend([255, 0]) self.send_command(data=[210, 1, 0, 100, 0, 0, 0, 0, 0], optional=optional, packet_type=1) self._on_state = True
'Turn off the switch.'
def turn_off(self, **kwargs):
optional = [3] optional.extend(self.dev_id) optional.extend([255, 0]) self.send_command(data=[210, 1, 0, 0, 0, 0, 0, 0, 0], optional=optional, packet_type=1) self._on_state = False
'Update the internal state of the switch.'
def value_changed(self, val):
self._on_state = val self.schedule_update_ha_state()
'Initialize a new Digital Ocean sensor.'
def __init__(self, do, droplet_id):
self._digital_ocean = do self._droplet_id = droplet_id self.data = None self._state = None
'Return the name of the switch.'
@property def name(self):
return self.data.name
'Return true if switch is on.'
@property def is_on(self):
return (self.data.status == 'active')
'Return the state attributes of the Digital Ocean droplet.'
@property def device_state_attributes(self):
return {ATTR_CREATED_AT: self.data.created_at, ATTR_DROPLET_ID: self.data.id, ATTR_DROPLET_NAME: self.data.name, ATTR_FEATURES: self.data.features, ATTR_IPV4_ADDRESS: self.data.ip_address, ATTR_IPV6_ADDRESS: self.data.ip_v6_address, ATTR_MEMORY: self.data.memory, ATTR_REGION: self.data.region['name'], ATTR_VCPUS: s...
'Boot-up the droplet.'
def turn_on(self, **kwargs):
if (self.data.status != 'active'): self.data.power_on()
'Shutdown the droplet.'
def turn_off(self, **kwargs):
if (self.data.status == 'active'): self.data.power_off()
'Get the latest data from the device and update the data.'
def update(self):
self._digital_ocean.update() for droplet in self._digital_ocean.data: if (droplet.id == self._droplet_id): self.data = droplet
'Initialize the myStrom switch.'
def __init__(self, name, resource):
from pymystrom import MyStromPlug self._name = name self._resource = resource self.data = {} self.plug = MyStromPlug(self._resource) self.update()
'Return the name of the switch.'
@property def name(self):
return self._name
'Return true if switch is on.'
@property def is_on(self):
return bool(self.data['relay'])
'Return the current power consumption in W.'
@property def current_power_w(self):
return round(self.data['power'], 2)
'Turn the switch on.'
def turn_on(self, **kwargs):
from pymystrom import exceptions try: self.plug.set_relay_on() except exceptions.MyStromConnectionError: _LOGGER.error("No route to device '%s'. Is device offline?", self._resource)
'Turn the switch off.'
def turn_off(self, **kwargs):
from pymystrom import exceptions try: self.plug.set_relay_off() except exceptions.MyStromConnectionError: _LOGGER.error("No route to device '%s'. Is device offline?", self._resource)
'Get the latest data from the device and update the data.'
def update(self):
from pymystrom import exceptions try: self.data = self.plug.get_status() except exceptions.MyStromConnectionError: self.data = {'power': 0, 'relay': False} _LOGGER.error("No route to device '%s'. Is device offline?", self._resource)
'Return true if switch is on.'
@property def is_on(self):
return self.vehicle.is_heater_on
'Turn the switch on.'
def turn_on(self, **kwargs):
self.vehicle.start_heater()
'Turn the switch off.'
def turn_off(self, **kwargs):
self.vehicle.stop_heater()
'Return the icon.'
@property def icon(self):
return RESOURCES[self._attribute][2]
'Initialize the switch.'
def __init__(self, hass, plm, address, name):
self._hass = hass self._plm = plm.protocol self._address = address self._name = name self._plm.add_update_callback(self.async_switch_update, {'address': self._address})
'No polling needed.'
@property def should_poll(self):
return False
'Return the the address of the node.'
@property def address(self):
return self._address
'Return the the name of the node.'
@property def name(self):
return self._name
'Return the boolean response if the node is on.'
@property def is_on(self):
onlevel = self._plm.get_device_attr(self._address, 'onlevel') _LOGGER.debug('on level for %s is %s', self._address, onlevel) return bool(onlevel)
'Provide attributes for display on device card.'
@property def device_state_attributes(self):
insteon_plm = get_component('insteon_plm') return insteon_plm.common_attributes(self)
'Return specified attribute for this device.'
def get_attr(self, key):
return self._plm.get_device_attr(self.address, key)
'Receive notification from transport that new data exists.'
@callback def async_switch_update(self, message):
_LOGGER.info('Received update calback from PLM for %s', self._address) self._hass.async_add_job(self.async_update_ha_state())
'Turn device on.'
@asyncio.coroutine def async_turn_on(self, **kwargs):
self._plm.turn_on(self._address)
'Turn device off.'
@asyncio.coroutine def async_turn_off(self, **kwargs):
self._plm.turn_off(self._address)
'Initialize the switch.'
def __init__(self, hass, object_id, friendly_name, command_on, command_off, command_state, value_template):
self._hass = hass self.entity_id = ENTITY_ID_FORMAT.format(object_id) self._name = friendly_name self._state = False self._command_on = command_on self._command_off = command_off self._command_state = command_state self._value_template = value_template
'Execute the actual commands.'
@staticmethod def _switch(command):
_LOGGER.info('Running command: %s', command) success = (subprocess.call(command, shell=True) == 0) if (not success): _LOGGER.error('Command failed: %s', command) return success
'Execute state command for return value.'
@staticmethod def _query_state_value(command):
_LOGGER.info('Running state command: %s', command) try: return_value = subprocess.check_output(command, shell=True) return return_value.strip().decode('utf-8') except subprocess.CalledProcessError: _LOGGER.error('Command failed: %s', command)
'Execute state command for return code.'
@staticmethod def _query_state_code(command):
_LOGGER.info('Running state command: %s', command) return (subprocess.call(command, shell=True) == 0)
'Only poll if we have state command.'
@property def should_poll(self):
return (self._command_state is not None)
'Return the name of the switch.'
@property def name(self):
return self._name
'Return true if device is on.'
@property def is_on(self):
return self._state
'Return true if we do optimistic updates.'
@property def assumed_state(self):
return (self._command_state is None)
'Query for state.'
def _query_state(self):
if (not self._command_state): _LOGGER.error('No state command specified') return if self._value_template: return CommandSwitch._query_state_value(self._command_state) return CommandSwitch._query_state_code(self._command_state)
'Update device state.'
def update(self):
if self._command_state: payload = str(self._query_state()) if self._value_template: payload = self._value_template.render_with_possible_json_value(payload) self._state = (payload.lower() == 'true')
'Turn the device on.'
def turn_on(self, **kwargs):
if (CommandSwitch._switch(self._command_on) and (not self._command_state)): self._state = True self.schedule_update_ha_state()
'Turn the device off.'
def turn_off(self, **kwargs):
if (CommandSwitch._switch(self._command_off) and (not self._command_state)): self._state = False self.schedule_update_ha_state()
'Initialize PulseAudio server.'
def __init__(self, host, port, buff_sz, tcp_timeout):
self._pa_host = host self._pa_port = int(port) self._buffer_size = int(buff_sz) self._tcp_timeout = int(tcp_timeout)
'Send a command to the pa server using a socket.'
def _send_command(self, cmd, response_expected):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(self._tcp_timeout) try: sock.connect((self._pa_host, self._pa_port)) _LOGGER.info('Calling pulseaudio: %s', cmd) sock.send((cmd + '\n').encode('utf-8')) if response_expected: return_dat...
'Get the full response back from pulseaudio.'
def _get_full_response(self, sock):
result = '' rcv_buffer = sock.recv(self._buffer_size) result += rcv_buffer.decode('utf-8') while (len(rcv_buffer) == self._buffer_size): rcv_buffer = sock.recv(self._buffer_size) result += rcv_buffer.decode('utf-8') return result
'Refresh state in case an alternate process modified this data.'
@util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS) def update_module_state(self):
self._current_module_state = self._send_command('list-modules', True)
'Send a command to pulseaudio to turn on the loopback.'
def turn_on(self, sink_name, source_name):
self._send_command(str.format(LOAD_CMD, sink_name, source_name), False)
'Send a command to pulseaudio to turn off the loopback.'
def turn_off(self, module_idx):
self._send_command(str.format(UNLOAD_CMD, module_idx), False)
'For a sink/source, return it\'s module id in our cache, if found.'
def get_module_idx(self, sink_name, source_name):
result = re.search(str.format(MOD_REGEX, re.escape(sink_name), re.escape(source_name)), self._current_module_state) if (result and result.group(1).isdigit()): return int(result.group(1)) return (-1)
'Initialize the Pulseaudio switch.'
def __init__(self, hass, name, pa_server, sink_name, source_name):
self._module_idx = (-1) self._hass = hass self._name = name self._sink_name = sink_name self._source_name = source_name self._pa_svr = pa_server
'Return the name of the switch.'
@property def name(self):
return self._name
'Return true if device is on.'
@property def is_on(self):
return (self._module_idx > 0)
'Turn the device on.'
def turn_on(self, **kwargs):
if (not self.is_on): self._pa_svr.turn_on(self._sink_name, self._source_name) self._pa_svr.update_module_state(no_throttle=True) self._module_idx = self._pa_svr.get_module_idx(self._sink_name, self._source_name) self.schedule_update_ha_state() else: _LOGGER.warning(IGNORE...
'Turn the device off.'
def turn_off(self, **kwargs):
if self.is_on: self._pa_svr.turn_off(self._module_idx) self._pa_svr.update_module_state(no_throttle=True) self._module_idx = self._pa_svr.get_module_idx(self._sink_name, self._source_name) self.schedule_update_ha_state() else: _LOGGER.warning(IGNORED_SWITCH_WARN)