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 get(ctx: Configuration, entity): """Get/read entity state from Home Assistant.""" ctx.auto_output("table") state = api.get_state(ctx, entity) if state: ctx.echo(helper.format_output(ctx, [state], const.COLUMNS_ENTITIES)) else: _LOGGING.error("Entity with id: '%s' not found.", entity)
def get(ctx: Configuration, entity): """Get/read entity state from Home Assistant.""" ctx.auto_output("table") state = api.get_state(ctx, entity) if state: ctx.echo( helper.format_output( ctx, [state], columns=ctx.columns if ctx.columns else const.COLUMNS_ENTITIES, ) ) else: _LOGGING.error("Entity with id: '%s' not found.", entity)
def cli(ctx: Configuration, service: str, entity: str) -> None: """Show the location of the config or an entity on a map.""" latitude = None longitude = None if entity: thing = entity data = api.get_state(ctx, entity) if data: attr = data.get('attributes', {}) latitude = attr.get('latitude') longitude = attr.get('longitude') thing = attr.get('friendly_name', entity) else: thing = "configuration" response = api.get_config(ctx) if response: latitude = response.get('latitude') longitude = response.get('longitude') thing = response.get('location_name', thing) if latitude and longitude: urlpattern = SERVICE.get(service) import urllib.parse if urlpattern: url = urlpattern.format(latitude, longitude, urllib.parse.quote_plus(thing)) ctx.echo("{} location is at {}, {}".format(thing, latitude, longitude)) webbrowser.open_new_tab(url) else: ctx.echo( "Could not find url pattern for service {}".format(service)) else: ctx.echo("No exact location info found in {}".format(thing)) sys.exit(2)
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)