2020-10-27 13:34:25 +00:00
|
|
|
"""Support for 1-Wire entities."""
|
2021-03-18 12:21:46 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-08-24 08:37:59 +00:00
|
|
|
from dataclasses import dataclass
|
2020-10-27 13:34:25 +00:00
|
|
|
import logging
|
2021-03-18 12:21:46 +00:00
|
|
|
from typing import Any
|
2020-10-27 13:34:25 +00:00
|
|
|
|
|
|
|
from pyownet import protocol
|
|
|
|
|
2021-08-24 08:37:59 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo, Entity, EntityDescription
|
2021-05-11 15:28:17 +00:00
|
|
|
from homeassistant.helpers.typing import StateType
|
2020-10-27 13:34:25 +00:00
|
|
|
|
2021-08-24 08:37:59 +00:00
|
|
|
from .const import READ_MODE_BOOL, READ_MODE_INT
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class OneWireEntityDescription(EntityDescription):
|
|
|
|
"""Class describing OneWire entities."""
|
|
|
|
|
|
|
|
read_mode: str | None = None
|
|
|
|
|
2020-10-27 13:34:25 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-05-09 11:16:23 +00:00
|
|
|
class OneWireEntity(Entity):
|
2020-11-09 22:21:16 +00:00
|
|
|
"""Implementation of a 1-Wire entity."""
|
2020-10-27 13:34:25 +00:00
|
|
|
|
2021-08-24 08:37:59 +00:00
|
|
|
entity_description: OneWireEntityDescription
|
|
|
|
|
2020-11-03 17:12:01 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2021-08-24 08:37:59 +00:00
|
|
|
description: OneWireEntityDescription,
|
|
|
|
device_id: str,
|
2021-05-11 15:28:17 +00:00
|
|
|
device_info: DeviceInfo,
|
2021-08-24 08:37:59 +00:00
|
|
|
device_file: str,
|
|
|
|
name: str,
|
2022-05-09 11:16:23 +00:00
|
|
|
owproxy: protocol._Proxy,
|
2021-05-20 13:58:17 +00:00
|
|
|
) -> None:
|
2020-11-09 22:21:16 +00:00
|
|
|
"""Initialize the entity."""
|
2021-08-24 08:37:59 +00:00
|
|
|
self.entity_description = description
|
|
|
|
self._attr_unique_id = f"/{device_id}/{description.key}"
|
|
|
|
self._attr_device_info = device_info
|
|
|
|
self._attr_name = name
|
2020-10-27 13:34:25 +00:00
|
|
|
self._device_file = device_file
|
2021-05-11 15:28:17 +00:00
|
|
|
self._state: StateType = None
|
|
|
|
self._value_raw: float | None = None
|
2022-05-09 11:16:23 +00:00
|
|
|
self._owproxy = owproxy
|
2020-10-27 13:34:25 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-18 12:21:46 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any] | None:
|
2020-11-09 22:21:16 +00:00
|
|
|
"""Return the state attributes of the entity."""
|
2021-08-24 08:37:59 +00:00
|
|
|
return {
|
|
|
|
"device_file": self._device_file,
|
|
|
|
"raw_value": self._value_raw,
|
|
|
|
}
|
2020-11-04 09:08:50 +00:00
|
|
|
|
2022-05-09 11:16:23 +00:00
|
|
|
def _read_value(self) -> str:
|
|
|
|
"""Read a value from the server."""
|
2021-05-11 15:28:17 +00:00
|
|
|
read_bytes: bytes = self._owproxy.read(self._device_file)
|
|
|
|
return read_bytes.decode().lstrip()
|
2020-10-27 13:34:25 +00:00
|
|
|
|
2022-05-09 11:16:23 +00:00
|
|
|
def _write_value(self, value: bytes) -> None:
|
|
|
|
"""Write a value to the server."""
|
2021-05-11 15:28:17 +00:00
|
|
|
self._owproxy.write(self._device_file, value)
|
2020-11-08 18:06:41 +00:00
|
|
|
|
2021-05-11 15:28:17 +00:00
|
|
|
def update(self) -> None:
|
2020-10-27 13:34:25 +00:00
|
|
|
"""Get the latest data from the device."""
|
|
|
|
try:
|
2022-05-09 11:16:23 +00:00
|
|
|
self._value_raw = float(self._read_value())
|
2020-10-27 13:34:25 +00:00
|
|
|
except protocol.Error as exc:
|
2022-05-09 11:16:23 +00:00
|
|
|
_LOGGER.error("Failure to read server value, got: %s", exc)
|
2021-05-11 15:28:17 +00:00
|
|
|
self._state = None
|
2020-11-03 08:07:22 +00:00
|
|
|
else:
|
2021-08-24 08:37:59 +00:00
|
|
|
if self.entity_description.read_mode == READ_MODE_INT:
|
2021-05-11 15:28:17 +00:00
|
|
|
self._state = int(self._value_raw)
|
2021-08-24 08:37:59 +00:00
|
|
|
elif self.entity_description.read_mode == READ_MODE_BOOL:
|
2021-05-11 15:28:17 +00:00
|
|
|
self._state = int(self._value_raw) == 1
|
2020-11-01 00:46:31 +00:00
|
|
|
else:
|
2021-05-11 15:28:17 +00:00
|
|
|
self._state = round(self._value_raw, 1)
|