2018-05-10 18:09:22 +00:00
|
|
|
"""Script to manage users for the Home Assistant auth provider."""
|
|
|
|
import argparse
|
2018-06-29 04:02:45 +00:00
|
|
|
import asyncio
|
2018-07-13 13:31:20 +00:00
|
|
|
import logging
|
2018-05-10 18:09:22 +00:00
|
|
|
import os
|
|
|
|
|
2018-07-13 13:31:20 +00:00
|
|
|
from homeassistant.auth import auth_manager_from_config
|
2018-06-29 04:02:45 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2018-05-10 18:09:22 +00:00
|
|
|
from homeassistant.config import get_default_config_dir
|
2018-07-13 09:43:08 +00:00
|
|
|
from homeassistant.auth.providers import homeassistant as hass_auth
|
2018-05-10 18:09:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def run(args):
|
|
|
|
"""Handle Home Assistant auth provider script."""
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description=("Manage Home Assistant users"))
|
|
|
|
parser.add_argument(
|
|
|
|
'--script', choices=['auth'])
|
|
|
|
parser.add_argument(
|
|
|
|
'-c', '--config',
|
|
|
|
default=get_default_config_dir(),
|
|
|
|
help="Directory that contains the Home Assistant configuration")
|
|
|
|
|
2018-06-29 04:02:45 +00:00
|
|
|
subparsers = parser.add_subparsers(dest='func')
|
|
|
|
subparsers.required = True
|
2018-05-10 18:09:22 +00:00
|
|
|
parser_list = subparsers.add_parser('list')
|
|
|
|
parser_list.set_defaults(func=list_users)
|
|
|
|
|
|
|
|
parser_add = subparsers.add_parser('add')
|
|
|
|
parser_add.add_argument('username', type=str)
|
|
|
|
parser_add.add_argument('password', type=str)
|
|
|
|
parser_add.set_defaults(func=add_user)
|
|
|
|
|
|
|
|
parser_validate_login = subparsers.add_parser('validate')
|
|
|
|
parser_validate_login.add_argument('username', type=str)
|
|
|
|
parser_validate_login.add_argument('password', type=str)
|
|
|
|
parser_validate_login.set_defaults(func=validate_login)
|
|
|
|
|
|
|
|
parser_change_pw = subparsers.add_parser('change_password')
|
|
|
|
parser_change_pw.add_argument('username', type=str)
|
|
|
|
parser_change_pw.add_argument('new_password', type=str)
|
|
|
|
parser_change_pw.set_defaults(func=change_password)
|
|
|
|
|
|
|
|
args = parser.parse_args(args)
|
2018-06-29 04:02:45 +00:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
hass = HomeAssistant(loop=loop)
|
2018-07-13 13:31:20 +00:00
|
|
|
loop.run_until_complete(run_command(hass, args))
|
|
|
|
|
|
|
|
# Triggers save on used storage helpers with delay (core auth)
|
|
|
|
logging.getLogger('homeassistant.core').setLevel(logging.WARNING)
|
|
|
|
loop.run_until_complete(hass.async_stop())
|
|
|
|
|
|
|
|
|
|
|
|
async def run_command(hass, args):
|
|
|
|
"""Run the command."""
|
2018-06-29 04:02:45 +00:00
|
|
|
hass.config.config_dir = os.path.join(os.getcwd(), args.config)
|
2018-07-13 13:31:20 +00:00
|
|
|
hass.auth = await auth_manager_from_config(hass, [{
|
|
|
|
'type': 'homeassistant',
|
|
|
|
}])
|
|
|
|
provider = hass.auth.auth_providers[0]
|
|
|
|
await provider.async_initialize()
|
|
|
|
await args.func(hass, provider, args)
|
2018-05-10 18:09:22 +00:00
|
|
|
|
|
|
|
|
2018-07-13 13:31:20 +00:00
|
|
|
async def list_users(hass, provider, args):
|
2018-05-10 18:09:22 +00:00
|
|
|
"""List the users."""
|
|
|
|
count = 0
|
2018-07-13 13:31:20 +00:00
|
|
|
for user in provider.data.users:
|
2018-05-10 18:09:22 +00:00
|
|
|
count += 1
|
|
|
|
print(user['username'])
|
|
|
|
|
|
|
|
print()
|
|
|
|
print("Total users:", count)
|
|
|
|
|
|
|
|
|
2018-07-13 13:31:20 +00:00
|
|
|
async def add_user(hass, provider, args):
|
2018-05-10 18:09:22 +00:00
|
|
|
"""Create a user."""
|
2018-07-13 13:31:20 +00:00
|
|
|
try:
|
|
|
|
provider.data.add_auth(args.username, args.password)
|
|
|
|
except hass_auth.InvalidUser:
|
|
|
|
print("Username already exists!")
|
|
|
|
return
|
|
|
|
|
|
|
|
# Save username/password
|
|
|
|
await provider.data.async_save()
|
2018-07-19 20:10:36 +00:00
|
|
|
print("Auth created")
|
2018-05-10 18:09:22 +00:00
|
|
|
|
|
|
|
|
2018-07-13 13:31:20 +00:00
|
|
|
async def validate_login(hass, provider, args):
|
2018-05-10 18:09:22 +00:00
|
|
|
"""Validate a login."""
|
|
|
|
try:
|
2018-07-13 13:31:20 +00:00
|
|
|
provider.data.validate_login(args.username, args.password)
|
2018-05-10 18:09:22 +00:00
|
|
|
print("Auth valid")
|
|
|
|
except hass_auth.InvalidAuth:
|
|
|
|
print("Auth invalid")
|
|
|
|
|
|
|
|
|
2018-07-13 13:31:20 +00:00
|
|
|
async def change_password(hass, provider, args):
|
2018-05-10 18:09:22 +00:00
|
|
|
"""Change password."""
|
|
|
|
try:
|
2018-07-13 13:31:20 +00:00
|
|
|
provider.data.change_password(args.username, args.new_password)
|
|
|
|
await provider.data.async_save()
|
2018-05-10 18:09:22 +00:00
|
|
|
print("Password changed")
|
|
|
|
except hass_auth.InvalidUser:
|
|
|
|
print("User not found")
|