Add StreamLabsWater to strict typing (#104957)

This commit is contained in:
Joost Lekkerkerker 2023-12-03 21:10:37 +01:00 committed by GitHub
parent ecc49e61f6
commit 833805f9be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 15 deletions

View File

@ -317,6 +317,7 @@ homeassistant.components.statistics.*
homeassistant.components.steamist.* homeassistant.components.steamist.*
homeassistant.components.stookalert.* homeassistant.components.stookalert.*
homeassistant.components.stream.* homeassistant.components.stream.*
homeassistant.components.streamlabswater.*
homeassistant.components.sun.* homeassistant.components.sun.*
homeassistant.components.surepetcare.* homeassistant.components.surepetcare.*
homeassistant.components.switch.* homeassistant.components.switch.*

View File

@ -3,6 +3,8 @@ from __future__ import annotations
from datetime import timedelta from datetime import timedelta
from streamlabswater.streamlabswater import StreamlabsClient
from homeassistant.components.binary_sensor import BinarySensorEntity from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -39,19 +41,19 @@ def setup_platform(
class StreamlabsLocationData: class StreamlabsLocationData:
"""Track and query location data.""" """Track and query location data."""
def __init__(self, location_id, client): def __init__(self, location_id: str, client: StreamlabsClient) -> None:
"""Initialize the location data.""" """Initialize the location data."""
self._location_id = location_id self._location_id = location_id
self._client = client self._client = client
self._is_away = None self._is_away = None
@Throttle(MIN_TIME_BETWEEN_LOCATION_UPDATES) @Throttle(MIN_TIME_BETWEEN_LOCATION_UPDATES)
def update(self): def update(self) -> None:
"""Query and store location data.""" """Query and store location data."""
location = self._client.get_location(self._location_id) location = self._client.get_location(self._location_id)
self._is_away = location["homeAway"] == "away" self._is_away = location["homeAway"] == "away"
def is_away(self): def is_away(self) -> bool | None:
"""Return whether away more is enabled.""" """Return whether away more is enabled."""
return self._is_away return self._is_away
@ -59,19 +61,21 @@ class StreamlabsLocationData:
class StreamlabsAwayMode(BinarySensorEntity): class StreamlabsAwayMode(BinarySensorEntity):
"""Monitor the away mode state.""" """Monitor the away mode state."""
def __init__(self, location_name, streamlabs_location_data): def __init__(
self, location_name: str, streamlabs_location_data: StreamlabsLocationData
) -> None:
"""Initialize the away mode device.""" """Initialize the away mode device."""
self._location_name = location_name self._location_name = location_name
self._streamlabs_location_data = streamlabs_location_data self._streamlabs_location_data = streamlabs_location_data
self._is_away = None self._is_away = None
@property @property
def name(self): def name(self) -> str:
"""Return the name for away mode.""" """Return the name for away mode."""
return f"{self._location_name} {NAME_AWAY_MODE}" return f"{self._location_name} {NAME_AWAY_MODE}"
@property @property
def is_on(self): def is_on(self) -> bool | None:
"""Return if away mode is on.""" """Return if away mode is on."""
return self._streamlabs_location_data.is_away() return self._streamlabs_location_data.is_away()

View File

@ -3,6 +3,8 @@ from __future__ import annotations
from datetime import timedelta from datetime import timedelta
from streamlabswater.streamlabswater import StreamlabsClient
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from homeassistant.const import UnitOfVolume from homeassistant.const import UnitOfVolume
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -48,7 +50,7 @@ def setup_platform(
class StreamlabsUsageData: class StreamlabsUsageData:
"""Track and query usage data.""" """Track and query usage data."""
def __init__(self, location_id, client): def __init__(self, location_id: str, client: StreamlabsClient) -> None:
"""Initialize the usage data.""" """Initialize the usage data."""
self._location_id = location_id self._location_id = location_id
self._client = client self._client = client
@ -57,22 +59,22 @@ class StreamlabsUsageData:
self._this_year = None self._this_year = None
@Throttle(MIN_TIME_BETWEEN_USAGE_UPDATES) @Throttle(MIN_TIME_BETWEEN_USAGE_UPDATES)
def update(self): def update(self) -> None:
"""Query and store usage data.""" """Query and store usage data."""
water_usage = self._client.get_water_usage_summary(self._location_id) water_usage = self._client.get_water_usage_summary(self._location_id)
self._today = round(water_usage["today"], 1) self._today = round(water_usage["today"], 1)
self._this_month = round(water_usage["thisMonth"], 1) self._this_month = round(water_usage["thisMonth"], 1)
self._this_year = round(water_usage["thisYear"], 1) self._this_year = round(water_usage["thisYear"], 1)
def get_daily_usage(self): def get_daily_usage(self) -> float | None:
"""Return the day's usage.""" """Return the day's usage."""
return self._today return self._today
def get_monthly_usage(self): def get_monthly_usage(self) -> float | None:
"""Return the month's usage.""" """Return the month's usage."""
return self._this_month return self._this_month
def get_yearly_usage(self): def get_yearly_usage(self) -> float | None:
"""Return the year's usage.""" """Return the year's usage."""
return self._this_year return self._this_year
@ -83,7 +85,9 @@ class StreamLabsDailyUsage(SensorEntity):
_attr_device_class = SensorDeviceClass.WATER _attr_device_class = SensorDeviceClass.WATER
_attr_native_unit_of_measurement = UnitOfVolume.GALLONS _attr_native_unit_of_measurement = UnitOfVolume.GALLONS
def __init__(self, location_name, streamlabs_usage_data): def __init__(
self, location_name: str, streamlabs_usage_data: StreamlabsUsageData
) -> None:
"""Initialize the daily water usage device.""" """Initialize the daily water usage device."""
self._location_name = location_name self._location_name = location_name
self._streamlabs_usage_data = streamlabs_usage_data self._streamlabs_usage_data = streamlabs_usage_data
@ -95,7 +99,7 @@ class StreamLabsDailyUsage(SensorEntity):
return f"{self._location_name} {NAME_DAILY_USAGE}" return f"{self._location_name} {NAME_DAILY_USAGE}"
@property @property
def native_value(self): def native_value(self) -> float | None:
"""Return the current daily usage.""" """Return the current daily usage."""
return self._streamlabs_usage_data.get_daily_usage() return self._streamlabs_usage_data.get_daily_usage()
@ -113,7 +117,7 @@ class StreamLabsMonthlyUsage(StreamLabsDailyUsage):
return f"{self._location_name} {NAME_MONTHLY_USAGE}" return f"{self._location_name} {NAME_MONTHLY_USAGE}"
@property @property
def native_value(self): def native_value(self) -> float | None:
"""Return the current monthly usage.""" """Return the current monthly usage."""
return self._streamlabs_usage_data.get_monthly_usage() return self._streamlabs_usage_data.get_monthly_usage()
@ -127,6 +131,6 @@ class StreamLabsYearlyUsage(StreamLabsDailyUsage):
return f"{self._location_name} {NAME_YEARLY_USAGE}" return f"{self._location_name} {NAME_YEARLY_USAGE}"
@property @property
def native_value(self): def native_value(self) -> float | None:
"""Return the current yearly usage.""" """Return the current yearly usage."""
return self._streamlabs_usage_data.get_yearly_usage() return self._streamlabs_usage_data.get_yearly_usage()

View File

@ -2932,6 +2932,16 @@ warn_return_any = true
warn_unreachable = true warn_unreachable = true
no_implicit_reexport = true no_implicit_reexport = true
[mypy-homeassistant.components.streamlabswater.*]
check_untyped_defs = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
warn_return_any = true
warn_unreachable = true
[mypy-homeassistant.components.sun.*] [mypy-homeassistant.components.sun.*]
check_untyped_defs = true check_untyped_defs = true
disallow_incomplete_defs = true disallow_incomplete_defs = true