Simplify boolean check in onewire (#145700)

This commit is contained in:
epenet 2025-05-27 13:30:44 +02:00 committed by GitHub
parent f295d72cd9
commit 12fdd7034a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 10 additions and 24 deletions

View File

@ -16,7 +16,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .const import DEVICE_KEYS_0_3, DEVICE_KEYS_0_7, DEVICE_KEYS_A_B, READ_MODE_BOOL
from .const import DEVICE_KEYS_0_3, DEVICE_KEYS_0_7, DEVICE_KEYS_A_B, READ_MODE_INT
from .entity import OneWireEntity, OneWireEntityDescription
from .onewirehub import (
SIGNAL_NEW_DEVICE_CONNECTED,
@ -37,13 +37,14 @@ class OneWireBinarySensorEntityDescription(
):
"""Class describing OneWire binary sensor entities."""
read_mode = READ_MODE_INT
DEVICE_BINARY_SENSORS: dict[str, tuple[OneWireBinarySensorEntityDescription, ...]] = {
"12": tuple(
OneWireBinarySensorEntityDescription(
key=f"sensed.{device_key}",
entity_registry_enabled_default=False,
read_mode=READ_MODE_BOOL,
translation_key="sensed_id",
translation_placeholders={"id": str(device_key)},
)
@ -53,7 +54,6 @@ DEVICE_BINARY_SENSORS: dict[str, tuple[OneWireBinarySensorEntityDescription, ...
OneWireBinarySensorEntityDescription(
key=f"sensed.{device_key}",
entity_registry_enabled_default=False,
read_mode=READ_MODE_BOOL,
translation_key="sensed_id",
translation_placeholders={"id": str(device_key)},
)
@ -63,7 +63,6 @@ DEVICE_BINARY_SENSORS: dict[str, tuple[OneWireBinarySensorEntityDescription, ...
OneWireBinarySensorEntityDescription(
key=f"sensed.{device_key}",
entity_registry_enabled_default=False,
read_mode=READ_MODE_BOOL,
translation_key="sensed_id",
translation_placeholders={"id": str(device_key)},
)
@ -78,7 +77,6 @@ HOBBYBOARD_EF: dict[str, tuple[OneWireBinarySensorEntityDescription, ...]] = {
OneWireBinarySensorEntityDescription(
key=f"hub/short.{device_key}",
entity_registry_enabled_default=False,
read_mode=READ_MODE_BOOL,
entity_category=EntityCategory.DIAGNOSTIC,
device_class=BinarySensorDeviceClass.PROBLEM,
translation_key="hub_short_id",
@ -162,4 +160,4 @@ class OneWireBinarySensorEntity(OneWireEntity, BinarySensorEntity):
"""Return true if sensor is on."""
if self._state is None:
return None
return bool(self._state)
return self._state == 1

View File

@ -51,6 +51,5 @@ MANUFACTURER_MAXIM = "Maxim Integrated"
MANUFACTURER_HOBBYBOARDS = "Hobby Boards"
MANUFACTURER_EDS = "Embedded Data Systems"
READ_MODE_BOOL = "bool"
READ_MODE_FLOAT = "float"
READ_MODE_INT = "int"

View File

@ -10,9 +10,8 @@ from pyownet import protocol
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import Entity, EntityDescription
from homeassistant.helpers.typing import StateType
from .const import READ_MODE_BOOL, READ_MODE_INT
from .const import READ_MODE_INT
@dataclass(frozen=True)
@ -45,7 +44,7 @@ class OneWireEntity(Entity):
self._attr_unique_id = f"/{device_id}/{description.key}"
self._attr_device_info = device_info
self._device_file = device_file
self._state: StateType = None
self._state: int | float | None = None
self._value_raw: float | None = None
self._owproxy = owproxy
@ -82,7 +81,5 @@ class OneWireEntity(Entity):
_LOGGER.debug("Fetching %s data recovered", self.name)
if self.entity_description.read_mode == READ_MODE_INT:
self._state = int(self._value_raw)
elif self.entity_description.read_mode == READ_MODE_BOOL:
self._state = int(self._value_raw) == 1
else:
self._state = self._value_raw

View File

@ -13,7 +13,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .const import DEVICE_KEYS_0_3, DEVICE_KEYS_0_7, DEVICE_KEYS_A_B, READ_MODE_BOOL
from .const import DEVICE_KEYS_0_3, DEVICE_KEYS_0_7, DEVICE_KEYS_A_B, READ_MODE_INT
from .entity import OneWireEntity, OneWireEntityDescription
from .onewirehub import (
SIGNAL_NEW_DEVICE_CONNECTED,
@ -32,13 +32,14 @@ SCAN_INTERVAL = timedelta(seconds=30)
class OneWireSwitchEntityDescription(OneWireEntityDescription, SwitchEntityDescription):
"""Class describing OneWire switch entities."""
read_mode = READ_MODE_INT
DEVICE_SWITCHES: dict[str, tuple[OneWireEntityDescription, ...]] = {
"05": (
OneWireSwitchEntityDescription(
key="PIO",
entity_registry_enabled_default=False,
read_mode=READ_MODE_BOOL,
translation_key="pio",
),
),
@ -47,7 +48,6 @@ DEVICE_SWITCHES: dict[str, tuple[OneWireEntityDescription, ...]] = {
OneWireSwitchEntityDescription(
key=f"PIO.{device_key}",
entity_registry_enabled_default=False,
read_mode=READ_MODE_BOOL,
translation_key="pio_id",
translation_placeholders={"id": str(device_key)},
)
@ -57,7 +57,6 @@ DEVICE_SWITCHES: dict[str, tuple[OneWireEntityDescription, ...]] = {
OneWireSwitchEntityDescription(
key=f"latch.{device_key}",
entity_registry_enabled_default=False,
read_mode=READ_MODE_BOOL,
translation_key="latch_id",
translation_placeholders={"id": str(device_key)},
)
@ -69,7 +68,6 @@ DEVICE_SWITCHES: dict[str, tuple[OneWireEntityDescription, ...]] = {
key="IAD",
entity_registry_enabled_default=False,
entity_category=EntityCategory.CONFIG,
read_mode=READ_MODE_BOOL,
translation_key="iad",
),
),
@ -78,7 +76,6 @@ DEVICE_SWITCHES: dict[str, tuple[OneWireEntityDescription, ...]] = {
OneWireSwitchEntityDescription(
key=f"PIO.{device_key}",
entity_registry_enabled_default=False,
read_mode=READ_MODE_BOOL,
translation_key="pio_id",
translation_placeholders={"id": str(device_key)},
)
@ -88,7 +85,6 @@ DEVICE_SWITCHES: dict[str, tuple[OneWireEntityDescription, ...]] = {
OneWireSwitchEntityDescription(
key=f"latch.{device_key}",
entity_registry_enabled_default=False,
read_mode=READ_MODE_BOOL,
translation_key="latch_id",
translation_placeholders={"id": str(device_key)},
)
@ -99,7 +95,6 @@ DEVICE_SWITCHES: dict[str, tuple[OneWireEntityDescription, ...]] = {
OneWireSwitchEntityDescription(
key=f"PIO.{device_key}",
entity_registry_enabled_default=False,
read_mode=READ_MODE_BOOL,
translation_key="pio_id",
translation_placeholders={"id": str(device_key)},
)
@ -115,7 +110,6 @@ HOBBYBOARD_EF: dict[str, tuple[OneWireEntityDescription, ...]] = {
OneWireSwitchEntityDescription(
key=f"hub/branch.{device_key}",
entity_registry_enabled_default=False,
read_mode=READ_MODE_BOOL,
entity_category=EntityCategory.CONFIG,
translation_key="hub_branch_id",
translation_placeholders={"id": str(device_key)},
@ -127,7 +121,6 @@ HOBBYBOARD_EF: dict[str, tuple[OneWireEntityDescription, ...]] = {
OneWireSwitchEntityDescription(
key=f"moisture/is_leaf.{device_key}",
entity_registry_enabled_default=False,
read_mode=READ_MODE_BOOL,
entity_category=EntityCategory.CONFIG,
translation_key="leaf_sensor_id",
translation_placeholders={"id": str(device_key)},
@ -138,7 +131,6 @@ HOBBYBOARD_EF: dict[str, tuple[OneWireEntityDescription, ...]] = {
OneWireSwitchEntityDescription(
key=f"moisture/is_moisture.{device_key}",
entity_registry_enabled_default=False,
read_mode=READ_MODE_BOOL,
entity_category=EntityCategory.CONFIG,
translation_key="moisture_sensor_id",
translation_placeholders={"id": str(device_key)},
@ -226,7 +218,7 @@ class OneWireSwitchEntity(OneWireEntity, SwitchEntity):
"""Return true if switch is on."""
if self._state is None:
return None
return bool(self._state)
return self._state == 1
def turn_on(self, **kwargs: Any) -> None:
"""Turn the entity on."""