Skip to content

Commit

Permalink
Merge 4be04b4 into e0c956c
Browse files Browse the repository at this point in the history
  • Loading branch information
KNXBroker committed Feb 23, 2021
2 parents e0c956c + 4be04b4 commit 6129798
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/sensor.md
Expand Up @@ -20,6 +20,7 @@ Sensors are monitoring temperature, air humidity, pressure etc. from KNX bus.
* `always_callback` defines if a callback/update should always be triggered no matter if the previous and the new state are identical.
* `value_type` controls how the value should be rendered in a human readable representation. The attribut may have may have the values `percent`, `temperature`, `illuminance`, `speed_ms` or `current`.
* `device_updated_cb` awaitable callback for each update.
* `value_template` may be used to modify the state of the sensor in Home-Assistant (e.g. rounding or adding an offset; see [template documentation](https://www.home-assistant.io/docs/configuration/templating) for details).

## [](#header-2)Example

Expand Down
1 change: 1 addition & 0 deletions home-assistant-plugin/custom_components/xknx/factory.py
Expand Up @@ -290,6 +290,7 @@ def _create_sensor(knx_module: XKNX, config: ConfigType) -> XknxSensor:
sync_state=config[SensorSchema.CONF_SYNC_STATE],
always_callback=config[SensorSchema.CONF_ALWAYS_CALLBACK],
value_type=config[CONF_TYPE],
value_template=config.get(SensorSchema.CONF_VALUE_TEMPLATE),
)


Expand Down
3 changes: 3 additions & 0 deletions home-assistant-plugin/custom_components/xknx/schema.py
Expand Up @@ -11,6 +11,7 @@
CONF_NAME,
CONF_PORT,
CONF_TYPE,
CONF_VALUE_TEMPLATE,
)
import homeassistant.helpers.config_validation as cv

Expand Down Expand Up @@ -361,6 +362,7 @@ class SensorSchema:
CONF_ALWAYS_CALLBACK = "always_callback"
CONF_STATE_ADDRESS = CONF_STATE_ADDRESS
CONF_SYNC_STATE = CONF_SYNC_STATE
CONF_VALUE_TEMPLATE = CONF_VALUE_TEMPLATE
DEFAULT_NAME = "KNX Sensor"

SCHEMA = vol.Schema(
Expand All @@ -374,6 +376,7 @@ class SensorSchema:
vol.Optional(CONF_ALWAYS_CALLBACK, default=False): cv.boolean,
vol.Required(CONF_STATE_ADDRESS): cv.string,
vol.Required(CONF_TYPE): vol.Any(int, float, str),
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
}
)

Expand Down
13 changes: 10 additions & 3 deletions home-assistant-plugin/custom_components/xknx/sensor.py
Expand Up @@ -2,6 +2,7 @@
from xknx.devices import Sensor as XknxSensor

from homeassistant.components.sensor import DEVICE_CLASSES
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity

from .const import DOMAIN
Expand All @@ -13,21 +14,27 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
entities = []
for device in hass.data[DOMAIN].xknx.devices:
if isinstance(device, XknxSensor):
entities.append(KNXSensor(device))
entities.append(KNXSensor(device, hass))
async_add_entities(entities)


class KNXSensor(KnxEntity, Entity):
"""Representation of a KNX sensor."""

def __init__(self, device: XknxSensor):
def __init__(self, device: XknxSensor, hass: HomeAssistant):
"""Initialize of a KNX sensor."""
if device.value_template is not None:
device.value_template.hass = hass
super().__init__(device)

@property
def state(self):
"""Return the state of the sensor."""
return self._device.resolve_state()
state = self._device.resolve_state()
template = self._device.value_template
if template is not None and state is not None:
state = template.async_render({"value": state})
return state

@property
def unit_of_measurement(self) -> str:
Expand Down
1 change: 1 addition & 0 deletions test/config_tests/config_v1_test.py
Expand Up @@ -523,6 +523,7 @@ def test_config_sensor_temperature_type(self):
"Kitchen.Temperature",
group_address_state="2/0/2",
value_type="temperature",
value_template="{{ value | round(0) }}",
),
)

Expand Down
1 change: 1 addition & 0 deletions xknx.yaml
Expand Up @@ -273,6 +273,7 @@ groups:
group_address_state: "2/0/2",
value_type: "temperature",
sync_state: True,
value_template: "{{ value | round(0) }}",
}

expose_sensor:
Expand Down
4 changes: 4 additions & 0 deletions xknx/devices/sensor.py
Expand Up @@ -30,6 +30,7 @@ def __init__(
sync_state: bool = True,
always_callback: bool = False,
value_type: Optional[str] = None,
value_template: Optional[str] = None,
device_updated_cb: Optional[DeviceCallbackType] = None,
):
"""Initialize Sensor class."""
Expand Down Expand Up @@ -61,6 +62,7 @@ def __init__(
after_update_cb=self.after_update,
)
self.always_callback = always_callback
self.value_template = value_template

def _iter_remote_values(self) -> Iterator["RemoteValue[Any]"]:
"""Iterate the devices RemoteValue classes."""
Expand All @@ -73,6 +75,7 @@ def from_config(cls, xknx: "XKNX", name: str, config: Any) -> "Sensor":
sync_state = config.get("sync_state", True)
always_callback = config.get("always_callback", False)
value_type = config.get("value_type")
value_template = config.get("value_template")

return cls(
xknx,
Expand All @@ -81,6 +84,7 @@ def from_config(cls, xknx: "XKNX", name: str, config: Any) -> "Sensor":
sync_state=sync_state,
always_callback=always_callback,
value_type=value_type,
value_template=value_template,
)

async def process_group_write(self, telegram: "Telegram") -> None:
Expand Down

0 comments on commit 6129798

Please sign in to comment.