2021-01-10 23:08:25 +00:00
|
|
|
"""Representation of Z-Wave sensors."""
|
2021-03-18 14:08:35 +00:00
|
|
|
from __future__ import annotations
|
2021-01-10 23:08:25 +00:00
|
|
|
|
2021-08-20 20:25:39 +00:00
|
|
|
from collections.abc import Mapping
|
2021-01-10 23:08:25 +00:00
|
|
|
import logging
|
2021-04-29 10:28:14 +00:00
|
|
|
from typing import cast
|
2021-01-10 23:08:25 +00:00
|
|
|
|
2021-07-28 21:15:27 +00:00
|
|
|
import voluptuous as vol
|
2021-01-10 23:08:25 +00:00
|
|
|
from zwave_js_server.client import Client as ZwaveClient
|
2021-09-27 10:50:14 +00:00
|
|
|
from zwave_js_server.const import CommandClass, ConfigurationValueType, NodeStatus
|
2021-08-30 16:40:56 +00:00
|
|
|
from zwave_js_server.const.command_class.meter import (
|
2021-08-20 20:25:39 +00:00
|
|
|
RESET_METER_OPTION_TARGET_VALUE,
|
|
|
|
RESET_METER_OPTION_TYPE,
|
|
|
|
)
|
2021-05-31 06:46:28 +00:00
|
|
|
from zwave_js_server.model.node import Node as ZwaveNode
|
2021-03-27 17:14:06 +00:00
|
|
|
from zwave_js_server.model.value import ConfigurationValue
|
2021-09-06 20:37:12 +00:00
|
|
|
from zwave_js_server.util.command_class.meter import get_meter_type
|
2021-01-10 23:08:25 +00:00
|
|
|
|
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
DOMAIN as SENSOR_DOMAIN,
|
2021-12-19 11:28:09 +00:00
|
|
|
SensorDeviceClass,
|
2021-03-22 18:50:29 +00:00
|
|
|
SensorEntity,
|
2021-08-20 20:25:39 +00:00
|
|
|
SensorEntityDescription,
|
2021-12-19 11:28:09 +00:00
|
|
|
SensorStateClass,
|
2021-01-10 23:08:25 +00:00
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-07-28 21:15:27 +00:00
|
|
|
from homeassistant.helpers import entity_platform
|
2021-08-18 17:27:41 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-12-19 11:28:09 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo, EntityCategory
|
2021-04-29 10:28:14 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-01-10 23:08:25 +00:00
|
|
|
|
2021-08-20 20:25:39 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_METER_TYPE,
|
|
|
|
ATTR_METER_TYPE_NAME,
|
|
|
|
ATTR_VALUE,
|
|
|
|
DATA_CLIENT,
|
|
|
|
DOMAIN,
|
|
|
|
ENTITY_DESC_KEY_BATTERY,
|
|
|
|
ENTITY_DESC_KEY_CO,
|
|
|
|
ENTITY_DESC_KEY_CO2,
|
|
|
|
ENTITY_DESC_KEY_CURRENT,
|
|
|
|
ENTITY_DESC_KEY_ENERGY_MEASUREMENT,
|
|
|
|
ENTITY_DESC_KEY_ENERGY_TOTAL_INCREASING,
|
|
|
|
ENTITY_DESC_KEY_HUMIDITY,
|
|
|
|
ENTITY_DESC_KEY_ILLUMINANCE,
|
2021-08-30 15:48:36 +00:00
|
|
|
ENTITY_DESC_KEY_MEASUREMENT,
|
2021-08-20 20:25:39 +00:00
|
|
|
ENTITY_DESC_KEY_POWER,
|
|
|
|
ENTITY_DESC_KEY_POWER_FACTOR,
|
|
|
|
ENTITY_DESC_KEY_PRESSURE,
|
|
|
|
ENTITY_DESC_KEY_SIGNAL_STRENGTH,
|
|
|
|
ENTITY_DESC_KEY_TARGET_TEMPERATURE,
|
|
|
|
ENTITY_DESC_KEY_TEMPERATURE,
|
2021-08-30 15:48:36 +00:00
|
|
|
ENTITY_DESC_KEY_TOTAL_INCREASING,
|
2021-08-20 20:25:39 +00:00
|
|
|
ENTITY_DESC_KEY_VOLTAGE,
|
|
|
|
SERVICE_RESET_METER,
|
|
|
|
)
|
2021-01-10 23:08:25 +00:00
|
|
|
from .discovery import ZwaveDiscoveryInfo
|
2022-01-08 12:23:29 +00:00
|
|
|
from .discovery_data_template import (
|
|
|
|
NumericSensorDataTemplate,
|
|
|
|
NumericSensorDataTemplateData,
|
|
|
|
)
|
2021-01-10 23:08:25 +00:00
|
|
|
from .entity import ZWaveBaseEntity
|
2022-02-14 20:38:22 +00:00
|
|
|
from .helpers import get_device_id, get_valueless_base_unique_id
|
2021-01-10 23:08:25 +00:00
|
|
|
|
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-09-27 10:50:14 +00:00
|
|
|
STATUS_ICON: dict[NodeStatus, str] = {
|
|
|
|
NodeStatus.ALIVE: "mdi:heart-pulse",
|
|
|
|
NodeStatus.ASLEEP: "mdi:sleep",
|
|
|
|
NodeStatus.AWAKE: "mdi:eye",
|
|
|
|
NodeStatus.DEAD: "mdi:robot-dead",
|
|
|
|
NodeStatus.UNKNOWN: "mdi:help-rhombus",
|
|
|
|
}
|
|
|
|
|
2021-01-10 23:08:25 +00:00
|
|
|
|
2021-11-17 17:26:32 +00:00
|
|
|
ENTITY_DESCRIPTION_KEY_MAP: dict[str, SensorEntityDescription] = {
|
|
|
|
ENTITY_DESC_KEY_BATTERY: SensorEntityDescription(
|
2021-08-20 20:25:39 +00:00
|
|
|
ENTITY_DESC_KEY_BATTERY,
|
2021-12-19 11:28:09 +00:00
|
|
|
device_class=SensorDeviceClass.BATTERY,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-20 20:25:39 +00:00
|
|
|
),
|
2021-11-17 17:26:32 +00:00
|
|
|
ENTITY_DESC_KEY_CURRENT: SensorEntityDescription(
|
2021-08-20 20:25:39 +00:00
|
|
|
ENTITY_DESC_KEY_CURRENT,
|
2021-12-19 11:28:09 +00:00
|
|
|
device_class=SensorDeviceClass.CURRENT,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-20 20:25:39 +00:00
|
|
|
),
|
2021-11-17 17:26:32 +00:00
|
|
|
ENTITY_DESC_KEY_VOLTAGE: SensorEntityDescription(
|
2021-08-20 20:25:39 +00:00
|
|
|
ENTITY_DESC_KEY_VOLTAGE,
|
2021-12-19 11:28:09 +00:00
|
|
|
device_class=SensorDeviceClass.VOLTAGE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-20 20:25:39 +00:00
|
|
|
),
|
2021-11-17 17:26:32 +00:00
|
|
|
ENTITY_DESC_KEY_ENERGY_MEASUREMENT: SensorEntityDescription(
|
2021-09-09 20:19:28 +00:00
|
|
|
ENTITY_DESC_KEY_ENERGY_MEASUREMENT,
|
2021-12-19 11:28:09 +00:00
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-20 20:25:39 +00:00
|
|
|
),
|
2021-11-17 17:26:32 +00:00
|
|
|
ENTITY_DESC_KEY_ENERGY_TOTAL_INCREASING: SensorEntityDescription(
|
2021-08-20 20:25:39 +00:00
|
|
|
ENTITY_DESC_KEY_ENERGY_TOTAL_INCREASING,
|
2021-12-19 11:28:09 +00:00
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
2021-08-20 20:25:39 +00:00
|
|
|
),
|
2021-11-17 17:26:32 +00:00
|
|
|
ENTITY_DESC_KEY_POWER: SensorEntityDescription(
|
2021-08-20 20:25:39 +00:00
|
|
|
ENTITY_DESC_KEY_POWER,
|
2021-12-19 11:28:09 +00:00
|
|
|
device_class=SensorDeviceClass.POWER,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-20 20:25:39 +00:00
|
|
|
),
|
2021-11-17 17:26:32 +00:00
|
|
|
ENTITY_DESC_KEY_POWER_FACTOR: SensorEntityDescription(
|
2021-08-20 20:25:39 +00:00
|
|
|
ENTITY_DESC_KEY_POWER_FACTOR,
|
2021-12-19 11:28:09 +00:00
|
|
|
device_class=SensorDeviceClass.POWER_FACTOR,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-20 20:25:39 +00:00
|
|
|
),
|
2021-11-17 17:26:32 +00:00
|
|
|
ENTITY_DESC_KEY_CO: SensorEntityDescription(
|
2021-08-20 20:25:39 +00:00
|
|
|
ENTITY_DESC_KEY_CO,
|
2021-12-19 11:28:09 +00:00
|
|
|
device_class=SensorDeviceClass.CO,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-20 20:25:39 +00:00
|
|
|
),
|
2021-11-17 17:26:32 +00:00
|
|
|
ENTITY_DESC_KEY_CO2: SensorEntityDescription(
|
2021-08-20 20:25:39 +00:00
|
|
|
ENTITY_DESC_KEY_CO2,
|
2021-12-19 11:28:09 +00:00
|
|
|
device_class=SensorDeviceClass.CO2,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-20 20:25:39 +00:00
|
|
|
),
|
2021-11-17 17:26:32 +00:00
|
|
|
ENTITY_DESC_KEY_HUMIDITY: SensorEntityDescription(
|
2021-08-20 20:25:39 +00:00
|
|
|
ENTITY_DESC_KEY_HUMIDITY,
|
2021-12-19 11:28:09 +00:00
|
|
|
device_class=SensorDeviceClass.HUMIDITY,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-20 20:25:39 +00:00
|
|
|
),
|
2021-11-17 17:26:32 +00:00
|
|
|
ENTITY_DESC_KEY_ILLUMINANCE: SensorEntityDescription(
|
2021-08-20 20:25:39 +00:00
|
|
|
ENTITY_DESC_KEY_ILLUMINANCE,
|
2021-12-19 11:28:09 +00:00
|
|
|
device_class=SensorDeviceClass.ILLUMINANCE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-20 20:25:39 +00:00
|
|
|
),
|
2021-11-17 17:26:32 +00:00
|
|
|
ENTITY_DESC_KEY_PRESSURE: SensorEntityDescription(
|
2021-08-20 20:25:39 +00:00
|
|
|
ENTITY_DESC_KEY_PRESSURE,
|
2021-12-19 11:28:09 +00:00
|
|
|
device_class=SensorDeviceClass.PRESSURE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-20 20:25:39 +00:00
|
|
|
),
|
2021-11-17 17:26:32 +00:00
|
|
|
ENTITY_DESC_KEY_SIGNAL_STRENGTH: SensorEntityDescription(
|
2021-08-20 20:25:39 +00:00
|
|
|
ENTITY_DESC_KEY_SIGNAL_STRENGTH,
|
2021-12-19 11:28:09 +00:00
|
|
|
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-10-25 16:14:44 +00:00
|
|
|
entity_registry_enabled_default=False,
|
2021-12-19 11:28:09 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-20 20:25:39 +00:00
|
|
|
),
|
2021-11-17 17:26:32 +00:00
|
|
|
ENTITY_DESC_KEY_TEMPERATURE: SensorEntityDescription(
|
2021-08-20 20:25:39 +00:00
|
|
|
ENTITY_DESC_KEY_TEMPERATURE,
|
2021-12-19 11:28:09 +00:00
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-20 20:25:39 +00:00
|
|
|
),
|
2021-11-17 17:26:32 +00:00
|
|
|
ENTITY_DESC_KEY_TARGET_TEMPERATURE: SensorEntityDescription(
|
2021-08-20 20:25:39 +00:00
|
|
|
ENTITY_DESC_KEY_TARGET_TEMPERATURE,
|
2021-12-19 11:28:09 +00:00
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
2021-08-20 20:25:39 +00:00
|
|
|
state_class=None,
|
|
|
|
),
|
2021-11-17 17:26:32 +00:00
|
|
|
ENTITY_DESC_KEY_MEASUREMENT: SensorEntityDescription(
|
2021-08-30 15:48:36 +00:00
|
|
|
ENTITY_DESC_KEY_MEASUREMENT,
|
|
|
|
device_class=None,
|
2021-12-19 11:28:09 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-30 15:48:36 +00:00
|
|
|
),
|
2021-11-17 17:26:32 +00:00
|
|
|
ENTITY_DESC_KEY_TOTAL_INCREASING: SensorEntityDescription(
|
2021-08-30 15:48:36 +00:00
|
|
|
ENTITY_DESC_KEY_TOTAL_INCREASING,
|
|
|
|
device_class=None,
|
2021-12-19 11:28:09 +00:00
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
2021-08-30 15:48:36 +00:00
|
|
|
),
|
2021-08-20 20:25:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-10 23:08:25 +00:00
|
|
|
async def async_setup_entry(
|
2021-04-29 10:28:14 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2021-01-10 23:08:25 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up Z-Wave sensor from config entry."""
|
|
|
|
client: ZwaveClient = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT]
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_add_sensor(info: ZwaveDiscoveryInfo) -> None:
|
|
|
|
"""Add Z-Wave Sensor."""
|
2021-03-18 14:08:35 +00:00
|
|
|
entities: list[ZWaveBaseEntity] = []
|
2021-01-10 23:08:25 +00:00
|
|
|
|
2021-11-17 17:26:32 +00:00
|
|
|
if info.platform_data:
|
|
|
|
data: NumericSensorDataTemplateData = info.platform_data
|
|
|
|
else:
|
|
|
|
data = NumericSensorDataTemplateData()
|
2021-08-20 20:25:39 +00:00
|
|
|
entity_description = ENTITY_DESCRIPTION_KEY_MAP.get(
|
2021-11-17 17:26:32 +00:00
|
|
|
data.entity_description_key or "", SensorEntityDescription("base_sensor")
|
|
|
|
)
|
2021-08-20 20:25:39 +00:00
|
|
|
|
2021-01-10 23:08:25 +00:00
|
|
|
if info.platform_hint == "string_sensor":
|
2021-11-17 17:26:32 +00:00
|
|
|
entities.append(
|
|
|
|
ZWaveStringSensor(config_entry, client, info, entity_description)
|
|
|
|
)
|
2021-01-10 23:08:25 +00:00
|
|
|
elif info.platform_hint == "numeric_sensor":
|
2021-08-20 20:25:39 +00:00
|
|
|
entities.append(
|
2021-11-17 17:26:32 +00:00
|
|
|
ZWaveNumericSensor(
|
|
|
|
config_entry,
|
|
|
|
client,
|
|
|
|
info,
|
|
|
|
entity_description,
|
|
|
|
data.unit_of_measurement,
|
|
|
|
)
|
2021-08-20 20:25:39 +00:00
|
|
|
)
|
2021-02-02 09:06:09 +00:00
|
|
|
elif info.platform_hint == "list_sensor":
|
2021-11-17 17:26:32 +00:00
|
|
|
entities.append(
|
|
|
|
ZWaveListSensor(config_entry, client, info, entity_description)
|
|
|
|
)
|
2021-03-27 17:14:06 +00:00
|
|
|
elif info.platform_hint == "config_parameter":
|
2021-08-20 20:25:39 +00:00
|
|
|
entities.append(
|
2021-11-17 17:26:32 +00:00
|
|
|
ZWaveConfigParameterSensor(
|
|
|
|
config_entry, client, info, entity_description
|
|
|
|
)
|
2021-08-20 20:25:39 +00:00
|
|
|
)
|
2021-07-29 12:18:38 +00:00
|
|
|
elif info.platform_hint == "meter":
|
2021-11-17 17:26:32 +00:00
|
|
|
entities.append(
|
|
|
|
ZWaveMeterSensor(config_entry, client, info, entity_description)
|
|
|
|
)
|
2021-01-10 23:08:25 +00:00
|
|
|
else:
|
|
|
|
LOGGER.warning(
|
|
|
|
"Sensor not implemented for %s/%s",
|
|
|
|
info.platform_hint,
|
|
|
|
info.primary_value.propertyname,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
2021-05-31 06:46:28 +00:00
|
|
|
@callback
|
|
|
|
def async_add_node_status_sensor(node: ZwaveNode) -> None:
|
|
|
|
"""Add node status sensor."""
|
|
|
|
async_add_entities([ZWaveNodeStatusSensor(config_entry, client, node)])
|
|
|
|
|
2021-07-13 08:31:49 +00:00
|
|
|
config_entry.async_on_unload(
|
2021-01-10 23:08:25 +00:00
|
|
|
async_dispatcher_connect(
|
2021-01-19 15:40:01 +00:00
|
|
|
hass,
|
|
|
|
f"{DOMAIN}_{config_entry.entry_id}_add_{SENSOR_DOMAIN}",
|
|
|
|
async_add_sensor,
|
2021-01-10 23:08:25 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-07-13 08:31:49 +00:00
|
|
|
config_entry.async_on_unload(
|
2021-05-31 06:46:28 +00:00
|
|
|
async_dispatcher_connect(
|
|
|
|
hass,
|
|
|
|
f"{DOMAIN}_{config_entry.entry_id}_add_node_status_sensor",
|
|
|
|
async_add_node_status_sensor,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-07-28 21:15:27 +00:00
|
|
|
platform = entity_platform.async_get_current_platform()
|
|
|
|
platform.async_register_entity_service(
|
|
|
|
SERVICE_RESET_METER,
|
|
|
|
{
|
|
|
|
vol.Optional(ATTR_METER_TYPE): vol.Coerce(int),
|
|
|
|
vol.Optional(ATTR_VALUE): vol.Coerce(int),
|
|
|
|
},
|
|
|
|
"async_reset_meter",
|
|
|
|
)
|
|
|
|
|
2021-01-10 23:08:25 +00:00
|
|
|
|
2021-03-22 18:50:29 +00:00
|
|
|
class ZwaveSensorBase(ZWaveBaseEntity, SensorEntity):
|
2021-01-10 23:08:25 +00:00
|
|
|
"""Basic Representation of a Z-Wave sensor."""
|
|
|
|
|
2021-02-06 11:08:25 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
client: ZwaveClient,
|
2021-11-17 17:26:32 +00:00
|
|
|
info: ZwaveDiscoveryInfo,
|
|
|
|
entity_description: SensorEntityDescription,
|
|
|
|
unit_of_measurement: str | None = None,
|
2021-02-06 11:08:25 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize a ZWaveSensorBase entity."""
|
2021-11-17 17:26:32 +00:00
|
|
|
super().__init__(config_entry, client, info)
|
2021-08-20 20:25:39 +00:00
|
|
|
self.entity_description = entity_description
|
2021-11-17 17:26:32 +00:00
|
|
|
self._attr_native_unit_of_measurement = unit_of_measurement
|
2021-06-01 08:26:22 +00:00
|
|
|
|
|
|
|
# Entity class attributes
|
2021-08-20 20:25:39 +00:00
|
|
|
self._attr_force_update = True
|
2021-06-01 08:26:22 +00:00
|
|
|
self._attr_name = self.generate_name(include_value_name=True)
|
2021-01-10 23:08:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ZWaveStringSensor(ZwaveSensorBase):
|
|
|
|
"""Representation of a Z-Wave String sensor."""
|
|
|
|
|
|
|
|
@property
|
2021-08-11 19:17:16 +00:00
|
|
|
def native_value(self) -> str | None:
|
2021-01-10 23:08:25 +00:00
|
|
|
"""Return state of the sensor."""
|
|
|
|
if self.info.primary_value.value is None:
|
|
|
|
return None
|
|
|
|
return str(self.info.primary_value.value)
|
|
|
|
|
|
|
|
@property
|
2021-08-11 19:17:16 +00:00
|
|
|
def native_unit_of_measurement(self) -> str | None:
|
2021-01-10 23:08:25 +00:00
|
|
|
"""Return unit of measurement the value is expressed in."""
|
|
|
|
if self.info.primary_value.metadata.unit is None:
|
|
|
|
return None
|
|
|
|
return str(self.info.primary_value.metadata.unit)
|
|
|
|
|
|
|
|
|
|
|
|
class ZWaveNumericSensor(ZwaveSensorBase):
|
|
|
|
"""Representation of a Z-Wave Numeric sensor."""
|
|
|
|
|
2022-01-08 12:23:29 +00:00
|
|
|
@callback
|
|
|
|
def on_value_update(self) -> None:
|
|
|
|
"""Handle scale changes for this value on value updated event."""
|
|
|
|
self._attr_native_unit_of_measurement = (
|
|
|
|
NumericSensorDataTemplate()
|
|
|
|
.resolve_data(self.info.primary_value)
|
|
|
|
.unit_of_measurement
|
|
|
|
)
|
|
|
|
|
2021-01-10 23:08:25 +00:00
|
|
|
@property
|
2021-08-11 19:17:16 +00:00
|
|
|
def native_value(self) -> float:
|
2021-01-10 23:08:25 +00:00
|
|
|
"""Return state of the sensor."""
|
|
|
|
if self.info.primary_value.value is None:
|
|
|
|
return 0
|
|
|
|
return round(float(self.info.primary_value.value), 2)
|
|
|
|
|
|
|
|
@property
|
2021-08-11 19:17:16 +00:00
|
|
|
def native_unit_of_measurement(self) -> str | None:
|
2021-01-10 23:08:25 +00:00
|
|
|
"""Return unit of measurement the value is expressed in."""
|
2021-11-17 17:26:32 +00:00
|
|
|
if self._attr_native_unit_of_measurement is not None:
|
|
|
|
return self._attr_native_unit_of_measurement
|
2021-01-10 23:08:25 +00:00
|
|
|
if self.info.primary_value.metadata.unit is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return str(self.info.primary_value.metadata.unit)
|
|
|
|
|
2021-07-29 12:18:38 +00:00
|
|
|
|
2021-08-18 13:11:10 +00:00
|
|
|
class ZWaveMeterSensor(ZWaveNumericSensor):
|
2021-07-29 12:18:38 +00:00
|
|
|
"""Representation of a Z-Wave Meter CC sensor."""
|
|
|
|
|
2021-08-20 20:25:39 +00:00
|
|
|
@property
|
|
|
|
def extra_state_attributes(self) -> Mapping[str, int | str] | None:
|
|
|
|
"""Return extra state attributes."""
|
2021-10-31 17:56:25 +00:00
|
|
|
if meter_type := get_meter_type(self.info.primary_value):
|
2021-08-20 20:25:39 +00:00
|
|
|
return {
|
|
|
|
ATTR_METER_TYPE: meter_type.value,
|
|
|
|
ATTR_METER_TYPE_NAME: meter_type.name,
|
|
|
|
}
|
|
|
|
return None
|
2021-07-29 12:18:38 +00:00
|
|
|
|
2021-07-28 21:15:27 +00:00
|
|
|
async def async_reset_meter(
|
|
|
|
self, meter_type: int | None = None, value: int | None = None
|
|
|
|
) -> None:
|
|
|
|
"""Reset meter(s) on device."""
|
|
|
|
node = self.info.node
|
|
|
|
primary_value = self.info.primary_value
|
|
|
|
options = {}
|
|
|
|
if meter_type is not None:
|
2021-08-20 20:25:39 +00:00
|
|
|
options[RESET_METER_OPTION_TYPE] = meter_type
|
2021-07-28 21:15:27 +00:00
|
|
|
if value is not None:
|
2021-08-20 20:25:39 +00:00
|
|
|
options[RESET_METER_OPTION_TARGET_VALUE] = value
|
2021-07-28 21:15:27 +00:00
|
|
|
args = [options] if options else []
|
|
|
|
await node.endpoints[primary_value.endpoint].async_invoke_cc_api(
|
|
|
|
CommandClass.METER, "reset", *args, wait_for_result=False
|
|
|
|
)
|
|
|
|
LOGGER.debug(
|
|
|
|
"Meters on node %s endpoint %s reset with the following options: %s",
|
|
|
|
node,
|
|
|
|
primary_value.endpoint,
|
|
|
|
options,
|
|
|
|
)
|
2021-08-03 20:50:14 +00:00
|
|
|
|
2021-02-02 09:06:09 +00:00
|
|
|
|
|
|
|
class ZWaveListSensor(ZwaveSensorBase):
|
|
|
|
"""Representation of a Z-Wave Numeric sensor with multiple states."""
|
|
|
|
|
2021-02-05 22:36:42 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
client: ZwaveClient,
|
2021-11-17 17:26:32 +00:00
|
|
|
info: ZwaveDiscoveryInfo,
|
|
|
|
entity_description: SensorEntityDescription,
|
|
|
|
unit_of_measurement: str | None = None,
|
2021-02-05 22:36:42 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize a ZWaveListSensor entity."""
|
2021-11-17 17:26:32 +00:00
|
|
|
super().__init__(
|
|
|
|
config_entry, client, info, entity_description, unit_of_measurement
|
|
|
|
)
|
2021-06-01 08:26:22 +00:00
|
|
|
|
|
|
|
# Entity class attributes
|
|
|
|
self._attr_name = self.generate_name(
|
2021-02-06 11:08:25 +00:00
|
|
|
include_value_name=True,
|
|
|
|
alternate_value_name=self.info.primary_value.property_name,
|
|
|
|
additional_info=[self.info.primary_value.property_key_name],
|
2021-02-05 22:36:42 +00:00
|
|
|
)
|
|
|
|
|
2021-02-02 09:06:09 +00:00
|
|
|
@property
|
2021-08-11 19:17:16 +00:00
|
|
|
def native_value(self) -> str | None:
|
2021-02-02 09:06:09 +00:00
|
|
|
"""Return state of the sensor."""
|
|
|
|
if self.info.primary_value.value is None:
|
|
|
|
return None
|
2021-01-10 23:08:25 +00:00
|
|
|
if (
|
2021-03-20 00:27:04 +00:00
|
|
|
str(self.info.primary_value.value)
|
|
|
|
not in self.info.primary_value.metadata.states
|
2021-01-10 23:08:25 +00:00
|
|
|
):
|
2021-02-05 22:36:42 +00:00
|
|
|
return str(self.info.primary_value.value)
|
2021-02-02 09:06:09 +00:00
|
|
|
return str(
|
|
|
|
self.info.primary_value.metadata.states[str(self.info.primary_value.value)]
|
2021-01-10 23:08:25 +00:00
|
|
|
)
|
2021-02-02 09:06:09 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-18 14:08:35 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, str] | None:
|
2021-02-02 09:06:09 +00:00
|
|
|
"""Return the device specific state attributes."""
|
|
|
|
# add the value's int value as property for multi-value (list) items
|
2021-08-20 20:25:39 +00:00
|
|
|
return {ATTR_VALUE: self.info.primary_value.value}
|
2021-03-27 17:14:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ZWaveConfigParameterSensor(ZwaveSensorBase):
|
|
|
|
"""Representation of a Z-Wave config parameter sensor."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
client: ZwaveClient,
|
2021-11-17 17:26:32 +00:00
|
|
|
info: ZwaveDiscoveryInfo,
|
|
|
|
entity_description: SensorEntityDescription,
|
|
|
|
unit_of_measurement: str | None = None,
|
2021-03-27 17:14:06 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize a ZWaveConfigParameterSensor entity."""
|
2021-11-17 17:26:32 +00:00
|
|
|
super().__init__(
|
|
|
|
config_entry, client, info, entity_description, unit_of_measurement
|
|
|
|
)
|
2021-06-01 08:26:22 +00:00
|
|
|
self._primary_value = cast(ConfigurationValue, self.info.primary_value)
|
|
|
|
|
|
|
|
# Entity class attributes
|
|
|
|
self._attr_name = self.generate_name(
|
2021-03-27 17:14:06 +00:00
|
|
|
include_value_name=True,
|
|
|
|
alternate_value_name=self.info.primary_value.property_name,
|
|
|
|
additional_info=[self.info.primary_value.property_key_name],
|
|
|
|
name_suffix="Config Parameter",
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
2021-08-11 19:17:16 +00:00
|
|
|
def native_value(self) -> str | None:
|
2021-03-27 17:14:06 +00:00
|
|
|
"""Return state of the sensor."""
|
|
|
|
if self.info.primary_value.value is None:
|
|
|
|
return None
|
|
|
|
if (
|
|
|
|
self._primary_value.configuration_value_type == ConfigurationValueType.RANGE
|
|
|
|
or (
|
|
|
|
not str(self.info.primary_value.value)
|
|
|
|
in self.info.primary_value.metadata.states
|
|
|
|
)
|
|
|
|
):
|
|
|
|
return str(self.info.primary_value.value)
|
|
|
|
return str(
|
|
|
|
self.info.primary_value.metadata.states[str(self.info.primary_value.value)]
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def extra_state_attributes(self) -> dict[str, str] | None:
|
|
|
|
"""Return the device specific state attributes."""
|
|
|
|
if self._primary_value.configuration_value_type == ConfigurationValueType.RANGE:
|
|
|
|
return None
|
|
|
|
# add the value's int value as property for multi-value (list) items
|
2021-08-20 20:25:39 +00:00
|
|
|
return {ATTR_VALUE: self.info.primary_value.value}
|
2021-05-31 06:46:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ZWaveNodeStatusSensor(SensorEntity):
|
|
|
|
"""Representation of a node status sensor."""
|
|
|
|
|
|
|
|
_attr_should_poll = False
|
2021-12-19 11:28:09 +00:00
|
|
|
_attr_entity_category = EntityCategory.DIAGNOSTIC
|
2021-05-31 06:46:28 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, config_entry: ConfigEntry, client: ZwaveClient, node: ZwaveNode
|
|
|
|
) -> None:
|
|
|
|
"""Initialize a generic Z-Wave device entity."""
|
|
|
|
self.config_entry = config_entry
|
|
|
|
self.client = client
|
|
|
|
self.node = node
|
|
|
|
name: str = (
|
|
|
|
self.node.name
|
|
|
|
or self.node.device_config.description
|
|
|
|
or f"Node {self.node.node_id}"
|
|
|
|
)
|
|
|
|
# Entity class attributes
|
|
|
|
self._attr_name = f"{name}: Node Status"
|
2022-02-14 20:38:22 +00:00
|
|
|
self._base_unique_id = get_valueless_base_unique_id(client, node)
|
|
|
|
self._attr_unique_id = f"{self._base_unique_id}.node_status"
|
2021-05-31 06:46:28 +00:00
|
|
|
# device is precreated in main handler
|
2021-10-29 00:00:12 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={get_device_id(self.client, self.node)},
|
|
|
|
)
|
2021-08-11 19:17:16 +00:00
|
|
|
self._attr_native_value: str = node.status.name.lower()
|
2021-05-31 06:46:28 +00:00
|
|
|
|
|
|
|
async def async_poll_value(self, _: bool) -> None:
|
|
|
|
"""Poll a value."""
|
2021-09-18 11:52:59 +00:00
|
|
|
# pylint: disable=no-self-use
|
2021-05-31 06:46:28 +00:00
|
|
|
raise ValueError("There is no value to poll for this entity")
|
|
|
|
|
2021-08-27 22:01:20 +00:00
|
|
|
@callback
|
2021-05-31 06:46:28 +00:00
|
|
|
def _status_changed(self, _: dict) -> None:
|
|
|
|
"""Call when status event is received."""
|
2021-08-11 19:17:16 +00:00
|
|
|
self._attr_native_value = self.node.status.name.lower()
|
2021-05-31 06:46:28 +00:00
|
|
|
self.async_write_ha_state()
|
|
|
|
|
2021-09-27 10:50:14 +00:00
|
|
|
@property
|
|
|
|
def icon(self) -> str | None:
|
|
|
|
"""Icon of the entity."""
|
|
|
|
return STATUS_ICON[self.node.status]
|
|
|
|
|
2021-05-31 06:46:28 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Call when entity is added."""
|
|
|
|
# Add value_changed callbacks.
|
|
|
|
for evt in ("wake up", "sleep", "dead", "alive"):
|
|
|
|
self.async_on_remove(self.node.on(evt, self._status_changed))
|
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass,
|
|
|
|
f"{DOMAIN}_{self.unique_id}_poll_value",
|
|
|
|
self.async_poll_value,
|
|
|
|
)
|
|
|
|
)
|
2021-12-27 11:31:31 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass,
|
2022-02-14 20:38:22 +00:00
|
|
|
f"{DOMAIN}_{self._base_unique_id}_remove_entity",
|
2021-12-27 11:31:31 +00:00
|
|
|
self.async_remove,
|
|
|
|
)
|
|
|
|
)
|
2021-05-31 06:46:28 +00:00
|
|
|
self.async_write_ha_state()
|