def edit(ctx: Configuration, entity, newstate, attributes, merge, json): """Edit entity state from Home Assistant.""" ctx.auto_output('data') new_state = newstate if json: _LOGGING.debug( "JSON found overriding/creating new state for entity %s", entity) wanted_state = json_.loads(json) elif new_state or attributes: wanted_state = {} existing_state = api.get_state(ctx, entity) if existing_state: ctx.echo("Existing state found for %s", entity) if merge: wanted_state = existing_state else: ctx.echo("No existing state found for '%s'", entity) if attributes: attributes_dict = helper.to_attributes(attributes) new_attr = wanted_state.get('attributes', {}) new_attr.update(attributes_dict) # This is not honoring merge! wanted_state['attributes'] = new_attr if newstate: wanted_state['state'] = newstate else: if not existing_state: raise ValueError("No new or existing state provided.") wanted_state['state'] = existing_state['state'] else: existing = api.get_state(ctx, entity) if existing: existing_raw = helper.raw_format_output(ctx.output, existing, ctx.yaml()) else: existing_raw = helper.raw_format_output(ctx.output, {}, ctx.yaml()) new = click.edit(existing_raw, extension='.{}'.format(ctx.output)) if new is not None: ctx.echo("Updating '%s'", entity) if ctx.output == 'yaml': wanted_state = ctx.yamlload(new) if ctx.output == 'json': wanted_state = json_.loads(new) api.set_state(ctx, entity, wanted_state) else: ctx.echo("No edits/changes returned from editor.") return _LOGGING.debug("wanted: %s", str(wanted_state)) result = api.set_state(ctx, entity, wanted_state) ctx.echo("Entity %s updated successfully", entity) _LOGGING.debug("Updated to: %s", result)
def state(ctx, entity, newstate, attributes, merge, json): """edit state from Home Assistant""" if json: response = req_raw(ctx, "post", "states/{}".format(entity), json) elif newstate or attributes: wanted_state = {} existing_state = None response = req_raw(ctx, "get", "states/{}".format(entity)) if (response.ok): click.echo("Existing state found for {}".format(entity)) existing_state = response.json() if (merge): wanted_state = existing_state else: click.echo("No existing state found for '{}'".format(entity)) if attributes: import shlex lexer = shlex.shlex(attributes, posix=True) lexer.whitespace_split = True lexer.whitespace = ',' attributes_dict = dict(pair.split('=', 1) for pair in lexer) newattr = wanted_state.get("attributes", {}) newattr.update(attributes_dict) wanted_state["attributes"] = newattr if newstate: wanted_state["state"] = newstate else: if not existing_state: raise ValueError("No new or existing state provided.") wanted_state["state"] = existing_state["state"] print("wanted:" + str(wanted_state)) newjson = raw_format_output("json", wanted_state) response = req_raw(ctx, "post", "states/{}".format(entity), newjson) else: ## no data passed, just use editor existing = req_raw(ctx, "get", "states/{}".format(entity)).json() existing = raw_format_output(ctx.output, existing) new = click.edit(existing, extension=".{}".format(ctx.output)) if new is not None: click.echo("Updating '{}'".format(entity)) if (ctx.output == "yaml"): new = json_.dumps(yaml.load(new)) response = req_raw(ctx, "post", "states/{}".format(entity), new) else: click.echo("No edits/changes.")
def fire(ctx: Configuration, event, json): """Fire event in Home Assistant.""" if json: click.echo("Fire {}".format(event)) response = api.fire_event(ctx, event, json) else: existing = raw_format_output(ctx.output, {}) # type: ignore new = click.edit(existing, extension='.{}'.format(ctx.output)) if new: click.echo("Fire {}".format(event)) if ctx.output == 'yaml': data = yaml.load(new) else: data = json_.loads(new) response = api.fire_event(ctx, event, data) else: click.echo("No edits/changes.") return if response: ctx.echo(raw_format_output(ctx.output, response)) # type: ignore
def fire(ctx: Configuration, event, json): """Fire event in Home Assistant.""" if json: click.echo("Fire {}".format(event)) response = api.fire_event(ctx, event, json) else: existing = raw_format_output(ctx.output, [{}], ctx.yaml()) new = click.edit(existing, extension='.{}'.format(ctx.output)) if new: click.echo("Fire {}".format(event)) if ctx.output == 'yaml': data = ctx.yamlload(new) else: data = json_.loads(new) response = api.fire_event(ctx, event, data) else: click.echo("No edits/changes.") return if response: ctx.echo(raw_format_output(ctx.output, [response], ctx.yaml()))
def event(ctx, event, json): """Edit/fire event in Home Assistant""" if json: click.echo("Fire {}".format(event)) response = req_raw(ctx, "post", "events/{}".format(event), json) response.raise_for_status() else: ## no data passed, just use editor existing = raw_format_output(ctx.output, {}) new = click.edit(existing, extension=".{}".format(ctx.output)) if new is not None: click.echo("Fire {}".format(event)) if (ctx.output == "yaml"): new = json_.dumps(yaml.load(new)) response = req_raw(ctx, "post", "events/{}".format(event), new) response.raise_for_status() else: print("No edits/changes.")
def edit(ctx: Configuration, entity, newstate, attributes, merge, json): """Edit entity state from Home Assistant.""" ctx.auto_output('data') if json: _LOGGING.debug( "json found overriding/creating new state for entity %s", entity ) wanted_state = json_.loads(json) elif newstate or attributes: wanted_state = {} existing_state = api.get_state(ctx, entity) if existing_state: ctx.echo("Existing state found for %s", entity) if merge: wanted_state = existing_state else: ctx.echo("No existing state found for '%s'", entity) if attributes: attributes_dict = helper.to_attributes(attributes) newattr = wanted_state.get('attributes', {}) newattr.update(attributes_dict) # this is not hornoring merge! wanted_state['attributes'] = newattr if newstate: wanted_state['state'] = newstate else: if not existing_state: raise ValueError("No new or existing state provided.") wanted_state['state'] = existing_state['state'] else: existing = api.get_state(ctx, entity) if existing: existingraw = helper.raw_format_output( ctx.output, [existing], ctx.yaml() )[0] else: existingraw = helper.raw_format_output( ctx.output, [{}], ctx.yaml() )[0] new = click.edit(existingraw, extension='.{}'.format(ctx.output)) if new is not None: ctx.echo("Updating '%s'", entity) if ctx.output == 'yaml': wanted_state = ctx.yamlload(new) if ctx.output == 'json': wanted_state = json_.loads(new) api.set_state(ctx, entity, wanted_state) else: ctx.echo("No edits/changes returned from editor.") return _LOGGING.debug("wanted: %s", str(wanted_state)) result = api.set_state(ctx, entity, wanted_state) ctx.echo("Entity %s updated succesfully", entity) _LOGGING.debug("Updated to: %s", result)