Problem with Insteon

There is no “attach file” here so I am going to paste the component. Save it as insteondimmeroh.py and place in your custom_components\light folder.

"""
Openhab light platform that implements lights.

"""
import logging
import requests

from homeassistant.components.light import (
    ATTR_BRIGHTNESS, Light)

_LOGGER = logging.getLogger(__name__)

DEFAULT_NAME = "Openhab Insteon Dimmer"
DEFAULT_BRIGHTNESS = "255"  
    
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
    """Setup Openhab Insteon Dimmer platform."""
    
    resource = config.get('resource')

    if resource is None:
        _LOGGER.error("Missing required variable: resource")
        return False

    try:
        requests.get(resource, timeout=10)
    except requests.exceptions.MissingSchema:
        _LOGGER.error("Missing resource or schema in configuration. "
                      "Add http:// or https:// to your URL")
        return False
    except requests.exceptions.ConnectionError:
        _LOGGER.error("No route to resource/endpoint: %s", resource)
        return False

    add_devices_callback([OpenhabLight(
        hass,
        config.get('name', DEFAULT_NAME),
        config.get('resource'),
        config.get('brightness', DEFAULT_BRIGHTNESS))]) 
    
class OpenhabLight(Light):
    """Provide an Openhab light."""

    # pylint: disable=too-many-arguments
    def __init__(self, hass, name, resource, brightness):
        """Initialize the light."""
        self._state = None
        self._hass = hass
        self._name = name
        self._resource = resource
        self._brightness = brightness

    @property
    def should_poll(self):
        """Polling for a light."""
        return True

    @property
    def name(self):
        """Return the name of the light if any."""
        return self._name

    @property
    def brightness(self):
        """Return the brightness of this light between 0..255."""
        return self._brightness

    @property
    def is_on(self):
        """Return true if light is on."""
        request = requests.get(self._resource+"/state", timeout=10)
        if int(float(request.text)) > 0:
            self._state = True
        else:
            self._state = False
        return self._state

    def turn_on(self, **kwargs):
        """Turn the light on."""
        onValue = str((kwargs.get(ATTR_BRIGHTNESS, int(self._brightness))/255)*100)
        request = requests.post(self._resource,
                                data=onValue,
                                timeout=10)
        if (request.status_code == 200) or (request.status_code == 201):
            self._state = True
        else:
            _LOGGER.info("HTTP Status Code: %s", request.status_code)
            _LOGGER.error("Can't turn on %s. Is resource/endpoint offline?", self._resource)

        self.update_ha_state()

    def turn_off(self, **kwargs):
        """Turn the light off."""
        request = requests.post(self._resource, data="0", timeout=10)
        if (request.status_code == 200) or (request.status_code == 201):
            self._state = False
        else:
            _LOGGER.error("Can't turn off %s. Is resource/endpoint offline?",
                          self._resource)

        self.update_ha_state()
3 Likes