Skip to main content

Provide access to Home Assistant API

Project description

m-hass-api

A Python package for interacting with Home Assistant API, complete with sample code, unit tests, and deployment documentation.

Project Structure

m-hass-api/
├── README.md
├── setup.py
├── requirements.txt
├── .gitignore
├── m_hass_api/
│   ├── __init__.py
│   └── sample.py
└── tests/
    ├── __init__.py
    └── test_sample.py

Installation

From Source (Development)

# Clone the repository
git clone <repository-url>
cd m-hass-api

# Create a virtual environment
python -m venv venv

# Activate the virtual environment
# On macOS/Linux:
source venv/bin/activate
# On Windows:
# venv\Scripts\activate

# Install the package in development mode
pip install -e .

From PyPI (Production)

pip install m-hass-api

Usage

Basic Example

from m_hass_api import HassApiClient

# Create a client instance
client = HassApiClient(base_url="https://api.example.com")

# Make a sample API call
result = client.get_data()
print(f"Result: {result}")

# Get data with parameters
filtered_result = client.get_data(param1="value1", param2="value2")
print(f"Filtered Result: {filtered_result}")

Running the Sample Script

# Activate your virtual environment first
source venv/bin/activate  # On macOS/Linux
# venv\Scripts\activate  # On Windows

# Run the sample
python -m m_hass_api.hass_api_client

Testing

Running All Tests

# Activate your virtual environment
source venv/bin/activate  # On macOS/Linux

# Install test dependencies
pip install -r requirements.txt

# Run all tests
pytest tests/

# Run with coverage
pytest tests/ --cov=m_hass_api --cov-report=html

# Open coverage report (macOS)
open htmlcov/index.html

Running Specific Test Files

pytest tests/test_hass_api_client.py

Running Specific Tests

# Run a specific test
pytest tests/test_sample.py::test_sample_client_initialization

# Run tests matching a pattern
pytest tests/ -k "test_get_data"

Viewing Test Output

# Verbose output
pytest tests/ -v

# Show print statements
pytest tests/ -v -s

Development

Project Setup

# Install development dependencies
pip install -r requirements.txt

# Install package in editable mode
pip install -e .

Code Quality

# Format code with black
black m_hass_api/ tests/

# Check code style with flake8
flake8 m_hass_api/ tests/

# Type checking with mypy (optional)
mypy m_hass_api/

Deployment

Building the Package

# Install build tools
pip install build twine

# Build the package
python -m build

# This will create:
# - dist/m_hass_api-0.1.0.tar.gz (source distribution)
# - dist/m_hass_api-0.1.0-py3-none-any.whl (wheel)

Testing the Package Locally

# Upload to TestPyPI
pip install twine
twine upload --repository testpypi dist/*

# Install from TestPyPI
pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple m-hass-api

Publishing to PyPI

Prerequisites:

  1. Create an account at https://pypi.org/account/register/
  2. Enable 2-Factor Authentication
  3. Generate an API token at https://pypi.org/manage/account/token/
# Upload to PyPI
twine upload dist/*

# Your package will be available at:
# https://pypi.org/project/m-hass-api/

Deployment Checklist

Before deploying, ensure:

  • Update version number in setup.py
  • Update README.md with latest changes
  • All tests pass: pytest tests/
  • Code is formatted: black m_hass_api/ tests/
  • No linting errors: flake8 m_hass_api/ tests/
  • Changelog is updated (if applicable)
  • Documentation is complete

Version Management

Update the version in setup.py following semantic versioning:

  • MAJOR: Incompatible API changes
  • MINOR: Backwards-compatible functionality additions
  • PATCH: Backwards-compatible bug fixes

Example: 0.1.00.1.1 (patch) → 0.2.0 (minor) → 1.0.0 (major)

GitHub Actions CI/CD (Optional)

You can automate testing and deployment with GitHub Actions. Create .github/workflows/ci.yml:

name: CI/CD

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.9'
      - name: Install dependencies
        run: |
          pip install -r requirements.txt
          pip install -e .
      - name: Run tests
        run: pytest tests/ --cov=m_hass_api

  deploy:
    needs: test
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.9'
      - name: Install build dependencies
        run: pip install build twine
      - name: Build package
        run: python -m build
      - name: Publish to PyPI
        env:
          TWINE_USERNAME: __token__
          TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
        run: twine upload dist/*

Troubleshooting

Common Issues

Import Error:

# Make sure you've installed the package
pip install -e .

Tests Failing:

# Ensure all dependencies are installed
pip install -r requirements.txt

Build Failing:

# Clean and rebuild
rm -rf build/ dist/ *.egg-info
python -m build

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass (pytest tests/)
  6. Commit your changes (git commit -m 'Add amazing feature')
  7. Push to the branch (git push origin feature/amazing-feature)
  8. Open a Pull Request

License

MIT License - feel free to use this template for your own projects.

Support

For issues, questions, or contributions, please open an issue on GitHub.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

m_hass_api-0.2.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

m_hass_api-0.2.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file m_hass_api-0.2.0.tar.gz.

File metadata

  • Download URL: m_hass_api-0.2.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for m_hass_api-0.2.0.tar.gz
Algorithm Hash digest
SHA256 87f4bc889330de225c2dd5c22fe1da885fe6539e4334f3d65ba20fb5ef47daa4
MD5 266f815a7d8e7504f9149c0f848457dd
BLAKE2b-256 b93a4b03db683dbc671d66b33963f80aa58f84c7bd2cdef8f64ef7c34d7f9992

See more details on using hashes here.

File details

Details for the file m_hass_api-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: m_hass_api-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for m_hass_api-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 723a58be11ce3bc5bff001c9045b748683dbe6279f62b312799c9b93cfe0da12
MD5 99b2d2ed49a34fe6e438630683044a26
BLAKE2b-256 3823b6b8f7444aac85909e64cc97a27c873722f24f930e24656b99160e64f3fe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page