Skip to content

Commit

Permalink
Merge pull request #389 from home-assistant-ecosystem/dev
Browse files Browse the repository at this point in the history
0.9.5
  • Loading branch information
fabaff authored Oct 1, 2022
2 parents d727cf1 + 42c55d0 commit 904de37
Show file tree
Hide file tree
Showing 21 changed files with 184 additions and 92 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ 3.8, 3.9 ]
python-version: [ 3.9, "3.10" ]

steps:
- uses: actions/checkout@v2
Expand All @@ -28,7 +28,9 @@ jobs:
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Black Check
uses: jpetrucciani/[email protected]
uses: jpetrucciani/[email protected]
with:
path: homeassistant_cli
- name: Lint with flake8
run: |
pip install flake8
Expand Down
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v2.19.4
rev: v2.38.2
hooks:
- id: pyupgrade
args: [--py37-plus]
- repo: https://github.com/psf/black
rev: 21.6b0
rev: 22.8.0
hooks:
- id: black
args:
- --safe
- --quiet
files: ^((homeassistant_cli|script|tests)/.+)?[^/]+\.py$
- repo: https://github.com/codespell-project/codespell
rev: v2.1.0
rev: v2.2.1
hooks:
- id: codespell
args:
Expand All @@ -30,7 +30,7 @@ repos:
- pydocstyle==5.0.2
files: ^(homeassistant_cli|script|tests)/.+\.py$
- repo: https://github.com/PyCQA/bandit
rev: 1.7.0
rev: 1.7.4
hooks:
- id: bandit
args:
Expand All @@ -39,11 +39,11 @@ repos:
- --configfile=tests/bandit.yaml
files: ^(homeassistant_cli|tests)/.+\.py$
- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.8.0
rev: v5.10.1
hooks:
- id: isort
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
rev: v4.3.0
hooks:
- id: check-executables-have-shebangs
stages: [manual]
Expand All @@ -56,7 +56,7 @@ repos:
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: https://github.com/adrienverge/yamllint.git
rev: v1.26.1
rev: v1.28.0
hooks:
- id: yamllint
- repo: https://github.com/prettier/prettier
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.9-alpine
FROM python:3.10-alpine
LABEL maintainer="Max Rydahl Andersen <[email protected]>"

WORKDIR /usr/src/app
Expand Down
12 changes: 6 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ To use latest pre-release from ``dev`` branch:

.. code:: bash
$ pip install git+https://github.com/home-assistant/home-assistant-cli@dev
$ pip install git+https://github.com/home-assistant-ecosystem/home-assistant-cli@dev
The developers of `hass-cli` usually provide up-to-date `packages <https://src.fedoraproject.org/rpms/home-assistant-cli>`_ for recent Fedora and EPEL releases. Use ``dnf`` for the installation:

Expand Down Expand Up @@ -89,13 +89,14 @@ recommended setup ``HASS_SERVER`` and ``HASS_TOKEN`` environment variables.
$ export HASS_SERVER=https://homeassistant.local:8123
$ export HASS_TOKEN=<secret>
Once that is enabled and you are using either ``zsh`` or ``bash`` run
the following to enable autocompletion for ``hass-cli`` commands.
Once that is enabled, run one of the following commands to enable
autocompletion for ``hass-cli`` commands.

.. code:: bash
$ source <(hass-cli completion zsh)
$ source <(_HASS_CLI_COMPLETE=bash_source hass-cli) # for bash
$ source <(_HASS_CLI_COMPLETE=zsh_source hass-cli) # for zsh
$ eval (_HASS_CLI_COMPLETE=fish_source hass-cli) # for fish
Usage
=======
Expand Down Expand Up @@ -488,7 +489,6 @@ Help
--table-format TEXT Which table format to use.
--sort-by TEXT Sort table by the jsonpath expression.
Example: last_changed
--version Show the version and exit.
--help Show this message and exit.
Commands:
Expand Down
3 changes: 1 addition & 2 deletions homeassistant_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,13 @@ def _default_token() -> Optional[str]:
'--table-format',
default='plain',
help="Which table format to use.",
autocompletion=autocompletion.table_formats,
shell_complete=autocompletion.table_formats,
)
@click.option(
'--sort-by',
default=None,
help='Sort table by the jsonpath expression. Example: last_changed',
)
@click.version_option()
@pass_context
def cli(
ctx: Configuration,
Expand Down
3 changes: 2 additions & 1 deletion homeassistant_cli/const.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Constants used by Home Assistant CLI (hass-cli)."""
PACKAGE_NAME = 'homeassistant_cli'

__version__ = '0.9.4'
__version__ = '0.9.5'
# __version__ = '0.10.0.dev0'

AUTO_SERVER = 'auto'
DEFAULT_SERVER = 'http://localhost:8123'
Expand Down
1 change: 1 addition & 0 deletions homeassistant_cli/hassconst.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@
URL_API_ERROR_LOG = "/api/error_log"
URL_API_LOG_OUT = "/api/log_out"
URL_API_TEMPLATE = "/api/template"
URL_API_HISTORY_PERIOD = "/api/history/period"

HTTP_OK = 200
HTTP_CREATED = 201
Expand Down
4 changes: 2 additions & 2 deletions homeassistant_cli/plugins/area.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def create(ctx, names):
'names',
nargs=-1,
required=True,
autocompletion=autocompletion.areas, # type: ignore
shell_complete=autocompletion.areas, # type: ignore
)
@pass_context
def delete(ctx, names):
Expand Down Expand Up @@ -114,7 +114,7 @@ def delete(ctx, names):
@click.argument(
'oldname',
required=True,
autocompletion=autocompletion.areas, # type: ignore
shell_complete=autocompletion.areas, # type: ignore
)
@click.argument('newname', required=True)
@pass_context
Expand Down
2 changes: 1 addition & 1 deletion homeassistant_cli/plugins/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def listcmd(ctx: Configuration, devicefilter: str):
@click.argument(
'area_id_or_name',
required=True,
autocompletion=autocompletion.areas, # type: ignore
shell_complete=autocompletion.areas, # type: ignore
)
@click.argument('names', nargs=-1, required=False)
@click.option(
Expand Down
96 changes: 93 additions & 3 deletions homeassistant_cli/plugins/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import re
import sys
from typing import Any, Dict, List, Pattern # noqa
from typing import Any, Dict, List, Optional, Pattern # noqa

import click

Expand All @@ -29,6 +29,8 @@ def listcmd(ctx: Configuration, entityfilter: str):
"""List all entities from Home Assistant."""
ctx.auto_output("table")

areas = api.get_areas(ctx)

entities = api.get_entities(ctx)

result = [] # type: List[Dict]
Expand All @@ -41,11 +43,19 @@ def listcmd(ctx: Configuration, entityfilter: str):
if entityfilterre.search(entity['entity_id']):
result.append(entity)

for entity in entities:
area = next(
(a for a in areas if a['area_id'] == entity['area_id']), None
)
if area:
entity['area_name'] = area['name']

cols = [
('ENTITY_ID', 'entity_id'),
('NAME', 'name'),
('DEVICE_ID', 'device_id'),
('PLATFORM', 'platform'),
('AREA', 'area_name'),
('CONFIG_ENTRY_ID', 'config_entry_id'),
('DISABLED_BY', 'disabled_by'),
]
Expand All @@ -57,17 +67,97 @@ def listcmd(ctx: Configuration, entityfilter: str):
)


@cli.command('assign')
@click.argument(
'area_id_or_name',
required=True,
shell_complete=autocompletion.areas, # type: ignore
)
@click.argument('names', nargs=-1, required=False)
@click.option(
'--match', help="Expression used to find entities matching that name"
)
@pass_context
def assign(
ctx: Configuration,
area_id_or_name,
names: List[str],
match: Optional[str] = None,
):
"""Update area on one or more entities.
NAMES - one or more name or id (Optional)
"""
ctx.auto_output("data")

entities = api.get_entities(ctx)

result = [] # type: List[Dict]

area = api.find_area(ctx, area_id_or_name)
if not area:
_LOGGING.error(
"Could not find area with id or name: %s", area_id_or_name
)
sys.exit(1)

if match:
if match == ".*":
result = entities
else:
entityfilterre = re.compile(match) # type: Pattern

for entity in entities:
if entityfilterre.search(entity['name']):
result.append(entity)

for id_or_name in names:
entity = next(
(x for x in entities if x['entity_id'] == id_or_name), None # type: ignore
)
if not entity:
entity = next(
(x for x in entities if x['name'] == id_or_name),
None, # type: ignore
)
if not entity:
_LOGGING.error(
"Could not find entity with id or name: %s", id_or_name
)
sys.exit(1)
result.append(entity)

for entity in result:
output = api.assign_entity_area(
ctx, entity['entity_id'], area['area_id']
)
if output['success']:
ctx.echo(
"Successfully assigned '{}' to '{}'".format(
area['name'], entity['entity_id']
)
)
else:
_LOGGING.error(
"Failed to assign '%s' to '%s'",
area['name'],
entity['entity_id'],
)

ctx.echo(str(output))


@cli.command('rename')
@click.argument(
'oldid',
required=True,
autocompletion=autocompletion.entities, # type: ignore
shell_complete=autocompletion.entities, # type: ignore
)
@click.option('--name', required=False)
@click.argument(
'newid',
required=False,
autocompletion=autocompletion.entities, # type: ignore
shell_complete=autocompletion.entities, # type: ignore
)
@pass_context
def rename(ctx, oldid, newid, name):
Expand Down
4 changes: 2 additions & 2 deletions homeassistant_cli/plugins/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def cli(ctx):
@click.argument(
'event',
required=True,
autocompletion=autocompletion.events, # type: ignore
shell_complete=autocompletion.events, # type: ignore
)
@click.option(
'--json',
Expand All @@ -37,7 +37,7 @@ 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)
response = api.fire_event(ctx, event, json_.loads(json))
else:
existing = raw_format_output(ctx.output, [{}], ctx.yaml())
new = click.edit(existing, extension='.{}'.format(ctx.output))
Expand Down
2 changes: 1 addition & 1 deletion homeassistant_cli/plugins/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@click.argument(
'entity',
required=False,
autocompletion=autocompletion.entities, # type: ignore
shell_complete=autocompletion.entities, # type: ignore
)
@click.option(
'--service', default='openstreetmap', type=click.Choice(SERVICE.keys())
Expand Down
6 changes: 3 additions & 3 deletions homeassistant_cli/plugins/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _report(ctx, cmd, method, response) -> None:

@cli.command()
@click.argument(
'method', autocompletion=autocompletion.api_methods # type: ignore
'method', shell_complete=autocompletion.api_methods # type: ignore
)
@pass_context
def get(ctx: Configuration, method):
Expand All @@ -54,7 +54,7 @@ def get(ctx: Configuration, method):

@cli.command()
@click.argument(
'method', autocompletion=autocompletion.api_methods # type: ignore
'method', shell_complete=autocompletion.api_methods # type: ignore
)
@click.option('--json')
@pass_context
Expand All @@ -72,7 +72,7 @@ def post(ctx: Configuration, method, json):

@cli.command("ws")
@click.argument(
'wstype', autocompletion=autocompletion.wsapi_methods # type: ignore
'wstype', shell_complete=autocompletion.wsapi_methods # type: ignore
)
@click.option('--json')
@pass_context
Expand Down
2 changes: 1 addition & 1 deletion homeassistant_cli/plugins/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def list_cmd(ctx: Configuration, servicefilter):
@click.argument(
'service',
required=True,
autocompletion=autocompletion.services, # type: ignore
shell_complete=autocompletion.services, # type: ignore
)
@click.option(
'--arguments', help="Comma separated key/value pairs to use as arguments."
Expand Down
Loading

0 comments on commit 904de37

Please sign in to comment.