Skip to content

Commit

Permalink
[add] unit of measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
soloam committed Feb 14, 2022
1 parent d5fbeef commit ad3e037
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
9 changes: 9 additions & 0 deletions custom_components/pid_controller/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,19 @@
DEFAULT_ROUND = "round"
DEFAULT_SAMPLE_TIME = 0
DEFAULT_WINDUP = 20
DEFAULT_UNIT_OF_MEASUREMENT = "points"
DEFAULT_DEVICE_CLASS = "None"
DEFAULT_ICON = "mdi:chart-bell-curve-cumulative"

# Other
ROUND_FLOOR = "floor"
ROUND_CEIL = "ceil"
ROUND_ROUND = "round"

# Attributes
ATTR_ICON = "icon"
ATTR_UNIT_OF_MEASUREMENT = "unit_of_measuremnt"
ATTR_DEVICE_CLASS = "device_class"
ATTR_PROPORTIONAL = "proportional"
ATTR_INTEGRAL = "integral"
ATTR_DERIVATIVE = "derivative"
Expand All @@ -59,6 +65,9 @@
ATTR_D = "d"

ATTR_TO_PROPERTY = [
ATTR_ICON,
ATTR_UNIT_OF_MEASUREMENT,
ATTR_DEVICE_CLASS,
ATTR_PROPORTIONAL,
ATTR_INTEGRAL,
ATTR_DERIVATIVE,
Expand Down
99 changes: 98 additions & 1 deletion custom_components/pid_controller/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,22 @@

import voluptuous as vol
from _sha1 import sha1
from homeassistant.components.sensor import SensorEntity
from homeassistant.components.sensor import (
SensorEntity,
SensorDeviceClass,
SensorStateClass,
)
from homeassistant.const import (
CONF_ENTITY_ID,
CONF_NAME,
CONF_ICON,
CONF_UNIQUE_ID,
EVENT_HOMEASSISTANT_START,
STATE_UNAVAILABLE,
CONF_MINIMUM,
CONF_MAXIMUM,
CONF_UNIT_OF_MEASUREMENT,
CONF_DEVICE_CLASS,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import TemplateError
Expand All @@ -45,6 +52,7 @@
{
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_UNIQUE_ID): cv.string,
vol.Optional(CONF_ICON, default=DEFAULT_ICON): cv.template,
vol.Required(CONF_SETPOINT): cv.template,
vol.Optional(CONF_PROPORTIONAL, default=0): cv.template,
vol.Optional(CONF_INTEGRAL, default=0): cv.template,
Expand All @@ -57,6 +65,11 @@
vol.Optional(CONF_ROUND, default=DEFAULT_ROUND): cv.template,
vol.Optional(CONF_SAMPLE_TIME, default=DEFAULT_SAMPLE_TIME): cv.template,
vol.Optional(CONF_WINDUP, default=DEFAULT_WINDUP): cv.template,
vol.Optional(CONF_WINDUP, default=DEFAULT_WINDUP): cv.template,
vol.Optional(
CONF_UNIT_OF_MEASUREMENT, default=DEFAULT_UNIT_OF_MEASUREMENT
): cv.template,
vol.Optional(CONF_DEVICE_CLASS, default=DEFAULT_DEVICE_CLASS): cv.template,
}
)
)
Expand All @@ -66,6 +79,7 @@ async def async_setup_platform(
hass: HomeAssistant, config, async_add_entities, discovery_info=None
):

icon = config.get(CONF_ICON)
set_point = config.get(CONF_SETPOINT)
proportional = config.get(CONF_PROPORTIONAL)
integral = config.get(CONF_INTEGRAL)
Expand All @@ -77,9 +91,12 @@ async def async_setup_platform(
round_type = config.get(CONF_ROUND)
sample_time = config.get(CONF_SAMPLE_TIME)
windup = config.get(CONF_WINDUP)
unit_of_measurement = config.get(CONF_UNIT_OF_MEASUREMENT)
device_class = config.get(CONF_DEVICE_CLASS)

## Process Templates.
for template in [
icon,
set_point,
sample_time,
windup,
Expand All @@ -91,6 +108,8 @@ async def async_setup_platform(
minimum,
maximum,
round_type,
unit_of_measurement,
device_class,
]:
if template is not None:
template.hass = hass
Expand All @@ -102,7 +121,10 @@ async def async_setup_platform(
hass,
config.get(CONF_UNIQUE_ID),
config.get(CONF_NAME),
icon,
set_point,
unit_of_measurement,
device_class,
sample_time,
windup,
proportional,
Expand All @@ -128,7 +150,10 @@ def __init__(
hass: HomeAssistant,
unique_id,
name,
icon,
set_point,
unit_of_measurement,
device_class,
sample_time,
windup,
proportional,
Expand All @@ -142,7 +167,10 @@ def __init__(
entity_id,
):
self._attr_name = name
self._icon_template = icon
self._set_point_template = set_point
self._unit_of_measurement_template = unit_of_measurement
self._device_class_template = device_class
self._sample_time_template = sample_time
self._windup_template = windup
self._attr_state = 0
Expand Down Expand Up @@ -183,6 +211,19 @@ def available(self) -> bool:
"""Return True if entity is available."""
return True

@property
def icon(self) -> str:
"""Returns Icon"""
icon = DEFAULT_ICON
if self._icon_template is not None:
try:
icon = self._icon_template.async_render(parse_result=False)
except (TemplateError, TypeError) as ex:
self.show_template_exception(ex, CONF_ICON)
icon = DEFAULT_ICON

return icon

@property
def state(self) -> StateType:
"""Return the state of the sensor."""
Expand Down Expand Up @@ -273,6 +314,36 @@ def set_point(self) -> float:

return float(0)

@property
def unit_of_measurement(self) -> str:
"""Returns Unit Of Measurement"""

if self._unit_of_measurement_template is not None:
try:
unit_of_measurement = self._unit_of_measurement_template.async_render(
parse_result=False
)
except (TemplateError, TypeError) as ex:
self.show_template_exception(ex, CONF_UNIT_OF_MEASUREMENT)
unit_of_measurement = DEFAULT_UNIT_OF_MEASUREMENT

return unit_of_measurement

@property
def device_class(self) -> SensorDeviceClass:
"""Returns Device Class"""

if self._device_class_template is not None:
try:
device_class = self._device_class_template.async_render(
parse_result=False
)
except (TemplateError, TypeError) as ex:
self.show_template_exception(ex, CONF_DEVICE_CLASS)
device_class = DEFAULT_DEVICE_CLASS

return device_class

@property
def sample_time(self) -> int:
"""Returns Sample Time"""
Expand Down Expand Up @@ -523,6 +594,14 @@ def _get_entities(self) -> None:
self._entities = []
self._force_update = []

if self._icon_template is not None:
try:
info = self._icon_template.async_render_to_info()
except (TemplateError, TypeError) as ex:
self.show_template_exception(ex, CONF_ICON)
else:
self._entities += info.entities

if self._set_point_template is not None:
try:
info = self._set_point_template.async_render_to_info()
Expand All @@ -532,6 +611,24 @@ def _get_entities(self) -> None:
self._entities += info.entities
self._reset_pid += info.entities

if self._unit_of_measurement_template is not None:
try:
info = self._unit_of_measurement_template.async_render_to_info()
except (TemplateError, TypeError) as ex:
self.show_template_exception(ex, CONF_UNIT_OF_MEASUREMENT)
else:
self._entities += info.entities
self._force_update += info.entities

if self._device_class_template is not None:
try:
info = self._device_class_template.async_render_to_info()
except (TemplateError, TypeError) as ex:
self.show_template_exception(ex, CONF_DEVICE_CLASS)
else:
self._entities += info.entities
self._force_update += info.entities

if self._sample_time_template is not None:
try:
info = self._sample_time_template.async_render_to_info()
Expand Down

0 comments on commit ad3e037

Please sign in to comment.