Metadata-Version: 2.4
Name: pydmi-weather
Version: 0.1.1
Summary: Python wrapper for DMI's free Open Data APIs (forecast now, observations later), sync and async
Author: Bjarne Riis
License-Expression: MIT
Project-URL: Source, https://github.com/briis/pydmi
Project-URL: DMI API docs, https://www.dmi.dk/friedata/dokumentation/forecast-data-edr-api
Project-URL: DMI terms of use, https://www.dmi.dk/friedata/dokumentation/terms-of-use
Keywords: dmi,weather,forecast,harmonie,home-assistant
Classifier: Programming Language :: Python :: 3
Classifier: Framework :: AsyncIO
Classifier: Topic :: Scientific/Engineering :: Atmospheric Science
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE.txt
Provides-Extra: async
Requires-Dist: aiohttp>=3.9; extra == "async"
Provides-Extra: test
Requires-Dist: pytest>=8; extra == "test"
Requires-Dist: pytest-asyncio>=0.23; extra == "test"
Requires-Dist: aiohttp>=3.9; extra == "test"
Dynamic: license-file

# pydmi-weather

Install as `pydmi-weather`, import as `pydmi`.

Python wrapper for the **free** Open Data APIs from DMI (Danmarks Meteorologiske Institut).

| API | Status |
|---|---|
| [Forecast Data EDR API](https://www.dmi.dk/friedata/dokumentation/forecast-data-edr-api) | ✅ `DMIForecast` / `DMIForecastAsync` |
| [Meteorological Observations API](https://www.dmi.dk/friedata/dokumentation/meteorological-observation-api) | planned (current conditions for the HA weather entity) |

- HARMONIE DINI model: ~2 km grid, hourly steps, ~60 h ahead, new run every 3 hours
- **Sync client**: standard library only, thread-safe. Use it in services like WeatherDataLogger.
- **Async client**: aiohttp, accepts an injected session. Use it in Home Assistant.
- Output in normal units (°C, hPa, m/s, mm, %) with unit-suffixed field names
- Derived Home Assistant condition strings, day/night aware
- Daily aggregates in your local time zone

No API key is needed. DMI removed authentication from `opendataapi.dmi.dk`.

## Install

```bash
pip install pydmi-weather    # sync client, no dependencies
pip install "pydmi-weather[async]"   # + aiohttp for the async client
```

## Usage

```python
from pydmi import DMIForecast, ATTRIBUTION

dmi = DMIForecast(55.64, 12.08)          # lat, lon
fc = dmi.get_forecast()                  # cached for update_interval (30 min)

print(fc.model_run, ATTRIBUTION)
now = fc.current()
print(now.temperature_c, now.condition, now.wind_speed_ms)

for h in fc.hourly()[:6]:
    print(h.datetime, h.temperature_c, h.precipitation_mm, h.condition)

for d in fc.daily():                     # Europe/Copenhagen by default
    print(d.date, d.temperature_min_c, d.temperature_max_c, d.precipitation_mm, d.condition)

payload = fc.to_dict()                   # JSON-ready, e.g. for MQTT
```

Async:

```python
from pydmi.client_async import DMIForecastAsync

async with DMIForecastAsync(55.64, 12.08, session=session) as dmi:
    fc = await dmi.get_forecast()
```

See `examples/` for an MQTT publisher (WeatherDataLogger style) and a Home Assistant
coordinator and weather entity.

## Respecting the free service

DMI Open Data is free to use, and the library is built to keep it that way:

| What | How |
|---|---|
| Fair use limit (500 req / 5 s, shared by everyone) | Responses are cached. `update_interval` defaults to 30 min and cannot go below 10 min. The model only changes every 3 h anyway. |
| HTTP 429 | The client honours `Retry-After`. While it waits it returns the cached forecast and does not contact DMI. |
| Minimal payload | One `/position` request, 13 parameters, CoverageJSON (DMI says it is >10× smaller than GeoJSON), gzip |
| Cacheable requests | Coordinates are rounded to 2 decimals (~1 km, finer than the 2 km grid) |
| Identification | `User-Agent: pydmi/<version>` |
| Concurrency | A lock makes concurrent callers share one request |

### Licence: CC BY 4.0, attribution required

DMI's data is licensed under [CC BY 4.0](https://www.dmi.dk/friedata/dokumentation/terms-of-use).
Wherever the data is shown, you must credit DMI. Use `pydmi.ATTRIBUTION`
(`fc.attribution`). In Home Assistant, set it as `_attr_attribution`.

## Data notes

- **Precipitation**: DMI's `total-precipitation` is accumulated from the start of the run.
  The library converts it to the amount per hour. `precipitation_mm` on an hour is the
  amount expected *during that hour* (the Home Assistant / met.no convention). The last
  step is `None`.
- **Condition**: the EDR API has no weather symbol, so one is derived. Priority order:
  thunder (lightning probability ≥ 30 %; note DMI returns this in percent, although its metadata says 0..1), precipitation (≥ 0.1 mm/h, with rain, snow,
  sleet or hail taken from `precipitation-type`), fog (visibility < 1 km), wind
  (≥ 13.9 m/s), then cloud cover. Night uses `clear-night`. All thresholds are in `const.py`.
- **Daily**: grouped by local date. Today covers the rest of the day. Later days with
  fewer than 12 hours (the end of the run) are dropped.
- **Model run**: `fc.model_run` is the first time step of the run (UTC).

## Development

```bash
pip install -e ".[test]"
pytest
```

The tests use a local mock server, so DMI is never contacted.
