def test_functional(): """Functional test""" client = VigilanceMeteoFranceProxy() zone = DepartmentWeatherAlert("32", client) # Test the forecast update date and time. It should be near today. test_date = (timezone("UTC").localize(datetime.datetime.utcnow()) - zone.bulletin_date) < datetime.timedelta(days=1) # Test if the URL url_pour_en_savoir_plus is available test_url = urlopen(zone.additional_info_URL).getcode() == 200 # Test to check if there is a overall criticity color for the department test_color = zone.department_color in ALERT_COLOR_LIST # Test the synthesis message test_summary = zone.summary_message is not None # Test the client used is the one given in argument at creation test_client = client == zone.proxy assert (test_date, test_url, test_color, test_summary, test_client) == ( True, True, True, True, True, )
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool: """Set up an Meteo-France account from a config entry.""" hass.data.setdefault(DOMAIN, {}) # Weather alert weather_alert_client = VigilanceMeteoFranceProxy() try: await hass.async_add_executor_job(weather_alert_client.update_data) except VigilanceMeteoError as exp: _LOGGER.error( "Unexpected error when creating the vigilance_meteoFrance proxy: %s ", exp) return False hass.data[DOMAIN]["weather_alert_client"] = weather_alert_client # Weather city = entry.data[CONF_CITY] try: client = await hass.async_add_executor_job(meteofranceClient, city) except meteofranceError as exp: _LOGGER.error( "Unexpected error when creating the meteofrance proxy: %s", exp) return False hass.data[DOMAIN][city] = MeteoFranceUpdater(client) await hass.async_add_executor_job(hass.data[DOMAIN][city].update) for platform in PLATFORMS: hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, platform)) _LOGGER.debug("meteo_france sensor platform loaded for %s", city) return True
def test_basic(): """Basic test.""" client = VigilanceMeteoFranceProxy() # first update client.update_data() test_status_1 = client.status == UPDATE_STATUS_XML_UPDATED # second update should do nothing as less than 60 secondes after first update client.update_data() test_status_2 = client.status == UPDATE_STATUS_CHECKSUM_CACHED_60S # simulation 2 minutes wait client._latest_check_date = client._latest_check_date - datetime.timedelta( seconds=120) client.update_data() test_status_3 = client.status == UPDATE_STATUS_SAME_CHECKSUM test_xml_tree = client.xml_tree is not None assert (test_status_1, test_status_2, test_status_3, test_xml_tree) == ( True, True, True, True, )
def setup(hass, config): """Set up the Meteo-France component.""" hass.data[DATA_METEO_FRANCE] = {} # Check if at least weather alert have to be monitored for one location. need_weather_alert_watcher = False for location in config[DOMAIN]: if CONF_MONITORED_CONDITIONS in location \ and 'weather_alert' in location[CONF_MONITORED_CONDITIONS]: need_weather_alert_watcher = True # If weather alert monitoring is expected initiate a client to be used by # all weather_alert entities. if need_weather_alert_watcher: from vigilancemeteo import VigilanceMeteoFranceProxy, \ VigilanceMeteoError weather_alert_client = VigilanceMeteoFranceProxy() try: weather_alert_client.update_data() except VigilanceMeteoError as exp: _LOGGER.error(exp) else: weather_alert_client = None hass.data[DATA_METEO_FRANCE]['weather_alert_client'] = weather_alert_client for location in config[DOMAIN]: city = location[CONF_CITY] from meteofrance.client import meteofranceClient, meteofranceError try: client = meteofranceClient(city) except meteofranceError as exp: _LOGGER.error(exp) return client.need_rain_forecast = bool( CONF_MONITORED_CONDITIONS in location and 'next_rain' in location[CONF_MONITORED_CONDITIONS]) hass.data[DATA_METEO_FRANCE][city] = MeteoFranceUpdater(client) hass.data[DATA_METEO_FRANCE][city].update() if CONF_MONITORED_CONDITIONS in location: monitored_conditions = location[CONF_MONITORED_CONDITIONS] _LOGGER.debug("meteo_france sensor platfrom loaded for %s", city) load_platform(hass, 'sensor', DOMAIN, { CONF_CITY: city, CONF_MONITORED_CONDITIONS: monitored_conditions }, config) load_platform(hass, 'weather', DOMAIN, {CONF_CITY: city}, config) return True
def test_first_try_xml_unreachable(fix_local_data): """Test behaviour when first checksum is unvailable.""" client = VigilanceMeteoFranceProxy() # fake the cheksum file to simulate unreachable file client.URL_VIGILANCE_METEO_XML = "./tests/fake_xml.xml" # should raise an error with pytest.raises(VigilanceMeteoError): client.update_data()
def test_results_when_proxy_raise_error(msg_format): """Test behaviour when Error are raised by proxy.""" client = VigilanceMeteoFranceProxy() # fake the cheksum file to simulate unreachable file client.URL_VIGILANCE_METEO_CHECKSUM = "file:./tests/fake_file.txt" zone = DepartmentWeatherAlert("2A", client) if msg_format == "text": excpected_result = "Impossible de récupérer l'information." else: # msg_format == "html" to avoid partial in codecov excpected_result = "<p>Impossible de récupérer l'information.</p>" assert zone.summary_message(msg_format) == excpected_result
def test_checksum_unreachable_and_bulletin_expired(fix_local_data): """Test checksum behaviour in case of error.""" # First update is OK client = VigilanceMeteoFranceProxy() client.update_data() # fake the cheksum file to simulate unreachable file client.URL_VIGILANCE_METEO_CHECKSUM = "file:./tests/fake_file.txt" # fake the date of bulletin. Make it exipred client._bulletin_date = client._bulletin_date - datetime.timedelta(days=2) # simulate 2 minutes wait client._latest_check_date = client._latest_check_date - datetime.timedelta( seconds=120) # should raise an error with pytest.raises(VigilanceMeteoError): client.update_data()
def test_checksum_unreachable_and_bulletin_valid(): """Test behaviour when checksum URL unreachable""" # First update OK client = VigilanceMeteoFranceProxy() client.update_data() first_checksum = client.checksum # fake the cheksum file to simulate unreachable file client.URL_VIGILANCE_METEO_CHECKSUM = "file:./tests/fake_file.txt" # simulate 2 minutes wait client._latest_check_date = client._latest_check_date - datetime.timedelta( seconds=120) client.update_data() # Should be no error and the value of the first update checksum assert (client.checksum, client.status) == ( first_checksum, UPDATE_STATUS_ERROR_BUT_PREVIOUS_BULLETIN_VALID, )
def __init__(self, department, vmf_proxy=None): """Class instance constructor. 2 arguments expected: - The department (Required) number as a 2 character String. Can be between 01 and 95, 2A, 2B or 99 (for Andorre). - a VigilanceMeteoFranceProxy object (Optional) to manage de communication with the Météo France online source. """ # Variables init self._alerts_list = {} self._department = None # If no VigilanceMeteoFranceProxy set in the parameter create a new one. if vmf_proxy is not None: self._viglance_MF_proxy = vmf_proxy else: self._viglance_MF_proxy = VigilanceMeteoFranceProxy() # Check _department variable using the property. # Warning the setter launch update_department_status() methods. self.department = department
def setup(hass, config): """Set up the Meteo-France component.""" hass.data[DATA_METEO_FRANCE] = {} # Check if at least weather alert have to be monitored for one location. need_weather_alert_watcher = False for location in config[DOMAIN]: if ( CONF_MONITORED_CONDITIONS in location and "weather_alert" in location[CONF_MONITORED_CONDITIONS] ): need_weather_alert_watcher = True # If weather alert monitoring is expected initiate a client to be used by # all weather_alert entities. if need_weather_alert_watcher: _LOGGER.debug("Weather Alert monitoring expected. Loading vigilancemeteo") weather_alert_client = VigilanceMeteoFranceProxy() try: weather_alert_client.update_data() except VigilanceMeteoError as exp: _LOGGER.error( "Unexpected error when creating the vigilance_meteoFrance proxy: %s ", exp, ) else: weather_alert_client = None hass.data[DATA_METEO_FRANCE]["weather_alert_client"] = weather_alert_client for location in config[DOMAIN]: city = location[CONF_CITY] try: client = meteofranceClient(city) except meteofranceError as exp: _LOGGER.error( "Unexpected error when creating the meteofrance proxy: %s", exp ) return client.need_rain_forecast = bool( CONF_MONITORED_CONDITIONS in location and "next_rain" in location[CONF_MONITORED_CONDITIONS] ) hass.data[DATA_METEO_FRANCE][city] = MeteoFranceUpdater(client) hass.data[DATA_METEO_FRANCE][city].update() if CONF_MONITORED_CONDITIONS in location: monitored_conditions = location[CONF_MONITORED_CONDITIONS] _LOGGER.debug("meteo_france sensor platform loaded for %s", city) load_platform( hass, "sensor", DOMAIN, {CONF_CITY: city, CONF_MONITORED_CONDITIONS: monitored_conditions}, config, ) load_platform(hass, "weather", DOMAIN, {CONF_CITY: city}, config) return True
def test_checksum(fix_local_data): """Test checksum property.""" client = VigilanceMeteoFranceProxy() client.update_data() assert client.checksum == "1751354976"
def test_bulletin_date(fix_local_data): """Test bulletin date property.""" client = VigilanceMeteoFranceProxy() client.update_data() assert client.bulletin_date.isoformat() == "2018-03-18T16:00:00+01:00"