# -*- coding: utf-8 -*- """Raincloudy helpers.""" from bs4 import BeautifulSoup from raincloudy.exceptions import RainCloudyException def generate_soup_html(data): """Return an BeautifulSoup HTML parser document.""" try: return BeautifulSoup(data, 'html.parser') except: raise def serial_finder(data): """ Find controller serial and faucet_serial from the setup page. :param data: text to be parsed :type data: BeautilSoup object :return: a dict with controller_serial and faucet_serial :rtype: dict :raises IndexError: if controller_serial was not found on the data """ if not isinstance(data, BeautifulSoup): raise TypeError("Function requires BeautifulSoup HTML element.") try: # The setup page contains a select box for each controller and each # faucet controllersElement = data.find_all('select', {'id': 'id_select_controller2'}) faucetsElement = data.find_all('select', {'id': 'id_select_faucet2'}) controllerSerial = controllersElement[0].text.split('-')[1].strip() faucetSerial = faucetsElement[0].text.split('-')[1].strip() # currently only one faucet is supported on the code # we have plans to support it in the future parsed_dict = {} parsed_dict['controller_serial'] = controllerSerial parsed_dict['faucet_serial'] = [faucetSerial] return parsed_dict except (AttributeError, IndexError, ValueError): raise RainCloudyException( 'Could not find any valid controller or faucet') def find_program_status(data, zone): """ Find on the HTML document if zoneX has the configuration of the auto-schedule/program (auto_watering) enabled. # expected result if enabled # # expected result if disabled # :param data: BeautifulSoup object :param zone: zone name from class='switch' :return: boolean if zone has program enabled :rtype: boolean :raises TypeError: if data is not a BeautifulSoup object :raises IndexError: if object not found """ if not isinstance(data, BeautifulSoup): raise TypeError("Function requires BeautilSoup HTML element.") try: child = data.find_all('input', {'class': 'switch'}) zone_id = 'id_{0}_program_toggle'.format(zone) for member in child: if member.get('type') == 'checkbox' and \ member.get('id') == zone_id: return bool(member.has_attr('checked')) raise IndexError except (AttributeError, IndexError, ValueError): raise RainCloudyException( 'Could not find any valid controller or faucet') def find_controller_or_faucet_name(data, p_type): """ Find on the HTML document the controller name. # expected result