importloggingimportvoluptuousasvol# Import the device class from the component that you want to supportfromhomeassistant.components.lightimportATTR_BRIGHTNESS,Light,PLATFORM_SCHEMAfromhomeassistant.constimportCONF_HOST,CONF_USERNAME,CONF_PASSWORDimporthomeassistant.helpers.config_validationascv# Home Assistant depends on 3rd party packages for API specific code.REQUIREMENTS=['awesome_lights==1.2.3']_LOGGER=logging.getLogger(__name__)# Validation of the user's configurationPLATFORM_SCHEMA=PLATFORM_SCHEMA.extend({vol.Required(CONF_HOST):cv.string,vol.Optional(CONF_USERNAME,default='admin'):cv.string,vol.Optional(CONF_PASSWORD):cv.string,})defsetup_platform(hass,config,add_devices,discovery_info=None):"""Setup the Awesome Light platform."""importawesomelights# Assign configuration variables. The configuration check takes care they are# present. host=config.get(CONF_HOST)username=config.get(CONF_USERNAME)password=config.get(CONF_PASSWORD)# Setup connection with devices/cloudhub=awesomelights.Hub(host,username,password)# Verify that passed in configuration worksifnothub.is_valid_login():_LOGGER.error("Could not connect to AwesomeLight hub")returnFalse# Add devicesadd_devices(AwesomeLight(light)forlightinhub.lights())classAwesomeLight(Light):"""Representation of an Awesome Light."""def__init__(self,light):"""Initialize an AwesomeLight."""self._light=lightself._name=light.nameself._state=Noneself._brightness=None@propertydefname(self):"""Return the display name of this light."""returnself._name@propertydefbrightness(self):"""Return the brightness of the light.
This method is optional. Removing it indicates to Home Assistant
that brightness is not supported for this light.
"""returnself._brightness@propertydefis_on(self):"""Return true if light is on."""returnself._statedefturn_on(self,**kwargs):"""Instruct the light to turn on.
You can skip the brightness part if your light does not support
brightness control.
"""self._light.brightness=kwargs.get(ATTR_BRIGHTNESS,255)self._light.turn_on()defturn_off(self,**kwargs):"""Instruct the light to turn off."""self._light.turn_off()defupdate(self):"""Fetch new state data for this light.
This is the only method that should fetch new data for Home Assistant.
"""self._light.update()self._state=self._light.is_on()self._brightness=self._light.brightness