Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Support for 1-Wire entities."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 import logging
7 from typing import Any
8 
9 from pyownet import protocol
10 
11 from homeassistant.helpers.device_registry import DeviceInfo
12 from homeassistant.helpers.entity import Entity, EntityDescription
13 from homeassistant.helpers.typing import StateType
14 
15 from .const import READ_MODE_BOOL, READ_MODE_INT
16 
17 
18 @dataclass(frozen=True)
20  """Class describing OneWire entities."""
21 
22  read_mode: str | None = None
23 
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 
29  """Implementation of a 1-Wire entity."""
30 
31  entity_description: OneWireEntityDescription
32  _attr_has_entity_name = True
33 
34  def __init__(
35  self,
36  description: OneWireEntityDescription,
37  device_id: str,
38  device_info: DeviceInfo,
39  device_file: str,
40  owproxy: protocol._Proxy,
41  ) -> None:
42  """Initialize the entity."""
43  self.entity_descriptionentity_description = description
44  self._last_update_success_last_update_success = True
45  self._attr_unique_id_attr_unique_id = f"/{device_id}/{description.key}"
46  self._attr_device_info_attr_device_info = device_info
47  self._device_file_device_file = device_file
48  self._state_state: StateType = None
49  self._value_raw_value_raw: float | None = None
50  self._owproxy_owproxy = owproxy
51 
52  @property
53  def extra_state_attributes(self) -> dict[str, Any] | None:
54  """Return the state attributes of the entity."""
55  return {
56  "device_file": self._device_file_device_file,
57  "raw_value": self._value_raw_value_raw,
58  }
59 
60  def _read_value(self) -> str:
61  """Read a value from the server."""
62  read_bytes: bytes = self._owproxy_owproxy.read(self._device_file_device_file)
63  return read_bytes.decode().lstrip()
64 
65  def _write_value(self, value: bytes) -> None:
66  """Write a value to the server."""
67  self._owproxy_owproxy.write(self._device_file_device_file, value)
68 
69  def update(self) -> None:
70  """Get the latest data from the device."""
71  try:
72  self._value_raw_value_raw = float(self._read_value_read_value())
73  except protocol.Error as exc:
74  if self._last_update_success_last_update_success:
75  _LOGGER.error("Error fetching %s data: %s", self.namename, exc)
76  self._last_update_success_last_update_success = False
77  self._state_state = None
78  else:
79  if not self._last_update_success_last_update_success:
80  self._last_update_success_last_update_success = True
81  _LOGGER.debug("Fetching %s data recovered", self.namename)
82  if self.entity_descriptionentity_description.read_mode == READ_MODE_INT:
83  self._state_state = int(self._value_raw_value_raw)
84  elif self.entity_descriptionentity_description.read_mode == READ_MODE_BOOL:
85  self._state_state = int(self._value_raw_value_raw) == 1
86  else:
87  self._state_state = round(self._value_raw_value_raw, 1)
dict[str, Any]|None extra_state_attributes(self)
Definition: entity.py:53
None __init__(self, OneWireEntityDescription description, str device_id, DeviceInfo device_info, str device_file, protocol._Proxy owproxy)
Definition: entity.py:41
str|UndefinedType|None name(self)
Definition: entity.py:738