2020-11-08 18:06:41 +00:00
|
|
|
"""Support for 1-Wire environment switches."""
|
2021-05-11 15:28:17 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-08-24 08:37:59 +00:00
|
|
|
from dataclasses import dataclass
|
2020-11-08 18:06:41 +00:00
|
|
|
import os
|
2022-05-09 11:16:23 +00:00
|
|
|
from typing import Any
|
2020-11-08 18:06:41 +00:00
|
|
|
|
2021-08-24 08:37:59 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
2021-05-11 15:28:17 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
2021-12-03 17:20:00 +00:00
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
2021-05-11 15:28:17 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-11-08 18:06:41 +00:00
|
|
|
|
2021-09-13 11:46:21 +00:00
|
|
|
from .const import (
|
2021-12-07 12:59:43 +00:00
|
|
|
DEVICE_KEYS_0_3,
|
2021-09-13 11:46:21 +00:00
|
|
|
DEVICE_KEYS_0_7,
|
|
|
|
DEVICE_KEYS_A_B,
|
|
|
|
DOMAIN,
|
|
|
|
READ_MODE_BOOL,
|
|
|
|
)
|
2022-05-09 11:16:23 +00:00
|
|
|
from .onewire_entities import OneWireEntity, OneWireEntityDescription
|
2020-11-08 18:06:41 +00:00
|
|
|
from .onewirehub import OneWireHub
|
|
|
|
|
2021-08-24 08:37:59 +00:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class OneWireSwitchEntityDescription(OneWireEntityDescription, SwitchEntityDescription):
|
|
|
|
"""Class describing OneWire switch entities."""
|
|
|
|
|
|
|
|
|
|
|
|
DEVICE_SWITCHES: dict[str, tuple[OneWireEntityDescription, ...]] = {
|
|
|
|
"05": (
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="PIO",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="PIO",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
),
|
2021-09-13 11:46:21 +00:00
|
|
|
"12": tuple(
|
|
|
|
[
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key=f"PIO.{id}",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name=f"PIO {id}",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
)
|
|
|
|
for id in DEVICE_KEYS_A_B
|
|
|
|
]
|
|
|
|
+ [
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key=f"latch.{id}",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name=f"Latch {id}",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
)
|
|
|
|
for id in DEVICE_KEYS_A_B
|
|
|
|
]
|
2021-08-24 08:37:59 +00:00
|
|
|
),
|
2021-11-14 16:30:03 +00:00
|
|
|
"26": (
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="IAD",
|
|
|
|
entity_registry_enabled_default=False,
|
2021-12-03 17:20:00 +00:00
|
|
|
entity_category=EntityCategory.CONFIG,
|
2021-11-14 16:30:03 +00:00
|
|
|
name="IAD",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
),
|
2021-09-13 11:46:21 +00:00
|
|
|
"29": tuple(
|
|
|
|
[
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key=f"PIO.{id}",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name=f"PIO {id}",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
)
|
|
|
|
for id in DEVICE_KEYS_0_7
|
|
|
|
]
|
|
|
|
+ [
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key=f"latch.{id}",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name=f"Latch {id}",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
)
|
|
|
|
for id in DEVICE_KEYS_0_7
|
|
|
|
]
|
2021-08-24 08:37:59 +00:00
|
|
|
),
|
2021-10-15 06:23:26 +00:00
|
|
|
"3A": tuple(
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key=f"PIO.{id}",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name=f"PIO {id}",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
)
|
|
|
|
for id in DEVICE_KEYS_A_B
|
|
|
|
),
|
2021-12-07 12:59:43 +00:00
|
|
|
"EF": (), # "HobbyBoard": special
|
2020-11-08 18:06:41 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 12:59:43 +00:00
|
|
|
# EF sensors are usually hobbyboards specialized sensors.
|
|
|
|
|
|
|
|
HOBBYBOARD_EF: dict[str, tuple[OneWireEntityDescription, ...]] = {
|
|
|
|
"HB_HUB": tuple(
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key=f"hub/branch.{id}",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name=f"Hub Branch {id} Enable",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
)
|
|
|
|
for id in DEVICE_KEYS_0_3
|
|
|
|
),
|
|
|
|
"HB_MOISTURE_METER": tuple(
|
|
|
|
[
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key=f"moisture/is_leaf.{id}",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name=f"Leaf Sensor {id} Enable",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
)
|
|
|
|
for id in DEVICE_KEYS_0_3
|
|
|
|
]
|
|
|
|
+ [
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key=f"moisture/is_moisture.{id}",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name=f"Moisture Sensor {id} Enable",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
)
|
|
|
|
for id in DEVICE_KEYS_0_3
|
|
|
|
]
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_sensor_types(
|
|
|
|
device_sub_type: str,
|
|
|
|
) -> dict[str, tuple[OneWireEntityDescription, ...]]:
|
|
|
|
"""Return the proper info array for the device type."""
|
|
|
|
if "HobbyBoard" in device_sub_type:
|
|
|
|
return HOBBYBOARD_EF
|
|
|
|
return DEVICE_SWITCHES
|
2020-11-08 18:06:41 +00:00
|
|
|
|
|
|
|
|
2021-05-11 15:28:17 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-11-08 18:06:41 +00:00
|
|
|
"""Set up 1-Wire platform."""
|
2022-05-31 10:26:15 +00:00
|
|
|
onewire_hub = hass.data[DOMAIN][config_entry.entry_id]
|
2020-11-08 18:06:41 +00:00
|
|
|
|
2022-05-31 10:26:15 +00:00
|
|
|
entities = await hass.async_add_executor_job(get_entities, onewire_hub)
|
2022-05-09 11:16:23 +00:00
|
|
|
async_add_entities(entities, True)
|
2020-11-08 18:06:41 +00:00
|
|
|
|
|
|
|
|
2022-05-31 10:26:15 +00:00
|
|
|
def get_entities(onewire_hub: OneWireHub) -> list[OneWireSwitch]:
|
2020-11-08 18:06:41 +00:00
|
|
|
"""Get a list of entities."""
|
2022-05-31 10:26:15 +00:00
|
|
|
if not onewire_hub.devices:
|
2021-05-11 15:28:17 +00:00
|
|
|
return []
|
|
|
|
|
2022-05-09 11:16:23 +00:00
|
|
|
entities: list[OneWireSwitch] = []
|
2020-11-08 18:06:41 +00:00
|
|
|
|
2022-05-31 10:26:15 +00:00
|
|
|
for device in onewire_hub.devices:
|
2021-10-22 09:45:40 +00:00
|
|
|
family = device.family
|
2021-12-07 12:59:43 +00:00
|
|
|
device_type = device.type
|
2021-10-22 09:45:40 +00:00
|
|
|
device_id = device.id
|
|
|
|
device_info = device.device_info
|
2021-12-07 12:59:43 +00:00
|
|
|
device_sub_type = "std"
|
|
|
|
if "EF" in family:
|
|
|
|
device_sub_type = "HobbyBoard"
|
|
|
|
family = device_type
|
2020-11-08 18:06:41 +00:00
|
|
|
|
2021-12-07 12:59:43 +00:00
|
|
|
if family not in get_sensor_types(device_sub_type):
|
2020-11-08 18:06:41 +00:00
|
|
|
continue
|
2021-12-07 12:59:43 +00:00
|
|
|
for description in get_sensor_types(device_sub_type)[family]:
|
2021-10-22 09:45:40 +00:00
|
|
|
device_file = os.path.join(os.path.split(device.path)[0], description.key)
|
2021-08-24 08:37:59 +00:00
|
|
|
name = f"{device_id} {description.name}"
|
2020-11-08 18:06:41 +00:00
|
|
|
entities.append(
|
2022-05-09 11:16:23 +00:00
|
|
|
OneWireSwitch(
|
2021-08-24 08:37:59 +00:00
|
|
|
description=description,
|
2020-12-02 14:11:20 +00:00
|
|
|
device_id=device_id,
|
2021-08-24 08:37:59 +00:00
|
|
|
device_file=device_file,
|
2020-12-02 14:11:20 +00:00
|
|
|
device_info=device_info,
|
2021-08-24 08:37:59 +00:00
|
|
|
name=name,
|
2022-05-31 10:26:15 +00:00
|
|
|
owproxy=onewire_hub.owproxy,
|
2020-11-08 18:06:41 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return entities
|
|
|
|
|
|
|
|
|
2022-05-09 11:16:23 +00:00
|
|
|
class OneWireSwitch(OneWireEntity, SwitchEntity):
|
2020-11-08 18:06:41 +00:00
|
|
|
"""Implementation of a 1-Wire switch."""
|
|
|
|
|
2021-08-24 08:37:59 +00:00
|
|
|
entity_description: OneWireSwitchEntityDescription
|
|
|
|
|
2020-11-08 18:06:41 +00:00
|
|
|
@property
|
2021-05-11 15:28:17 +00:00
|
|
|
def is_on(self) -> bool:
|
2020-11-08 18:06:41 +00:00
|
|
|
"""Return true if sensor is on."""
|
2021-05-11 15:28:17 +00:00
|
|
|
return bool(self._state)
|
2020-11-08 18:06:41 +00:00
|
|
|
|
2021-05-11 15:28:17 +00:00
|
|
|
def turn_on(self, **kwargs: Any) -> None:
|
2020-11-08 18:06:41 +00:00
|
|
|
"""Turn the entity on."""
|
2022-05-09 11:16:23 +00:00
|
|
|
self._write_value(b"1")
|
2020-11-08 18:06:41 +00:00
|
|
|
|
2021-05-11 15:28:17 +00:00
|
|
|
def turn_off(self, **kwargs: Any) -> None:
|
2020-11-08 18:06:41 +00:00
|
|
|
"""Turn the entity off."""
|
2022-05-09 11:16:23 +00:00
|
|
|
self._write_value(b"0")
|