mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 20:27:08 +00:00
Make setup fail if location is not available (#15967)
* Make setup fail if location is not available * Lint
This commit is contained in:
parent
800eb4d86a
commit
10a2ecd1d6
@ -1,24 +1,26 @@
|
|||||||
"""
|
"""
|
||||||
This component provides HA sensor support for the worldtides.info API.
|
Support for the worldtides.info API.
|
||||||
|
|
||||||
For more details about this platform, please refer to the documentation at
|
For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/sensor.worldtidesinfo/
|
https://home-assistant.io/components/sensor.worldtidesinfo/
|
||||||
"""
|
"""
|
||||||
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
from datetime import timedelta
|
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
from homeassistant.const import (CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE,
|
from homeassistant.const import (
|
||||||
CONF_NAME, STATE_UNKNOWN)
|
ATTR_ATTRIBUTION, CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME)
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
CONF_ATTRIBUTION = "Data provided by WorldTides"
|
||||||
|
|
||||||
DEFAULT_NAME = 'WorldTidesInfo'
|
DEFAULT_NAME = 'WorldTidesInfo'
|
||||||
|
|
||||||
SCAN_INTERVAL = timedelta(seconds=3600)
|
SCAN_INTERVAL = timedelta(seconds=3600)
|
||||||
@ -42,7 +44,13 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
if None in (lat, lon):
|
if None in (lat, lon):
|
||||||
_LOGGER.error("Latitude or longitude not set in Home Assistant config")
|
_LOGGER.error("Latitude or longitude not set in Home Assistant config")
|
||||||
|
|
||||||
add_devices([WorldTidesInfoSensor(name, lat, lon, key)], True)
|
tides = WorldTidesInfoSensor(name, lat, lon, key)
|
||||||
|
tides.update()
|
||||||
|
if tides.data.get('error') == 'No location found':
|
||||||
|
_LOGGER.error("Location not available")
|
||||||
|
return
|
||||||
|
|
||||||
|
add_devices([tides])
|
||||||
|
|
||||||
|
|
||||||
class WorldTidesInfoSensor(Entity):
|
class WorldTidesInfoSensor(Entity):
|
||||||
@ -64,13 +72,14 @@ class WorldTidesInfoSensor(Entity):
|
|||||||
@property
|
@property
|
||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
"""Return the state attributes of this device."""
|
"""Return the state attributes of this device."""
|
||||||
attr = {}
|
attr = {ATTR_ATTRIBUTION: CONF_ATTRIBUTION}
|
||||||
if "High" in str(self.data['extremes'][0]['type']):
|
|
||||||
|
if 'High' in str(self.data['extremes'][0]['type']):
|
||||||
attr['high_tide_time_utc'] = self.data['extremes'][0]['date']
|
attr['high_tide_time_utc'] = self.data['extremes'][0]['date']
|
||||||
attr['high_tide_height'] = self.data['extremes'][0]['height']
|
attr['high_tide_height'] = self.data['extremes'][0]['height']
|
||||||
attr['low_tide_time_utc'] = self.data['extremes'][1]['date']
|
attr['low_tide_time_utc'] = self.data['extremes'][1]['date']
|
||||||
attr['low_tide_height'] = self.data['extremes'][1]['height']
|
attr['low_tide_height'] = self.data['extremes'][1]['height']
|
||||||
elif "Low" in str(self.data['extremes'][0]['type']):
|
elif 'Low' in str(self.data['extremes'][0]['type']):
|
||||||
attr['high_tide_time_utc'] = self.data['extremes'][1]['date']
|
attr['high_tide_time_utc'] = self.data['extremes'][1]['date']
|
||||||
attr['high_tide_height'] = self.data['extremes'][1]['height']
|
attr['high_tide_height'] = self.data['extremes'][1]['height']
|
||||||
attr['low_tide_time_utc'] = self.data['extremes'][0]['date']
|
attr['low_tide_time_utc'] = self.data['extremes'][0]['date']
|
||||||
@ -81,30 +90,30 @@ class WorldTidesInfoSensor(Entity):
|
|||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state of the device."""
|
"""Return the state of the device."""
|
||||||
if self.data:
|
if self.data:
|
||||||
if "High" in str(self.data['extremes'][0]['type']):
|
if 'High' in str(self.data['extremes'][0]['type']):
|
||||||
tidetime = time.strftime('%I:%M %p', time.localtime(
|
tidetime = time.strftime('%I:%M %p', time.localtime(
|
||||||
self.data['extremes'][0]['dt']))
|
self.data['extremes'][0]['dt']))
|
||||||
return "High tide at %s" % (tidetime)
|
return "High tide at {}".format(tidetime)
|
||||||
if "Low" in str(self.data['extremes'][0]['type']):
|
if 'Low' in str(self.data['extremes'][0]['type']):
|
||||||
tidetime = time.strftime('%I:%M %p', time.localtime(
|
tidetime = time.strftime('%I:%M %p', time.localtime(
|
||||||
self.data['extremes'][0]['dt']))
|
self.data['extremes'][0]['dt']))
|
||||||
return "Low tide at %s" % (tidetime)
|
return "Low tide at {}".format(tidetime)
|
||||||
return STATE_UNKNOWN
|
return None
|
||||||
return STATE_UNKNOWN
|
return None
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data from WorldTidesInfo API."""
|
"""Get the latest data from WorldTidesInfo API."""
|
||||||
start = int(time.time())
|
start = int(time.time())
|
||||||
resource = 'https://www.worldtides.info/api?extremes&length=86400' \
|
resource = ('https://www.worldtides.info/api?extremes&length=86400'
|
||||||
'&key=%s&lat=%s&lon=%s&start=%s' % (self._key, self._lat,
|
'&key={}&lat={}&lon={}&start={}').format(
|
||||||
self._lon, start)
|
self._key, self._lat, self._lon, start)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.data = requests.get(resource, timeout=10).json()
|
self.data = requests.get(resource, timeout=10).json()
|
||||||
_LOGGER.debug("Data = %s", self.data)
|
_LOGGER.debug("Data: %s", self.data)
|
||||||
_LOGGER.info("Tide data queried with start time set to: %s",
|
_LOGGER.debug(
|
||||||
(start))
|
"Tide data queried with start time set to: %s", start)
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
_LOGGER.error("Check WorldTidesInfo %s", err.args)
|
_LOGGER.error(
|
||||||
|
"Error retrieving data from WorldTidesInfo: %s", err.args)
|
||||||
self.data = None
|
self.data = None
|
||||||
raise
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user