SHARE
TWEET

Untitled

a guest Sep 25th, 2017 35 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import voluptuous as vol
  2. from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
  3. from homeassistant.const import (
  4.     STATE_ON, STATE_OFF, STATE_UNKNOWN, CONF_NAME, CONF_FILENAME)
  5. import homeassistant.helpers.config_validation as cv
  6. import struct
  7. import socket
  8.  
  9. DOMAIN = 'esp8266_curtains'
  10. DEFAULT_NAME = 'Curtains'
  11. ICON = 'mdi:switch'
  12.  
  13. PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
  14.     vol.Required('ip'): cv.string,
  15.     vol.Optional('port', default=7788): cv.positive_int
  16. })
  17.  
  18. def setup_platform(hass, config, add_devices, discovery_info=None):
  19.     add_devices([Esp8266Curtain(config.get('ip'), config.get('port'))])
  20.  
  21. class Esp8266Curtain(SwitchDevice):
  22.     def __init__(self, ip, port):
  23.         self.ip = ip
  24.         self.port = port
  25.         self._state = False
  26.  
  27.     @property
  28.     def available(self):
  29.         return True
  30.  
  31.     @property
  32.     def name(self):
  33.         return "ESP8266 Curtains"
  34.  
  35.     @property
  36.     def is_on(self):
  37.         return self._state
  38.  
  39.     @property
  40.     def state_attributes(self):
  41.         return False
  42.  
  43.     def _send(self, command):
  44.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  45.         s.settimeout(5)
  46.         s.connect((self.ip, self.port))
  47.         s.send(command.encode())
  48.         s.close()
  49.  
  50.     def update(self):
  51.         pass
  52.  
  53.     def turn_on(self):
  54.         if self._state == False:
  55.             self._send("open")
  56.         self._state = True
  57.  
  58.     def turn_off(self):
  59.         if self._state == True:
  60.             self._send("close")
  61.         self._state = False
RAW Paste Data
Top