2021-02-24 18:39:35 +00:00
|
|
|
"""Support for Z-Wave controls using the number platform."""
|
2021-03-18 14:08:35 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-02-24 18:39:35 +00:00
|
|
|
from zwave_js_server.client import Client as ZwaveClient
|
|
|
|
|
|
|
|
from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN, NumberEntity
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-04-29 10:28:14 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-02-24 18:39:35 +00:00
|
|
|
|
2021-07-13 08:31:49 +00:00
|
|
|
from .const import DATA_CLIENT, DOMAIN
|
2021-02-24 18:39:35 +00:00
|
|
|
from .discovery import ZwaveDiscoveryInfo
|
|
|
|
from .entity import ZWaveBaseEntity
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
2021-04-29 10:28:14 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2021-02-24 18:39:35 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up Z-Wave Number entity from Config Entry."""
|
|
|
|
client: ZwaveClient = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT]
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_add_number(info: ZwaveDiscoveryInfo) -> None:
|
|
|
|
"""Add Z-Wave number entity."""
|
2021-03-18 14:08:35 +00:00
|
|
|
entities: list[ZWaveBaseEntity] = []
|
2021-08-16 17:36:20 +00:00
|
|
|
if info.platform_hint == "volume":
|
|
|
|
entities.append(ZwaveVolumeNumberEntity(config_entry, client, info))
|
|
|
|
else:
|
|
|
|
entities.append(ZwaveNumberEntity(config_entry, client, info))
|
2021-02-24 18:39:35 +00:00
|
|
|
async_add_entities(entities)
|
|
|
|
|
2021-07-13 08:31:49 +00:00
|
|
|
config_entry.async_on_unload(
|
2021-02-24 18:39:35 +00:00
|
|
|
async_dispatcher_connect(
|
|
|
|
hass,
|
|
|
|
f"{DOMAIN}_{config_entry.entry_id}_add_{NUMBER_DOMAIN}",
|
|
|
|
async_add_number,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ZwaveNumberEntity(ZWaveBaseEntity, NumberEntity):
|
|
|
|
"""Representation of a Z-Wave number entity."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, config_entry: ConfigEntry, client: ZwaveClient, info: ZwaveDiscoveryInfo
|
|
|
|
) -> None:
|
|
|
|
"""Initialize a ZwaveNumberEntity entity."""
|
|
|
|
super().__init__(config_entry, client, info)
|
|
|
|
if self.info.primary_value.metadata.writeable:
|
|
|
|
self._target_value = self.info.primary_value
|
|
|
|
else:
|
|
|
|
self._target_value = self.get_zwave_value("targetValue")
|
|
|
|
|
2021-06-01 08:26:22 +00:00
|
|
|
# Entity class attributes
|
|
|
|
self._attr_name = self.generate_name(
|
|
|
|
include_value_name=True, alternate_value_name=info.platform_hint
|
|
|
|
)
|
|
|
|
|
2021-02-24 18:39:35 +00:00
|
|
|
@property
|
|
|
|
def min_value(self) -> float:
|
|
|
|
"""Return the minimum value."""
|
|
|
|
if self.info.primary_value.metadata.min is None:
|
|
|
|
return 0
|
|
|
|
return float(self.info.primary_value.metadata.min)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def max_value(self) -> float:
|
|
|
|
"""Return the maximum value."""
|
|
|
|
if self.info.primary_value.metadata.max is None:
|
|
|
|
return 255
|
|
|
|
return float(self.info.primary_value.metadata.max)
|
|
|
|
|
|
|
|
@property
|
2021-06-29 13:07:31 +00:00
|
|
|
def value(self) -> float | None:
|
2021-02-24 18:39:35 +00:00
|
|
|
"""Return the entity value."""
|
|
|
|
if self.info.primary_value.value is None:
|
|
|
|
return None
|
|
|
|
return float(self.info.primary_value.value)
|
|
|
|
|
|
|
|
@property
|
2021-03-18 14:08:35 +00:00
|
|
|
def unit_of_measurement(self) -> str | None:
|
2021-02-24 18:39:35 +00:00
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
|
|
|
if self.info.primary_value.metadata.unit is None:
|
|
|
|
return None
|
|
|
|
return str(self.info.primary_value.metadata.unit)
|
|
|
|
|
|
|
|
async def async_set_value(self, value: float) -> None:
|
|
|
|
"""Set new value."""
|
|
|
|
await self.info.node.async_set_value(self._target_value, value)
|
2021-08-16 17:36:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ZwaveVolumeNumberEntity(ZWaveBaseEntity, NumberEntity):
|
|
|
|
"""Representation of a volume number entity."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, config_entry: ConfigEntry, client: ZwaveClient, info: ZwaveDiscoveryInfo
|
|
|
|
) -> None:
|
|
|
|
"""Initialize a ZwaveVolumeNumberEntity entity."""
|
|
|
|
super().__init__(config_entry, client, info)
|
|
|
|
self.correction_factor = int(
|
|
|
|
self.info.primary_value.metadata.max - self.info.primary_value.metadata.min
|
|
|
|
)
|
|
|
|
# Fallback in case we can't properly calculate correction factor
|
|
|
|
if self.correction_factor == 0:
|
|
|
|
self.correction_factor = 1
|
|
|
|
|
|
|
|
# Entity class attributes
|
|
|
|
self._attr_min_value = 0
|
|
|
|
self._attr_max_value = 1
|
|
|
|
self._attr_step = 0.01
|
|
|
|
self._attr_name = self.generate_name(include_value_name=True)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def value(self) -> float | None:
|
|
|
|
"""Return the entity value."""
|
|
|
|
if self.info.primary_value.value is None:
|
|
|
|
return None
|
|
|
|
return float(self.info.primary_value.value) / self.correction_factor
|
|
|
|
|
|
|
|
async def async_set_value(self, value: float) -> None:
|
|
|
|
"""Set new value."""
|
|
|
|
await self.info.node.async_set_value(
|
|
|
|
self.info.primary_value, round(value * self.correction_factor)
|
|
|
|
)
|