Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ repos:
- id: prettier
args: ["--tab-width", "2"]
- repo: https://github.com/asottile/pyupgrade
rev: v3.21.0
rev: v3.22.0
hooks:
- id: pyupgrade
args: [--py37-plus]
Expand Down
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# import os
# import sys
# sys.path.insert(0, os.path.abspath('.'))
from typing import Any, List
from typing import Any

# -- Project information -----------------------------------------------------

Expand All @@ -40,7 +40,7 @@
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns: List[Any] = []
exclude_patterns: list[Any] = []


# -- Options for HTML output -------------------------------------------------
Expand Down
2 changes: 0 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python

# This is a shim to allow GitHub to detect the package, build is done with poetry
# Taken from https://github.com/Textualize/rich

Expand Down
2 changes: 1 addition & 1 deletion src/sensor_state_data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
__all__ = [
"BaseDeviceClass",
"BaseValue",
"BinarySensorValue",
"BinarySensorDescription",
"BinarySensorDeviceClass",
"BinarySensorValue",
"DeviceClass",
"DeviceKey",
"Event",
Expand Down
6 changes: 3 additions & 3 deletions src/sensor_state_data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def binary_descriptions(
def primary_device_id(self) -> str | None:
"""Return the primary device id."""
if self._device_id_to_type:
return list(self._device_id_to_type)[0]
return next(iter(self._device_id_to_type))
return None

@property
Expand Down Expand Up @@ -218,7 +218,7 @@ def update_predefined_binary_sensor(
def update_predefined_sensor(
self,
base_description: BaseSensorDescription,
native_value: None | str | int | float | date | datetime | Decimal,
native_value: None | str | float | date | datetime | Decimal,
key: str | None = None,
name: str | None = None,
device_id: str | None = None,
Expand Down Expand Up @@ -268,7 +268,7 @@ def update_sensor(
self,
key: str,
native_unit_of_measurement: Units | None,
native_value: None | str | int | float | date | datetime | Decimal,
native_value: None | str | float | date | datetime | Decimal,
device_class: SensorDeviceClass | None = None,
name: str | None = None,
device_id: str | None = None,
Expand Down
9 changes: 4 additions & 5 deletions src/sensor_state_data/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
from __future__ import annotations

from enum import Enum
from typing import Any, TypeVar
from typing import TYPE_CHECKING, Any

_StrEnumT = TypeVar("_StrEnumT", bound="StrEnum")
if TYPE_CHECKING:
from typing import Self


class StrEnum(str, Enum):
"""Partial backport of Python 3.11's StrEnum for our basic use cases."""

def __new__(
cls: type[_StrEnumT], value: str, *args: Any, **kwargs: Any
) -> _StrEnumT:
def __new__(cls, value: str, *args: Any, **kwargs: Any) -> Self:
"""Create a new StrEnum instance."""
if not isinstance(value, str):
raise TypeError(f"{value!r} is not a string")
Expand Down
16 changes: 14 additions & 2 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,25 @@


def test_sensor_device_class():
assert str(DeviceClass.TEMPERATURE) == "temperature"
assert DeviceClass.TEMPERATURE == "temperature"
assert str(DeviceClass.TEMPERATURE) == "temperature" # type: ignore[unreachable]


def test_binary_sensor_device_class():
assert str(BinarySensorDeviceClass.POWER) == "power"
assert BinarySensorDeviceClass.POWER == "power"
assert str(BinarySensorDeviceClass.POWER) == "power" # type: ignore[unreachable]


def test_primary_device_id():
class MySensorData(SensorData):
def _start_update(self, data: Any) -> None:
self.set_device_type("Primary", device_id="a")
self.set_device_type("Secondary", device_id="b")

sensor_data = MySensorData()
assert sensor_data.primary_device_id is None
sensor_data.update(None)
assert sensor_data.primary_device_id == "a"


def test_no_precision():
Expand Down
Loading