Add strict typing to openexchangerates (#76004)

This commit is contained in:
Martin Hjelmare 2022-08-01 11:35:31 +02:00 committed by GitHub
parent 81d1786a16
commit 826de707e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 24 deletions

View File

@ -186,6 +186,7 @@ homeassistant.components.nut.*
homeassistant.components.oncue.* homeassistant.components.oncue.*
homeassistant.components.onewire.* homeassistant.components.onewire.*
homeassistant.components.open_meteo.* homeassistant.components.open_meteo.*
homeassistant.components.openexchangerates.*
homeassistant.components.openuv.* homeassistant.components.openuv.*
homeassistant.components.peco.* homeassistant.components.peco.*
homeassistant.components.overkiz.* homeassistant.components.overkiz.*

View File

@ -4,18 +4,13 @@ from __future__ import annotations
from datetime import timedelta from datetime import timedelta
from http import HTTPStatus from http import HTTPStatus
import logging import logging
from typing import Any
import requests import requests
import voluptuous as vol import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import ( from homeassistant.const import CONF_API_KEY, CONF_BASE, CONF_NAME, CONF_QUOTE
ATTR_ATTRIBUTION,
CONF_API_KEY,
CONF_BASE,
CONF_NAME,
CONF_QUOTE,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -49,10 +44,10 @@ def setup_platform(
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Set up the Open Exchange Rates sensor.""" """Set up the Open Exchange Rates sensor."""
name = config.get(CONF_NAME) name: str = config[CONF_NAME]
api_key = config.get(CONF_API_KEY) api_key: str = config[CONF_API_KEY]
base = config.get(CONF_BASE) base: str = config[CONF_BASE]
quote = config.get(CONF_QUOTE) quote: str = config[CONF_QUOTE]
parameters = {"base": base, "app_id": api_key} parameters = {"base": base, "app_id": api_key}
@ -70,50 +65,55 @@ def setup_platform(
class OpenexchangeratesSensor(SensorEntity): class OpenexchangeratesSensor(SensorEntity):
"""Representation of an Open Exchange Rates sensor.""" """Representation of an Open Exchange Rates sensor."""
def __init__(self, rest, name, quote): _attr_attribution = ATTRIBUTION
def __init__(self, rest: OpenexchangeratesData, name: str, quote: str) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
self.rest = rest self.rest = rest
self._name = name self._name = name
self._quote = quote self._quote = quote
self._state = None self._state: float | 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 native_value(self): def native_value(self) -> float | None:
"""Return the state of the sensor.""" """Return the state of the sensor."""
return self._state return self._state
@property @property
def extra_state_attributes(self): def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return other attributes of the sensor.""" """Return other attributes of the sensor."""
attr = self.rest.data attr = self.rest.data
attr[ATTR_ATTRIBUTION] = ATTRIBUTION
return attr return attr
def update(self): def update(self) -> None:
"""Update current conditions.""" """Update current conditions."""
self.rest.update() self.rest.update()
value = self.rest.data if (value := self.rest.data) is None:
self._state = round(value[str(self._quote)], 4) self._attr_available = False
return
self._attr_available = True
self._state = round(value[self._quote], 4)
class OpenexchangeratesData: class OpenexchangeratesData:
"""Get data from Openexchangerates.org.""" """Get data from Openexchangerates.org."""
def __init__(self, resource, parameters, quote): def __init__(self, resource: str, parameters: dict[str, str], quote: str) -> None:
"""Initialize the data object.""" """Initialize the data object."""
self._resource = resource self._resource = resource
self._parameters = parameters self._parameters = parameters
self._quote = quote self._quote = quote
self.data = None self.data: dict[str, Any] | None = None
@Throttle(MIN_TIME_BETWEEN_UPDATES) @Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self): def update(self) -> None:
"""Get the latest data from openexchangerates.org.""" """Get the latest data from openexchangerates.org."""
try: try:
result = requests.get(self._resource, params=self._parameters, timeout=10) result = requests.get(self._resource, params=self._parameters, timeout=10)
@ -121,4 +121,3 @@ class OpenexchangeratesData:
except requests.exceptions.HTTPError: except requests.exceptions.HTTPError:
_LOGGER.error("Check the Openexchangerates API key") _LOGGER.error("Check the Openexchangerates API key")
self.data = None self.data = None
return False

View File

@ -1769,6 +1769,17 @@ no_implicit_optional = true
warn_return_any = true warn_return_any = true
warn_unreachable = true warn_unreachable = true
[mypy-homeassistant.components.openexchangerates.*]
check_untyped_defs = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
no_implicit_optional = true
warn_return_any = true
warn_unreachable = true
[mypy-homeassistant.components.openuv.*] [mypy-homeassistant.components.openuv.*]
check_untyped_defs = true check_untyped_defs = true
disallow_incomplete_defs = true disallow_incomplete_defs = true