Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add templating to MQTT Cover tilt_status #24355

Merged
merged 1 commit into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions homeassistant/components/mqtt/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
CONF_SET_POSITION_TOPIC = 'set_position_topic'
CONF_TILT_COMMAND_TOPIC = 'tilt_command_topic'
CONF_TILT_STATUS_TOPIC = 'tilt_status_topic'
CONF_TILT_STATUS_TEMPLATE = 'tilt_status_template'

CONF_PAYLOAD_CLOSE = 'payload_close'
CONF_PAYLOAD_OPEN = 'payload_open'
Expand Down Expand Up @@ -110,6 +111,7 @@ def validate_options(value):
vol.Optional(CONF_TILT_STATE_OPTIMISTIC,
default=DEFAULT_TILT_OPTIMISTIC): cv.boolean,
vol.Optional(CONF_TILT_STATUS_TOPIC): mqtt.valid_subscribe_topic,
vol.Optional(CONF_TILT_STATUS_TEMPLATE): cv.template,
vol.Optional(CONF_UNIQUE_ID): cv.string,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
}).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema).extend(
Expand Down Expand Up @@ -203,17 +205,26 @@ async def _subscribe_topics(self):
set_position_template = self._config.get(CONF_SET_POSITION_TEMPLATE)
if set_position_template is not None:
set_position_template.hass = self.hass
tilt_status_template = self._config.get(CONF_TILT_STATUS_TEMPLATE)
if tilt_status_template is not None:
tilt_status_template.hass = self.hass

topics = {}

@callback
def tilt_updated(msg):
"""Handle tilt updates."""
if (msg.payload.isnumeric() and
(self._config[CONF_TILT_MIN] <= int(msg.payload) <=
payload = msg.payload
if tilt_status_template is not None:
payload = \
tilt_status_template.async_render_with_possible_json_value(
payload)

if (payload.isnumeric() and
(self._config[CONF_TILT_MIN] <= int(payload) <=
self._config[CONF_TILT_MAX])):

level = self.find_percentage_in_range(float(msg.payload))
level = self.find_percentage_in_range(float(payload))
self._tilt_value = level
self.async_write_ha_state()

Expand Down
74 changes: 74 additions & 0 deletions tests/components/mqtt/test_cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,39 @@ async def test_tilt_via_topic(hass, mqtt_mock):
assert current_cover_tilt_position == 50


async def test_tilt_via_topic_template(hass, mqtt_mock):
"""Test tilt by updating status via MQTT and template."""
assert await async_setup_component(hass, cover.DOMAIN, {
cover.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
'command_topic': 'command-topic',
'qos': 0,
'payload_open': 'OPEN',
'payload_close': 'CLOSE',
'payload_stop': 'STOP',
'tilt_command_topic': 'tilt-command-topic',
'tilt_status_topic': 'tilt-status-topic',
'tilt_status_template': '{{ (value | multiply(0.01)) | int }}',
'tilt_opened_value': 400,
'tilt_closed_value': 125
}
})

async_fire_mqtt_message(hass, 'tilt-status-topic', '99')

current_cover_tilt_position = hass.states.get(
'cover.test').attributes['current_tilt_position']
assert current_cover_tilt_position == 0

async_fire_mqtt_message(hass, 'tilt-status-topic', '5000')

current_cover_tilt_position = hass.states.get(
'cover.test').attributes['current_tilt_position']
assert current_cover_tilt_position == 50


async def test_tilt_via_topic_altered_range(hass, mqtt_mock):
"""Test tilt status via MQTT with altered tilt range."""
assert await async_setup_component(hass, cover.DOMAIN, {
Expand Down Expand Up @@ -643,6 +676,47 @@ async def test_tilt_via_topic_altered_range(hass, mqtt_mock):
assert current_cover_tilt_position == 50


async def test_tilt_via_topic_template_altered_range(hass, mqtt_mock):
"""Test tilt status via MQTT and template with altered tilt range."""
assert await async_setup_component(hass, cover.DOMAIN, {
cover.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
'command_topic': 'command-topic',
'qos': 0,
'payload_open': 'OPEN',
'payload_close': 'CLOSE',
'payload_stop': 'STOP',
'tilt_command_topic': 'tilt-command-topic',
'tilt_status_topic': 'tilt-status-topic',
'tilt_status_template': '{{ (value | multiply(0.01)) | int }}',
'tilt_opened_value': 400,
'tilt_closed_value': 125,
'tilt_min': 0,
'tilt_max': 50
}
})

async_fire_mqtt_message(hass, 'tilt-status-topic', '99')

current_cover_tilt_position = hass.states.get(
'cover.test').attributes['current_tilt_position']
assert current_cover_tilt_position == 0

async_fire_mqtt_message(hass, 'tilt-status-topic', '5000')

current_cover_tilt_position = hass.states.get(
'cover.test').attributes['current_tilt_position']
assert current_cover_tilt_position == 100

async_fire_mqtt_message(hass, 'tilt-status-topic', '2500')

current_cover_tilt_position = hass.states.get(
'cover.test').attributes['current_tilt_position']
assert current_cover_tilt_position == 50


async def test_tilt_position(hass, mqtt_mock):
"""Test tilt via method invocation."""
assert await async_setup_component(hass, cover.DOMAIN, {
Expand Down