Skip to content

Commit

Permalink
add sensor for system warnings and if translation of value fails
Browse files Browse the repository at this point in the history
  • Loading branch information
bj00rn committed Nov 9, 2023
1 parent 398e9bd commit 83ea059
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 25 deletions.
24 changes: 0 additions & 24 deletions custom_components/saleryd_hrv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
DOMAIN,
PLATFORMS,
STARTUP_MESSAGE,
SUPPORTED_FIRMWARES,
UNSUPPORTED_FIRMWARES,
)
from .coordinator import SalerydLokeDataUpdateCoordinator

Expand All @@ -28,24 +26,6 @@
_LOGGER: logging.Logger = logging.getLogger(__package__)


def log_unsupported_firmware(data):
"""Write to logs if firmware version is unsupported"""
version = data.get("*SC")
if version:
if version not in SUPPORTED_FIRMWARES:
_LOGGER.warning(
"Your control system version is (%s). This integration has been verified to work with the following versions: %s",
version,
", ".join(SUPPORTED_FIRMWARES),
)
if version in UNSUPPORTED_FIRMWARES:
_LOGGER.error(
"Your control system version is (%s). This integration is incompatible with the following versions: %s",
version,
", ".join(UNSUPPORTED_FIRMWARES),
)


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up this integration using UI."""
if hass.data.get(DOMAIN) is None:
Expand All @@ -70,11 +50,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
update_interval=SCAN_INTERVAL,
)

await asyncio.sleep(
SCAN_INTERVAL.seconds
) # sleep to ensure coordinator collects all data
await coordinator.async_config_entry_first_refresh()
log_unsupported_firmware(coordinator.data)

hass.data[DOMAIN][entry.entry_id] = coordinator

Expand Down
2 changes: 2 additions & 0 deletions custom_components/saleryd_hrv/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@

HEATER_MODE_LOW = 0
HEATER_MODE_HIGH = 1

WARNING_POWER_OFF = "Avstängd"
68 changes: 67 additions & 1 deletion custom_components/saleryd_hrv/sensor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Sensor platform"""

from datetime import timedelta
import logging
from typing import Any

from homeassistant.components.sensor import (
Expand All @@ -18,23 +20,29 @@
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.util import slugify
from homeassistant.util import Throttle, slugify

from .const import (
CLIENT_STATE,
DEFAULT_NAME,
DOMAIN,
HEATER_MODE_HIGH,
HEATER_MODE_LOW,
ISSUE_URL,
SUPPORTED_FIRMWARES,
TEMPERATURE_MODE_COOL,
TEMPERATURE_MODE_ECO,
TEMPERATURE_MODE_NORMAL,
UNSUPPORTED_FIRMWARES,
VENTILATION_MODE_AWAY,
VENTILATION_MODE_BOOST,
VENTILATION_MODE_HOME,
WARNING_POWER_OFF,
)
from .entity import SalerydLokeEntity

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


class SalerydLokeSensor(SalerydLokeEntity, SensorEntity):
"""Sensor base class."""
Expand All @@ -50,12 +58,45 @@ def __init__(

super().__init__(coordinator, entry_id, entity_description)

@Throttle(min_time=timedelta(minutes=1))
def _log_unknown_sensor_value(self, value):
_LOGGER.warning(
"Unknown value [%s] for sensor [%s] [%s], feel free to file an issue with the integration at %s and provide this message.",
value,
self.entity_description.name,
self.entity_description.key,
ISSUE_URL,
)

@Throttle(min_time=timedelta(days=1))
def _log_unsupported_firmware(self, version):
"""Write to logs if firmware version is unsupported"""
_LOGGER.error(
"Your control system version is (%s). This integration is incompatible with the following versions: %s",
version,
", ".join(UNSUPPORTED_FIRMWARES),
)

@Throttle(min_time=timedelta(days=1))
def _log_unknown_firmware(self, version):
_LOGGER.warning(
"Unknown support for your control system version (%s), feel free to file an issue with the integration at %s and provide this message. This integration has been verified to work with the following versions: %s",
version,
ISSUE_URL,
", ".join(SUPPORTED_FIRMWARES),
)

def _translate_value(self, value):
if value is None:
return value

if self.entity_description.key == "MG":
if value == HEATER_MODE_LOW:
return 900
elif value == HEATER_MODE_HIGH:
return 1800
else:
self._log_unknown_sensor_value(value)

if self.entity_description.key == "MT":
if value == TEMPERATURE_MODE_NORMAL:
Expand All @@ -64,6 +105,8 @@ def _translate_value(self, value):
return "Eco"
elif value == TEMPERATURE_MODE_COOL:
return "Cool"
else:
self._log_unknown_sensor_value(value)

if self.entity_description.key == "MF":
if value == VENTILATION_MODE_HOME:
Expand All @@ -72,6 +115,20 @@ def _translate_value(self, value):
return "Away"
elif value == VENTILATION_MODE_BOOST:
return "Boost"
else:
self._log_unknown_sensor_value(value)

if self.entity_description.key == "*EB":
if isinstance(value, str):
if WARNING_POWER_OFF in value:
return "Power off"
self._log_unknown_sensor_value(value)

if self.entity_description.key == "*SC":
if value not in SUPPORTED_FIRMWARES:
self._log_unknown_firmware(value)
if value in UNSUPPORTED_FIRMWARES:
self._log_unsupported_firmware(value)

return value

Expand Down Expand Up @@ -277,6 +334,15 @@ def extra_state_attributes(self) -> dict[str, Any] | None:
entity_category=EntityCategory.DIAGNOSTIC,
),
},
"control_system_warning": {
"klass": SalerydLokeSensor,
"description": SensorEntityDescription(
key="*EB",
icon="mdi:alert",
name="System warning",
device_class=SensorDeviceClass.ENUM,
),
},
}


Expand Down

0 comments on commit 83ea059

Please sign in to comment.