-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement pydantic for sockets (#435)
* Implement pydantic for sockets * Add type hint * Use constant * Incorrect constant * Update pytradfri/device/socket_control.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Put back unintended delete * Add files to strict typing * Add missing type hints Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
- Loading branch information
1 parent
00f959e
commit 672a1f0
Showing
6 changed files
with
87 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,46 @@ | ||
"""Represent a socket.""" | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
from typing import TYPE_CHECKING | ||
|
||
from ..const import ATTR_DEVICE_STATE | ||
from pydantic import BaseModel, Field | ||
|
||
from ..const import ATTR_DEVICE_STATE, ATTR_ID | ||
|
||
|
||
class SocketResponse(BaseModel): | ||
"""Represent API response for a blind.""" | ||
|
||
id: int = Field(alias=ATTR_ID) | ||
state: int = Field(alias=ATTR_DEVICE_STATE) | ||
|
||
|
||
if TYPE_CHECKING: | ||
# avoid cyclic import at runtime. | ||
from . import Device | ||
|
||
|
||
class Socket: | ||
"""Represent a socket.""" | ||
|
||
def __init__(self, device, index): | ||
def __init__(self, device: Device, index: int) -> None: | ||
"""Create object of class.""" | ||
self.device = device | ||
self.index = index | ||
|
||
@property | ||
def state(self): | ||
def state(self) -> bool: | ||
"""State.""" | ||
return self.raw.get(ATTR_DEVICE_STATE) == 1 | ||
return self.raw.state == 1 | ||
|
||
@property | ||
def raw(self) -> dict[str, Any]: | ||
def raw(self) -> SocketResponse: | ||
"""Return raw data that it represents.""" | ||
socket_control_response = self.device.raw.socket_control | ||
assert socket_control_response is not None | ||
return socket_control_response[self.index] | ||
|
||
def __repr__(self): | ||
def __repr__(self) -> str: | ||
"""Return representation of class object.""" | ||
state = "on" if self.state else "off" | ||
return f"<Socket #{self.index} - name: {self.device.name}, state: {state}>" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters