Skip to content

Commit

Permalink
Merge pull request #30 from saniho/AddLimitOfHourForPrevision
Browse files Browse the repository at this point in the history
Add limit of hour for prevision
  • Loading branch information
saniho authored Mar 14, 2024
2 parents 7f66094 + 7cecd5a commit 13bbc62
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 264 deletions.
3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

310 changes: 63 additions & 247 deletions .idea/workspace.xml

Large diffs are not rendered by default.

20 changes: 18 additions & 2 deletions custom_components/apiMareeInfo/apiMareeInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def getlisteport(self, nomport):
nomport)
url = "https://ws.meteoconsult.fr/meteoconsultmarine/android/100/fr/v30/recherche.php?rech=%s&type=48" % (
nomport)
_url = \
"http://ws.meteoconsult.fr/meteoconsultmarine/android/100/fr/v30/recherche.php?rech=%s&type=48" % (
nomport)

print(url)
retour = self.getjson(url)
print(retour)
Expand Down Expand Up @@ -111,6 +115,7 @@ def __init__(self):
self._donnees = {}
self._nomDuPort = None
self._dateCourante = None
self._maxhours = None
self._lat = None
self._lng = None
self._message = ""
Expand All @@ -130,6 +135,9 @@ def setport(self, lat, lng):
self._lat = lat
self._lng = lng

def setmaxhours(self, maxhours):
self._maxhours = maxhours

def getinformationport(self, jsondata=None, outfile=None, origine="MeteoMarine", info=None):
if (jsondata is None):
jsondata = self.getjson(origine, info)
Expand All @@ -138,8 +146,13 @@ def getinformationport(self, jsondata=None, outfile=None, origine="MeteoMarine",
with open(outfile, 'w') as outfilev:
json.dump(jsondata, outfilev)
if origine == "MeteoMarine":
self._nomDuPort = jsondata["contenu"]["marees"][0]['lieu']
self._dateCourante = jsondata["contenu"]["marees"][0]['datetime']
#_LOGGER.error( jsondata )
if len( jsondata["contenu"]["marees"]) == 0:
self._error = True
else:
self._nomDuPort = jsondata["contenu"]["marees"][0]['lieu']
self._dateCourante = jsondata["contenu"]["marees"][0]['datetime']
self._error = False
elif origine == "stormio":
if "station" in jsondata["meta"]:
self._nomDuPort = jsondata["meta"]["station"]['name']
Expand All @@ -157,6 +170,7 @@ def getinformationport(self, jsondata=None, outfile=None, origine="MeteoMarine",
a = {}
myMarees = {}
dicoPrevis = {}
#_LOGGER.error( "origine : %s / error : %s"%(origine, self._error))
if (origine == "MeteoMarine") and (not self._error):
j = 0
for maree in jsondata["contenu"]["marees"][:6]:
Expand Down Expand Up @@ -246,6 +260,8 @@ def getnomcompletduport(self):

def getdatecourante(self):
return self._dateCourante
def getmaxhours(self):
return self._maxhours

def gethttptimerequest(self):
return self._httptimerequest
Expand Down
3 changes: 2 additions & 1 deletion custom_components/apiMareeInfo/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

# delai pour l'update http, toutes les 3 heures
CONF_SCAN_INTERVAL_HTTP = datetime.timedelta(seconds=60 * 60 * 3)
CONF_MAXHOURS = "MAX_HOURS"

__VERSION__ = "1.1.4"
__VERSION__ = "1.1.5"

__name__ = "apiMareeInfo"
2 changes: 1 addition & 1 deletion custom_components/apiMareeInfo/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"domain": "apiMareeInfo",
"name": "apiMareeInfo sensor",
"documentation": "",
"version": "1.1.4",
"version": "1.1.5",
"requirements": [
],
"dependencies": [],
Expand Down
19 changes: 16 additions & 3 deletions custom_components/apiMareeInfo/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .const import ( # isort:skip
__name__,
# CONF_SCAN_INTERVAL_HTTP,
CONF_MAXHOURS
)

_LOGGER = logging.getLogger(__name__)
Expand All @@ -35,21 +36,23 @@
vol.Required(CONF_LATITUDE): cv.string,
vol.Required(CONF_LONGITUDE): cv.string,
vol.Optional(CONF_STORM_KEY): cv.string,
vol.Optional(CONF_MAXHOURS): cv.string,
}
)

from . import apiMareeInfo, sensorApiMaree


class myMareeInfo:
def __init__(self, idDuPort, lat, lng, stormkey, _update_interval):
def __init__(self, idDuPort, lat, lng, stormkey, maxhours, _update_interval):
self._lastSynchro = None
self._update_interval = _update_interval
self._idDuPort = idDuPort
self._lat = lat
self._lng = lng
self._origine = "MeteoMarine"
self._stormkey = stormkey
self._maxhours = maxhours
self._myMaree = apiMareeInfo.ApiMareeInfo()
pass

Expand All @@ -64,6 +67,7 @@ def update(self, ):
self._myMaree.getinformationport(origine=self._origine)
else:
self._myMaree.getinformationport(origine="stormio", info={"stormkey": self._stormkey})
self._myMaree.setmaxhours(self._maxhours)
self._lastSynchro = datetime.datetime.now()

def getIdPort(self):
Expand All @@ -86,11 +90,16 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
lat = config.get(CONF_LATITUDE)
lng = config.get(CONF_LONGITUDE)
stormkey = config.get(CONF_STORM_KEY)
maxhours = config.get(CONF_MAXHOURS)
session = []
except:
_LOGGER.exception("Could not run my apiMaree Extension miss argument ?")
return False
myPort = myMareeInfo(idDuPort, lat, lng, stormkey, update_interval_http)
if maxhours is None:
maxhours = 6
else:
maxhours = int( maxhours )
myPort = myMareeInfo(idDuPort, lat, lng, stormkey, maxhours, update_interval_http)
myPort.update()
add_entities([infoMareeSensor(session, name, update_interval, myPort)], True)
# add_entities([infoMareePluieSensor(session, name, update_interval, myPort)], True)
Expand Down Expand Up @@ -138,6 +147,7 @@ def _update(self):
try:
state, status_counts = self._sAM.getstatus()
except:
_LOGGER.error( "erreur dans getStatus()")
return
self._attributes = {ATTR_ATTRIBUTION: ""}
self._attributes.update(status_counts)
Expand All @@ -147,7 +157,10 @@ def _update(self):
def extra_state_attributes(self):
"""Return the state attributes."""
return self._attributes

@property
def device_state_attributes(self):
"""Return the state attributes."""
return self._attributes
@property
def icon(self):
"""Icon to use in the frontend."""
Expand Down
4 changes: 3 additions & 1 deletion custom_components/apiMareeInfo/sensorApiMaree.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ def getstatus(self):
status_counts["next_coeff_%s" % i] = "%s" % pMaree["coeff"]
status_counts["timeLastCall"] = datetime.datetime.now()

maxTime = datetime.datetime.now() + datetime.timedelta( hours = self._myPort.getmaxhours() )
dicoPrevis = []
for maDate in self._myPort.getprevis().keys():
if maDate.replace(tzinfo=None) >= datetime.datetime.now():
if maDate.replace(tzinfo=None) >= datetime.datetime.now() and \
maDate.replace(tzinfo=None) <= maxTime:
dico = {}
dico["datetime"] = maDate
for clefPrevis in self._myPort.getprevis()[maDate].keys():
Expand Down
1 change: 1 addition & 0 deletions file_20240314.json

Large diffs are not rendered by default.

22 changes: 13 additions & 9 deletions testMareeInfo.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
def testPortMeteoMarine():
import json

with open('./tests/json/meteomarine/SJM_20220214.json') as f:
with open('./file_20240314.json') as f:
dataJson = json.load(f)
dataJson = None
from custom_components.apiMareeInfo import apiMareeInfo, sensorApiMaree

_myMaree = apiMareeInfo.ApiMareeInfo()
# lat, lng = "46.7711", "-2.05306"
lat, lng = "46.4967", "-1.79667"
lat, lng = "46.009313818464726", "-1.1740585688883056"
lat, lng = "45.0015181", "-1.1999562"
lat, lng = "46.7711", "-2.05306"
#lat, lng = "46.4967", "-1.79667"
#lat, lng = "46.009313818464726", "-1.1740585688883056"
#lat, lng = "45.0015181", "-1.1999562"
lat, lng = "46.7711", "-2.05306"
_myMaree.setport(lat, lng)
_myMaree.getinformationport(outfile="file_20220726.json")
dataJson = None
#_myMaree.getinformationport(jsondata =dataJson)
#_myMaree.getinformationport(outfile="file_20240314.json")
_myMaree.getinformationport()
_myMaree.setmaxhours(6)
#dataJson = None
_myMaree.getinformationport(dataJson)
# print(_myMaree.getinfo())
# print(_myMaree.getnomduport())
Expand Down Expand Up @@ -68,10 +72,10 @@ def testListePorts():

_myPort = apiMareeInfo.ListePorts()
# a = _myPort.getlisteport("olonne")
a = _myPort.getlisteport("Arromanches")
a = _myPort.getlisteport("Brévin")
print(a)


testPortMeteoMarine()
# testPortStormGlass()
# testListePorts()
#testListePorts()

0 comments on commit 13bbc62

Please sign in to comment.