From f7da5b45c05897b4b32f762210d770fa7bd12ce1 Mon Sep 17 00:00:00 2001 From: David HERNANDEZ Date: Sun, 2 Oct 2022 22:56:04 +0200 Subject: [PATCH 1/3] Feat - Testing the MagicMock method to unittest the get requests for forecast methods --- data/test_response_scrapper_method.pbz2 | Bin 0 -> 11886 bytes src/emhass/forecast.py | 4 +++ tests/test_forecast.py | 35 ++++++++++++++++-------- 3 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 data/test_response_scrapper_method.pbz2 diff --git a/data/test_response_scrapper_method.pbz2 b/data/test_response_scrapper_method.pbz2 new file mode 100644 index 0000000000000000000000000000000000000000..50ac9deca444be05c982aadb352d343bf72ee941 GIT binary patch literal 11886 zcmV-!E|JkfT4*^jL0KkKS@({xF#&SOfBygf{eOG^|NsC0|NsC0|Ns5u&Hw-)00a<0 zFaS{iU_C!R_!O1y-9|a|_Ya>so3_@@0H#O2K8^R@Ek192di%S|Z2Bj9_26~gsXFe< zx7>BgZSGy}d2NQ;y59-ca0j4BA3cTa)or^j-~bfwj*0~h*y*n0R%=?#wODWyDK(9= zvsJx&&sA`&HBRnyB~@}wi&@xeiI&L3fd>q2SWRrY);DJCJ9yzFh$c-M08LG%jWsaD znK3lUgvq9uK{800004DH39o(w=H%JtI#^ z=Bc35LrnwJXa<@z0000001W^%0000)kcdnnkOT^z^;G`}HBZ?UJ+)KyPbZ}h)csS` z*woVsdMNchL80n-Lwch_5u<4u0qOt&5=050AO%m;LrJEArlu+%lPYbcJg1(jXfseW z{YmN>l>Jjc0j7WhK+pg;{8SgYM$kMLD2KGY6xB%Vx7$DM%l;?-KMEV<7%W(R(e!E; zSdKe>+8VHnLVvIKsVoAa1Yn>HO|Zi90v$^cfe^F>z%a0+4~hKoPG07Zf<`r;BY-|M zhl>R8E``;tVZDL0&Ecs{#sMq5z|@F{;R$IR4N0h=wTAVPMBDN1Xx&Q?gi5~lafMbw zKxLI#VP-+400;q4uo1uQ59P#P+QR=AYFwSVkw!XL>h0!jbU z0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-dK%Nw*CIphWCGni@TIhfPB?Ycl%Mb!0W`)K2zN4lJ zC>bCGe8Vmn*XZo8Nir>iB^W1fQ$kvc%K-`5BfMa!IIjXC0Io%lnJSAGAp!^?BctO; z*w@H=?_qZUK8f^1`?3~-hJuO;lBFaJP)0(TZ#6_jC0qm}wF!hM{Y4aDlTNYLEUcv- zfjQ=IaU5oBA)LUr&xEExS4yfwCAel9g8cDv^^i9Fz`o%v`dlD}Z2^H*t_|6a znqrtD<)hy2Gd#Hh>NIDf`5-}mWbmEE&oadtbdo|YuxgdcG>SBw-t_}G^XB{KC!FPR ztZtZ+x!LQL%YxNmFQuF3;rO2s{31P!FQkCv`z42Oj7gdDz0f~)f!#=NaPI)X@b}mv zNMrD1K!B5P`2OMr#g5f(=eu0ZKC}6reV1g%p)*R2h z&h`JBJLjEVFvwarHRJ1Z>i375KIZoCz$5hT$igxj_^VvLnk^3({=dOsJVCxBpZi{4 zSJPJLJu=!EX(ayp1`>O3y2dtC>izCO;n4Gk-azq^`L022`q6^;T+Z?GV!qxlCX^EN zm^a73R`2|cJZTc)Wy2lS@4;zRuexIrPMz(xUk3^`qL{`U4j4MZ{m9rcB$z5$7&5I> omul4phkW9WLjNzx&;2;RuSLelh#341Q~L|KBAh5ld&gK9fJUPSBme*a literal 0 HcmV?d00001 diff --git a/src/emhass/forecast.py b/src/emhass/forecast.py index d45193ef..100b0b54 100644 --- a/src/emhass/forecast.py +++ b/src/emhass/forecast.py @@ -170,6 +170,10 @@ def get_weather_forecast(self, method: Optional[str] = 'scrapper', freq=freq_scrap).round(freq_scrap) # Using the clearoutside webpage response = get("https://clearoutside.com/forecast/"+str(round(self.lat, 2))+"/"+str(round(self.lon, 2))+"?desktop=true") + '''import bz2 # Uncomment to save a serialized response for tests + import _pickle as cPickle + with bz2.BZ2File("test_response_scrapper_method.pbz2", "w") as f: + cPickle.dump(response, f)''' soup = BeautifulSoup(response.content, 'html.parser') table = soup.find_all(id='day_0')[0] list_names = table.find_all(class_='fc_detail_label') diff --git a/tests/test_forecast.py b/tests/test_forecast.py index b230615d..eb753795 100644 --- a/tests/test_forecast.py +++ b/tests/test_forecast.py @@ -2,8 +2,11 @@ # -*- coding: utf-8 -*- import unittest +from unittest.mock import MagicMock, patch import pandas as pd import pathlib, pickle, json, copy, yaml +import bz2 +import _pickle as cPickle from emhass.retrieve_hass import retrieve_hass from emhass.forecast import forecast @@ -66,15 +69,6 @@ def setUp(self): } def test_get_weather_forecast(self): - # self.assertTrue(self.df_input_data.isnull().sum().sum()==0) - # self.assertIsInstance(self.df_weather_scrap, type(pd.DataFrame())) - # self.assertTrue(col in self.df_weather_scrap.columns for col in ['ghi', 'dni', 'dhi', 'temp_air']) - # self.assertIsInstance(self.df_weather_scrap.index, pd.core.indexes.datetimes.DatetimeIndex) - # self.assertIsInstance(self.df_weather_scrap.index.dtype, pd.core.dtypes.dtypes.DatetimeTZDtype) - # self.assertEqual(self.df_weather_scrap.index.tz, self.fcst.time_zone) - # self.assertTrue(self.fcst.start_forecast < ts for ts in self.df_weather_scrap.index) - # self.assertEqual(len(self.df_weather_scrap), - # int(self.optim_conf['delta_forecast'].total_seconds()/3600/self.fcst.timeStep)) self.df_weather_csv = self.fcst.get_weather_forecast(method='csv') self.assertEqual(self.fcst.weather_forecast_method, 'csv') self.assertIsInstance(self.df_weather_csv, type(pd.DataFrame())) @@ -91,6 +85,25 @@ def test_get_weather_forecast(self): self.assertEqual(P_PV_forecast.index.tz, self.fcst.time_zone) self.assertEqual(len(self.df_weather_csv), len(P_PV_forecast)) + @patch('emhass.forecast.requests') + def test_get_weather_forecast_scrapper_method(self, mock_requests): + data = bz2.BZ2File(str(pathlib.Path(root+'/data/test_response_scrapper_method.pbz2')), "rb") + response = cPickle.load(data) + mock_response = MagicMock() + mock_response.content = copy.deepcopy(response.content) + mock_response.status_code = 200 + # specify the return value of the get() method + mock_requests.get.return_value = mock_response + df_weather_scrap = self.fcst.get_weather_forecast(method='scrapper') + self.assertEqual(self.fcst.weather_forecast_method, 'scrapper') + self.assertIsInstance(df_weather_scrap, type(pd.DataFrame())) + self.assertIsInstance(df_weather_scrap.index, pd.core.indexes.datetimes.DatetimeIndex) + self.assertIsInstance(df_weather_scrap.index.dtype, pd.core.dtypes.dtypes.DatetimeTZDtype) + self.assertEqual(df_weather_scrap.index.tz, self.fcst.time_zone) + self.assertTrue(self.fcst.start_forecast < ts for ts in df_weather_scrap.index) + self.assertEqual(len(df_weather_scrap), + int(self.optim_conf['delta_forecast'].total_seconds()/3600/self.fcst.timeStep)) + def test_get_forecasts_with_lists(self): with open(root+'/config_emhass.yaml', 'r') as file: params = yaml.load(file, Loader=yaml.FullLoader) @@ -181,7 +194,7 @@ def test_get_power_from_weather(self): self.plant_conf['strings_per_inverter'] = [1, 1] self.fcst = forecast(self.retrieve_hass_conf, self.optim_conf, self.plant_conf, None, root, logger, get_data_from_file=self.get_data_from_file) - df_weather_scrap = self.fcst.get_weather_forecast(method='scrapper') + df_weather_scrap = self.fcst.get_weather_forecast(method='csv') P_PV_forecast = self.fcst.get_power_from_weather(df_weather_scrap) self.assertIsInstance(P_PV_forecast, pd.core.series.Series) self.assertIsInstance(P_PV_forecast.index, pd.core.indexes.datetimes.DatetimeIndex) @@ -193,7 +206,7 @@ def test_get_power_from_weather(self): df_input_data = self.input_data_dict['rh'].df_final.copy() self.fcst = forecast(self.retrieve_hass_conf, self.optim_conf, self.plant_conf, params, root, logger, get_data_from_file=self.get_data_from_file) - df_weather_scrap = self.fcst.get_weather_forecast(method='scrapper') + df_weather_scrap = self.fcst.get_weather_forecast(method='csv') P_PV_forecast = self.fcst.get_power_from_weather(df_weather_scrap, set_mix_forecast=True, df_now=df_input_data) self.assertIsInstance(P_PV_forecast, pd.core.series.Series) self.assertIsInstance(P_PV_forecast.index, pd.core.indexes.datetimes.DatetimeIndex) From bf7a07f73d7cdcb16fd09ff4ceab0519142b7ab4 Mon Sep 17 00:00:00 2001 From: David HERNANDEZ Date: Tue, 4 Oct 2022 00:31:26 +0200 Subject: [PATCH 2/3] Feat - Added MagicMock to request methods and added the new Solar.Forecast API --- CHANGELOG.md | 2 + README.md | 2 + data/test_response_scrapper_method.pbz2 | Bin 11886 -> 2574 bytes data/test_response_solarforecast_method.pbz2 | Bin 0 -> 1193 bytes data/test_response_solcast_method.pbz2 | Bin 0 -> 1238 bytes docs/forecasts.md | 7 +++ secrets_emhass(example).yaml | 3 +- src/emhass/forecast.py | 41 +++++++++++++-- src/emhass/utils.py | 5 ++ tests/test_forecast.py | 50 ++++++++++++++----- 10 files changed, 92 insertions(+), 18 deletions(-) create mode 100644 data/test_response_solarforecast_method.pbz2 create mode 100644 data/test_response_solcast_method.pbz2 diff --git a/CHANGELOG.md b/CHANGELOG.md index bf87621b..8c233449 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ - Added more detailed examples to the forecast module documentation. - Improved handling of datatime indexes in DataFrames on forecast module. - Added warning messages if passed list values contains non numeric items. +- Added missing unittests for forecast module with request.get dependencies using MagicMock. +- Added the Solar.Forecast method. ## [0.3.19] - 2022-09-14 ### Fix diff --git a/README.md b/README.md index b5b879d3..d99afa27 100644 --- a/README.md +++ b/README.md @@ -308,6 +308,8 @@ Here is the list of the other additional dictionnary keys that can be passed at - `solcast_rooftop_id` for the ID of your rooftop for the SolCast service implementation. +- `solar_forecast_kwp` for the PV peak installed power in kW used for the solar.forecast API call. + ### A naive Model Predictive Controller A MPC controller was introduced in v0.3.0. This is an informal/naive representation of a MPC controller. diff --git a/data/test_response_scrapper_method.pbz2 b/data/test_response_scrapper_method.pbz2 index 50ac9deca444be05c982aadb352d343bf72ee941..1b3b580f53ec4fc5cdafd16fc00bd890f2dbb7c2 100644 GIT binary patch literal 2574 zcmV+p3i0(qT4*^jL0KkKSycyw4*&%$|NsC0|NsAg|Mvg?|NsC0|M&0z|NsC0|NsC0 z|NH;{|Nr0yzX2@wJ1V_`t1m&l*S4*^MFiApc!}zIPfbmvZ9(ZYXwxz@fIUqE)EYDm z0q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`?3~-hJuO;lBFaJP)0(TZ#6_jC0qm}wF!hM{Y4aDlTNYLEUcv- zfjQ=IaU5oBA)LUr&xEExS4yfwCAel9g8cDv^^i9Fz`o%v`dlD}Z2^H*t_|6a znqrtD<)hy2Gd#Hh>NIDf`5-}mWbmEE&oadtbdo|YuxgdcG>SBw-t_}G^XB{KC!FPR ztZtZ+x!LQL%YxNmFQuF3;rO2s{31P!FQkCv`z42Oj7gdDz0f~)f!#=NaPI)X@b}mv zNMrD1K!B5P`2OMr#g5f(=eu0ZKC}6reV1g%p)*R2h z&h`JBJLjEVFvwarHRJ1Z>i375KIZoCz$5hT$igxj_^VvLnk^3({=dOsJVCxBpZi{4 zSJPJLJu=!EX(ayp1`>O3y2dtC>izCO;n4Gk-azq^`L022`q6^;T+Z?GV!qxlCX^EN zm^a73R`2|cJZTc)Wy2lS@4;zRuexIrPMz(xUk3^`qL{`U4j4MZ{m9rcB$z5$7&5I> omul4phkW9WLjNzx&;2;RuSLelh#341Q~L|KBAh5ld&gK9fJUPSBme*a diff --git a/data/test_response_solarforecast_method.pbz2 b/data/test_response_solarforecast_method.pbz2 new file mode 100644 index 0000000000000000000000000000000000000000..ef8cc4f885ff66bda0134cef0d6578ed9b0f2867 GIT binary patch literal 1193 zcmV;a1XlY(T4*^jL0KkKSrSN`$rl4p502&$%8Z-ut007Vc0iZO<000Jn27mwn z00000&<#&eN_v?#jHjlJ1qL7*0iXyM44E1RfC5PpOq!ligvsh?&>Kh@13=IK zGzNeG00003KmY&$&@=|r&;Sg80HOdPz32g%ASXs<5Dd&C{ENdq=pN>BU}Te-V3}yg zrL1BSO?F{9>^UW9ctFG8X?l1nAr4Hz6SCn`RyvWhECg05vSWsh6)ql9e!LHWXdy;I z5P%~PS3oVwiq^{EUi-_stN+tf*ZifT?Or9ij8>t9ZozA28(eVhSFSL&4h!KelY-ed z=13NbBjbSJsT!X74A%e%Q6UbqQ#S7QmPhg!q5v791qgr$5719Gd7S|Z=W4w&t4#A? zAw+!Ru}fNQF)e#PQ=3ioWuhJ>S-?P8Tdzel7&+*bkqug{6~st(OzvQ2T-|7ji&R}h z`bqcQ9|Pib;lxTpNIIcfmzpdo0ZBqahS6-Vu%d_%%jukXW>!m67U<_3Kv2Lk)5^xN z{4}$gnK}19<=cdv#tN(AZ@(}`U>dZf69Ofz7P8dy__VzI3Hu~AY$CjC!2%OjwBex-!ULkjposvH45=s- zd5NJShFkWhTvYQOk=sMB6lq{-Vh58X?^)nOD#9TVNwW}_UV>n;M`WG*xRqKi)h$&$ zRZbpiNKg}&`y_`#bj6>YLQ0S*$wPw842@Rw&2E9Nu9E<>sUZ>46+h=z)s_!!Qf$w; z5n)t8Ml+q>B}4P6uT)( z2)jv(F$0IDP9$ZdI>HldPgh?rWh^r`+20W9b}~_@=GpMJ4N6fcawgI>jzwe;A!wcO z;7J#Nz#oMrw4}gBG=Q>zvMI=P=2<|JPM%x57az20#!S9;j^GO@yKHox0JT9s|V z&Sr~5wUCI4V*rXPGfTEmGjL3BsFPLdN4|lJX>@Ovn5u_`%&kNKkR?2V2Ru@QoiQ|)- zmVerk?rz-Aap)A;PVa=+Yo3k01y)YYB}BH|M(!ielv@wa#doWV%PMjPM??6Pn|X9R zb?1peh?X@sHe|gshg_2by8S=smC`y-g0u#Grj4e-@Y%zfk`{jm&>#fh6P#Vi6yZWZ HNn~d9yb}!j literal 0 HcmV?d00001 diff --git a/data/test_response_solcast_method.pbz2 b/data/test_response_solcast_method.pbz2 new file mode 100644 index 0000000000000000000000000000000000000000..cd7da0313588bef465105cff9fcc5bcfc0ec5bfb GIT binary patch literal 1238 zcmV;{1S$JMT4*^jL0KkKSvnPnUH}3#fB*mg`}bCR{bc_?*1rGm|Mu(tU-SRX{r&B4 ze}4bB^}WyoSe~;7W`^K|lT_18O`}nb2-EchX{HEm1jt|}0x~cFCYm%D2-69)14#5H z0yM58Z-a^0qOt%000000QCR>4FCXW z0j7}(ZAMc~l*C~MMvO*HG%{#300x6V0MG%T8UdlEfB*mhX`l@TKr}J{0Rf)~sa-8#2=POd< z%CB3ER<93ZgLO&Dwa?#Ja>LG9$o*cK^&mj=%1}>9VK#=HrlbGI3^Eq1oL$Ou#g)&=SBVx zDTl1?Kbc;Lp&e@M*;g%7Q;P7z=F_^oCR=hm9h~R zpwJGLpzuf$rNBfMk(A&Zu)+~U4FF8OtC1&3%zaNX0KNs-)ki3riiBBf4A5|n5(*>D zm3Tp}ViPjcJggH6p&=+_ot_ojbbXB)%fW{VEx^bjh8K%z1*8Xo2P(UEpJ05myp4UZaN=c{D&|uOkFo`z!At>j<>RlYHb(uw@ zt_mL_mEDUbc$qX;Vj@kw&J9Hdo>NkQ71WurFd%zN&3xWe5!@8bs+3 z%`rxl8;x`6O)vR%5k3B}gbP1$7DXi-F+Mdj7HFB7)ly%(wWK5Lalj(RpFrBTp*vPe zhCe9F6ta27y4Z0Gt}=!=P!_O&;BqO9Tnoyc|IWuyd8z)Fk0P^ ze5&xlkrT{D>c?%v;dGaz2dKO%NQ^zcaH-M)9%&2YrX}XJI-A37Bx&^Qsl5&;(6tZn zz5*}hl%rjvJ4MnE*dPcRA;;4Y+f0gJfoh5(+dlbpBOISqhC3`r$mP+IJYN2Y%6(#z zP_M^^pI*s_oDay@NZdo|^aii`i31EM`nmM*?!Esab^ss|;6L$qBvXY61EE-j;G*#x Ai2wiq literal 0 HcmV?d00001 diff --git a/docs/forecasts.md b/docs/forecasts.md index 069d1b8e..f0681328 100644 --- a/docs/forecasts.md +++ b/docs/forecasts.md @@ -27,6 +27,13 @@ For example: curl -i -H 'Content-Type:application/json' -X POST -d '{"solcast_rooftop_id":"","solcast_api_key":""}' http://localhost:5000/action/dayahead-optim ``` +A thrd method uses the Solar.Forecast service. You will need to set `method=solar.forecast` and use just one parameter `solar_forecast_kwp` (the PV peak installed power in kW) that should be passed at runtime. + +For example, for a 5 kWp installation: +``` +curl -i -H 'Content-Type:application/json' -X POST -d '{"solar_forecast_kwp":5}' http://localhost:5000/action/dayahead-optim +``` + ## Load power forecast The default method for load forecast is a naive method, also called persistence. This is obtained using `method=naive`. This method simply assumes that the forecast for a future period will be equal to the observed values in a past period. The past period is controlled using parameter `delta_forecast` and the default value for this is 24h. diff --git a/secrets_emhass(example).yaml b/secrets_emhass(example).yaml index 60e1bc2c..312ae7b3 100644 --- a/secrets_emhass(example).yaml +++ b/secrets_emhass(example).yaml @@ -8,4 +8,5 @@ lat: 45.83 lon: 6.86 alt: 4807.8 solcast_api_key: yoursecretsolcastapikey -solcast_rooftop_id: yourrooftopid \ No newline at end of file +solcast_rooftop_id: yourrooftopid +solar_forecast_kwp: 5 \ No newline at end of file diff --git a/src/emhass/forecast.py b/src/emhass/forecast.py index 100b0b54..0ca01af3 100644 --- a/src/emhass/forecast.py +++ b/src/emhass/forecast.py @@ -170,10 +170,6 @@ def get_weather_forecast(self, method: Optional[str] = 'scrapper', freq=freq_scrap).round(freq_scrap) # Using the clearoutside webpage response = get("https://clearoutside.com/forecast/"+str(round(self.lat, 2))+"/"+str(round(self.lon, 2))+"?desktop=true") - '''import bz2 # Uncomment to save a serialized response for tests - import _pickle as cPickle - with bz2.BZ2File("test_response_scrapper_method.pbz2", "w") as f: - cPickle.dump(response, f)''' soup = BeautifulSoup(response.content, 'html.parser') table = soup.find_all(id='day_0')[0] list_names = table.find_all(class_='fc_detail_label') @@ -201,6 +197,10 @@ def get_weather_forecast(self, method: Optional[str] = 'scrapper', data['relative_humidity'] = raw_data['Relative Humidity (%)'] data['precipitable_water'] = pvlib.atmosphere.gueymard94_pw( data['temp_air'], data['relative_humidity']) + '''import bz2 # Uncomment to save a serialized data for tests + import _pickle as cPickle + with bz2.BZ2File("test_response_scrapper_method.pbz2", "w") as f: + cPickle.dump(data, f)''' elif method == 'solcast': # using solcast API # Retrieve data from the solcast API headers = { @@ -224,6 +224,36 @@ def get_weather_forecast(self, method: Optional[str] = 'scrapper', data = pd.DataFrame.from_dict(data_dict) # Define index data.set_index('ts', inplace=True) + '''import bz2 # Uncomment to save a serialized data for tests + import _pickle as cPickle + with bz2.BZ2File("test_response_solcast_method.pbz2", "w") as f: + cPickle.dump(data, f)''' + elif method == 'solar.forecast': # using the solar.forecast API + # Retrieve data from the solar.forecast API + headers = { + "Accept": "application/json" + } + url = "https://api.forecast.solar/estimate/"+str(round(self.lat, 2))+"/"+str(round(self.lon, 2))+\ + "/"+str(self.plant_conf["surface_tilt"])+"/"+str(self.plant_conf["surface_azimuth"]-180)+\ + "/"+str(self.retrieve_hass_conf["solar_forecast_kwp"]) + response = get(url, headers=headers) + data_raw = response.json() + data_dict = {'ts':list(data_raw['result']['watts'].keys()), 'yhat':list(data_raw['result']['watts'].values())} + # Form the final DataFrame + data = pd.DataFrame.from_dict(data_dict) + data.set_index('ts', inplace=True) + data.index = pd.to_datetime(data.index) + data = data.tz_localize(self.forecast_dates.tz) + data = data.reindex(index=self.forecast_dates) + mask_up_data_df = data.copy(deep=True).fillna(method = "ffill").isnull() + mask_down_data_df = data.copy(deep=True).fillna(method = "bfill").isnull() + data.interpolate(inplace=True) + data.loc[data.index[mask_up_data_df['yhat']==True],:] = 0.0 + data.loc[data.index[mask_down_data_df['yhat']==True],:] = 0.0 + '''import bz2 # Uncomment to save a serialized data for tests + import _pickle as cPickle + with bz2.BZ2File("test_response_solarforecast_method.pbz2", "w") as f: + cPickle.dump(data, f)''' elif method == 'csv': # reading from a csv file weather_csv_file_path = self.root + csv_path # Loading the csv file, we will consider that this is the PV power in W @@ -317,7 +347,8 @@ def get_power_from_weather(self, df_weather: pd.DataFrame, """ # If using csv method we consider that yhat is the PV power in W - if self.weather_forecast_method == 'solcast' or self.weather_forecast_method == 'csv' or self.weather_forecast_method == 'list': + if self.weather_forecast_method == 'solcast' or self.weather_forecast_method == 'solar.forecast' or \ + self.weather_forecast_method == 'csv' or self.weather_forecast_method == 'list': P_PV_forecast = df_weather['yhat'] P_PV_forecast.name = None else: # We will transform the weather data into electrical power diff --git a/src/emhass/utils.py b/src/emhass/utils.py index 9864c060..1019f478 100644 --- a/src/emhass/utils.py +++ b/src/emhass/utils.py @@ -197,8 +197,13 @@ def treat_runtimeparams(runtimeparams: str, params:str, retrieve_hass_conf: dict optim_conf['set_def_constant'] = runtimeparams['set_def_constant'] if 'solcast_api_key' in runtimeparams.keys(): retrieve_hass_conf['solcast_api_key'] = runtimeparams['solcast_api_key'] + optim_conf['weather_forecast_method'] = 'solcast' if 'solcast_rooftop_id' in runtimeparams.keys(): retrieve_hass_conf['solcast_rooftop_id'] = runtimeparams['solcast_rooftop_id'] + optim_conf['weather_forecast_method'] = 'solcast' + if 'solar_forecast_kwp' in runtimeparams.keys(): + retrieve_hass_conf['solar_forecast_kwp'] = runtimeparams['solar_forecast_kwp'] + optim_conf['weather_forecast_method'] = 'solar.forecast' params = json.dumps(params) return params, retrieve_hass_conf, optim_conf diff --git a/tests/test_forecast.py b/tests/test_forecast.py index eb753795..6d3ff9c8 100644 --- a/tests/test_forecast.py +++ b/tests/test_forecast.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- import unittest -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock import pandas as pd import pathlib, pickle, json, copy, yaml import bz2 @@ -24,7 +24,7 @@ class TestForecast(unittest.TestCase): def setUp(self): self.get_data_from_file = True params = None - retrieve_hass_conf, optim_conf, plant_conf = get_yaml_parse(pathlib.Path(root+'/config_emhass.yaml'), use_secrets=False) + retrieve_hass_conf, optim_conf, plant_conf = get_yaml_parse(pathlib.Path(root+'/config_emhass.yaml'), use_secrets=True) self.retrieve_hass_conf, self.optim_conf, self.plant_conf = \ retrieve_hass_conf, optim_conf, plant_conf self.rh = retrieve_hass(self.retrieve_hass_conf['hass_url'], self.retrieve_hass_conf['long_lived_token'], @@ -47,7 +47,7 @@ def setUp(self): self.fcst = forecast(self.retrieve_hass_conf, self.optim_conf, self.plant_conf, params, root, logger, get_data_from_file=self.get_data_from_file) # The default for test is csv read - self.df_weather_scrap = self.fcst.get_weather_forecast(method='csv') # Still need to unittest these methods: 'scrapper','solcast','forecast.solar' + self.df_weather_scrap = self.fcst.get_weather_forecast(method='csv') self.P_PV_forecast = self.fcst.get_power_from_weather(self.df_weather_scrap) self.P_load_forecast = self.fcst.get_load_forecast(method=optim_conf['load_forecast_method']) self.df_input_data_dayahead = pd.concat([self.P_PV_forecast, self.P_load_forecast], axis=1) @@ -85,17 +85,13 @@ def test_get_weather_forecast(self): self.assertEqual(P_PV_forecast.index.tz, self.fcst.time_zone) self.assertEqual(len(self.df_weather_csv), len(P_PV_forecast)) - @patch('emhass.forecast.requests') - def test_get_weather_forecast_scrapper_method(self, mock_requests): + def test_get_weather_forecast_scrapper_method(self): data = bz2.BZ2File(str(pathlib.Path(root+'/data/test_response_scrapper_method.pbz2')), "rb") - response = cPickle.load(data) - mock_response = MagicMock() - mock_response.content = copy.deepcopy(response.content) - mock_response.status_code = 200 - # specify the return value of the get() method - mock_requests.get.return_value = mock_response + data = cPickle.load(data) + self.fcst.get_weather_forecast = MagicMock(return_value=data) df_weather_scrap = self.fcst.get_weather_forecast(method='scrapper') - self.assertEqual(self.fcst.weather_forecast_method, 'scrapper') + self.fcst.get_weather_forecast.assert_called_with(method='scrapper') + self.fcst.get_weather_forecast.assert_called_once() self.assertIsInstance(df_weather_scrap, type(pd.DataFrame())) self.assertIsInstance(df_weather_scrap.index, pd.core.indexes.datetimes.DatetimeIndex) self.assertIsInstance(df_weather_scrap.index.dtype, pd.core.dtypes.dtypes.DatetimeTZDtype) @@ -104,6 +100,36 @@ def test_get_weather_forecast_scrapper_method(self, mock_requests): self.assertEqual(len(df_weather_scrap), int(self.optim_conf['delta_forecast'].total_seconds()/3600/self.fcst.timeStep)) + def test_get_weather_forecast_solcast_method(self): + data = bz2.BZ2File(str(pathlib.Path(root+'/data/test_response_solcast_method.pbz2')), "rb") + data = cPickle.load(data) + self.fcst.get_weather_forecast = MagicMock(return_value=data) + df_weather_solcast = self.fcst.get_weather_forecast(method='solcast') + self.fcst.get_weather_forecast.assert_called_with(method='solcast') + self.fcst.get_weather_forecast.assert_called_once() + self.assertIsInstance(df_weather_solcast, type(pd.DataFrame())) + self.assertIsInstance(df_weather_solcast.index, pd.core.indexes.datetimes.DatetimeIndex) + self.assertIsInstance(df_weather_solcast.index.dtype, pd.core.dtypes.dtypes.DatetimeTZDtype) + self.assertEqual(df_weather_solcast.index.tz, self.fcst.time_zone) + self.assertTrue(self.fcst.start_forecast < ts for ts in df_weather_solcast.index) + self.assertEqual(len(df_weather_solcast), + int(self.optim_conf['delta_forecast'].total_seconds()/3600/self.fcst.timeStep)) + + def test_get_weather_forecast_solcast_method(self): + data = bz2.BZ2File(str(pathlib.Path(root+'/data/test_response_solarforecast_method.pbz2')), "rb") + data = cPickle.load(data) + self.fcst.get_weather_forecast = MagicMock(return_value=data) + df_weather_solarforecast = self.fcst.get_weather_forecast(method='solar.forecast') + self.fcst.get_weather_forecast.assert_called_with(method='solar.forecast') + self.fcst.get_weather_forecast.assert_called_once() + self.assertIsInstance(df_weather_solarforecast, type(pd.DataFrame())) + self.assertIsInstance(df_weather_solarforecast.index, pd.core.indexes.datetimes.DatetimeIndex) + self.assertIsInstance(df_weather_solarforecast.index.dtype, pd.core.dtypes.dtypes.DatetimeTZDtype) + self.assertEqual(df_weather_solarforecast.index.tz, self.fcst.time_zone) + self.assertTrue(self.fcst.start_forecast < ts for ts in df_weather_solarforecast.index) + self.assertEqual(len(df_weather_solarforecast), + int(self.optim_conf['delta_forecast'].total_seconds()/3600/self.fcst.timeStep)) + def test_get_forecasts_with_lists(self): with open(root+'/config_emhass.yaml', 'r') as file: params = yaml.load(file, Loader=yaml.FullLoader) From cb2988e3ca73e6a9ce49441a6d4da9af8b9af2d3 Mon Sep 17 00:00:00 2001 From: David HERNANDEZ Date: Tue, 4 Oct 2022 00:34:55 +0200 Subject: [PATCH 3/3] Fix - Turned off use secrets --- tests/test_forecast.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_forecast.py b/tests/test_forecast.py index 6d3ff9c8..dd94b889 100644 --- a/tests/test_forecast.py +++ b/tests/test_forecast.py @@ -24,7 +24,7 @@ class TestForecast(unittest.TestCase): def setUp(self): self.get_data_from_file = True params = None - retrieve_hass_conf, optim_conf, plant_conf = get_yaml_parse(pathlib.Path(root+'/config_emhass.yaml'), use_secrets=True) + retrieve_hass_conf, optim_conf, plant_conf = get_yaml_parse(pathlib.Path(root+'/config_emhass.yaml'), use_secrets=False) self.retrieve_hass_conf, self.optim_conf, self.plant_conf = \ retrieve_hass_conf, optim_conf, plant_conf self.rh = retrieve_hass(self.retrieve_hass_conf['hass_url'], self.retrieve_hass_conf['long_lived_token'],