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_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 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: _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"