1 """Support to keep track of user controlled booleans for within automation."""
3 from __future__
import annotations
6 from typing
import Any, Self
8 import voluptuous
as vol
32 DOMAIN =
"input_boolean"
34 _LOGGER = logging.getLogger(__name__)
36 CONF_INITIAL =
"initial"
38 STORAGE_FIELDS: VolDictType = {
39 vol.Required(CONF_NAME): vol.All(str, vol.Length(min=1)),
40 vol.Optional(CONF_INITIAL): cv.boolean,
41 vol.Optional(CONF_ICON): cv.icon,
44 CONFIG_SCHEMA = vol.Schema(
46 DOMAIN: cv.schema_with_slug_keys(
49 vol.Optional(CONF_NAME): cv.string,
50 vol.Optional(CONF_INITIAL): cv.boolean,
51 vol.Optional(CONF_ICON): cv.icon,
57 extra=vol.ALLOW_EXTRA,
60 RELOAD_SERVICE_SCHEMA = vol.Schema({})
66 """Input boolean collection stored in storage."""
68 CREATE_UPDATE_SCHEMA = vol.Schema(STORAGE_FIELDS)
71 """Validate the config is valid."""
76 """Suggest an ID based on the config."""
77 return info[CONF_NAME]
79 async
def _update_data(self, item: dict, update_data: dict) -> dict:
80 """Return a new updated data object."""
82 return {CONF_ID: item[CONF_ID]} | update_data
86 def is_on(hass: HomeAssistant, entity_id: str) -> bool:
87 """Test if input_boolean is True."""
88 return hass.states.is_state(entity_id, STATE_ON)
91 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
92 """Set up an input boolean."""
93 component = EntityComponent[InputBoolean](_LOGGER, DOMAIN, hass)
95 id_manager = collection.IDManager()
97 yaml_collection = collection.YamlCollection(
98 logging.getLogger(f
"{__name__}.yaml_collection"), id_manager
100 collection.sync_entity_lifecycle(
101 hass, DOMAIN, DOMAIN, component, yaml_collection, InputBoolean
105 Store(hass, STORAGE_VERSION, STORAGE_KEY),
108 collection.sync_entity_lifecycle(
109 hass, DOMAIN, DOMAIN, component, storage_collection, InputBoolean
112 await yaml_collection.async_load(
113 [{CONF_ID: id_, **(conf
or {})}
for id_, conf
in config.get(DOMAIN, {}).items()]
115 await storage_collection.async_load()
117 collection.DictStorageCollectionWebsocket(
118 storage_collection, DOMAIN, DOMAIN, STORAGE_FIELDS, STORAGE_FIELDS
121 async
def reload_service_handler(service_call: ServiceCall) ->
None:
122 """Remove all input booleans and load new ones from config."""
123 conf = await component.async_prepare_reload(skip_reset=
True)
126 await yaml_collection.async_load(
128 {CONF_ID: id_, **(conf
or {})}
129 for id_, conf
in conf.get(DOMAIN, {}).items()
137 reload_service_handler,
138 schema=RELOAD_SERVICE_SCHEMA,
141 component.async_register_entity_service(SERVICE_TURN_ON,
None,
"async_turn_on")
143 component.async_register_entity_service(SERVICE_TURN_OFF,
None,
"async_turn_off")
145 component.async_register_entity_service(SERVICE_TOGGLE,
None,
"async_toggle")
151 """Representation of a boolean input."""
153 _unrecorded_attributes = frozenset({ATTR_EDITABLE})
155 _attr_should_poll =
False
159 """Initialize a boolean input."""
166 """Return entity instance initialized from storage."""
167 input_bool = cls(config)
168 input_bool.editable =
True
173 """Return entity instance initialized from yaml."""
174 input_bool = cls(config)
175 input_bool.entity_id = f
"{DOMAIN}.{config[CONF_ID]}"
176 input_bool.editable =
False
181 """Return name of the boolean input."""
186 """Return the icon to be used for this entity."""
191 """Return the state attributes of the entity."""
192 return {ATTR_EDITABLE: self.editable}
195 """Call when entity about to be added to hass."""
198 if self.
_config_config.
get(CONF_INITIAL)
is not None:
202 self.
_attr_is_on_attr_is_on = state
is not None and state.state == STATE_ON
205 """Turn the entity on."""
210 """Turn the entity off."""
215 """Handle when the config is updated."""
None async_write_ha_state(self)
State|None async_get_last_state(self)
web.Response get(self, web.Request request, str config_key)
None async_register_admin_service(HomeAssistant hass, str domain, str service, Callable[[ServiceCall], Awaitable[None]|None] service_func, VolSchemaType schema=vol.Schema({}, extra=vol.PREVENT_EXTRA))