From 762d7357b51c5f3b17524e537e1e731d4a38a5bb Mon Sep 17 00:00:00 2001
From: jdelaney72 <20731268+jdelaney72@users.noreply.github.com>
Date: Tue, 1 Sep 2020 03:42:39 -0700
Subject: [PATCH] Fix outdated api url in noaa_tides (#39370)

* Fix outdated dependency in noaa_tides

* Catch exceptions when instantiating new Station

* Add myself to codeowners
---
 CODEOWNERS                                    |  1 +
 .../components/noaa_tides/manifest.json       |  4 +--
 homeassistant/components/noaa_tides/sensor.py | 31 +++++++++++++------
 requirements_all.txt                          |  6 ++--
 4 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/CODEOWNERS b/CODEOWNERS
index 85cde0973f9..0bb10972910 100644
--- a/CODEOWNERS
+++ b/CODEOWNERS
@@ -281,6 +281,7 @@ homeassistant/components/nilu/* @hfurubotten
 homeassistant/components/nissan_leaf/* @filcole
 homeassistant/components/nmbs/* @thibmaek
 homeassistant/components/no_ip/* @fabaff
+homeassistant/components/noaa_tides/* @jdelaney72
 homeassistant/components/notify/* @home-assistant/core
 homeassistant/components/notify_events/* @matrozov @papajojo
 homeassistant/components/notion/* @bachya
diff --git a/homeassistant/components/noaa_tides/manifest.json b/homeassistant/components/noaa_tides/manifest.json
index 3e95ff523b7..f0343d88c84 100644
--- a/homeassistant/components/noaa_tides/manifest.json
+++ b/homeassistant/components/noaa_tides/manifest.json
@@ -2,6 +2,6 @@
   "domain": "noaa_tides",
   "name": "NOAA Tides",
   "documentation": "https://www.home-assistant.io/integrations/noaa_tides",
-  "requirements": ["py_noaa==0.3.0"],
-  "codeowners": []
+  "requirements": ["noaa-coops==0.1.8"],
+  "codeowners": ["@jdelaney72"]
 }
diff --git a/homeassistant/components/noaa_tides/sensor.py b/homeassistant/components/noaa_tides/sensor.py
index 063a163a8ab..a0453e3acb1 100644
--- a/homeassistant/components/noaa_tides/sensor.py
+++ b/homeassistant/components/noaa_tides/sensor.py
@@ -2,7 +2,8 @@
 from datetime import datetime, timedelta
 import logging
 
-from py_noaa import coops  # pylint: disable=import-error
+import noaa_coops as coops  # pylint: disable=import-error
+import requests
 import voluptuous as vol
 
 from homeassistant.components.sensor import PLATFORM_SCHEMA
@@ -12,6 +13,7 @@ from homeassistant.const import (
     CONF_TIME_ZONE,
     CONF_UNIT_SYSTEM,
 )
+from homeassistant.exceptions import PlatformNotReady
 import homeassistant.helpers.config_validation as cv
 from homeassistant.helpers.entity import Entity
 
@@ -51,24 +53,35 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
     else:
         unit_system = UNIT_SYSTEMS[0]
 
-    noaa_sensor = NOAATidesAndCurrentsSensor(name, station_id, timezone, unit_system)
-
-    noaa_sensor.update()
-    if noaa_sensor.data is None:
-        _LOGGER.error("Unable to setup NOAA Tides Sensor")
+    try:
+        station = coops.Station(station_id, unit_system)
+    except KeyError:
+        _LOGGER.error("NOAA Tides Sensor station_id %s does not exist", station_id)
         return
+    except requests.exceptions.ConnectionError as exception:
+        _LOGGER.error(
+            "Connection error during setup in NOAA Tides Sensor for station_id: %s",
+            station_id,
+        )
+        raise PlatformNotReady from exception
+
+    noaa_sensor = NOAATidesAndCurrentsSensor(
+        name, station_id, timezone, unit_system, station
+    )
+
     add_entities([noaa_sensor], True)
 
 
 class NOAATidesAndCurrentsSensor(Entity):
     """Representation of a NOAA Tides and Currents sensor."""
 
-    def __init__(self, name, station_id, timezone, unit_system):
+    def __init__(self, name, station_id, timezone, unit_system, station):
         """Initialize the sensor."""
         self._name = name
         self._station_id = station_id
         self._timezone = timezone
         self._unit_system = unit_system
+        self._station = station
         self.data = None
 
     @property
@@ -110,15 +123,13 @@ class NOAATidesAndCurrentsSensor(Entity):
 
     def update(self):
         """Get the latest data from NOAA Tides and Currents API."""
-
         begin = datetime.now()
         delta = timedelta(days=2)
         end = begin + delta
         try:
-            df_predictions = coops.get_data(
+            df_predictions = self._station.get_data(
                 begin_date=begin.strftime("%Y%m%d %H:%M"),
                 end_date=end.strftime("%Y%m%d %H:%M"),
-                stationid=self._station_id,
                 product="predictions",
                 datum="MLLW",
                 interval="hilo",
diff --git a/requirements_all.txt b/requirements_all.txt
index 1f2681ec9ea..90d54dc36a5 100644
--- a/requirements_all.txt
+++ b/requirements_all.txt
@@ -966,6 +966,9 @@ niko-home-control==0.2.1
 # homeassistant.components.nilu
 niluclient==0.1.2
 
+# homeassistant.components.noaa_tides
+noaa-coops==0.1.8
+
 # homeassistant.components.notify_events
 notify-events==1.0.4
 
@@ -1203,9 +1206,6 @@ pyW800rf32==0.1
 # homeassistant.components.nextbus
 py_nextbusnext==0.1.4
 
-# homeassistant.components.noaa_tides
-# py_noaa==0.3.0
-
 # homeassistant.components.ads
 pyads==3.2.2
 
-- 
GitLab