Skip to content

Commit

Permalink
use @cached_property for fixed @Property attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
zxdavb committed Dec 31, 2024
1 parent b6b6e12 commit 8dfabdf
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 24 deletions.
5 changes: 3 additions & 2 deletions src/evohomeasync/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from __future__ import annotations

import logging
from functools import cached_property
from typing import TYPE_CHECKING, Any, Final

from . import exceptions as exc
Expand Down Expand Up @@ -65,7 +66,7 @@ def __init__(self, entity_id: int, auth: Auth, logger: logging.Logger) -> None:
def __str__(self) -> str:
return f"{self.__class__.__name__}(id='{self._id}')"

@property
@cached_property
def id(self) -> str:
return str(self._id)

Expand Down Expand Up @@ -404,7 +405,7 @@ class Gateway(_DeviceBase): # Gateway portion of a Device

# Config attrs...

@property
@cached_property
def mac_address(self) -> str:
return self._config["mac_id"]

Expand Down
9 changes: 5 additions & 4 deletions src/evohomeasync2/control_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import annotations

import json
from functools import cached_property
from typing import TYPE_CHECKING, Final, NoReturn

from evohome.helpers import camel_to_snake
Expand Down Expand Up @@ -105,15 +106,15 @@ def zone_by_name(self) -> dict[str, Zone]:

# Config attrs...

@property # TODO: deprecate in favour of .id attr
@cached_property # TODO: deprecate in favour of .id attr
def systemId(self) -> str: # noqa: N802
return self._id

@property # RENAMED val: was model_type
@cached_property # RENAMED val: was model_type
def model(self) -> TcsModelType:
return self._config[SZ_MODEL_TYPE]

@property
@cached_property
def allowed_system_modes(self) -> tuple[EvoAllowedSystemModesResponseT, ...]:
"""
"allowedSystemModes": [
Expand All @@ -129,7 +130,7 @@ def allowed_system_modes(self) -> tuple[EvoAllowedSystemModesResponseT, ...]:

return tuple(self._config[SZ_ALLOWED_SYSTEM_MODES])

@property # a convenience attr, derived from allowed_system_modes
@cached_property # a convenience attr, derived from allowed_system_modes
def modes(self) -> tuple[SystemMode, ...]:
return tuple(d[SZ_SYSTEM_MODE] for d in self.allowed_system_modes)

Expand Down
5 changes: 3 additions & 2 deletions src/evohomeasync2/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

from functools import cached_property
from typing import TYPE_CHECKING, Final, NoReturn

from evohome.helpers import camel_to_snake
Expand Down Expand Up @@ -60,11 +61,11 @@ def __init__(self, location: Location, config: EvoGwyConfigResponseT) -> None:

# Config attrs...

@property # TODO: deprecate in favour of .id attr
@cached_property # TODO: deprecate in favour of .id attr
def gatewayId(self) -> str: # noqa: N802
return self._id

@property # RENAMED val: was mac
@cached_property # RENAMED val: was mac
def mac_address(self) -> str:
return self._config[SZ_MAC]

Expand Down
15 changes: 8 additions & 7 deletions src/evohomeasync2/hotwater.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import annotations

from functools import cached_property
from typing import TYPE_CHECKING, Final

from evohome.helpers import camel_to_snake
Expand Down Expand Up @@ -65,19 +66,19 @@ def __init__(self, tcs: ControlSystem, config: EvoDhwConfigResponseT) -> None:

# Config attrs...

@property # TODO: deprecate in favour of .id attr
@cached_property # TODO: deprecate in favour of .id attr
def dhwId(self) -> str: # noqa: N802
return self._id

@property
@cached_property
def name(self) -> str:
return "Domestic Hot Water"

@property
@cached_property
def type(self) -> str:
return "DomesticHotWater"

@property # NOTE: renamed config key: was schedule_capabilities_response
@cached_property # NOTE: renamed config key: was schedule_capabilities_response
def schedule_capabilities(self) -> EvoDhwScheduleCapabilitiesResponseT:
"""
"scheduleCapabilitiesResponse": {
Expand All @@ -89,7 +90,7 @@ def schedule_capabilities(self) -> EvoDhwScheduleCapabilitiesResponseT:

return self._config[SZ_SCHEDULE_CAPABILITIES_RESPONSE]

@property # NOTE: renamed config key: was dhw_state_capabilities_response
@cached_property # NOTE: renamed config key: was dhw_state_capabilities_response
def state_capabilities(self) -> EvoDhwStateCapabilitiesResponseT:
"""
"dhwStateCapabilitiesResponse": {
Expand All @@ -102,11 +103,11 @@ def state_capabilities(self) -> EvoDhwStateCapabilitiesResponseT:

return self._config[SZ_DHW_STATE_CAPABILITIES_RESPONSE]

@property
@cached_property
def allowed_modes(self) -> tuple[ZoneMode, ...]:
return tuple(self.state_capabilities[SZ_ALLOWED_MODES])

@property
@cached_property
def allowed_states(self) -> tuple[DhwState, ...]:
return tuple(self.state_capabilities["allowed_states"])

Expand Down
3 changes: 2 additions & 1 deletion src/evohomeasync2/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import logging
from datetime import datetime as dt, timedelta as td, tzinfo
from functools import cached_property
from typing import TYPE_CHECKING

from aiozoneinfo import async_get_time_zone
Expand Down Expand Up @@ -120,7 +121,7 @@ def __init__(

# Config attrs...

@property # TODO: deprecate in favour of .id attr
@cached_property # TODO: deprecate in favour of .id attr
def locationId(self) -> str: # noqa: N802
return self._id

Expand Down
15 changes: 8 additions & 7 deletions src/evohomeasync2/zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import json
from datetime import UTC, datetime as dt, timedelta as td
from functools import cached_property
from http import HTTPStatus
from typing import TYPE_CHECKING, Any, Final

Expand Down Expand Up @@ -114,7 +115,7 @@ def __str__(self) -> str:

# Config attrs...

@property
@cached_property
def id(self) -> str:
return self._id

Expand Down Expand Up @@ -472,11 +473,11 @@ def __init__(self, tcs: ControlSystem, config: EvoZonConfigResponseT) -> None:

# Config attrs...

@property # TODO: deprecate in favour of .id attr
@cached_property # TODO: deprecate in favour of .id attr
def zoneId(self) -> str: # noqa: N802
return self._id

@property
@cached_property
def model(self) -> ZoneModelType:
return self._config[SZ_MODEL_TYPE]

Expand All @@ -486,11 +487,11 @@ def name(self) -> str:
return self._status[SZ_NAME]
return self._config[SZ_NAME]

@property
@cached_property
def type(self) -> ZoneType:
return self._config[SZ_ZONE_TYPE]

@property
@cached_property
def schedule_capabilities(self) -> EvoZonScheduleCapabilitiesResponseT:
"""
"scheduleCapabilities": {
Expand All @@ -503,7 +504,7 @@ def schedule_capabilities(self) -> EvoZonScheduleCapabilitiesResponseT:

return self._config[SZ_SCHEDULE_CAPABILITIES]

@property
@cached_property
def setpoint_capabilities(self) -> EvoZonSetpointCapabilitiesResponseT:
"""
"setpointCapabilities": {
Expand All @@ -520,7 +521,7 @@ def setpoint_capabilities(self) -> EvoZonSetpointCapabilitiesResponseT:

return self._config[SZ_SETPOINT_CAPABILITIES]

@property
@cached_property
def allowed_modes(self) -> tuple[ZoneMode, ...]:
return tuple(self.setpoint_capabilities[SZ_ALLOWED_SETPOINT_MODES])

Expand Down
3 changes: 2 additions & 1 deletion tests/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import inspect
import json
from functools import cached_property
from pathlib import Path
from typing import TYPE_CHECKING, Any

Expand All @@ -30,5 +31,5 @@ def get_property_methods(obj: object) -> list[str]:
return [
name
for name, value in inspect.getmembers(obj.__class__)
if isinstance(value, property)
if isinstance(value, property | cached_property)
]

0 comments on commit 8dfabdf

Please sign in to comment.