mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Add toggle service to input_boolean (#3432)
This commit is contained in:
parent
c89a77dc74
commit
de4cc5034e
@ -9,7 +9,8 @@ import logging
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON, STATE_ON)
|
ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON, SERVICE_TOGGLE,
|
||||||
|
STATE_ON)
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import ToggleEntity
|
from homeassistant.helpers.entity import ToggleEntity
|
||||||
from homeassistant.helpers.entity_component import EntityComponent
|
from homeassistant.helpers.entity_component import EntityComponent
|
||||||
@ -25,7 +26,7 @@ CONF_NAME = "name"
|
|||||||
CONF_INITIAL = "initial"
|
CONF_INITIAL = "initial"
|
||||||
CONF_ICON = "icon"
|
CONF_ICON = "icon"
|
||||||
|
|
||||||
TOGGLE_SERVICE_SCHEMA = vol.Schema({
|
SERVICE_SCHEMA = vol.Schema({
|
||||||
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
|
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -45,6 +46,11 @@ def turn_off(hass, entity_id):
|
|||||||
hass.services.call(DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id})
|
hass.services.call(DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id})
|
||||||
|
|
||||||
|
|
||||||
|
def toggle(hass, entity_id):
|
||||||
|
"""Set input_boolean to False."""
|
||||||
|
hass.services.call(DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: entity_id})
|
||||||
|
|
||||||
|
|
||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
"""Set up input boolean."""
|
"""Set up input boolean."""
|
||||||
if not isinstance(config.get(DOMAIN), dict):
|
if not isinstance(config.get(DOMAIN), dict):
|
||||||
@ -72,20 +78,24 @@ def setup(hass, config):
|
|||||||
if not entities:
|
if not entities:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def toggle_service(service):
|
def handler_service(service):
|
||||||
"""Handle a calls to the input boolean services."""
|
"""Handle a calls to the input boolean services."""
|
||||||
target_inputs = component.extract_from_service(service)
|
target_inputs = component.extract_from_service(service)
|
||||||
|
|
||||||
for input_b in target_inputs:
|
for input_b in target_inputs:
|
||||||
if service.service == SERVICE_TURN_ON:
|
if service.service == SERVICE_TURN_ON:
|
||||||
input_b.turn_on()
|
input_b.turn_on()
|
||||||
else:
|
elif service.service == SERVICE_TURN_OFF:
|
||||||
input_b.turn_off()
|
input_b.turn_off()
|
||||||
|
else:
|
||||||
|
input_b.toggle()
|
||||||
|
|
||||||
hass.services.register(DOMAIN, SERVICE_TURN_OFF, toggle_service,
|
hass.services.register(DOMAIN, SERVICE_TURN_OFF, handler_service,
|
||||||
schema=TOGGLE_SERVICE_SCHEMA)
|
schema=SERVICE_SCHEMA)
|
||||||
hass.services.register(DOMAIN, SERVICE_TURN_ON, toggle_service,
|
hass.services.register(DOMAIN, SERVICE_TURN_ON, handler_service,
|
||||||
schema=TOGGLE_SERVICE_SCHEMA)
|
schema=SERVICE_SCHEMA)
|
||||||
|
hass.services.register(DOMAIN, SERVICE_TOGGLE, handler_service,
|
||||||
|
schema=SERVICE_SCHEMA)
|
||||||
|
|
||||||
component.add_entities(entities)
|
component.add_entities(entities)
|
||||||
|
|
||||||
|
@ -63,6 +63,13 @@ class TestInputBoolean(unittest.TestCase):
|
|||||||
self.assertFalse(
|
self.assertFalse(
|
||||||
input_boolean.is_on(self.hass, entity_id))
|
input_boolean.is_on(self.hass, entity_id))
|
||||||
|
|
||||||
|
input_boolean.toggle(self.hass, entity_id)
|
||||||
|
|
||||||
|
self.hass.block_till_done()
|
||||||
|
|
||||||
|
self.assertTrue(
|
||||||
|
input_boolean.is_on(self.hass, entity_id))
|
||||||
|
|
||||||
def test_config_options(self):
|
def test_config_options(self):
|
||||||
"""Test configuration options."""
|
"""Test configuration options."""
|
||||||
count_start = len(self.hass.states.entity_ids())
|
count_start = len(self.hass.states.entity_ids())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user