From d78103cf16af627c8fa953bd7cfc5563d519d4a4 Mon Sep 17 00:00:00 2001 From: anders Date: Mon, 8 Jan 2024 17:44:32 +0100 Subject: [PATCH] binary_sensor implementation copied from ha default --- ThermIQ_Card.yaml | 18 ++++++------ .../thermiq_mqtt/binary_sensor.py | 28 ++++++++++++++----- .../thermiq_mqtt/heatpump/__init__.py | 2 +- .../thermiq_mqtt/input_number.py | 4 +-- custom_components/thermiq_mqtt/sensor.py | 4 +-- 5 files changed, 35 insertions(+), 21 deletions(-) diff --git a/ThermIQ_Card.yaml b/ThermIQ_Card.yaml index da662ac..4cba9bc 100644 --- a/ThermIQ_Card.yaml +++ b/ThermIQ_Card.yaml @@ -22,12 +22,12 @@ entities: @keyframes blink { to {visibility: hidden;}} -
+
- {% if states('binary_sensor.thermiq_mqtt_vp1_hotwaterproduction_on')=='True' %} + {% if states('binary_sensor.thermiq_mqtt_vp1_hotwaterproduction_on')=='on' %}
vp missing
{% else %} - {% if (states('binary_sensor.thermiq_mqtt_vp1_opt_hgw_installed')=='True') and (states('binary_sensor.thermiq_mqtt_vp1_compressor_on')=='True') %} + {% if (states('binary_sensor.thermiq_mqtt_vp1_opt_hgw_installed')=='on') and (states('binary_sensor.thermiq_mqtt_vp1_compressor_on')=='on') %}
vp missing
{% else %}
vp missing
@@ -39,14 +39,14 @@ entities:
{{ states('sensor.thermiq_mqtt_vp1_boiler_t') }} C
{{ states('sensor.thermiq_mqtt_vp1_hgw_water_t') }} C
{{ states('sensor.thermiq_mqtt_vp1_supplyline_t') }} C
-
{{ states('sensor.thermiq_mqtt_vp1_supply_pump_speed') }} %
-
{{ states('sensor.thermiq_mqtt_vp1_brine_pump_speed') }} %
+
{{ states('sensor.thermiq_mqtt_vp1_supply_pump_speed') }} %
+
{{ states('sensor.thermiq_mqtt_vp1_brine_pump_speed') }} %
{{ states('sensor.thermiq_mqtt_vp1_returnline_t') }} C
{{ states('sensor.thermiq_mqtt_vp1_pressurepipe_t') }} C
- - - - + + + + EVU
diff --git a/custom_components/thermiq_mqtt/binary_sensor.py b/custom_components/thermiq_mqtt/binary_sensor.py index cc88e5a..ec65b7b 100644 --- a/custom_components/thermiq_mqtt/binary_sensor.py +++ b/custom_components/thermiq_mqtt/binary_sensor.py @@ -1,15 +1,23 @@ import logging +from typing import TYPE_CHECKING, Literal, final from homeassistant.core import HomeAssistant, callback from homeassistant.const import STATE_OFF, STATE_ON -from homeassistant.components.binary_sensor import BinarySensorEntity +from homeassistant.components.binary_sensor import ( + BinarySensorDeviceClass, + BinarySensorEntity, + BinarySensorEntityDescription, +) from homeassistant.const import ( ATTR_IDENTIFIERS, ATTR_MANUFACTURER, ATTR_MODEL, ATTR_NAME, -) + STATE_OFF, + STATE_ON, + EntityCategory) + from homeassistant.helpers.device_registry import DeviceEntryType from homeassistant.const import ( @@ -31,6 +39,11 @@ reg_id, ) +if TYPE_CHECKING: + from functools import cached_property +else: + from homeassistant.backports.functools import cached_property + _LOGGER = logging.getLogger(__name__) @@ -130,19 +143,20 @@ def should_poll(self): """No need to poll. Coordinator notifies entity of updates.""" return False + @final @property - def state(self): + def state(self) -> Literal["on", "off"]: """Return the state of the sensor.""" - return True if (self._state) else False + return STATE_ON if (self._state) else STATE_OFF @property def vp_reg(self): """Return the device class of the sensor.""" return self._vp_reg - @property - def is_on(self): - return STATE_ON if (self._state) else STATE_OFF + @cached_property + def is_on(self) -> bool: + return (self._state==True) @property def sorter(self): diff --git a/custom_components/thermiq_mqtt/heatpump/__init__.py b/custom_components/thermiq_mqtt/heatpump/__init__.py index a0e214c..3a4b477 100644 --- a/custom_components/thermiq_mqtt/heatpump/__init__.py +++ b/custom_components/thermiq_mqtt/heatpump/__init__.py @@ -247,7 +247,7 @@ async def update_config(self, entry): self._set_topic = self._mqtt_base + "set" self._hpstate["mqtt_counter"] = 0 - # Provide some debug info + # Provide some debug info _LOGGER.debug( f"INFO: {self._domain}_{self._id} mqtt_node: [{entry.data[CONF_MQTT_NODE]}]" ) diff --git a/custom_components/thermiq_mqtt/input_number.py b/custom_components/thermiq_mqtt/input_number.py index e55af0c..220aff4 100644 --- a/custom_components/thermiq_mqtt/input_number.py +++ b/custom_components/thermiq_mqtt/input_number.py @@ -16,7 +16,7 @@ CONF_MODE, CONF_NAME, CONF_UNIT_OF_MEASUREMENT, - TEMP_CELSIUS, + UnitOfTemperature, ) from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity_platform import EntityPlatform @@ -120,7 +120,7 @@ def create_input_number_entity(heatpump, name) -> CustomInputNumber: ] ): icon = "mdi:temperature-celsius" - unit = TEMP_CELSIUS + unit = UnitOfTemperature.CELSIUS else: unit = reg_id[name][2] icon = "mdi:gauge" diff --git a/custom_components/thermiq_mqtt/sensor.py b/custom_components/thermiq_mqtt/sensor.py index eeb924e..dc51b08 100644 --- a/custom_components/thermiq_mqtt/sensor.py +++ b/custom_components/thermiq_mqtt/sensor.py @@ -14,7 +14,7 @@ from homeassistant.helpers.entity import Entity, async_generate_entity_id from homeassistant.const import ( - TEMP_CELSIUS, + UnitOfTemperature, PERCENTAGE ) @@ -126,7 +126,7 @@ def __init__( ] ): self._icon = "mdi:temperature-celsius" - self._unit =TEMP_CELSIUS + self._unit =UnitOfTemperature.CELSIUS elif vp_type in [ "sensor_boolean", ]: