Skip to content

Commit

Permalink
linter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sim0nx committed Nov 3, 2024
1 parent 34c11c0 commit d16ff78
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 137 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ max_line_length = 240
ij_continuation_indent_size = 2
ij_python_blank_line_at_file_end = true

[{*.yml_sample,*.yaml_sample,*.yml,*.yaml}]
[{*.yml_sample,*.yaml_sample,*.yml,*.yaml,*.toml}]
indent_size = 2
26 changes: 13 additions & 13 deletions openhab/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(
password: typing.Optional[str] = None,
http_auth: typing.Optional[httpx.Auth] = None,
timeout: typing.Optional[float] = None,
oauth2_config: typing.Optional[typing.Dict[str, typing.Any]] = None,
oauth2_config: typing.Optional[dict[str, typing.Any]] = None,
) -> None:
"""Class constructor.
Expand Down Expand Up @@ -136,7 +136,7 @@ def _check_req_return(req: httpx.Response) -> None:
if not 200 <= req.status_code < 300:
req.raise_for_status()

def req_get(self, uri_path: str, params: typing.Optional[typing.Union[typing.Dict[str, typing.Any], list, tuple]] = None) -> typing.Any:
def req_get(self, uri_path: str, params: typing.Optional[typing.Union[dict[str, typing.Any], list, tuple]] = None) -> typing.Any:
"""Helper method for initiating a HTTP GET request.
Besides doing the actual request, it also checks the return value and returns the resulting decoded
Expand All @@ -155,7 +155,7 @@ def req_get(self, uri_path: str, params: typing.Optional[typing.Union[typing.Dic
def req_post(
self,
uri_path: str,
data: typing.Optional[typing.Union[str, bytes, typing.Mapping[str, typing.Any], typing.Iterable[typing.Tuple[str, typing.Optional[str]]]]] = None,
data: typing.Optional[typing.Union[str, bytes, typing.Mapping[str, typing.Any], typing.Iterable[tuple[str, typing.Optional[str]]]]] = None,
) -> None:
"""Helper method for initiating a HTTP POST request.
Expand Down Expand Up @@ -207,7 +207,7 @@ def req_put(
self._check_req_return(r)

# fetch all items
def fetch_all_items(self) -> typing.Dict[str, openhab.items.Item]:
def fetch_all_items(self) -> dict[str, openhab.items.Item]:
"""Returns all items defined in openHAB.
Returns:
Expand Down Expand Up @@ -317,7 +317,7 @@ def logout(self) -> bool:

return res.status_code == 200

def _oauth2_token_updater(self, token: typing.Dict[str, typing.Any], refresh_token: typing.Any = None, access_token: typing.Any = None) -> None:
def _oauth2_token_updater(self, token: dict[str, typing.Any], refresh_token: typing.Any = None, access_token: typing.Any = None) -> None:
if self.oauth2_config is None:
raise ValueError('OAuth2 configuration is not set; invalid action!')

Expand All @@ -329,15 +329,15 @@ def _oauth2_token_updater(self, token: typing.Dict[str, typing.Any], refresh_tok
def create_or_update_item(
self,
name: str,
_type: typing.Union[str, typing.Type[openhab.items.Item]],
_type: typing.Union[str, type[openhab.items.Item]],
quantity_type: typing.Optional[str] = None,
label: typing.Optional[str] = None,
category: typing.Optional[str] = None,
tags: typing.Optional[typing.List[str]] = None,
group_names: typing.Optional[typing.List[str]] = None,
group_type: typing.Optional[typing.Union[str, typing.Type[openhab.items.Item]]] = None,
tags: typing.Optional[list[str]] = None,
group_names: typing.Optional[list[str]] = None,
group_type: typing.Optional[typing.Union[str, type[openhab.items.Item]]] = None,
function_name: typing.Optional[str] = None,
function_params: typing.Optional[typing.List[str]] = None,
function_params: typing.Optional[list[str]] = None,
) -> None:
"""Creates a new item in openHAB if there is no item with name 'name' yet.
Expand All @@ -359,7 +359,7 @@ def create_or_update_item(
Can be one of ['EQUALITY', 'AND', 'OR', 'NAND', 'NOR', 'AVG', 'SUM', 'MAX', 'MIN', 'COUNT', 'LATEST', 'EARLIEST']
function_params: Optional list of function params (no documentation found), depending on function name.
"""
paramdict: typing.Dict[str, typing.Union[str, typing.List[str], typing.Dict[str, typing.Union[str, typing.List[str]]]]] = {}
paramdict: dict[str, typing.Union[str, list[str], dict[str, typing.Union[str, list[str]]]]] = {}

if isinstance(_type, type):
if issubclass(_type, openhab.items.Item):
Expand Down Expand Up @@ -425,7 +425,7 @@ def get_item_persistence(
page: int = 0,
page_length: int = 0,
boundary: bool = False,
) -> typing.Iterator[typing.Dict[str, typing.Union[str, int]]]:
) -> typing.Iterator[dict[str, typing.Union[str, int]]]:
"""Method for fetching persistence data for a given item.
Args:
Expand All @@ -443,7 +443,7 @@ def get_item_persistence(
"state": "23"
}
"""
params: typing.Dict[str, typing.Any] = {
params: dict[str, typing.Any] = {
'boundary': str(boundary).lower(),
'page': page,
'pagelength': page_length,
Expand Down
20 changes: 9 additions & 11 deletions openhab/command_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CommandType(metaclass=abc.ABCMeta):
"""Base command type class."""

TYPENAME = ''
SUPPORTED_TYPENAMES: typing.List[str] = []
SUPPORTED_TYPENAMES: list[str] = []
UNDEF = 'UNDEF'
NULL = 'NULL'
UNDEFINED_STATES = [UNDEF, NULL]
Expand All @@ -48,8 +48,8 @@ def is_undefined(cls, value: typing.Any) -> bool:
def get_type_for(
cls,
typename: str,
parent_cls: typing.Optional[typing.Type['CommandType']] = None,
) -> typing.Union[typing.Type['CommandType'], None]:
parent_cls: typing.Optional[type['CommandType']] = None,
) -> typing.Union[type['CommandType'], None]:
"""Get a class type for a given typename."""
if parent_cls is None:
parent_cls = CommandType
Expand Down Expand Up @@ -230,7 +230,7 @@ class ColorType(CommandType):
SUPPORTED_TYPENAMES = [TYPENAME]

@classmethod
def parse(cls, value: str) -> typing.Optional[typing.Tuple[float, float, float]]:
def parse(cls, value: str) -> typing.Optional[tuple[float, float, float]]:
"""Parse a given value."""
if value in ColorType.UNDEFINED_STATES:
return None
Expand All @@ -251,7 +251,7 @@ def parse(cls, value: str) -> typing.Optional[typing.Tuple[float, float, float]]
return h, s, b

@classmethod
def validate(cls, value: typing.Union[str, typing.Tuple[float, float, float]]) -> None:
def validate(cls, value: typing.Union[str, tuple[float, float, float]]) -> None:
"""Value validation method.
Valid values are in format H,S,B.
Expand Down Expand Up @@ -283,7 +283,7 @@ class DecimalType(CommandType):
SUPPORTED_TYPENAMES = [TYPENAME, 'Quantity']

@classmethod
def parse(cls, value: str) -> typing.Union[None, typing.Tuple[typing.Union[int, float], str]]:
def parse(cls, value: str) -> typing.Union[None, tuple[typing.Union[int, float], str]]:
"""Parse a given value."""
if value in DecimalType.UNDEFINED_STATES:
return None
Expand All @@ -306,7 +306,7 @@ def parse(cls, value: str) -> typing.Union[None, typing.Tuple[typing.Union[int,
raise ValueError

@classmethod
def validate(cls, value: typing.Union[float, typing.Tuple[float, str], str]) -> None:
def validate(cls, value: typing.Union[float, tuple[float, str], str]) -> None:
"""Value validation method.
Valid values are any of data_type:
Expand Down Expand Up @@ -611,7 +611,7 @@ class PointType(CommandType):
SUPPORTED_TYPENAMES = [TYPENAME]

@classmethod
def parse(cls, value: str) -> typing.Optional[typing.Tuple[float, float, float]]:
def parse(cls, value: str) -> typing.Optional[tuple[float, float, float]]:
"""Parse a given value."""
if value in PercentType.UNDEFINED_STATES:
return None
Expand All @@ -630,9 +630,7 @@ def parse(cls, value: str) -> typing.Optional[typing.Tuple[float, float, float]]
return latitude, longitude, altitude

@classmethod
def validate(
cls, value: typing.Optional[typing.Union[str, typing.Tuple[typing.Union[float, int], typing.Union[float, int], typing.Union[float, int]]]]
) -> None:
def validate(cls, value: typing.Optional[typing.Union[str, tuple[typing.Union[float, int], typing.Union[float, int], typing.Union[float, int]]]]) -> None:
"""Value validation method.
A valid PointType is a tuple of three decimal values representing:
Expand Down
4 changes: 2 additions & 2 deletions openhab/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Oauth2User(pydantic.BaseModel):
"""Nested user structure within an oauth2 token."""

name: str
roles: typing.List[str]
roles: list[str]


class Oauth2Token(pydantic.BaseModel):
Expand All @@ -62,7 +62,7 @@ class Oauth2Token(pydantic.BaseModel):
expires_in: int
expires_at: float = time.time() - 10
refresh_token: str
scope: typing.Union[str, typing.List[str]] = 'admin'
scope: typing.Union[str, list[str]] = 'admin'
token_type: str
user: Oauth2User

Expand Down
40 changes: 20 additions & 20 deletions openhab/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
class Item:
"""Base item class."""

types: typing.Sequence[typing.Type[openhab.command_types.CommandType]] = []
state_types: typing.Sequence[typing.Type[openhab.command_types.CommandType]] = []
command_event_types: typing.Sequence[typing.Type[openhab.command_types.CommandType]] = []
state_event_types: typing.Sequence[typing.Type[openhab.command_types.CommandType]] = []
state_changed_event_types: typing.Sequence[typing.Type[openhab.command_types.CommandType]] = []
types: typing.Sequence[type[openhab.command_types.CommandType]] = []
state_types: typing.Sequence[type[openhab.command_types.CommandType]] = []
command_event_types: typing.Sequence[type[openhab.command_types.CommandType]] = []
state_event_types: typing.Sequence[type[openhab.command_types.CommandType]] = []
state_changed_event_types: typing.Sequence[type[openhab.command_types.CommandType]] = []

TYPENAME = 'unknown'

Expand Down Expand Up @@ -144,7 +144,7 @@ def unit_of_measure(self) -> str:
return self._unitOfMeasure

@property
def members(self) -> typing.Dict[str, typing.Any]:
def members(self) -> dict[str, typing.Any]:
"""If item is a type of Group, it will return all member items for this group.
For none group item empty dictionary will be returned.
Expand All @@ -155,7 +155,7 @@ def members(self) -> typing.Dict[str, typing.Any]:
"""
return self._members

def _validate_value(self, value: typing.Union[str, typing.Type[openhab.command_types.CommandType]]) -> None:
def _validate_value(self, value: typing.Union[str, type[openhab.command_types.CommandType]]) -> None:
"""Private method for verifying the new value before modifying the state of the item."""
if self.type_ == 'String':
if not isinstance(value, (str, bytes)):
Expand All @@ -176,7 +176,7 @@ def _validate_value(self, value: typing.Union[str, typing.Type[openhab.command_t
else:
raise ValueError

def _parse_rest(self, value: str) -> typing.Tuple[str, str]:
def _parse_rest(self, value: str) -> tuple[str, str]:
"""Parse a REST result into a native object."""
return value, ''

Expand Down Expand Up @@ -289,7 +289,7 @@ def persistence(
page: int = 0,
page_length: int = 0,
boundary: bool = False,
) -> typing.Iterator[typing.Dict[str, typing.Union[str, int]]]:
) -> typing.Iterator[dict[str, typing.Union[str, int]]]:
"""Method for fetching persistence data for a given item.
Args:
Expand Down Expand Up @@ -321,8 +321,8 @@ class GroupItem(Item):
"""String item type."""

TYPENAME = 'Group'
types: typing.List[typing.Type[openhab.command_types.CommandType]] = []
state_types: typing.List[typing.Type[openhab.command_types.CommandType]] = []
types: list[type[openhab.command_types.CommandType]] = []
state_types: list[type[openhab.command_types.CommandType]] = []


class StringItem(Item):
Expand Down Expand Up @@ -382,7 +382,7 @@ def __ne__(self, other: object) -> bool:

return not self.__eq__(other)

def _parse_rest(self, value: str) -> typing.Tuple[datetime.datetime, str]: # type: ignore[override]
def _parse_rest(self, value: str) -> tuple[datetime.datetime, str]: # type: ignore[override]
"""Parse a REST result into a native object.
Args:
Expand Down Expand Up @@ -470,7 +470,7 @@ class NumberItem(Item):
types = [openhab.command_types.DecimalType]
state_types = types

def _parse_rest(self, value: str) -> typing.Tuple[typing.Union[float, None], str]: # type: ignore[override]
def _parse_rest(self, value: str) -> tuple[typing.Union[float, None], str]: # type: ignore[override]
"""Parse a REST result into a native object.
Args:
Expand Down Expand Up @@ -499,7 +499,7 @@ def _parse_rest(self, value: str) -> typing.Tuple[typing.Union[float, None], str

raise ValueError(f'{self.__class__}: unable to parse value "{value}"')

def _rest_format(self, value: typing.Union[float, typing.Tuple[float, str], str]) -> typing.Union[str, bytes]:
def _rest_format(self, value: typing.Union[float, tuple[float, str], str]) -> typing.Union[str, bytes]:
"""Format a value before submitting to openHAB.
Args:
Expand Down Expand Up @@ -545,7 +545,7 @@ class DimmerItem(Item):
types = [openhab.command_types.OnOffType, openhab.command_types.PercentType, openhab.command_types.IncreaseDecreaseType]
state_types = [openhab.command_types.PercentType]

def _parse_rest(self, value: str) -> typing.Tuple[float, str]: # type: ignore[override]
def _parse_rest(self, value: str) -> tuple[float, str]: # type: ignore[override]
"""Parse a REST result into a native object.
Args:
Expand Down Expand Up @@ -595,7 +595,7 @@ class ColorItem(DimmerItem):
types = [openhab.command_types.OnOffType, openhab.command_types.PercentType, openhab.command_types.IncreaseDecreaseType, openhab.command_types.ColorType]
state_types = [openhab.command_types.ColorType]

def _parse_rest(self, value: str) -> typing.Tuple[typing.Optional[typing.Tuple[float, float, float]], str]: # type: ignore[override]
def _parse_rest(self, value: str) -> tuple[typing.Optional[tuple[float, float, float]], str]: # type: ignore[override]
"""Parse a REST result into a native object.
Args:
Expand All @@ -608,7 +608,7 @@ def _parse_rest(self, value: str) -> typing.Tuple[typing.Optional[typing.Tuple[f
result = openhab.command_types.ColorType.parse(value)
return result, ''

def _rest_format(self, value: typing.Union[typing.Tuple[int, int, float], str, int]) -> str:
def _rest_format(self, value: typing.Union[tuple[int, int, float], str, int]) -> str:
"""Format a value before submitting to openHAB.
Args:
Expand All @@ -634,7 +634,7 @@ class RollershutterItem(Item):
types = [openhab.command_types.UpDownType, openhab.command_types.PercentType, openhab.command_types.StopMoveType]
state_types = [openhab.command_types.PercentType]

def _parse_rest(self, value: str) -> typing.Tuple[int, str]: # type: ignore[override]
def _parse_rest(self, value: str) -> tuple[int, str]: # type: ignore[override]
"""Parse a REST result into a native object.
Args:
Expand Down Expand Up @@ -680,7 +680,7 @@ class LocationItem(Item):
types = [openhab.command_types.PointType]
state_types = [openhab.command_types.PointType]

def _parse_rest(self, value: str) -> typing.Tuple[typing.Optional[typing.Tuple[float, float, float]], str]: # type: ignore[override]
def _parse_rest(self, value: str) -> tuple[typing.Optional[tuple[float, float, float]], str]: # type: ignore[override]
"""Parse a REST result into a native object.
Args:
Expand All @@ -692,7 +692,7 @@ def _parse_rest(self, value: str) -> typing.Tuple[typing.Optional[typing.Tuple[f
"""
return openhab.command_types.PointType.parse(value), ''

def _rest_format(self, value: typing.Union[typing.Tuple[float, float, float], str]) -> str:
def _rest_format(self, value: typing.Union[tuple[float, float, float], str]) -> str:
"""Format a value before submitting to openHAB.
Args:
Expand Down
2 changes: 1 addition & 1 deletion openhab/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ def __init__(self, openhab_conn: 'openhab.client.OpenHAB') -> None:
self.openhab = openhab_conn
self.logger = logging.getLogger(__name__)

def get(self) -> typing.List[typing.Dict[str, typing.Any]]:
def get(self) -> list[dict[str, typing.Any]]:
"""Get all rules."""
return self.openhab.req_get('/rules')
Loading

0 comments on commit d16ff78

Please sign in to comment.