This commit is contained in:
Paulus Schoutsen
2019-07-31 12:25:30 -07:00
parent da05dfe708
commit 4de97abc3a
2676 changed files with 163166 additions and 140084 deletions

View File

@@ -8,7 +8,12 @@ import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
ATTR_ATTRIBUTION, CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME)
ATTR_ATTRIBUTION,
CONF_API_KEY,
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_NAME,
)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
@@ -16,16 +21,18 @@ _LOGGER = logging.getLogger(__name__)
ATTRIBUTION = "Data provided by WorldTides"
DEFAULT_NAME = 'WorldTidesInfo'
DEFAULT_NAME = "WorldTidesInfo"
SCAN_INTERVAL = timedelta(seconds=3600)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_API_KEY): cv.string,
vol.Optional(CONF_LATITUDE): cv.latitude,
vol.Optional(CONF_LONGITUDE): cv.longitude,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
})
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_API_KEY): cv.string,
vol.Optional(CONF_LATITUDE): cv.latitude,
vol.Optional(CONF_LONGITUDE): cv.longitude,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
@@ -41,7 +48,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
tides = WorldTidesInfoSensor(name, lat, lon, key)
tides.update()
if tides.data.get('error') == 'No location found':
if tides.data.get("error") == "No location found":
_LOGGER.error("Location not available")
return
@@ -69,29 +76,31 @@ class WorldTidesInfoSensor(Entity):
"""Return the state attributes of this device."""
attr = {ATTR_ATTRIBUTION: ATTRIBUTION}
if 'High' in str(self.data['extremes'][0]['type']):
attr['high_tide_time_utc'] = self.data['extremes'][0]['date']
attr['high_tide_height'] = self.data['extremes'][0]['height']
attr['low_tide_time_utc'] = self.data['extremes'][1]['date']
attr['low_tide_height'] = self.data['extremes'][1]['height']
elif 'Low' in str(self.data['extremes'][0]['type']):
attr['high_tide_time_utc'] = self.data['extremes'][1]['date']
attr['high_tide_height'] = self.data['extremes'][1]['height']
attr['low_tide_time_utc'] = self.data['extremes'][0]['date']
attr['low_tide_height'] = self.data['extremes'][0]['height']
if "High" in str(self.data["extremes"][0]["type"]):
attr["high_tide_time_utc"] = self.data["extremes"][0]["date"]
attr["high_tide_height"] = self.data["extremes"][0]["height"]
attr["low_tide_time_utc"] = self.data["extremes"][1]["date"]
attr["low_tide_height"] = self.data["extremes"][1]["height"]
elif "Low" in str(self.data["extremes"][0]["type"]):
attr["high_tide_time_utc"] = self.data["extremes"][1]["date"]
attr["high_tide_height"] = self.data["extremes"][1]["height"]
attr["low_tide_time_utc"] = self.data["extremes"][0]["date"]
attr["low_tide_height"] = self.data["extremes"][0]["height"]
return attr
@property
def state(self):
"""Return the state of the device."""
if self.data:
if 'High' in str(self.data['extremes'][0]['type']):
tidetime = time.strftime('%I:%M %p', time.localtime(
self.data['extremes'][0]['dt']))
if "High" in str(self.data["extremes"][0]["type"]):
tidetime = time.strftime(
"%I:%M %p", time.localtime(self.data["extremes"][0]["dt"])
)
return "High tide at {}".format(tidetime)
if 'Low' in str(self.data['extremes'][0]['type']):
tidetime = time.strftime('%I:%M %p', time.localtime(
self.data['extremes'][0]['dt']))
if "Low" in str(self.data["extremes"][0]["type"]):
tidetime = time.strftime(
"%I:%M %p", time.localtime(self.data["extremes"][0]["dt"])
)
return "Low tide at {}".format(tidetime)
return None
return None
@@ -99,16 +108,15 @@ class WorldTidesInfoSensor(Entity):
def update(self):
"""Get the latest data from WorldTidesInfo API."""
start = int(time.time())
resource = ('https://www.worldtides.info/api?extremes&length=86400'
'&key={}&lat={}&lon={}&start={}').format(
self._key, self._lat, self._lon, start)
resource = (
"https://www.worldtides.info/api?extremes&length=86400"
"&key={}&lat={}&lon={}&start={}"
).format(self._key, self._lat, self._lon, start)
try:
self.data = requests.get(resource, timeout=10).json()
_LOGGER.debug("Data: %s", self.data)
_LOGGER.debug(
"Tide data queried with start time set to: %s", start)
_LOGGER.debug("Tide data queried with start time set to: %s", start)
except ValueError as err:
_LOGGER.error(
"Error retrieving data from WorldTidesInfo: %s", err.args)
_LOGGER.error("Error retrieving data from WorldTidesInfo: %s", err.args)
self.data = None