Use EntityDescription - onewire (#55003)
This commit is contained in:
parent
2796f65453
commit
ccaf0d5c75
10 changed files with 836 additions and 721 deletions
|
@ -1,22 +1,24 @@
|
|||
"""Support for 1-Wire entities."""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from pyownet import protocol
|
||||
|
||||
from homeassistant.helpers.entity import DeviceInfo, Entity
|
||||
from homeassistant.helpers.entity import DeviceInfo, Entity, EntityDescription
|
||||
from homeassistant.helpers.typing import StateType
|
||||
|
||||
from .const import (
|
||||
SENSOR_TYPE_COUNT,
|
||||
SENSOR_TYPE_SENSED,
|
||||
SENSOR_TYPES,
|
||||
SWITCH_TYPE_LATCH,
|
||||
SWITCH_TYPE_PIO,
|
||||
)
|
||||
from .model import DeviceComponentDescription
|
||||
from .const import READ_MODE_BOOL, READ_MODE_INT
|
||||
|
||||
|
||||
@dataclass
|
||||
class OneWireEntityDescription(EntityDescription):
|
||||
"""Class describing OneWire entities."""
|
||||
|
||||
read_mode: str | None = None
|
||||
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -24,57 +26,32 @@ _LOGGER = logging.getLogger(__name__)
|
|||
class OneWireBaseEntity(Entity):
|
||||
"""Implementation of a 1-Wire entity."""
|
||||
|
||||
entity_description: OneWireEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
device_file: str,
|
||||
entity_type: str,
|
||||
entity_name: str,
|
||||
description: OneWireEntityDescription,
|
||||
device_id: str,
|
||||
device_info: DeviceInfo,
|
||||
default_disabled: bool,
|
||||
unique_id: str,
|
||||
device_file: str,
|
||||
name: str,
|
||||
) -> None:
|
||||
"""Initialize the entity."""
|
||||
self._name = f"{name} {entity_name or entity_type.capitalize()}"
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = f"/{device_id}/{description.key}"
|
||||
self._attr_device_info = device_info
|
||||
self._attr_name = name
|
||||
self._device_file = device_file
|
||||
self._entity_type = entity_type
|
||||
self._device_class = SENSOR_TYPES[entity_type][1]
|
||||
self._unit_of_measurement = SENSOR_TYPES[entity_type][0]
|
||||
self._device_info = device_info
|
||||
self._state: StateType = None
|
||||
self._value_raw: float | None = None
|
||||
self._default_disabled = default_disabled
|
||||
self._unique_id = unique_id
|
||||
|
||||
@property
|
||||
def name(self) -> str | None:
|
||||
"""Return the name of the entity."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def device_class(self) -> str | None:
|
||||
"""Return the class of this device."""
|
||||
return self._device_class
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self) -> dict[str, Any] | None:
|
||||
"""Return the state attributes of the entity."""
|
||||
return {"device_file": self._device_file, "raw_value": self._value_raw}
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str | None:
|
||||
"""Return a unique ID."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo | None:
|
||||
"""Return device specific attributes."""
|
||||
return self._device_info
|
||||
|
||||
@property
|
||||
def entity_registry_enabled_default(self) -> bool:
|
||||
"""Return if the entity should be enabled when first added to the entity registry."""
|
||||
return not self._default_disabled
|
||||
return {
|
||||
"device_file": self._device_file,
|
||||
"raw_value": self._value_raw,
|
||||
}
|
||||
|
||||
|
||||
class OneWireProxyEntity(OneWireBaseEntity):
|
||||
|
@ -82,22 +59,20 @@ class OneWireProxyEntity(OneWireBaseEntity):
|
|||
|
||||
def __init__(
|
||||
self,
|
||||
description: OneWireEntityDescription,
|
||||
device_id: str,
|
||||
device_name: str,
|
||||
device_info: DeviceInfo,
|
||||
entity_path: str,
|
||||
entity_specs: DeviceComponentDescription,
|
||||
device_file: str,
|
||||
name: str,
|
||||
owproxy: protocol._Proxy,
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(
|
||||
name=device_name,
|
||||
device_file=entity_path,
|
||||
entity_type=entity_specs["type"],
|
||||
entity_name=entity_specs["name"],
|
||||
description=description,
|
||||
device_id=device_id,
|
||||
device_info=device_info,
|
||||
default_disabled=entity_specs.get("default_disabled", False),
|
||||
unique_id=f"/{device_id}/{entity_specs['path']}",
|
||||
device_file=device_file,
|
||||
name=name,
|
||||
)
|
||||
self._owproxy = owproxy
|
||||
|
||||
|
@ -118,13 +93,9 @@ class OneWireProxyEntity(OneWireBaseEntity):
|
|||
_LOGGER.error("Owserver failure in read(), got: %s", exc)
|
||||
self._state = None
|
||||
else:
|
||||
if self._entity_type == SENSOR_TYPE_COUNT:
|
||||
if self.entity_description.read_mode == READ_MODE_INT:
|
||||
self._state = int(self._value_raw)
|
||||
elif self._entity_type in [
|
||||
SENSOR_TYPE_SENSED,
|
||||
SWITCH_TYPE_LATCH,
|
||||
SWITCH_TYPE_PIO,
|
||||
]:
|
||||
elif self.entity_description.read_mode == READ_MODE_BOOL:
|
||||
self._state = int(self._value_raw) == 1
|
||||
else:
|
||||
self._state = round(self._value_raw, 1)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue