Example #1
0
    def decora(self, switch_name, command, brightness):
        logging.debug('switch:' + switch_name + ' command:' + str(command) +
                      ' brightness: ' + brightness)
        username = Home.private.get('Decora', 'username')
        password = Home.private.get('Decora', 'password')

        session = DecoraWiFiSession()
        session.login(username, password)

        perms = session.user.get_residential_permissions()
        all_residences = []
        for permission in perms:
            if permission.residentialAccountId is not None:
                acct = ResidentialAccount(session,
                                          permission.residentialAccountId)
                for res in acct.get_residences():
                    all_residences.append(res)
            elif permission.residenceId is not None:
                res = Residence(session, permission.residenceId)
                all_residences.append(res)
        for residence in all_residences:
            for switch in residence.get_iot_switches():
                attribs = {}
                if switch.name == switch_name:
                    if brightness is not "None":
                        attribs['brightness'] = brightness
                    if command == 'ON':
                        attribs['power'] = 'ON'
                    else:
                        attribs['power'] = 'OFF'
                    logging.debug(switch.name + ':' + str(attribs))
                switch.update_attributes(attribs)

        Person.logout(session)
Example #2
0
def setup_platform(
    hass: HomeAssistant,
    config: ConfigType,
    add_entities: AddEntitiesCallback,
    discovery_info: DiscoveryInfoType | None = None,
) -> None:
    """Set up the Decora WiFi platform."""

    email = config[CONF_USERNAME]
    password = config[CONF_PASSWORD]
    session = DecoraWiFiSession()

    try:
        success = session.login(email, password)

        # If login failed, notify user.
        if success is None:
            msg = "Failed to log into myLeviton Services. Check credentials."
            _LOGGER.error(msg)
            persistent_notification.create(hass,
                                           msg,
                                           title=NOTIFICATION_TITLE,
                                           notification_id=NOTIFICATION_ID)
            return

        # Gather all the available devices...
        perms = session.user.get_residential_permissions()
        all_switches = []
        for permission in perms:
            if permission.residentialAccountId is not None:
                acct = ResidentialAccount(session,
                                          permission.residentialAccountId)
                for residence in acct.get_residences():
                    for switch in residence.get_iot_switches():
                        all_switches.append(switch)
            elif permission.residenceId is not None:
                residence = Residence(session, permission.residenceId)
                for switch in residence.get_iot_switches():
                    all_switches.append(switch)

        add_entities(DecoraWifiLight(sw) for sw in all_switches)
    except ValueError:
        _LOGGER.error("Failed to communicate with myLeviton Service")

    # Listen for the stop event and log out.
    def logout(event):
        """Log out..."""
        try:
            if session is not None:
                Person.logout(session)
        except ValueError:
            _LOGGER.error("Failed to log out of myLeviton Service")

    hass.bus.listen(EVENT_HOMEASSISTANT_STOP, logout)
Example #3
0
 def _get_residences(self):
     perms = self.session.user.get_residential_permissions()
     self.residences = []
     for permission in perms:
         if permission.residentialAccountId is not None:
             acct = ResidentialAccount(
                 self.session, permission.residentialAccountId)
             for res in acct.get_residences():
                 self.residences.append(res)
         elif permission.residenceId is not None:
             res = Residence(self.session, permission.residenceId)
             self.residences.append(res)
Example #4
0
def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up the Decora WiFi platform."""
    # pylint: disable=import-error, no-name-in-module
    from decora_wifi import DecoraWiFiSession
    from decora_wifi.models.person import Person
    from decora_wifi.models.residential_account import ResidentialAccount
    from decora_wifi.models.residence import Residence

    email = config.get(CONF_USERNAME)
    password = config.get(CONF_PASSWORD)
    session = DecoraWiFiSession()

    try:
        success = session.login(email, password)

        # If login failed, notify user.
        if success is None:
            msg = 'Failed to log into myLeviton Services. Check credentials.'
            _LOGGER.error(msg)
            hass.components.persistent_notification.create(
                msg, title=NOTIFICATION_TITLE, notification_id=NOTIFICATION_ID)
            return False

        # Gather all the available devices...
        perms = session.user.get_residential_permissions()
        all_switches = []
        for permission in perms:
            if permission.residentialAccountId is not None:
                acct = ResidentialAccount(session,
                                          permission.residentialAccountId)
                for residence in acct.get_residences():
                    for switch in residence.get_iot_switches():
                        all_switches.append(switch)
            elif permission.residenceId is not None:
                residence = Residence(session, permission.residenceId)
                for switch in residence.get_iot_switches():
                    all_switches.append(switch)

        add_entities(DecoraWifiLight(sw) for sw in all_switches)
    except ValueError:
        _LOGGER.error('Failed to communicate with myLeviton Service.')

    # Listen for the stop event and log out.
    def logout(event):
        """Log out..."""
        try:
            if session is not None:
                Person.logout(session)
        except ValueError:
            _LOGGER.error('Failed to log out of myLeviton Service.')

    hass.bus.listen(EVENT_HOMEASSISTANT_STOP, logout)
Example #5
0
 def get_switch_id(self):
     for permission in self.perms:
         acct = ResidentialAccount(self.session,
                                   permission.residentialAccountId)
         residences = acct.get_residences()
         LOG.info('Residences found :' + str(residences))
         for residence in residences:
             switches = residence.get_iot_switches()
             LOG.info('Switches found :' + str(switches))
             for switch in switches:
                 switch_id = switch
                 break
             break
         break
     return switch_id
if len(sys.argv) > 5:
    decora_bright = int(sys.argv[5])
else:
    decora_bright = None

session = DecoraWiFiSession()
session.login(decora_email, decora_pass)

# Gather all the available devices...

perms = session.user.get_residential_permissions()
all_residences = []
for permission in perms:
    if permission.residentialAccountId is not None:
        acct = ResidentialAccount(session, permission.residentialAccountId)
        for res in acct.get_residences():
            all_residences.append(res)
    elif permission.residenceId is not None:
        res = Residence(session, permission.residenceId)
        all_residences.append(res)
all_switches = []
for residence in all_residences:
    attribs = {}
    if decora_bright is not None:
        attribs['brightness'] = decora_bright
    if decora_cmd == 'ON':
        attribs['power'] = 'ON'
    else:
        attribs['power'] = 'OFF'
    residence.update_by_id_iot_switches(decora_switch_id, attribs)