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
|
|
|
|
|
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-05-01 22:37:19 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo, Entity
|
2021-05-11 15:28:17 +00:00
|
|
|
from homeassistant.helpers.typing import StateType
|
2020-10-27 13:34:25 +00:00
|
|
|
|
2020-11-08 18:06:41 +00:00
|
|
|
from .const import (
|
|
|
|
SENSOR_TYPE_COUNT,
|
|
|
|
SENSOR_TYPE_SENSED,
|
|
|
|
SENSOR_TYPES,
|
|
|
|
SWITCH_TYPE_LATCH,
|
|
|
|
SWITCH_TYPE_PIO,
|
|
|
|
)
|
2021-05-11 15:28:17 +00:00
|
|
|
from .model import DeviceComponentDescription
|
2020-10-27 13:34:25 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-11-09 22:21:16 +00:00
|
|
|
class OneWireBaseEntity(Entity):
|
|
|
|
"""Implementation of a 1-Wire entity."""
|
2020-10-27 13:34:25 +00:00
|
|
|
|
2020-11-03 17:12:01 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2021-05-11 15:28:17 +00:00
|
|
|
name: str,
|
|
|
|
device_file: str,
|
2020-11-09 22:21:16 +00:00
|
|
|
entity_type: str,
|
2021-05-11 15:28:17 +00:00
|
|
|
entity_name: str,
|
|
|
|
device_info: DeviceInfo,
|
|
|
|
default_disabled: bool,
|
|
|
|
unique_id: str,
|
2021-05-20 13:58:17 +00:00
|
|
|
) -> None:
|
2020-11-09 22:21:16 +00:00
|
|
|
"""Initialize the entity."""
|
|
|
|
self._name = f"{name} {entity_name or entity_type.capitalize()}"
|
2020-10-27 13:34:25 +00:00
|
|
|
self._device_file = device_file
|
2020-11-09 22:21:16 +00:00
|
|
|
self._entity_type = entity_type
|
|
|
|
self._device_class = SENSOR_TYPES[entity_type][1]
|
|
|
|
self._unit_of_measurement = SENSOR_TYPES[entity_type][0]
|
2020-10-27 13:34:25 +00:00
|
|
|
self._device_info = device_info
|
2021-05-11 15:28:17 +00:00
|
|
|
self._state: StateType = None
|
|
|
|
self._value_raw: float | None = None
|
2020-11-04 09:08:50 +00:00
|
|
|
self._default_disabled = default_disabled
|
2021-05-11 15:28:17 +00:00
|
|
|
self._unique_id = unique_id
|
2020-10-27 13:34:25 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-18 12:21:46 +00:00
|
|
|
def name(self) -> str | None:
|
2020-11-09 22:21:16 +00:00
|
|
|
"""Return the name of the entity."""
|
2020-10-27 13:34:25 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
2021-03-18 12:21:46 +00:00
|
|
|
def device_class(self) -> str | None:
|
2020-10-27 13:34:25 +00:00
|
|
|
"""Return the class of this device."""
|
|
|
|
return self._device_class
|
|
|
|
|
|
|
|
@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."""
|
2020-10-27 13:34:25 +00:00
|
|
|
return {"device_file": self._device_file, "raw_value": self._value_raw}
|
|
|
|
|
|
|
|
@property
|
2021-03-18 12:21:46 +00:00
|
|
|
def unique_id(self) -> str | None:
|
2020-10-27 13:34:25 +00:00
|
|
|
"""Return a unique ID."""
|
2020-12-02 14:11:20 +00:00
|
|
|
return self._unique_id
|
2020-10-27 13:34:25 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-01 22:37:19 +00:00
|
|
|
def device_info(self) -> DeviceInfo | None:
|
2020-10-27 13:34:25 +00:00
|
|
|
"""Return device specific attributes."""
|
|
|
|
return self._device_info
|
|
|
|
|
2020-11-04 09:08:50 +00:00
|
|
|
@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
|
|
|
|
|
2020-10-27 13:34:25 +00:00
|
|
|
|
2020-11-09 22:21:16 +00:00
|
|
|
class OneWireProxyEntity(OneWireBaseEntity):
|
|
|
|
"""Implementation of a 1-Wire entity connected through owserver."""
|
2020-10-27 13:34:25 +00:00
|
|
|
|
2020-11-03 17:12:01 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2020-12-02 14:11:20 +00:00
|
|
|
device_id: str,
|
|
|
|
device_name: str,
|
2021-05-11 05:11:51 +00:00
|
|
|
device_info: DeviceInfo,
|
2020-12-02 14:11:20 +00:00
|
|
|
entity_path: str,
|
2021-05-11 15:28:17 +00:00
|
|
|
entity_specs: DeviceComponentDescription,
|
2020-11-03 17:12:01 +00:00
|
|
|
owproxy: protocol._Proxy,
|
2021-05-20 13:58:17 +00:00
|
|
|
) -> None:
|
2020-10-27 13:34:25 +00:00
|
|
|
"""Initialize the sensor."""
|
2020-11-04 09:08:50 +00:00
|
|
|
super().__init__(
|
2020-12-02 14:11:20 +00:00
|
|
|
name=device_name,
|
|
|
|
device_file=entity_path,
|
|
|
|
entity_type=entity_specs["type"],
|
|
|
|
entity_name=entity_specs["name"],
|
|
|
|
device_info=device_info,
|
|
|
|
default_disabled=entity_specs.get("default_disabled", False),
|
|
|
|
unique_id=f"/{device_id}/{entity_specs['path']}",
|
2020-11-04 09:08:50 +00:00
|
|
|
)
|
2020-10-27 13:34:25 +00:00
|
|
|
self._owproxy = owproxy
|
|
|
|
|
2021-05-11 15:28:17 +00:00
|
|
|
def _read_value_ownet(self) -> str:
|
2020-10-27 13:34:25 +00:00
|
|
|
"""Read a value from the owserver."""
|
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
|
|
|
|
2021-05-11 15:28:17 +00:00
|
|
|
def _write_value_ownet(self, value: bytes) -> None:
|
2020-11-08 18:06:41 +00:00
|
|
|
"""Write a value to the owserver."""
|
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:
|
2020-11-03 08:07:22 +00:00
|
|
|
self._value_raw = float(self._read_value_ownet())
|
2020-10-27 13:34:25 +00:00
|
|
|
except protocol.Error as exc:
|
|
|
|
_LOGGER.error("Owserver failure in read(), got: %s", exc)
|
2021-05-11 15:28:17 +00:00
|
|
|
self._state = None
|
2020-11-03 08:07:22 +00:00
|
|
|
else:
|
2020-11-09 22:21:16 +00:00
|
|
|
if self._entity_type == SENSOR_TYPE_COUNT:
|
2021-05-11 15:28:17 +00:00
|
|
|
self._state = int(self._value_raw)
|
2020-11-09 22:21:16 +00:00
|
|
|
elif self._entity_type in [
|
2020-11-08 18:06:41 +00:00
|
|
|
SENSOR_TYPE_SENSED,
|
|
|
|
SWITCH_TYPE_LATCH,
|
|
|
|
SWITCH_TYPE_PIO,
|
|
|
|
]:
|
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)
|