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 logging
|
|
|
|
import os
|
2021-05-11 15:28:17 +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
|
2021-07-30 11:35:49 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_IDENTIFIERS,
|
|
|
|
ATTR_MANUFACTURER,
|
|
|
|
ATTR_MODEL,
|
|
|
|
ATTR_NAME,
|
|
|
|
CONF_TYPE,
|
|
|
|
)
|
2021-05-11 15:28:17 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-11-08 18:06:41 +00:00
|
|
|
|
2021-08-24 08:37:59 +00:00
|
|
|
from .const import CONF_TYPE_OWSERVER, DOMAIN, READ_MODE_BOOL
|
|
|
|
from .onewire_entities import OneWireEntityDescription, OneWireProxyEntity
|
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,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
"12": (
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="PIO.A",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="PIO A",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="PIO.B",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="PIO B",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="latch.A",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="Latch A",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="latch.B",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="Latch B",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
"29": (
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="PIO.0",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="PIO 0",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="PIO.1",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="PIO 1",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="PIO.2",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="PIO 2",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="PIO.3",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="PIO 3",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="PIO.4",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="PIO 4",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="PIO.5",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="PIO 5",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="PIO.6",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="PIO 6",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="PIO.7",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="PIO 7",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="latch.0",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="Latch 0",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="latch.1",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="Latch 1",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="latch.2",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="Latch 2",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="latch.3",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="Latch 3",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="latch.4",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="Latch 4",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="latch.5",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="Latch 5",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="latch.6",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="Latch 6",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
OneWireSwitchEntityDescription(
|
|
|
|
key="latch.7",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
name="Latch 7",
|
|
|
|
read_mode=READ_MODE_BOOL,
|
|
|
|
),
|
|
|
|
),
|
2020-11-08 18:06:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
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."""
|
|
|
|
# Only OWServer implementation works with switches
|
|
|
|
if config_entry.data[CONF_TYPE] == CONF_TYPE_OWSERVER:
|
2021-05-07 12:21:03 +00:00
|
|
|
onewirehub = hass.data[DOMAIN][config_entry.entry_id]
|
2020-11-08 18:06:41 +00:00
|
|
|
|
|
|
|
entities = await hass.async_add_executor_job(get_entities, onewirehub)
|
|
|
|
async_add_entities(entities, True)
|
|
|
|
|
|
|
|
|
2021-08-24 08:37:59 +00:00
|
|
|
def get_entities(onewirehub: OneWireHub) -> list[SwitchEntity]:
|
2020-11-08 18:06:41 +00:00
|
|
|
"""Get a list of entities."""
|
2021-05-11 15:28:17 +00:00
|
|
|
if not onewirehub.devices:
|
|
|
|
return []
|
|
|
|
|
2021-08-24 08:37:59 +00:00
|
|
|
entities: list[SwitchEntity] = []
|
2020-11-08 18:06:41 +00:00
|
|
|
|
|
|
|
for device in onewirehub.devices:
|
|
|
|
family = device["family"]
|
|
|
|
device_type = device["type"]
|
2020-12-02 14:11:20 +00:00
|
|
|
device_id = os.path.split(os.path.split(device["path"])[0])[1]
|
2020-11-08 18:06:41 +00:00
|
|
|
|
|
|
|
if family not in DEVICE_SWITCHES:
|
|
|
|
continue
|
|
|
|
|
2021-05-11 15:28:17 +00:00
|
|
|
device_info: DeviceInfo = {
|
2021-07-30 11:35:49 +00:00
|
|
|
ATTR_IDENTIFIERS: {(DOMAIN, device_id)},
|
|
|
|
ATTR_MANUFACTURER: "Maxim Integrated",
|
|
|
|
ATTR_MODEL: device_type,
|
|
|
|
ATTR_NAME: device_id,
|
2020-11-08 18:06:41 +00:00
|
|
|
}
|
2021-08-24 08:37:59 +00:00
|
|
|
for description in DEVICE_SWITCHES[family]:
|
|
|
|
device_file = os.path.join(
|
|
|
|
os.path.split(device["path"])[0], description.key
|
2020-11-08 18:06:41 +00:00
|
|
|
)
|
2021-08-24 08:37:59 +00:00
|
|
|
name = f"{device_id} {description.name}"
|
2020-11-08 18:06:41 +00:00
|
|
|
entities.append(
|
2020-11-09 22:21:16 +00:00
|
|
|
OneWireProxySwitch(
|
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,
|
2020-12-02 14:11:20 +00:00
|
|
|
owproxy=onewirehub.owproxy,
|
2020-11-08 18:06:41 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return entities
|
|
|
|
|
|
|
|
|
2020-11-09 22:21:16 +00:00
|
|
|
class OneWireProxySwitch(OneWireProxyEntity, 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."""
|
|
|
|
self._write_value_ownet(b"1")
|
|
|
|
|
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."""
|
|
|
|
self._write_value_ownet(b"0")
|