Fix NOAA tides warnings (#99856)

This commit is contained in:
Jan Bouwhuis 2023-09-07 21:49:03 +02:00 committed by GitHub
parent 4ce9c1f304
commit cd8426152f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,6 +3,7 @@ from __future__ import annotations
from datetime import datetime, timedelta from datetime import datetime, timedelta
import logging import logging
from typing import TYPE_CHECKING, Any, Literal, TypedDict
import noaa_coops as coops import noaa_coops as coops
import requests import requests
@ -17,6 +18,9 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util.unit_system import METRIC_SYSTEM from homeassistant.util.unit_system import METRIC_SYSTEM
if TYPE_CHECKING:
from pandas import Timestamp
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONF_STATION_ID = "station_id" CONF_STATION_ID = "station_id"
@ -76,40 +80,56 @@ def setup_platform(
add_entities([noaa_sensor], True) add_entities([noaa_sensor], True)
class NOAATidesData(TypedDict):
"""Representation of a single tide."""
time_stamp: list[Timestamp]
hi_lo: list[Literal["L"] | Literal["H"]]
predicted_wl: list[float]
class NOAATidesAndCurrentsSensor(SensorEntity): class NOAATidesAndCurrentsSensor(SensorEntity):
"""Representation of a NOAA Tides and Currents sensor.""" """Representation of a NOAA Tides and Currents sensor."""
_attr_attribution = "Data provided by NOAA" _attr_attribution = "Data provided by NOAA"
def __init__(self, name, station_id, timezone, unit_system, station): def __init__(self, name, station_id, timezone, unit_system, station) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
self._name = name self._name = name
self._station_id = station_id self._station_id = station_id
self._timezone = timezone self._timezone = timezone
self._unit_system = unit_system self._unit_system = unit_system
self._station = station self._station = station
self.data = None self.data: NOAATidesData | None = None
@property @property
def name(self): def name(self) -> str:
"""Return the name of the sensor.""" """Return the name of the sensor."""
return self._name return self._name
@property @property
def extra_state_attributes(self): def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes of this device.""" """Return the state attributes of this device."""
attr = {} attr: dict[str, Any] = {}
if self.data is None: if self.data is None:
return attr return attr
if self.data["hi_lo"][1] == "H": if self.data["hi_lo"][1] == "H":
attr["high_tide_time"] = self.data.index[1].strftime("%Y-%m-%dT%H:%M") attr["high_tide_time"] = self.data["time_stamp"][1].strftime(
"%Y-%m-%dT%H:%M"
)
attr["high_tide_height"] = self.data["predicted_wl"][1] attr["high_tide_height"] = self.data["predicted_wl"][1]
attr["low_tide_time"] = self.data.index[2].strftime("%Y-%m-%dT%H:%M") attr["low_tide_time"] = self.data["time_stamp"][2].strftime(
"%Y-%m-%dT%H:%M"
)
attr["low_tide_height"] = self.data["predicted_wl"][2] attr["low_tide_height"] = self.data["predicted_wl"][2]
elif self.data["hi_lo"][1] == "L": elif self.data["hi_lo"][1] == "L":
attr["low_tide_time"] = self.data.index[1].strftime("%Y-%m-%dT%H:%M") attr["low_tide_time"] = self.data["time_stamp"][1].strftime(
"%Y-%m-%dT%H:%M"
)
attr["low_tide_height"] = self.data["predicted_wl"][1] attr["low_tide_height"] = self.data["predicted_wl"][1]
attr["high_tide_time"] = self.data.index[2].strftime("%Y-%m-%dT%H:%M") attr["high_tide_time"] = self.data["time_stamp"][2].strftime(
"%Y-%m-%dT%H:%M"
)
attr["high_tide_height"] = self.data["predicted_wl"][2] attr["high_tide_height"] = self.data["predicted_wl"][2]
return attr return attr
@ -118,7 +138,7 @@ class NOAATidesAndCurrentsSensor(SensorEntity):
"""Return the state of the device.""" """Return the state of the device."""
if self.data is None: if self.data is None:
return None return None
api_time = self.data.index[0] api_time = self.data["time_stamp"][0]
if self.data["hi_lo"][0] == "H": if self.data["hi_lo"][0] == "H":
tidetime = api_time.strftime("%-I:%M %p") tidetime = api_time.strftime("%-I:%M %p")
return f"High tide at {tidetime}" return f"High tide at {tidetime}"
@ -142,8 +162,13 @@ class NOAATidesAndCurrentsSensor(SensorEntity):
units=self._unit_system, units=self._unit_system,
time_zone=self._timezone, time_zone=self._timezone,
) )
self.data = df_predictions.head() api_data = df_predictions.head()
_LOGGER.debug("Data = %s", self.data) self.data = NOAATidesData(
time_stamp=list(api_data.index),
hi_lo=list(api_data["hi_lo"].values),
predicted_wl=list(api_data["predicted_wl"].values),
)
_LOGGER.debug("Data = %s", api_data)
_LOGGER.debug( _LOGGER.debug(
"Recent Tide data queried with start time set to %s", "Recent Tide data queried with start time set to %s",
begin.strftime("%m-%d-%Y %H:%M"), begin.strftime("%m-%d-%Y %H:%M"),