Skip to content

Commit f208497

Browse files
committed
Add missing enum values and improve formatting in enums
1 parent a6306bf commit f208497

7 files changed

Lines changed: 27 additions & 31 deletions

File tree

pyoverkiz/enums/base.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
from __future__ import annotations
44

55
import logging
6-
from enum import Enum
7-
from typing import TypeVar, cast
8-
9-
_EnumT = TypeVar("_EnumT", bound=Enum)
6+
from typing import Any, Self, cast
107

118

129
class UnknownEnumMixin:
@@ -19,13 +16,14 @@ class UnknownEnumMixin:
1916
__missing_message__ = "Unsupported value %s has been returned for %s"
2017

2118
@classmethod
22-
def _missing_(cls, value): # type: ignore[override]
19+
def _missing_(cls, value: object) -> Self:
2320
"""Return `UNKNOWN` and log unrecognized values."""
2421
message = getattr(cls, "__missing_message__", cls.__missing_message__)
2522
logging.getLogger(cls.__module__).warning(message, value, cls)
26-
return cls.UNKNOWN
23+
# Type checker cannot infer UNKNOWN exists on Self, but all subclasses define it
24+
return cast(Self, cls.UNKNOWN) # type: ignore[attr-defined]
2725

2826
@classmethod
29-
def from_value(cls: type[_EnumT], value: object) -> _EnumT:
27+
def from_value(cls: type[Self], value: object) -> Self:
3028
"""Return enum for `value`, falling back to `UNKNOWN`."""
31-
return cast(_EnumT, cls(value))
29+
return cast(Self, cast(Any, cls)(value))

pyoverkiz/enums/gateway.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,12 @@ class GatewayType(UnknownEnumMixin, IntEnum):
6565
@property
6666
def beautify_name(self) -> str:
6767
"""Return a human friendly name for the gateway type."""
68-
name = self.name.replace("_", " ").title()
69-
name = name.replace("Tahoma", "TaHoma")
70-
name = name.replace("Rts", "RTS")
71-
return name
68+
return (
69+
self.name.replace("_", " ")
70+
.title()
71+
.replace("Tahoma", "TaHoma")
72+
.replace("Rts", "RTS")
73+
)
7274

7375

7476
@unique
@@ -97,10 +99,12 @@ class GatewaySubType(UnknownEnumMixin, IntEnum):
9799
@property
98100
def beautify_name(self) -> str:
99101
"""Return a human friendly name for the gateway sub-type."""
100-
name = self.name.replace("_", " ").title()
101-
name = name.replace("Tahoma", "TaHoma")
102-
name = name.replace("Rts", "RTS")
103-
return name
102+
return (
103+
self.name.replace("_", " ")
104+
.title()
105+
.replace("Tahoma", "TaHoma")
106+
.replace("Rts", "RTS")
107+
)
104108

105109

106110
@unique

pyoverkiz/enums/general.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ class FailureType(UnknownEnumMixin, IntEnum):
247247
TIME_OUT_ON_COMMAND_PROGRESS = 20003
248248
DEVICE_NO_ANSWER = 60004
249249

250+
250251
@unique
251252
class EventName(UnknownEnumMixin, StrEnum):
252253
"""Enumeration of event names emitted by Overkiz."""
@@ -433,8 +434,3 @@ class EventName(UnknownEnumMixin, StrEnum):
433434
ZONE_CREATED = "ZoneCreatedEvent"
434435
ZONE_DELETED = "ZoneDeletedEvent"
435436
ZONE_UPDATED = "ZoneUpdatedEvent"
436-
437-
@classmethod
438-
def _missing_(cls, value): # type: ignore
439-
_LOGGER.warning(f"Unsupported value {value} has been returned for {cls}")
440-
return cls.UNKNOWN

pyoverkiz/enums/protocol.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,3 @@ class Protocol(UnknownEnumMixin, StrEnum):
4444
WISER = "wiser"
4545
ZIGBEE = "zigbee"
4646
ZWAVE = "zwave"
47-
48-
__missing_message__ = "Unsupported protocol %s has been returned for %s"

pyoverkiz/enums/ui.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class UIClass(UnknownEnumMixin, StrEnum):
8383

8484
UNKNOWN = "unknown"
8585

86+
8687
@unique
8788
class UIWidget(UnknownEnumMixin, StrEnum):
8889
"""Enumeration of UI widgets used by Overkiz for device presentation."""
@@ -424,8 +425,3 @@ class UIWidget(UnknownEnumMixin, StrEnum):
424425
ZIGBEE_STACK = "ZigbeeStack"
425426

426427
UNKNOWN = "unknown"
427-
428-
@classmethod
429-
def _missing_(cls, value): # type: ignore
430-
_LOGGER.warning(f"Unsupported value {value} has been returned for {cls}")
431-
return cls.UNKNOWN

pyoverkiz/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ def to_payload(self) -> dict[str, object]:
501501
if self.parameters is not None:
502502
payload["parameters"] = [
503503
p if isinstance(p, (str, int, float, bool)) else str(p)
504-
for p in self.parameters # type: ignore[arg-type]
504+
for p in self.parameters
505505
]
506506

507507
return payload

pyoverkiz/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ def create_server_config(
3737
configuration_url: str | None = None,
3838
) -> ServerConfig:
3939
"""Generate server configuration with the provided endpoint and metadata."""
40+
resolved_server = (
41+
server if isinstance(server, Server) or server is None else Server(server)
42+
)
43+
resolved_type = type if isinstance(type, APIType) else APIType(type)
4044
return ServerConfig(
41-
server=server, # type: ignore[arg-type]
45+
server=resolved_server,
4246
name=name,
4347
endpoint=endpoint,
4448
manufacturer=manufacturer,
4549
configuration_url=configuration_url,
46-
type=type, # type: ignore[arg-type]
50+
type=resolved_type,
4751
)
4852

4953

0 commit comments

Comments
 (0)