A Home Assistant custom integration for accessing energy consumption data from Fortum's MittFortum service.
- Energy Consumption Monitoring: Track your energy usage over time
- Cost Tracking: Monitor energy costs in SEK
- Secure OAuth2 Authentication: Uses Fortum's official authentication system
- Automatic Token Refresh: Handles token expiration automatically
- Device Integration: Creates a device in Home Assistant for easy management
This integration is not yet available in the default HACS repositories, but you can add it as a custom repository:
- Open HACS in Home Assistant
- Click on the 3 dots in the top right corner
- Select "Custom repositories"
- Add the repository URL:
https://github.com/selleronom/mittfortum - Select "Integration" as the category
- Click the "ADD" button
- Search for "MittFortum" in HACS and install it
- Restart Home Assistant
- Download the latest release from the releases page
- Copy the
custom_components/mittfortumdirectory to your Home Assistantcustom_componentsdirectory - Restart Home Assistant
- Go to Configuration > Integrations
- Click "Add Integration"
- Search for "MittFortum"
- Enter your MittFortum username and password
- Complete the setup
The integration creates the following entities:
- Energy Consumption Sensor: Total energy consumption in kWh
- Total Cost Sensor: Total energy cost in SEK
This integration follows modern Home Assistant development practices:
custom_components/mittfortum/
├── __init__.py # Integration setup and teardown
├── api/ # API client modules
│ ├── __init__.py
│ ├── auth.py # OAuth2 authentication client
│ ├── client.py # Main API client
│ └── endpoints.py # API endpoint definitions
├── sensors/ # Sensor entity modules
│ ├── __init__.py
│ ├── energy.py # Energy consumption sensor
│ └── cost.py # Cost sensor
├── config_flow.py # Configuration flow
├── const.py # Constants and configuration
├── coordinator.py # Data update coordinator
├── device.py # Device representation
├── entity.py # Base entity class
├── exceptions.py # Custom exceptions
├── models.py # Data models
├── sensor.py # Sensor platform setup
├── utils.py # Utility functions
├── manifest.json # Integration manifest
├── strings.json # UI strings
└── translations/ # Localization files
└── en.json
- Handles the complete OAuth2 flow with Fortum's SSO system
- Manages token lifecycle (access, refresh, ID tokens)
- Implements PKCE (Proof Key for Code Exchange) for security
- Provides high-level API for consuming Fortum services
- Handles authentication headers and token refresh
- Implements proper error handling and retry logic
- Manages data updates from the API
- Implements efficient polling with configurable intervals
- Handles API errors gracefully
- Type-safe data structures for API responses
- Consistent data validation and transformation
- Easy serialization/deserialization
- Comprehensive error hierarchy
- Clear error messages for debugging
- Proper exception chaining
- Type Safety: Full type annotations with pyrefly support
- Error Handling: Comprehensive exception handling with proper error messages
- Testing: Unit and integration tests with high coverage
- Code Quality: Pre-commit hooks with black, isort, flake8, and pyrefly
- Documentation: Comprehensive docstrings and README
- Logging: Structured logging for debugging and monitoring
# Install development dependencies
pip install -r requirements-dev.txt
# Install pre-commit hooks
pre-commit install
# Run tests
pytest
# Run type checking
pyrefly check
# Format code
black custom_components/mittfortum
isort custom_components/mittfortumThe project includes comprehensive tests:
# Run all tests
pytest
# Run unit tests only
pytest tests/unit
# Run integration tests only
pytest tests/integration
# Run with coverage
pytest --cov=custom_components.mittfortumThe project uses several tools to maintain code quality:
- Black: Code formatting
- isort: Import sorting
- flake8: Linting
- pyrefly: Type checking
- pre-commit: Git hooks for automated checks
- Authentication Failed: Verify your MittFortum credentials
- No Data: Check that you have energy consumption data in your MittFortum account
- Connection Issues: Verify your internet connection and Home Assistant's network access
Add the following to your configuration.yaml to enable debug logging:
logger:
default: info
logs:
custom_components.mittfortum: debug- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite and ensure all tests pass
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Complete refactor with modern Home Assistant practices
- Improved OAuth2 authentication flow
- Better error handling and logging
- Comprehensive test suite
- Type safety with pyrefly
- Modular architecture
- Previous version with basic functionality
- Fortum for providing the API
- Home Assistant community for guidance and best practices
To set up this integration, you need to provide your MittFortum username and password.
Once the integration is set up, you can start monitoring your energy usage from Home Assistant. Please note that this integration requires a MittFortum account. If you don't have an account, you can create one on the MittFortum website.