Skip to content

Commit

Permalink
Merge pull request #15 from bj00rn/feat/add-virtual-sensor-for-target…
Browse files Browse the repository at this point in the history
…-temperature

Feat/add virtual sensor for target temperature
  • Loading branch information
bj00rn authored Nov 17, 2023
2 parents e2c1067 + e14b7ba commit 73c0b6d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Name | Description | Unit | State attributes
`system_name` | control system name | `str` |
`system_version` | control system version | `str` |
`system_warning` | system warning | boolean | system error codes
`target_temperature` | target air temperature | `°C` |
`temperature_mode` | current temperature mode setting | `str` |
`ventilation_mode` | current ventilation mode setting | `str` |

Expand Down
3 changes: 2 additions & 1 deletion custom_components/saleryd_hrv/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"""

# Other constants
CLIENT_STATE = "*HRV_CLIENT_STATE"
KEY_CLIENT_STATE = "*HRV_CLIENT_STATE"
KEY_TARGET_TEMPERATURE = "*TARGET_TEMPERATURE"

TEMPERATURE_MODE_NORMAL = 0
TEMPERATURE_MODE_ECO = 1
Expand Down
9 changes: 7 additions & 2 deletions custom_components/saleryd_hrv/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from pysaleryd.client import Client

from .const import CLIENT_STATE, DOMAIN
from .const import DOMAIN, KEY_CLIENT_STATE, KEY_TARGET_TEMPERATURE

_LOGGER: logging.Logger = logging.getLogger(__package__)

Expand All @@ -20,8 +20,13 @@ def __init__(self, hass: HomeAssistant, client: Client, update_interval) -> None
self.client = client
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=update_interval)

def inject_virtual_keys(self, data):
"""Inject additional keys for virtual sensors not present in the data set"""
data[KEY_CLIENT_STATE] = self.client.state
data[KEY_TARGET_TEMPERATURE] = None

async def _async_update_data(self):
"""Fetch the latest data from the source."""
data = self.client.data
data[CLIENT_STATE] = self.client.state
self.inject_virtual_keys(data)
return data
44 changes: 36 additions & 8 deletions custom_components/saleryd_hrv/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
from homeassistant.util import Throttle, slugify

from .const import (
CLIENT_STATE,
DEFAULT_NAME,
DOMAIN,
HEATER_ACTIVE_MODE_OFF,
HEATER_ACTIVE_MODE_ON,
HEATER_MODE_HIGH,
HEATER_MODE_LOW,
ISSUE_URL,
KEY_CLIENT_STATE,
KEY_TARGET_TEMPERATURE,
SUPPORTED_FIRMWARES,
SYSTEM_ACTIVE_MODE_OFF,
SYSTEM_ACTIVE_MODE_ON,
Expand Down Expand Up @@ -98,6 +99,19 @@ def _translate_value(self, value):
return any(value)

value = value[0] if isinstance(value, list) else value

if self.entity_description.key == KEY_TARGET_TEMPERATURE:
try:
temperature_mode = self.coordinator.data.get("MT")[0]
if temperature_mode == TEMPERATURE_MODE_COOL:
return self.coordinator.data.get("TF")[0]
elif temperature_mode == TEMPERATURE_MODE_ECO:
return self.coordinator.data.get("TE")[0]
elif temperature_mode == TEMPERATURE_MODE_NORMAL:
return self.coordinator.data.get("TD")[0]
except TypeError as exc:
_LOGGER.debug(exc)

if self.entity_description.key == "MG":
if value == HEATER_MODE_LOW:
return 900
Expand Down Expand Up @@ -172,12 +186,15 @@ def extra_state_attributes(self) -> dict[str, Any] | None:
value = value[0] if isinstance(value, list) else value

if self.entity_description.key == "MT":
if value == TEMPERATURE_MODE_COOL:
attrs["target_temperature"] = self.coordinator.data.get("TF")[0]
elif value == TEMPERATURE_MODE_ECO:
attrs["target_temperature"] = self.coordinator.data.get("TE")[0]
elif value == TEMPERATURE_MODE_NORMAL:
attrs["target_temperature"] = self.coordinator.data.get("TD")[0]
try:
if value == TEMPERATURE_MODE_COOL:
attrs["target_temperature"] = self.coordinator.data.get("TF")[0]
elif value == TEMPERATURE_MODE_ECO:
attrs["target_temperature"] = self.coordinator.data.get("TE")[0]
elif value == TEMPERATURE_MODE_NORMAL:
attrs["target_temperature"] = self.coordinator.data.get("TD")[0]
except TypeError as exc:
_LOGGER.debug(exc)
elif self.entity_description.key == "MF" and value == VENTILATION_MODE_BOOST:
attrs["minutes_left"] = self.coordinator.data.get("*FI")

Expand Down Expand Up @@ -277,6 +294,17 @@ def extra_state_attributes(self) -> dict[str, Any] | None:
device_class=SensorDeviceClass.ENUM,
),
},
"target_temperature": {
"klass": SalerydLokeSensor,
"description": SensorEntityDescription(
key=KEY_TARGET_TEMPERATURE,
icon="mdi:home-thermometer",
name="Target temperature",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
},
"temperature_mode": {
"klass": SalerydLokeSensor,
"description": SensorEntityDescription(
Expand Down Expand Up @@ -342,7 +370,7 @@ def extra_state_attributes(self) -> dict[str, Any] | None:
"connection_state": {
"klass": SalerydLokeSensor,
"description": SensorEntityDescription(
key=CLIENT_STATE,
key=KEY_CLIENT_STATE,
icon="mdi:wrench-clock",
name="Connection state",
device_class=SensorDeviceClass.ENUM,
Expand Down

0 comments on commit 73c0b6d

Please sign in to comment.