From 512a23d5941f310562fbddfeb72d6e5590d09c52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=2E=20=C3=81rkosi=20R=C3=B3bert?= Date: Wed, 20 Jul 2022 17:27:22 +0200 Subject: [PATCH] Availability template added --- README.md | 2 + .../cover_rf_time_based/cover.py | 43 +++++++++++++++++-- .../cover_rf_time_based/manifest.json | 2 +- info.md | 2 +- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index cc5ad31..b91a1cb 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ cover: send_stop_at_ends: False #optional always_confident: False #optional device_class: garage #optional + availability_template: "{{ is_state('binary_sensor.rfbridge_status', 'on') }}" #optional ``` **OR**: @@ -65,6 +66,7 @@ Optional settings: - `send_stop_at_ends` defaults to `False`. If set to `True`, the Stop script will be run after the cover reaches to 0 or 100 (closes or opens completely). This is for people who use interlocked relays in the scripts to drive the covers, which need to be released when the covers reach the end positions. - `always_confident` defaults to `False`. If set to `True`, the calculated state will always be assumed to be accurate. This mainly affects UI components - for example, if the cover is fully opened, the open button will be disabled. Make sure to [set](#cover_rf_time_basedset_known_position) the current state after first setup and only use this entity to control the covers. Not recommended to be `True` for RF-based covers. - `device_class` defaults to `shutter` if not specified. See the docs for availale [cover device classes](http://dev-docs.home-assistant.io/en/master/api/components.html#homeassistant.components.cover.CoverDeviceClass). +- `availability_template` if not specified will make the cover always available. You can use a template returning `True` or `False` in order to toggle availability of the cover based on other elements. Useful to link with the connection status of your RF Bridge or relays device. ### Example scripts.yaml entry #### RF covers diff --git a/custom_components/cover_rf_time_based/cover.py b/custom_components/cover_rf_time_based/cover.py index 437def3..cde5ce4 100644 --- a/custom_components/cover_rf_time_based/cover.py +++ b/custom_components/cover_rf_time_based/cover.py @@ -23,6 +23,7 @@ SERVICE_CLOSE_COVER, SERVICE_OPEN_COVER, SERVICE_STOP_COVER, + STATE_UNAVAILABLE, ) import homeassistant.helpers.config_validation as cv @@ -45,6 +46,7 @@ CONF_CLOSE_SCRIPT_ENTITY_ID = 'close_script_entity_id' CONF_STOP_SCRIPT_ENTITY_ID = 'stop_script_entity_id' CONF_COVER_ENTITY_ID = 'cover_entity_id' +CONF_AVAILABILITY_TPL = 'availability_template' ATTR_CONFIDENT = 'confident' ATTR_ACTION = 'action' ATTR_POSITION_TYPE = 'position_type' @@ -63,6 +65,7 @@ vol.Optional(CONF_SEND_STOP_AT_ENDS, default=DEFAULT_SEND_STOP_AT_ENDS): cv.boolean, vol.Optional(CONF_ALWAYS_CONFIDENT, default=DEFAULT_ALWAYS_CONFIDENT): cv.boolean, vol.Optional(CONF_DEVICE_CLASS, default=DEFAULT_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA, + vol.Optional(CONF_AVAILABILITY_TPL): cv.template, } ) @@ -125,7 +128,19 @@ def devices_from_config(domain_config): send_stop_at_ends = config.pop(CONF_SEND_STOP_AT_ENDS) always_confident = config.pop(CONF_ALWAYS_CONFIDENT) device_class = config.pop(CONF_DEVICE_CLASS) - device = CoverTimeBased(device_id, name, travel_time_down, travel_time_up, open_script_entity_id, close_script_entity_id, stop_script_entity_id, cover_entity_id, send_stop_at_ends, always_confident, device_class) + availability_template = config.pop(CONF_AVAILABILITY_TPL, None) + device = CoverTimeBased(device_id, + name, + travel_time_down, + travel_time_up, + open_script_entity_id, + close_script_entity_id, + stop_script_entity_id, + cover_entity_id, + send_stop_at_ends, + always_confident, + device_class, + availability_template) devices.append(device) return devices @@ -146,7 +161,19 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= class CoverTimeBased(CoverEntity, RestoreEntity): - def __init__(self, device_id, name, travel_time_down, travel_time_up, open_script_entity_id, close_script_entity_id, stop_script_entity_id, cover_entity_id, send_stop_at_ends, always_confident, device_class): + def __init__(self, + device_id, + name, + travel_time_down, + travel_time_up, + open_script_entity_id, + close_script_entity_id, + stop_script_entity_id, + cover_entity_id, + send_stop_at_ends, + always_confident, + device_class, + availability_template): """Initialize the cover.""" from xknx.devices import TravelCalculator self._travel_time_down = travel_time_down @@ -162,6 +189,7 @@ def __init__(self, device_id, name, travel_time_down, travel_time_up, open_scrip self._target_position = 0 self._processing_known_position = False self._unique_id = device_id + self._availability_template = availability_template if name: self._name = name @@ -195,9 +223,18 @@ def _handle_stop(self): @property def unconfirmed_state(self): - """Return the assume state as a string to persist through restarts .""" + """Return the assume state as a string to persist through restarts.""" return str(self._assume_uncertain_position) + @property + def available(self): + """Return availability based on external template. Always available if no template specified.""" + if self._availability_template is None: + return True + else: + self._availability_template.hass = self.hass + return self._availability_template.async_render() + @property def name(self): """Return the name of the cover.""" diff --git a/custom_components/cover_rf_time_based/manifest.json b/custom_components/cover_rf_time_based/manifest.json index 2eefd59..b419a01 100644 --- a/custom_components/cover_rf_time_based/manifest.json +++ b/custom_components/cover_rf_time_based/manifest.json @@ -1,7 +1,7 @@ { "domain": "cover_rf_time_based", "name": "Cover Time Based RF (trigger script)", - "version": "1.0.8", + "version": "1.1.2", "documentation": "https://github.com/nagyrobi/home-assistant-custom-components-cover-rf-time-based", "iot_class": "assumed_state", "requirements": [ diff --git a/info.md b/info.md index 59e7141..1bca3be 100644 --- a/info.md +++ b/info.md @@ -9,7 +9,7 @@ With this component you can add a time-based cover. You have to set triggering s - State can be updated based on independent, external sensors (for example a contact or reed sensor at closed or opened state) - State can mimic the operation based on external sensors (for example by monitoring the air for closing or opening RF codes) so usage in parrallel with controllers outside HA is possible - Ability to take care care of queuing the transmission of the codes and keeping an appropriate delay between them to minimize 'missed' commands -- Can be used on top of any existing cover integration, or directly with Tasmota or ESPHome firmwares running on various ESP-based modules. +- Can be used on top of any existing cover integration, or directly with ESPHome or Tasmota firmwares running on various ESP-based modules. ## Component authors & contributors "@davidramosweb",