Tutorial for creating a custom component

I started a tutorial series on creating custom components: https://aarongodfrey.dev/home%20automation/building_a_home_assistant_custom_component_part_1/

Would love to get feedback if any of the content is useful or if I missed anything obvious.

14 Likes

Hi Aaron
This is extremely usefully and I look forward to part 2.
/B

1 Like

Here’s the 2nd part of the tutorial, covering unit testing, continuous integration and pre-commit: https://aarongodfrey.dev/home%20automation/building_a_home_assistant_custom_component_part_2/

4 Likes

Hi,
I tried to implement the pre-commit but keep getting the below error on many of the imports. I do the development in a Dev Container, so I don’t know if that has any influence:

custom_components/meteobridge/__init__.py:6: error: Cannot find implementation
or library stub for module named 'homeassistant.helpers.device_registry' 
[import]
    import homeassistant.helpers.device_registry as dr

I get several of these message, both for Home Assistant imports and for Local imports from const.py file.

I must surely do something wrong, but I am not sure what is it. I have a .pre-commit-config.yaml file in my root directory that looks like this:

repos:
  - repo: https://github.com/asottile/pyupgrade
    rev: v2.3.0
    hooks:
      - id: pyupgrade
        args: [--py37-plus]
  - repo: https://github.com/psf/black
    rev: 19.10b0
    hooks:
      - id: black
        args:
          - --safe
          - --quiet
        files: ^((homeassistant|script|tests)/.+)?[^/]+\.py$
  - repo: https://github.com/codespell-project/codespell
    rev: v1.16.0
    hooks:
      - id: codespell
        args:
          - --ignore-words-list=hass,alot,datas,dof,dur,farenheit,hist,iff,ines,ist,lightsensor,mut,nd,pres,referer,ser,serie,te,technik,ue,uint,visability,wan,wanna,withing
          - --skip="./.*,*.csv,*.json"
          - --quiet-level=2
        exclude_types: [csv, json]
  - repo: https://gitlab.com/pycqa/flake8
    rev: 3.8.1
    hooks:
      - id: flake8
        additional_dependencies:
          - flake8-docstrings==1.5.0
          - pydocstyle==5.0.2
        files: ^(homeassistant|script|tests)/.+\.py$
  - repo: https://github.com/PyCQA/bandit
    rev: 1.6.2
    hooks:
      - id: bandit
        args:
          - --quiet
          - --format=custom
          - --configfile=tests/bandit.yaml
        files: ^(homeassistant|script|tests)/.+\.py$
  - repo: https://github.com/pre-commit/mirrors-isort
    rev: v4.3.21
    hooks:
      - id: isort
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v2.4.0
    hooks:
      - id: check-executables-have-shebangs
        stages: [manual]
      - id: check-json
  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.770
    hooks:
      - id: mypy
        args:
          - --pretty
          - --show-error-codes
          - --show-error-context

And I installed pre-commit as per instructions.

What I would suggest to do in this instance is add an additional argument to the mypy pre-commit check to ignore missing imports.

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 9931e3a..5f58332 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -56,3 +56,4 @@ repos:
           - --pretty
           - --show-error-codes
           - --show-error-context
+          - --ignore-missing-imports

See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports for more information on the cause of that error.

Thanks, that worked. Now on to reading why it shows up, and see if I can fix it :sweat_smile: