async def async_setup(opp: OpenPeerPower, config: ConfigType) -> bool: """Set up an input slider.""" component = EntityComponent(_LOGGER, DOMAIN, opp) id_manager = collection.IDManager() yaml_collection = collection.YamlCollection( logging.getLogger(f"{__name__}.yaml_collection"), id_manager) collection.sync_entity_lifecycle(opp, DOMAIN, DOMAIN, component, yaml_collection, InputNumber.from_yaml) storage_collection = NumberStorageCollection( Store(opp, STORAGE_VERSION, STORAGE_KEY), logging.getLogger(f"{__name__}.storage_collection"), id_manager, ) collection.sync_entity_lifecycle(opp, DOMAIN, DOMAIN, component, storage_collection, InputNumber) await yaml_collection.async_load([{ CONF_ID: id_, **(conf or {}) } for id_, conf in config.get(DOMAIN, {}).items()]) await storage_collection.async_load() collection.StorageCollectionWebsocket(storage_collection, DOMAIN, DOMAIN, CREATE_FIELDS, UPDATE_FIELDS).async_setup(opp) async def reload_service_handler(service_call: ServiceCall) -> None: """Reload yaml entities.""" conf = await component.async_prepare_reload(skip_reset=True) if conf is None: conf = {DOMAIN: {}} await yaml_collection.async_load([{ CONF_ID: id_, **conf } for id_, conf in conf.get(DOMAIN, {}).items()]) openpeerpower.helpers.service.async_register_admin_service( opp, DOMAIN, SERVICE_RELOAD, reload_service_handler, schema=RELOAD_SERVICE_SCHEMA, ) component.async_register_entity_service( SERVICE_SET_VALUE, {vol.Required(ATTR_VALUE): vol.Coerce(float)}, "async_set_value", ) component.async_register_entity_service(SERVICE_INCREMENT, {}, "async_increment") component.async_register_entity_service(SERVICE_DECREMENT, {}, "async_decrement") return True
async def async_setup(opp: OpenPeerPower, config: dict) -> bool: """Set up configured zones as well as Open Peer Power zone if necessary.""" component = entity_component.EntityComponent(_LOGGER, DOMAIN, opp) id_manager = collection.IDManager() yaml_collection = collection.IDLessCollection( logging.getLogger(f"{__name__}.yaml_collection"), id_manager) collection.sync_entity_lifecycle(opp, DOMAIN, DOMAIN, component, yaml_collection, Zone.from_yaml) storage_collection = ZoneStorageCollection( storage.Store(opp, STORAGE_VERSION, STORAGE_KEY), logging.getLogger(f"{__name__}.storage_collection"), id_manager, ) collection.sync_entity_lifecycle(opp, DOMAIN, DOMAIN, component, storage_collection, Zone) if config[DOMAIN]: await yaml_collection.async_load(config[DOMAIN]) await storage_collection.async_load() collection.StorageCollectionWebsocket(storage_collection, DOMAIN, DOMAIN, CREATE_FIELDS, UPDATE_FIELDS).async_setup(opp) async def reload_service_handler(service_call: ServiceCall) -> None: """Remove all zones and load new ones from config.""" conf = await component.async_prepare_reload(skip_reset=True) if conf is None: return await yaml_collection.async_load(conf[DOMAIN]) service.async_register_admin_service( opp, DOMAIN, SERVICE_RELOAD, reload_service_handler, schema=RELOAD_SERVICE_SCHEMA, ) if component.get_entity("zone.home"): return True home_zone = Zone(_home_conf(opp)) home_zone.entity_id = ENTITY_ID_HOME await component.async_add_entities([home_zone]) async def core_config_updated(_: Event) -> None: """Handle core config updated.""" await home_zone.async_update_config(_home_conf(opp)) opp.bus.async_listen(EVENT_CORE_CONFIG_UPDATE, core_config_updated) opp.data[DOMAIN] = storage_collection return True
async def async_setup(opp: OpenPeerPower, config: ConfigType) -> bool: """Set up an input select.""" component = EntityComponent(_LOGGER, DOMAIN, opp) id_manager = collection.IDManager() yaml_collection = collection.YamlCollection( logging.getLogger(f"{__name__}.yaml_collection"), id_manager) collection.sync_entity_lifecycle(opp, DOMAIN, DOMAIN, component, yaml_collection, Timer.from_yaml) storage_collection = TimerStorageCollection( Store(opp, STORAGE_VERSION, STORAGE_KEY), logging.getLogger(f"{__name__}.storage_collection"), id_manager, ) collection.sync_entity_lifecycle(opp, DOMAIN, DOMAIN, component, storage_collection, Timer) await yaml_collection.async_load([{ CONF_ID: id_, **cfg } for id_, cfg in config.get(DOMAIN, {}).items()]) await storage_collection.async_load() collection.StorageCollectionWebsocket(storage_collection, DOMAIN, DOMAIN, CREATE_FIELDS, UPDATE_FIELDS).async_setup(opp) async def reload_service_handler(service_call: ServiceCall) -> None: """Reload yaml entities.""" conf = await component.async_prepare_reload(skip_reset=True) if conf is None: conf = {DOMAIN: {}} await yaml_collection.async_load([{ CONF_ID: id_, **cfg } for id_, cfg in conf.get(DOMAIN, {}).items()]) openpeerpower.helpers.service.async_register_admin_service( opp, DOMAIN, SERVICE_RELOAD, reload_service_handler, schema=RELOAD_SERVICE_SCHEMA, ) component.async_register_entity_service( SERVICE_START, { vol.Optional(ATTR_DURATION, default=DEFAULT_DURATION): cv.time_period }, "async_start", ) component.async_register_entity_service(SERVICE_PAUSE, {}, "async_pause") component.async_register_entity_service(SERVICE_CANCEL, {}, "async_cancel") component.async_register_entity_service(SERVICE_FINISH, {}, "async_finish") return True
async def async_setup(opp: OpenPeerPowerType, config: ConfigType): """Set up the person component.""" entity_component = EntityComponent(_LOGGER, DOMAIN, opp) id_manager = collection.IDManager() yaml_collection = collection.YamlCollection( logging.getLogger(f"{__name__}.yaml_collection"), id_manager) storage_collection = PersonStorageCollection( PersonStore(opp, STORAGE_VERSION, STORAGE_KEY), logging.getLogger(f"{__name__}.storage_collection"), id_manager, yaml_collection, ) collection.attach_entity_component_collection( entity_component, yaml_collection, lambda conf: Person(conf, False)) collection.attach_entity_component_collection( entity_component, storage_collection, lambda conf: Person(conf, True)) collection.attach_entity_registry_cleaner(opp, DOMAIN, DOMAIN, yaml_collection) collection.attach_entity_registry_cleaner(opp, DOMAIN, DOMAIN, storage_collection) await yaml_collection.async_load(await filter_yaml_data(opp, config.get(DOMAIN, []))) await storage_collection.async_load() opp.data[DOMAIN] = (yaml_collection, storage_collection) collection.StorageCollectionWebsocket( storage_collection, DOMAIN, DOMAIN, CREATE_FIELDS, UPDATE_FIELDS).async_setup(opp, create_list=False) websocket_api.async_register_command(opp, ws_list_person) async def _handle_user_removed(event: Event) -> None: """Handle a user being removed.""" user_id = event.data[ATTR_USER_ID] for person in storage_collection.async_items(): if person[CONF_USER_ID] == user_id: await storage_collection.async_update_item( person[CONF_ID], {CONF_USER_ID: None}) opp.bus.async_listen(EVENT_USER_REMOVED, _handle_user_removed) async def async_reload_yaml(call: ServiceCall): """Reload YAML.""" conf = await entity_component.async_prepare_reload(skip_reset=True) if conf is None: return await yaml_collection.async_load(await filter_yaml_data( opp, conf.get(DOMAIN, []))) service.async_register_admin_service(opp, DOMAIN, SERVICE_RELOAD, async_reload_yaml) return True
async def async_setup(opp: OpenPeerPowerType, config: ConfigType) -> bool: """Set up an input text.""" component = EntityComponent(_LOGGER, DOMAIN, opp) id_manager = collection.IDManager() yaml_collection = collection.YamlCollection( logging.getLogger(f"{__name__}.yaml_collection"), id_manager) collection.attach_entity_component_collection(component, yaml_collection, InputText.from_yaml) storage_collection = InputTextStorageCollection( Store(opp, STORAGE_VERSION, STORAGE_KEY), logging.getLogger(f"{__name__}.storage_collection"), id_manager, ) collection.attach_entity_component_collection(component, storage_collection, InputText) await yaml_collection.async_load([{ CONF_ID: id_, **(conf or {}) } for id_, conf in config.get(DOMAIN, {}).items()]) await storage_collection.async_load() collection.StorageCollectionWebsocket(storage_collection, DOMAIN, DOMAIN, CREATE_FIELDS, UPDATE_FIELDS).async_setup(opp) collection.attach_entity_registry_cleaner(opp, DOMAIN, DOMAIN, yaml_collection) collection.attach_entity_registry_cleaner(opp, DOMAIN, DOMAIN, storage_collection) async def reload_service_handler(service_call: ServiceCallType) -> None: """Reload yaml entities.""" conf = await component.async_prepare_reload(skip_reset=True) if conf is None: conf = {DOMAIN: {}} await yaml_collection.async_load([{ CONF_ID: id_, **(cfg or {}) } for id_, cfg in conf.get(DOMAIN, {}).items()]) openpeerpower.helpers.service.async_register_admin_service( opp, DOMAIN, SERVICE_RELOAD, reload_service_handler, schema=RELOAD_SERVICE_SCHEMA, ) component.async_register_entity_service( SERVICE_SET_VALUE, {vol.Required(ATTR_VALUE): cv.string}, "async_set_value") return True
async def async_setup(opp: OpenPeerPower, config: ConfigType) -> bool: """Set up an input boolean.""" component = EntityComponent(_LOGGER, DOMAIN, opp) id_manager = collection.IDManager() yaml_collection = collection.YamlCollection( logging.getLogger(f"{__name__}.yaml_collection"), id_manager) collection.sync_entity_lifecycle(opp, DOMAIN, DOMAIN, component, yaml_collection, InputBoolean.from_yaml) storage_collection = InputBooleanStorageCollection( Store(opp, STORAGE_VERSION, STORAGE_KEY), logging.getLogger(f"{__name__}.storage_collection"), id_manager, ) collection.sync_entity_lifecycle(opp, DOMAIN, DOMAIN, component, storage_collection, InputBoolean) await yaml_collection.async_load([{ CONF_ID: id_, **(conf or {}) } for id_, conf in config.get(DOMAIN, {}).items()]) await storage_collection.async_load() collection.StorageCollectionWebsocket(storage_collection, DOMAIN, DOMAIN, CREATE_FIELDS, UPDATE_FIELDS).async_setup(opp) async def reload_service_handler(service_call: ServiceCall) -> None: """Remove all input booleans and load new ones from config.""" conf = await component.async_prepare_reload(skip_reset=True) if conf is None: return await yaml_collection.async_load([{ CONF_ID: id_, **(conf or {}) } for id_, conf in conf.get(DOMAIN, {}).items()]) openpeerpower.helpers.service.async_register_admin_service( opp, DOMAIN, SERVICE_RELOAD, reload_service_handler, schema=RELOAD_SERVICE_SCHEMA, ) component.async_register_entity_service(SERVICE_TURN_ON, {}, "async_turn_on") component.async_register_entity_service(SERVICE_TURN_OFF, {}, "async_turn_off") component.async_register_entity_service(SERVICE_TOGGLE, {}, "async_toggle") return True
async def async_setup(opp: OpenPeerPower, config: dict): """Set up the Tag component.""" opp.data[DOMAIN] = {} id_manager = TagIDManager() opp.data[DOMAIN][TAGS] = storage_collection = TagStorageCollection( Store(opp, STORAGE_VERSION, STORAGE_KEY), logging.getLogger(f"{__name__}.storage_collection"), id_manager, ) await storage_collection.async_load() collection.StorageCollectionWebsocket(storage_collection, DOMAIN, DOMAIN, CREATE_FIELDS, UPDATE_FIELDS).async_setup(opp) return True
async def async_setup(opp: OpenPeerPower, config: dict): """Set up the Image integration.""" image_dir = pathlib.Path(opp.config.path(DOMAIN)) opp.data[DOMAIN] = storage_collection = ImageStorageCollection( opp, image_dir) await storage_collection.async_load() collection.StorageCollectionWebsocket( storage_collection, DOMAIN, DOMAIN, CREATE_FIELDS, UPDATE_FIELDS, ).async_setup(opp, create_create=False) opp.http.register_view(ImageUploadView) opp.http.register_view(ImageServeView(image_dir, storage_collection)) return True
async def async_setup(opp: OpenPeerPower, config: ConfigType) -> bool: """Set up an input select.""" component = EntityComponent(_LOGGER, DOMAIN, opp) id_manager = collection.IDManager() yaml_collection = collection.YamlCollection( logging.getLogger(f"{__name__}.yaml_collection"), id_manager) collection.sync_entity_lifecycle(opp, DOMAIN, DOMAIN, component, yaml_collection, InputSelect.from_yaml) storage_collection = InputSelectStorageCollection( Store(opp, STORAGE_VERSION, STORAGE_KEY), logging.getLogger(f"{__name__}.storage_collection"), id_manager, ) collection.sync_entity_lifecycle(opp, DOMAIN, DOMAIN, component, storage_collection, InputSelect) await yaml_collection.async_load([{ CONF_ID: id_, **cfg } for id_, cfg in config.get(DOMAIN, {}).items()]) await storage_collection.async_load() collection.StorageCollectionWebsocket(storage_collection, DOMAIN, DOMAIN, CREATE_FIELDS, UPDATE_FIELDS).async_setup(opp) async def reload_service_handler(service_call: ServiceCall) -> None: """Reload yaml entities.""" conf = await component.async_prepare_reload(skip_reset=True) if conf is None: conf = {DOMAIN: {}} await yaml_collection.async_load([{ CONF_ID: id_, **cfg } for id_, cfg in conf.get(DOMAIN, {}).items()]) openpeerpower.helpers.service.async_register_admin_service( opp, DOMAIN, SERVICE_RELOAD, reload_service_handler, schema=RELOAD_SERVICE_SCHEMA, ) component.async_register_entity_service( SERVICE_SELECT_OPTION, {vol.Required(ATTR_OPTION): cv.string}, "async_select_option", ) component.async_register_entity_service( SERVICE_SELECT_NEXT, {vol.Optional(ATTR_CYCLE, default=True): bool}, "async_next", ) component.async_register_entity_service( SERVICE_SELECT_PREVIOUS, {vol.Optional(ATTR_CYCLE, default=True): bool}, "async_previous", ) component.async_register_entity_service( SERVICE_SELECT_FIRST, {}, callback(lambda entity, call: entity.async_select_index(0)), ) component.async_register_entity_service( SERVICE_SELECT_LAST, {}, callback(lambda entity, call: entity.async_select_index(-1)), ) component.async_register_entity_service( SERVICE_SET_OPTIONS, { vol.Required(ATTR_OPTIONS): vol.All(cv.ensure_list, vol.Length(min=1), [cv.string]) }, "async_set_options", ) return True
async def async_setup(opp: OpenPeerPower, config: ConfigType): """Set up the Lovelace commands.""" mode = config[DOMAIN][CONF_MODE] yaml_resources = config[DOMAIN].get(CONF_RESOURCES) frontend.async_register_built_in_panel(opp, DOMAIN, config={"mode": mode}) async def reload_resources_service_handler(service_call: ServiceCall) -> None: """Reload yaml resources.""" try: conf = await async_opp_config_yaml(opp) except OpenPeerPowerError as err: _LOGGER.error(err) return integration = await async_get_integration(opp, DOMAIN) config = await async_process_component_config(opp, conf, integration) resource_collection = await create_yaml_resource_col( opp, config[DOMAIN].get(CONF_RESOURCES) ) opp.data[DOMAIN]["resources"] = resource_collection if mode == MODE_YAML: default_config = dashboard.LovelaceYAML(opp, None, None) resource_collection = await create_yaml_resource_col(opp, yaml_resources) async_register_admin_service( opp, DOMAIN, SERVICE_RELOAD_RESOURCES, reload_resources_service_handler, schema=RESOURCE_RELOAD_SERVICE_SCHEMA, ) else: default_config = dashboard.LovelaceStorage(opp, None) if yaml_resources is not None: _LOGGER.warning( "Lovelace is running in storage mode. Define resources via user interface" ) resource_collection = resources.ResourceStorageCollection(opp, default_config) collection.StorageCollectionWebsocket( resource_collection, "lovelace/resources", "resource", RESOURCE_CREATE_FIELDS, RESOURCE_UPDATE_FIELDS, ).async_setup(opp, create_list=False) opp.components.websocket_api.async_register_command( websocket.websocket_lovelace_config ) opp.components.websocket_api.async_register_command( websocket.websocket_lovelace_save_config ) opp.components.websocket_api.async_register_command( websocket.websocket_lovelace_delete_config ) opp.components.websocket_api.async_register_command( websocket.websocket_lovelace_resources ) opp.components.websocket_api.async_register_command( websocket.websocket_lovelace_dashboards ) opp.data[DOMAIN] = { # We store a dictionary mapping url_path: config. None is the default. "dashboards": {None: default_config}, "resources": resource_collection, "yaml_dashboards": config[DOMAIN].get(CONF_DASHBOARDS, {}), } if opp.config.safe_mode: return True async def storage_dashboard_changed(change_type, item_id, item): """Handle a storage dashboard change.""" url_path = item[CONF_URL_PATH] if change_type == collection.CHANGE_REMOVED: frontend.async_remove_panel(opp, url_path) await opp.data[DOMAIN]["dashboards"].pop(url_path).async_delete() return if change_type == collection.CHANGE_ADDED: existing = opp.data[DOMAIN]["dashboards"].get(url_path) if existing: _LOGGER.warning( "Cannot register panel at %s, it is already defined in %s", url_path, existing, ) return opp.data[DOMAIN]["dashboards"][url_path] = dashboard.LovelaceStorage( opp, item ) update = False else: opp.data[DOMAIN]["dashboards"][url_path].config = item update = True try: _register_panel(opp, url_path, MODE_STORAGE, item, update) except ValueError: _LOGGER.warning("Failed to %s panel %s from storage", change_type, url_path) # Process YAML dashboards for url_path, dashboard_conf in opp.data[DOMAIN]["yaml_dashboards"].items(): # For now always mode=yaml config = dashboard.LovelaceYAML(opp, url_path, dashboard_conf) opp.data[DOMAIN]["dashboards"][url_path] = config try: _register_panel(opp, url_path, MODE_YAML, dashboard_conf, False) except ValueError: _LOGGER.warning("Panel url path %s is not unique", url_path) # Process storage dashboards dashboards_collection = dashboard.DashboardsCollection(opp) dashboards_collection.async_add_listener(storage_dashboard_changed) await dashboards_collection.async_load() collection.StorageCollectionWebsocket( dashboards_collection, "lovelace/dashboards", "dashboard", STORAGE_DASHBOARD_CREATE_FIELDS, STORAGE_DASHBOARD_UPDATE_FIELDS, ).async_setup(opp, create_list=False) return True
async def async_setup(opp: OpenPeerPower, config: Dict) -> bool: """Set up configured zones as well as Open Peer Power zone if necessary.""" component = entity_component.EntityComponent(_LOGGER, DOMAIN, opp) id_manager = collection.IDManager() yaml_collection = IDLessCollection( logging.getLogger(f"{__name__}.yaml_collection"), id_manager) collection.attach_entity_component_collection( component, yaml_collection, lambda conf: Zone(conf, False)) storage_collection = ZoneStorageCollection( storage.Store(opp, STORAGE_VERSION, STORAGE_KEY), logging.getLogger(f"{__name__}_storage_collection"), id_manager, ) collection.attach_entity_component_collection( component, storage_collection, lambda conf: Zone(conf, True)) if DOMAIN in config: await yaml_collection.async_load(config[DOMAIN]) await storage_collection.async_load() collection.StorageCollectionWebsocket(storage_collection, DOMAIN, DOMAIN, CREATE_FIELDS, UPDATE_FIELDS).async_setup(opp) async def _collection_changed(change_type: str, item_id: str, config: Optional[Dict]) -> None: """Handle a collection change: clean up entity registry on removals.""" if change_type != collection.CHANGE_REMOVED: return ent_reg = await entity_registry.async_get_registry(opp) ent_reg.async_remove( cast(str, ent_reg.async_get_entity_id(DOMAIN, DOMAIN, item_id))) storage_collection.async_add_listener(_collection_changed) async def reload_service_handler(service_call: ServiceCall) -> None: """Remove all zones and load new ones from config.""" conf = await component.async_prepare_reload(skip_reset=True) if conf is None: return await yaml_collection.async_load(conf.get(DOMAIN, [])) service.async_register_admin_service( opp, DOMAIN, SERVICE_RELOAD, reload_service_handler, schema=RELOAD_SERVICE_SCHEMA, ) if component.get_entity("zone.home"): return True home_zone = Zone( _home_conf(opp), True, ) home_zone.entity_id = ENTITY_ID_HOME await component.async_add_entities([home_zone]) # type: ignore async def core_config_updated(_: Event) -> None: """Handle core config updated.""" await home_zone.async_update_config(_home_conf(opp)) opp.bus.async_listen(EVENT_CORE_CONFIG_UPDATE, core_config_updated) opp.data[DOMAIN] = storage_collection return True
async def async_setup(opp: OpenPeerPower, config: ConfigType) -> bool: """Set up an input datetime.""" component = EntityComponent(_LOGGER, DOMAIN, opp) id_manager = collection.IDManager() yaml_collection = collection.YamlCollection( logging.getLogger(f"{__name__}.yaml_collection"), id_manager) collection.sync_entity_lifecycle(opp, DOMAIN, DOMAIN, component, yaml_collection, InputDatetime.from_yaml) storage_collection = DateTimeStorageCollection( Store(opp, STORAGE_VERSION, STORAGE_KEY), logging.getLogger(f"{__name__}.storage_collection"), id_manager, ) collection.sync_entity_lifecycle(opp, DOMAIN, DOMAIN, component, storage_collection, InputDatetime) await yaml_collection.async_load([{ CONF_ID: id_, **cfg } for id_, cfg in config.get(DOMAIN, {}).items()]) await storage_collection.async_load() collection.StorageCollectionWebsocket(storage_collection, DOMAIN, DOMAIN, CREATE_FIELDS, UPDATE_FIELDS).async_setup(opp) async def reload_service_handler(service_call: ServiceCall) -> None: """Reload yaml entities.""" conf = await component.async_prepare_reload(skip_reset=True) if conf is None: conf = {DOMAIN: {}} await yaml_collection.async_load([{ CONF_ID: id_, **cfg } for id_, cfg in conf.get(DOMAIN, {}).items()]) openpeerpower.helpers.service.async_register_admin_service( opp, DOMAIN, SERVICE_RELOAD, reload_service_handler, schema=RELOAD_SERVICE_SCHEMA, ) component.async_register_entity_service( "set_datetime", vol.All( vol.Schema( { vol.Optional(ATTR_DATE): cv.date, vol.Optional(ATTR_TIME): cv.time, vol.Optional(ATTR_DATETIME): cv.datetime, vol.Optional(ATTR_TIMESTAMP): vol.Coerce(float), }, extra=vol.ALLOW_EXTRA, ), cv.has_at_least_one_key(ATTR_DATE, ATTR_TIME, ATTR_DATETIME, ATTR_TIMESTAMP), validate_set_datetime_attrs, ), "async_set_datetime", ) return True
async def async_setup(opp: OpenPeerPowerType, config: ConfigType) -> bool: """Set up an input datetime.""" component = EntityComponent(_LOGGER, DOMAIN, opp) id_manager = collection.IDManager() yaml_collection = collection.YamlCollection( logging.getLogger(f"{__name__}.yaml_collection"), id_manager ) collection.attach_entity_component_collection( component, yaml_collection, InputDatetime.from_yaml ) storage_collection = DateTimeStorageCollection( Store(opp, STORAGE_VERSION, STORAGE_KEY), logging.getLogger(f"{__name__}.storage_collection"), id_manager, ) collection.attach_entity_component_collection( component, storage_collection, InputDatetime ) await yaml_collection.async_load( [{CONF_ID: id_, **cfg} for id_, cfg in config.get(DOMAIN, {}).items()] ) await storage_collection.async_load() collection.StorageCollectionWebsocket( storage_collection, DOMAIN, DOMAIN, CREATE_FIELDS, UPDATE_FIELDS ).async_setup(opp) collection.attach_entity_registry_cleaner(opp, DOMAIN, DOMAIN, yaml_collection) collection.attach_entity_registry_cleaner(opp, DOMAIN, DOMAIN, storage_collection) async def reload_service_handler(service_call: ServiceCallType) -> None: """Reload yaml entities.""" conf = await component.async_prepare_reload(skip_reset=True) if conf is None: conf = {DOMAIN: {}} await yaml_collection.async_load( [{CONF_ID: id_, **cfg} for id_, cfg in conf.get(DOMAIN, {}).items()] ) openpeerpower.helpers.service.async_register_admin_service( opp, DOMAIN, SERVICE_RELOAD, reload_service_handler, schema=RELOAD_SERVICE_SCHEMA, ) async def async_set_datetime_service(entity, call): """Handle a call to the input datetime 'set datetime' service.""" time = call.data.get(ATTR_TIME) date = call.data.get(ATTR_DATE) dttm = call.data.get(ATTR_DATETIME) if ( dttm and (date or time) or entity.has_date and not (date or dttm) or entity.has_time and not (time or dttm) ): _LOGGER.error( "Invalid service data for %s input_datetime.set_datetime: %s", entity.entity_id, str(call.data), ) return if dttm: date = dttm.date() time = dttm.time() entity.async_set_datetime(date, time) component.async_register_entity_service( SERVICE_SET_DATETIME, { vol.Optional(ATTR_DATE): cv.date, vol.Optional(ATTR_TIME): cv.time, vol.Optional(ATTR_DATETIME): cv.datetime, }, async_set_datetime_service, ) return True
async def test_storage_collection_websocket(opp, opp_ws_client): """Test exposing a storage collection via websockets.""" store = storage.Store(opp, 1, "test-data") coll = MockStorageCollection(store, _LOGGER) changes = track_changes(coll) collection.StorageCollectionWebsocket( coll, "test_item/collection", "test_item", { vol.Required("name"): str, vol.Required("immutable_string"): str }, { vol.Optional("name"): str }, ).async_setup(opp) client = await opp_ws_client(opp) # Create invalid await client.send_json({ "id": 1, "type": "test_item/collection/create", "name": 1, # Forgot to add immutable_string }) response = await client.receive_json() assert not response["success"] assert response["error"]["code"] == "invalid_format" assert len(changes) == 0 # Create await client.send_json({ "id": 2, "type": "test_item/collection/create", "name": "Initial Name", "immutable_string": "no-changes", }) response = await client.receive_json() assert response["success"] assert response["result"] == { "id": "initial_name", "name": "Initial Name", "immutable_string": "no-changes", } assert len(changes) == 1 assert changes[0] == (collection.CHANGE_ADDED, "initial_name", response["result"]) # List await client.send_json({"id": 3, "type": "test_item/collection/list"}) response = await client.receive_json() assert response["success"] assert response["result"] == [{ "id": "initial_name", "name": "Initial Name", "immutable_string": "no-changes", }] assert len(changes) == 1 # Update invalid data await client.send_json({ "id": 4, "type": "test_item/collection/update", "test_item_id": "initial_name", "immutable_string": "no-changes", }) response = await client.receive_json() assert not response["success"] assert response["error"]["code"] == "invalid_format" assert len(changes) == 1 # Update invalid item await client.send_json({ "id": 5, "type": "test_item/collection/update", "test_item_id": "non-existing", "name": "Updated name", }) response = await client.receive_json() assert not response["success"] assert response["error"]["code"] == "not_found" assert len(changes) == 1 # Update await client.send_json({ "id": 6, "type": "test_item/collection/update", "test_item_id": "initial_name", "name": "Updated name", }) response = await client.receive_json() assert response["success"] assert response["result"] == { "id": "initial_name", "name": "Updated name", "immutable_string": "no-changes", } assert len(changes) == 2 assert changes[1] == (collection.CHANGE_UPDATED, "initial_name", response["result"]) # Delete invalid ID await client.send_json({ "id": 7, "type": "test_item/collection/update", "test_item_id": "non-existing" }) response = await client.receive_json() assert not response["success"] assert response["error"]["code"] == "not_found" assert len(changes) == 2 # Delete await client.send_json({ "id": 8, "type": "test_item/collection/delete", "test_item_id": "initial_name" }) response = await client.receive_json() assert response["success"] assert len(changes) == 3 assert changes[2] == ( collection.CHANGE_REMOVED, "initial_name", { "id": "initial_name", "immutable_string": "no-changes", "name": "Updated name", }, )