Esempio n. 1
0
def test_remote_api_broken(local_auth_response_200, local_url,
                           remote_auth_response_200, remote_url, sprinkler_id):
    """ Tests the broken_remote_api decorator """
    with requests_mock.Mocker() as mock:
        mock.post(
            '{}/login/auth'.format(remote_url),
            text=json.dumps(remote_auth_response_200),
            cookies=remote_cookies)
        mock.post(
            '{}/auth/login'.format(local_url),
            text=json.dumps(local_auth_response_200),
            cookies=local_cookies)
        mock.post(
            '{}/s/{}/api/4/program/1/start'.format(remote_url, sprinkler_id),
            exc=rm.exceptions.BrokenAPICall(
                'start() currently broken in remote API'))

        auth_local = rm.Authenticator.create_local('192.168.1.100', '12345')
        auth_remote = rm.Authenticator.create_remote('*****@*****.**', '12345')

        with pytest.raises(rm.exceptions.BrokenAPICall) as exc_info:
            client = rm.Client(auth_remote)
            client.programs._broken_remote_api_test()
            assert 'currently broken in remote API' in str(exc_info)

        client = rm.Client(auth_local)
        assert client.programs._broken_remote_api_test() == {'status': 'ok'}
Esempio n. 2
0
def test_all_operations(
        restrictions_current_response_200, restrictions_global_response_200,
        restrictions_hourly_response_200, restrictions_raindelay_response_200,
        local_cookies, local_url, local_auth_response_200):
    """ Tests getting the program list """
    with requests_mock.Mocker() as mock:
        mock.post(
            '{}/auth/login'.format(local_url),
            text=json.dumps(local_auth_response_200),
            cookies=local_cookies)
        mock.get(
            '{}/restrictions/currently'.format(local_url),
            text=json.dumps(restrictions_current_response_200),
            cookies=local_cookies)
        mock.get(
            '{}/restrictions/global'.format(local_url),
            text=json.dumps(restrictions_global_response_200),
            cookies=local_cookies)
        mock.get(
            '{}/restrictions/hourly'.format(local_url),
            text=json.dumps(restrictions_hourly_response_200),
            cookies=local_cookies)
        mock.get(
            '{}/restrictions/raindelay'.format(local_url),
            text=json.dumps(restrictions_raindelay_response_200),
            cookies=local_cookies)

        auth = rm.Authenticator.create_local('192.168.1.100', '12345')
        client = rm.Client(auth).restrictions
        assert client.current() == restrictions_current_response_200
        assert client.hourly() == restrictions_hourly_response_200
        assert client.raindelay() == restrictions_raindelay_response_200
        assert client.universal() == restrictions_global_response_200
Esempio n. 3
0
def test_all_operations(
        client_general_response_200, local_cookies, local_url,
        local_auth_response_200, watering_log_response_200,
        watering_logdate_response_200, watering_logdatedetails_response_200,
        watering_logdetails_response_200, watering_queue_response_200,
        watering_runs_response_200):
    """ Tests getting the program list """
    with requests_mock.Mocker() as mock:
        mock.post(
            '{}/auth/login'.format(local_url),
            text=json.dumps(local_auth_response_200),
            cookies=local_cookies)
        mock.get(
            '{}/watering/log'.format(local_url),
            text=json.dumps(watering_log_response_200),
            cookies=local_cookies)
        mock.get(
            '{}/watering/log/2017-06-29/2'.format(local_url),
            text=json.dumps(watering_logdate_response_200),
            cookies=local_cookies)
        mock.get(
            '{}/watering/log/details'.format(local_url),
            text=json.dumps(watering_logdetails_response_200),
            cookies=local_cookies)
        mock.get(
            '{}/watering/log/details/2017-06-29/2'.format(local_url),
            text=json.dumps(watering_logdatedetails_response_200),
            cookies=local_cookies)
        mock.get(
            '{}/watering/queue'.format(local_url),
            text=json.dumps(watering_queue_response_200),
            cookies=local_cookies)
        mock.get(
            '{}/watering/past/2017-06-29/2'.format(local_url),
            text=json.dumps(watering_runs_response_200),
            cookies=local_cookies)
        mock.post(
            '{}/watering/stopall'.format(local_url),
            text=json.dumps(client_general_response_200),
            cookies=local_cookies)

        auth = rm.Authenticator.create_local('192.168.1.100', '12345')
        client = rm.Client(auth).watering
        assert client.log() == watering_log_response_200
        assert client.log('2017-06-29', 2) == watering_logdate_response_200
        assert client.log('6/29/2017', 2) == watering_logdate_response_200
        assert client.log(details=True) == watering_logdetails_response_200
        assert client.log(
            '2017-06-29', 2,
            details=True) == watering_logdatedetails_response_200
        assert client.log(
            '6/29/2017', 2,
            details=True) == watering_logdatedetails_response_200
        assert client.queue() == watering_queue_response_200
        assert client.runs('2017-06-29', 2) == watering_runs_response_200
        assert client.runs('6/29/2017', 2) == watering_runs_response_200
        assert client.stop_all() == client_general_response_200
Esempio n. 4
0
def setup_platform(hass, config, add_devices, discovery_info=None):
    """Set this component up under its platform."""
    import regenmaschine as rm

    _LOGGER.debug('Config data: %s', config)

    ip_address = config.get(CONF_IP_ADDRESS, None)
    email_address = config.get(CONF_EMAIL, None)
    password = config[CONF_PASSWORD]
    zone_run_time = config[CONF_ZONE_RUN_TIME]

    try:
        if ip_address:
            _LOGGER.debug('Configuring local API')

            port = config[CONF_PORT]
            ssl = config[CONF_SSL]
            auth = rm.Authenticator.create_local(
                ip_address, password, port=port, https=ssl)
        elif email_address:
            _LOGGER.debug('Configuring remote API')
            auth = rm.Authenticator.create_remote(email_address, password)

        _LOGGER.debug('Querying against: %s', auth.url)

        client = rm.Client(auth)
        device_name = client.provision.device_name()['name']
        device_mac = client.provision.wifi()['macAddress']

        entities = []
        for program in client.programs.all().get('programs', {}):
            if not program.get('active'):
                continue

            _LOGGER.debug('Adding program: %s', program)
            entities.append(
                RainMachineProgram(client, device_name, device_mac, program))

        for zone in client.zones.all().get('zones', {}):
            if not zone.get('active'):
                continue

            _LOGGER.debug('Adding zone: %s', zone)
            entities.append(
                RainMachineZone(client, device_name, device_mac, zone,
                                zone_run_time))

        add_devices(entities)
    except rm.exceptions.HTTPError as exc_info:
        _LOGGER.error('An HTTP error occurred while talking with RainMachine')
        _LOGGER.debug(exc_info)
        return False
    except UnboundLocalError as exc_info:
        _LOGGER.error('Could not authenticate against RainMachine')
        _LOGGER.debug(exc_info)
        return False
Esempio n. 5
0
def test_all_operations(
        client_general_response_200, local_cookies, local_url,
        local_auth_response_200, zones_all_response_200,
        zones_all_advanced_response_200, zones_get_response_200,
        zones_get_advanced_response_200, zones_simulate_response_200):
    """ Tests getting the program list """
    with requests_mock.Mocker() as mock:
        mock.post(
            '{}/auth/login'.format(local_url),
            text=json.dumps(local_auth_response_200),
            cookies=local_cookies)
        mock.get(
            '{}/zone'.format(local_url),
            text=json.dumps(zones_all_response_200),
            cookies=local_cookies)
        mock.get(
            '{}/zone/properties'.format(local_url),
            text=json.dumps(zones_all_advanced_response_200),
            cookies=local_cookies)
        mock.post(
            '{}/zone/simulate'.format(local_url),
            text=json.dumps(zones_simulate_response_200),
            cookies=local_cookies)
        mock.get(
            '{}/zone/1'.format(local_url),
            text=json.dumps(zones_get_response_200),
            cookies=local_cookies)
        mock.get(
            '{}/zone/1/properties'.format(local_url),
            text=json.dumps(zones_get_advanced_response_200),
            cookies=local_cookies)
        mock.post(
            '{}/zone/1/start'.format(local_url),
            text=json.dumps(client_general_response_200),
            cookies=local_cookies)
        mock.post(
            '{}/zone/1/stop'.format(local_url),
            text=json.dumps(client_general_response_200),
            cookies=local_cookies)

        auth = rm.Authenticator.create_local('192.168.1.100', '12345')
        client = rm.Client(auth).zones
        assert client.all() == zones_all_response_200
        assert client.all(True) == zones_all_advanced_response_200
        assert client.get(1) == zones_get_response_200
        assert client.get(1, True) == zones_get_advanced_response_200
        assert client.simulate(
            zones_get_advanced_response_200) == zones_simulate_response_200
        assert client.start(1, 60) == client_general_response_200
        assert client.stop(1) == client_general_response_200
Esempio n. 6
0
def test_all_operations(local_cookies, local_url, local_auth_response_200,
                        parser_current_response_200):
    """ Tests getting the program list """
    with requests_mock.Mocker() as mock:
        mock.post(
            '{}/auth/login'.format(local_url),
            text=json.dumps(local_auth_response_200),
            cookies=local_cookies)
        mock.get(
            '{}/parser'.format(local_url),
            text=json.dumps(parser_current_response_200),
            cookies=local_cookies)

        auth = rm.Authenticator.create_local('192.168.1.100', '12345')
        client = rm.Client(auth).parsers
        assert client.current() == parser_current_response_200
Esempio n. 7
0
def test_cookies(local_auth_response_200, local_cookies, local_url,
                 programs_all_response_200):
    """ Tests connection pooling with a session """
    with requests_mock.Mocker() as mock:
        mock.post(
            '{}/auth/login'.format(local_url),
            text=json.dumps(local_auth_response_200),
            cookies=local_cookies)
        mock.get(
            '{}/program'.format(local_url),
            text=json.dumps(programs_all_response_200),
            cookies=local_cookies)

        auth = rm.Authenticator.create_local('192.168.1.100', '12345')
        client = rm.Client(auth)
        assert client.programs.all() == programs_all_response_200
        assert client.programs.cookies == local_cookies
Esempio n. 8
0
def test_all_operations(client_general_response_200, local_cookies, local_url,
                        local_auth_response_200, programs_all_response_200,
                        programs_get_response_200,
                        programs_nextrun_response_200,
                        programs_running_response_200):
    """ Tests getting the program list """
    with requests_mock.Mocker() as mock:
        mock.post(
            '{}/auth/login'.format(local_url),
            text=json.dumps(local_auth_response_200),
            cookies=local_cookies)
        mock.get(
            '{}/program'.format(local_url),
            text=json.dumps(programs_all_response_200),
            cookies=local_cookies)
        mock.get(
            '{}/program/nextrun'.format(local_url),
            text=json.dumps(programs_nextrun_response_200),
            cookies=local_cookies)
        mock.get(
            '{}/program/1'.format(local_url),
            text=json.dumps(programs_get_response_200),
            cookies=local_cookies)
        mock.post(
            '{}/program/1/start'.format(local_url),
            text=json.dumps(client_general_response_200),
            cookies=local_cookies)
        mock.post(
            '{}/program/1/stop'.format(local_url),
            text=json.dumps(client_general_response_200),
            cookies=local_cookies)
        mock.get(
            '{}/watering/program'.format(local_url),
            text=json.dumps(programs_running_response_200),
            cookies=local_cookies)

        auth = rm.Authenticator.create_local('192.168.1.100', '12345')
        client = rm.Client(auth).programs
        assert client.all() == programs_all_response_200
        assert client.get(1) == programs_get_response_200
        assert client.next() == programs_nextrun_response_200
        assert client.running() == programs_running_response_200
        assert client.start(1) == client_general_response_200
        assert client.stop(1) == client_general_response_200
Esempio n. 9
0
def test_all_operations(local_cookies, local_url, local_auth_response_200,
                        stats_details_response_200, stats_ondate_response_200,
                        stats_upcoming_response_200):
    """ Tests getting the program list """
    with requests_mock.Mocker() as mock:
        mock.post('{}/auth/login'.format(local_url),
                  text=json.dumps(local_auth_response_200),
                  cookies=local_cookies)
        mock.get('{}/dailystats/2017-06-29'.format(local_url),
                 text=json.dumps(stats_ondate_response_200),
                 cookies=local_cookies)
        mock.get('{}/dailystats'.format(local_url),
                 text=json.dumps(stats_upcoming_response_200),
                 cookies=local_cookies)
        mock.get('{}/dailystats/details'.format(local_url),
                 text=json.dumps(stats_details_response_200),
                 cookies=local_cookies)

        auth = rm.Authenticator.create_local('192.168.1.100', '12345')
        client = rm.Client(auth).stats
        assert client.on_date('2017-06-29') == stats_ondate_response_200
        assert client.on_date('6/29/2017') == stats_ondate_response_200
        assert client.upcoming() == stats_upcoming_response_200
        assert client.upcoming(True) == stats_details_response_200
Esempio n. 10
0
def test_all_operations(local_cookies, local_url, local_auth_response_200,
                        provision_name_response_200,
                        provision_settings_response_200,
                        provision_wifi_response_200):
    """ Tests getting the program list """
    with requests_mock.Mocker() as mock:
        mock.post('{}/auth/login'.format(local_url),
                  text=json.dumps(local_auth_response_200),
                  cookies=local_cookies)
        mock.get('{}/provision'.format(local_url),
                 text=json.dumps(provision_settings_response_200),
                 cookies=local_cookies)
        mock.get('{}/provision/name'.format(local_url),
                 text=json.dumps(provision_name_response_200),
                 cookies=local_cookies)
        mock.get('{}/provision/wifi'.format(local_url),
                 text=json.dumps(provision_wifi_response_200),
                 cookies=local_cookies)

        auth = rm.Authenticator.create_local('192.168.1.100', '12345')
        client = rm.Client(auth).provision
        assert client.device_name() == provision_name_response_200
        assert client.settings() == provision_settings_response_200
        assert client.wifi() == provision_wifi_response_200
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
    """Set this component up under its platform."""
    import regenmaschine as rm

    ip_address = config.get(CONF_IP_ADDRESS)
    _LOGGER.debug('IP address: %s', ip_address)

    email_address = config.get(CONF_EMAIL)
    _LOGGER.debug('Email address: %s', email_address)

    password = config.get(CONF_PASSWORD)
    _LOGGER.debug('Password: %s', password)

    zone_run_time = config.get(CONF_ZONE_RUN_TIME)
    _LOGGER.debug('Zone run time: %s', zone_run_time)

    try:
        if ip_address:
            port = config.get(CONF_PORT)
            _LOGGER.debug('Port: %s', port)

            ssl = config.get(CONF_SSL)
            _LOGGER.debug('SSL: %s', ssl)

            _LOGGER.debug('Configuring local API')
            auth = rm.Authenticator.create_local(ip_address,
                                                 password,
                                                 port=port,
                                                 https=ssl)
        elif email_address:
            _LOGGER.debug('Configuring remote API')
            auth = rm.Authenticator.create_remote(email_address, password)

        _LOGGER.debug('Querying against: %s', auth.url)

        _LOGGER.debug('Instantiating RainMachine client')
        client = rm.Client(auth)

        rainmachine_device_name = client.provision.device_name().get('name')

        entities = []
        for program in client.programs.all().get('programs'):
            if not program.get('active'):
                continue

            _LOGGER.debug('Adding program: %s', program)
            entities.append(
                RainMachineProgram(client,
                                   program,
                                   device_name=rainmachine_device_name))

        for zone in client.zones.all().get('zones'):
            if not zone.get('active'):
                continue

            _LOGGER.debug('Adding zone: %s', zone)
            entities.append(
                RainMachineZone(
                    client,
                    zone,
                    zone_run_time,
                    device_name=rainmachine_device_name,
                ))

        async_add_devices(entities)
    except rm.exceptions.HTTPError as exc_info:
        _LOGGER.error('An HTTP error occurred while talking with RainMachine')
        _LOGGER.debug(exc_info)
        return False
    except UnboundLocalError as exc_info:
        _LOGGER.error('Could not authenticate against RainMachine')
        _LOGGER.debug(exc_info)
        return False