Code Monkey home page Code Monkey logo

cookiecutter-homeassistant-component's Introduction

cookiecutter-homeassistant-component

A cookiecutter template for generating a new homeassistant custom component.

Features

  • Generates the appropriate file structure for a minimal homeassistant custom component.
  • Generates metadata to allow submission to HACS.
  • Contains everything needed to get started with writing unit tests for your custom component.
  • Contains an optional pre-commit config that mimics what the homeassistant core uses to help make your code compliant in case you want to eventually add it to the core codebase.

Installation

To get started you will need to install cookiecutter.

$ pip install cookiecutter

Then generate your custom component:

$ cookiecutter https://github.com/boralyl/cookiecutter-homeassistant-component

Context Options

You will be asked to provide the following values to configure your component:

Field Default Description
domain my_component The domain name of your custom component.
name My Component The human readable name of your component.
docs_url https://github.com/user/my_component/ The URL pointing to documentation for your component.
owner @user Your github username prefixed with @.
config_flow yes Signifies if your component will support configuring via the UI via config flow.
iot_class assumed_state The Internet of Things classification for your component. Read more in the Home Assistant documentation.
version 1.0.0 The version number for your custom component. Read more in the Home Assistant documentation.

Pre-Commit

After your project is created a .pre-commit-config.yaml file is created in the root of your project.
This is completely optional, but will help you to conform you code to the homeassistant standards. The configuration matches that used by homeassistant/core.

To start using it first install pre-commit, then initialize it.

$ pip install pre-commit
$ pre-commit install
pre-commit installed at .git/hooks/pre-commit

This will cause pre-commit checks to be run prior to every commit. Any failed checks will prevent you from pushing your code.

$ git commit -a
pyupgrade................................................................Passed
black....................................................................Passed
codespell................................................................Passed
flake8...................................................................Passed
bandit...................................................................Passed
isort....................................................................Passed
Check JSON...........................................(no files to check)Skipped
mypy.....................................................................Passed

Testing

After generating your project you can install the test requirements using pip:

$ pip install -r requirements.test.txt

Once the test requirements are installed you can run the test suite. A simple working test is provided out of the box.

$ pytest
Test session starts (platform: linux, Python 3.7.5, pytest 5.4.2, pytest-sugar 0.9.3)
rootdir: /home/aaron/projects/my-component, inifile: setup.cfg, testpaths: tests
plugins: aiohttp-0.3.0, requests-mock-1.8.0, timeout-1.3.4, sugar-0.9.3, cov-2.8.1, homeassistant-0.1.0
collecting ...
 tests/test_init.py ✓                                                                                                                                                          100% ██████████

----------- coverage: platform linux, python 3.7.5-final-0 -----------
Name                                         Stmts   Miss  Cover   Missing
--------------------------------------------------------------------------
custom_components/__init__.py                    0      0   100%
custom_components/my_component/__init__.py       3      0   100%
custom_components/my_component/const.py          1      0   100%
--------------------------------------------------------------------------
TOTAL                                            4      0   100%


Results (0.07s):
       1 passed

cookiecutter-homeassistant-component's People

Contributors

boralyl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cookiecutter-homeassistant-component's Issues

pre-commit fails due to flake8 gitlab mirror being deleted

I tried to use pre-commit but I get an error due to the flake8 team having deleted the gitlab mirror:

$ git commit -a
[INFO] Initializing environment for https://github.com/asottile/pyupgrade.
[INFO] Initializing environment for https://github.com/psf/black.
[INFO] Initializing environment for https://github.com/codespell-project/codespell.
[INFO] Initializing environment for https://gitlab.com/pycqa/flake8.
info: please complete authentication in your browser...
An unexpected error has occurred: CalledProcessError: command: ('/usr/lib/git-core/git', 'fetch', 'origin', '--tags')
return code: 128
stdout: (none)
stderr:
    remote: The project you were looking for could not be found or you don't have permission to view it.
    fatal: repository 'https://gitlab.com/pycqa/flake8.git/' not found
Check the log at /home/raf/.cache/pre-commit/pre-commit.log

I then tried to comment the flake8 package from the yaml but I get other errors that I do not understand:

[INFO] Installing environment for https://github.com/pre-commit/mirrors-mypy.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: CalledProcessError: command: ('/home/raf/.cache/pre-commit/repotg0zhz3j/py_env-python3/bin/python', '-mpip', 'install', '.')
return code: 1
stdout:
    Processing /home/raf/.cache/pre-commit/repotg0zhz3j
      Installing build dependencies: started
      Installing build dependencies: finished with status 'done'
      Getting requirements to build wheel: started
      Getting requirements to build wheel: finished with status 'done'
      Installing backend dependencies: started
      Installing backend dependencies: finished with status 'done'
      Preparing metadata (pyproject.toml): started
      Preparing metadata (pyproject.toml): finished with status 'done'
    Collecting mypy==0.770 (from pre_commit_dummy_package==0.0.0)
      Downloading mypy-0.770-py3-none-any.whl.metadata (1.4 kB)
    Collecting typed-ast<1.5.0,>=1.4.0 (from mypy==0.770->pre_commit_dummy_package==0.0.0)
      Downloading typed_ast-1.4.3.tar.gz (210 kB)
         ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 210.9/210.9 kB 5.3 MB/s eta 0:00:00
      Installing build dependencies: started
      Installing build dependencies: finished with status 'done'
      Getting requirements to build wheel: started
      Getting requirements to build wheel: finished with status 'done'
      Installing backend dependencies: started
      Installing backend dependencies: finished with status 'done'
      Preparing metadata (pyproject.toml): started
      Preparing metadata (pyproject.toml): finished with status 'done'
    Collecting typing-extensions>=3.7.4 (from mypy==0.770->pre_commit_dummy_package==0.0.0)
      Downloading typing_extensions-4.12.1-py3-none-any.whl.metadata (3.0 kB)
    Collecting mypy-extensions<0.5.0,>=0.4.3 (from mypy==0.770->pre_commit_dummy_package==0.0.0)
      Downloading mypy_extensions-0.4.4.tar.gz (4.2 kB)
      Installing build dependencies: started
      Installing build dependencies: finished with status 'done'
      Getting requirements to build wheel: started
      Getting requirements to build wheel: finished with status 'done'
      Installing backend dependencies: started
      Installing backend dependencies: finished with status 'done'
      Preparing metadata (pyproject.toml): started
      Preparing metadata (pyproject.toml): finished with status 'done'
    Downloading mypy-0.770-py3-none-any.whl (1.9 MB)
       ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.9/1.9 MB 36.4 MB/s eta 0:00:00
    Downloading typing_extensions-4.12.1-py3-none-any.whl (37 kB)
    Building wheels for collected packages: pre_commit_dummy_package, mypy-extensions, typed-ast
      Building wheel for pre_commit_dummy_package (pyproject.toml): started
      Building wheel for pre_commit_dummy_package (pyproject.toml): finished with status 'done'
      Created wheel for pre_commit_dummy_package: filename=pre_commit_dummy_package-0.0.0-py3-none-any.whl size=1120 sha256=5f2c803329dedc2761eccf5b64aad25a1de7c03664d400870b252fbc64173fce
      Stored in directory: /tmp/pip-ephem-wheel-cache-44u4k88u/wheels/95/cf/3e/6b4c297218befed411473c464fd69465f95212e1efa66a9527
      Building wheel for mypy-extensions (pyproject.toml): started
      Building wheel for mypy-extensions (pyproject.toml): finished with status 'done'
      Created wheel for mypy-extensions: filename=mypy_extensions-0.4.4-py2.py3-none-any.whl size=4471 sha256=fe3d5b27504f8f8c3bd038ea93147d4c1bedb02b8c279af75b210709f278338e
      Stored in directory: /home/raf/.cache/pip/wheels/c8/8b/b5/4826a8f3859371d619b2b20d683910a65d271460f6807fd54f
      Building wheel for typed-ast (pyproject.toml): started
      Building wheel for typed-ast (pyproject.toml): finished with status 'error'
    Successfully built pre_commit_dummy_package mypy-extensions
    Failed to build typed-ast
stderr:
      error: subprocess-exited-with-error
      
      × Building wheel for typed-ast (pyproject.toml) did not run successfully.
      │ exit code: 1
      ╰─> [26 lines of output]
          running bdist_wheel
          running build
          running build_py
          creating build
          creating build/lib.linux-x86_64-cpython-312
          creating build/lib.linux-x86_64-cpython-312/typed_ast
          copying typed_ast/__init__.py -> build/lib.linux-x86_64-cpython-312/typed_ast
          copying typed_ast/conversions.py -> build/lib.linux-x86_64-cpython-312/typed_ast
          copying typed_ast/ast27.py -> build/lib.linux-x86_64-cpython-312/typed_ast
          copying typed_ast/ast3.py -> build/lib.linux-x86_64-cpython-312/typed_ast
          creating build/lib.linux-x86_64-cpython-312/typed_ast/tests
          copying ast3/tests/test_basics.py -> build/lib.linux-x86_64-cpython-312/typed_ast/tests
          running build_ext
          building '_ast27' extension
          creating build/temp.linux-x86_64-cpython-312
          creating build/temp.linux-x86_64-cpython-312/ast27
          creating build/temp.linux-x86_64-cpython-312/ast27/Custom
          creating build/temp.linux-x86_64-cpython-312/ast27/Parser
          creating build/temp.linux-x86_64-cpython-312/ast27/Python
          x86_64-linux-gnu-gcc -fno-strict-overflow -Wsign-compare -DNDEBUG -g -O2 -Wall -fPIC -Iast27/Include -I/home/raf/.cache/pre-commit/repotg0zhz3j/py_env-python3/include -I/usr/include/python3.12 -c ast27/Custom/typed_ast.c -o build/temp.linux-x86_64-cpython-312/ast27/Custom/typed_ast.o
          In file included from ast27/Custom/typed_ast.c:3:
          ast27/Custom/../Include/compile.h:5:10: fatal error: code.h: No such file or directory
              5 | #include "code.h"
                |          ^~~~~~~~
          compilation terminated.
          error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1
          [end of output]
      
      note: This error originates from a subprocess, and is likely not a problem with pip.
      ERROR: Failed building wheel for typed-ast
    ERROR: Could not build wheels for typed-ast, which is required to install pyproject.toml-based projects
Check the log at /home/raf/.cache/pre-commit/pre-commit.log

Please note that I just created the project from the cookiecutter template and didn´t change the created files.

Permission error

The error message is in Norwegian, but it says the the process cannot access the file because it is used by another process.

`D:\Kladd\e>cookiecutter https://github.com/boralyl/cookiecutter-homeassistant-component
domain [my_component]: elprice
name [My Component]: ElPrice
docs_url [https://github.com/user/my_component/]:
owner [@user]: @stigvi
Select config_flow:
1 - yes
2 - no
Choose from 1, 2 [1]: 2
Select iot_class:
1 - Assumed State
2 - Cloud Polling
3 - Cloud Push
4 - Local Polling
5 - Local Push
Choose from 1, 2, 3, 4, 5 [1]: 1
Traceback (most recent call last):
File "C:\Users\STIGVI~1.HEI\AppData\Local\Temp\tmpz14vwgnu.py", line 6, in
os.rename(os.path.join("..", repo_dir), os.path.join("..", new_repo_dir))
PermissionError: [WinError 32] Prosessen får ikke tilgang til filen fordi den brukes av en annen prosess: '..\elprice' -> '..\elprice'
ERROR: Stopping generation because post_gen_project hook script didn't exit successfully
Hook script failed (exit status: 1)

D:\Kladd\e>`

error generating files

I am getting the following error running cookiecutter in a windows environment after entering the final option in the wizard:

Choose from 1, 2 [1]:
Select iot_class:
1 - Assumed State
2 - Cloud Polling
3 - Cloud Push
4 - Local Polling
5 - Local Push
Choose from 1, 2, 3, 4, 5 [1]:
Traceback (most recent call last):
File "c:\users\j\appdata\local\temp\tmpqwx1ms.py", line 6, in
os.rename(os.path.join("..", repo_dir), os.path.join("..", new_repo_dir))
WindowsError: [Error 32] The process cannot access the file because it is being used by another process
ERROR: Stopping generation because post_gen_project hook script didn't exit successfully
Hook script failed (exit status: 1)

Cannot install dependencies & run test suite

This could be related to some recent updates, but I'm getting the following issues:

  • Can't install the dependencies with pip install -r requirements.test.txt --upgrade because of ERROR: Cannot install -r requirements.test.txt (line 3) and pytest-cov==2.9.0 because these package versions have conflicting dependencies., see
    pip-output.txt for full output. Once the versions of pytest-cov is unpinned, install can proceed, but perhaps that leads to later issues.
  • pytest fails to test anything with the following output:
  /Users/wbyoung/Code/watersmart/venv/lib/python3.12/site-packages/_pytest/python.py:151: PytestUnhandledCoroutineWarning: async def functions are not natively supported and have been skipped.
  You need to install a suitable plugin for your async framework, for example:
    - anyio
    - pytest-asyncio
    - pytest-tornasync
    - pytest-trio
    - pytest-twisted
    warnings.warn(PytestUnhandledCoroutineWarning(msg.format(nodeid)))
  • Once asyncio_mode = auto is added to the [tool:pytest] section of setup.cfg, tests proceed, but the setup is never invoked and stderr includes the following: ERROR:homeassistant.setup:Setup failed for 'watersmart': Integration not found..

Any suggestions on how to proceed?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.