Skip to content

Instantly share code, notes, and snippets.

@tylermenezes
Created September 25, 2017 06:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylermenezes/22a5e8fe21612605644b3ee97fac7a49 to your computer and use it in GitHub Desktop.
Save tylermenezes/22a5e8fe21612605644b3ee97fac7a49 to your computer and use it in GitHub Desktop.
Viewsonic Projector Controller for Home Assistant
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
@rsarkhani
Copy link

Hi,
i was looking for a integration between ViewSonic projector and HA and i come across this code, can you tell me how to use/import it in HA?

@Gamerayers
Copy link

I am very curious about including this too. I just got a viewsonic projector and would LOVE to make it work with my system.

@tylermenezes
Copy link
Author

No idea if this still works, my projector was from 2009 or so. I imagine it may have changed since then and I no longer have a projector at all.

Make a folder called custom_components inside your .homeassistant config folder (thu place where your YAML files are) and put it in there.

@rsarkhani
Copy link

Thanks for your reply,
i think since then the structure of custom_component has changed a bit, it requires manifest file now:
image

i have tried this but didnt have much luck with it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment