Created
September 25, 2017 06:03
-
-
Save tylermenezes/22a5e8fe21612605644b3ee97fac7a49 to your computer and use it in GitHub Desktop.
Viewsonic Projector Controller for Home Assistant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import voluptuous as vol | |
import logging | |
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA) | |
from homeassistant.const import ( | |
STATE_ON, STATE_OFF, STATE_UNKNOWN, CONF_NAME, CONF_FILENAME) | |
import homeassistant.helpers.config_validation as cv | |
import http.client | |
from base64 import b64encode | |
from time import time | |
DOMAIN = 'viewsonic' | |
DEFAULT_NAME = 'Viewsonic Projector' | |
ICON = 'mdi:projector' | |
_LOGGER = logging.getLogger(__name__) | |
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | |
vol.Required('ip'): cv.string, | |
vol.Required('username'): cv.string, | |
vol.Required('password'): cv.string, | |
}) | |
def setup_platform(hass, config, add_devices, discovery_info=None): | |
add_devices([Viewsonic(config.get('ip'), config.get('username'), config.get('password'))]) | |
class Viewsonic(SwitchDevice): | |
@property | |
def available(self): | |
return True | |
@property | |
def name(self): | |
return "Viewsonic" | |
@property | |
def is_on(self): | |
return self._state | |
@property | |
def state_attributes(self): | |
return False | |
def __init__(self, ip, username, password): | |
self.ip = ip | |
self.username = username | |
self.password = password | |
self._toggled_at = 0 | |
self.update() | |
def request(self, toggle): | |
conn = http.client.HTTPConnection(self.ip) | |
conn.request("GET", "/protect/execPwr.cgi{}".format('?PWRCHG=1' if toggle else ''), headers={ | |
'Authorization': 'Basic {}'.format(b64encode('{}:{}'.format(self.username, self.password).encode()).decode()) | |
}) | |
res = conn.getresponse().read().decode() | |
cstate = True if (res == 'Power Off') else False | |
_LOGGER.info("Projector update; button says {} (power is {})".format(res, cstate)) | |
return cstate | |
def toggle(self): | |
if (time() - self._toggled_at > 60): | |
self._toggled_at = time() | |
self.request(True) | |
self._state = not self._state | |
else: | |
_LOGGER.warning("Can't change projector state, still changing") | |
def update(self): | |
if (time() - self._toggled_at > 60): | |
self._state = self.request(False) | |
else: | |
_LOGGER.info("Skipping projector update: still changing state.") | |
def turn_on(self): | |
self.toggle() if (not self._state) else False | |
def turn_off(self): | |
self.toggle() if (self._state) else False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for your reply,
![image](https://user-images.githubusercontent.com/48775468/146284918-1a207365-15c6-4ed7-8b4f-64323afdff45.png)
i think since then the structure of custom_component has changed a bit, it requires manifest file now:
i have tried this but didnt have much luck with it