Skip to content

Commit

Permalink
Refactor the python package to new name (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
klaasnicolaas authored Jan 18, 2022
1 parent f5a8f41 commit b5a4260
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 136 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tweet-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
# GitHub event payload
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#release
tweet-message: |
⬆️ ${{ github.event.release.tag_name }} of ${{ github.event.repository.name }} just released 🎉 #update @klaasnicolaas #python #package #pure_energie #release #bot #assistant
⬆️ ${{ github.event.release.tag_name }} of ${{ github.event.repository.name }} just released 🎉 #update @klaasnicolaas #python #package #net2grid #energy #release #bot #assistant
Check out the release notes here: ${{ github.event.release.html_url }}
env:
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repos:
language: system
types: [python]
entry: poetry run bandit
files: ^pure_energie/
files: ^net2grid/
require_serial: true
- id: black
name: ☕️ Format using black
Expand Down Expand Up @@ -134,7 +134,7 @@ repos:
language: system
types: [python]
entry: poetry run pyupgrade
args: [--py36-plus]
args: [--py38-plus]
- id: trailing-whitespace
name: ✄ Trim Trailing Whitespace
language: system
Expand Down
62 changes: 33 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Python - PEM Client
## Python - NET2GRID Client

<!-- PROJECT SHIELDS -->
[![GitHub Release][releases-shield]][releases]
Expand All @@ -18,28 +18,32 @@
[![Code Coverage][codecov-shield]][codecov-url]
[![Build Status][build-shield]][build-url]

Asynchronous Python client for the Pure Energie Meter device.
Asynchronous Python client for NET2GRID devices.

## About

A python package with which you can read the data from your [Pure Energie Meter][pem] device via a local API, made by the company [Pure Energie][pure-energie]. With this device you can read your smart meter via the P1 serial port.
A python package with which you can read the data from a [NET2GRID][net2grid] device via a local API. NET2GRID supplies energy hardware to brands, that can market it as a white label product.

## Supported SmartBridges

- SBWF3102 ([Pure Energie][pure-energie])

## Installation

```bash
pip install pure-energie
pip install net2grid
```

## Usage

```py
import asyncio

from pure_energie import PureEnergie
from net2grid import Net2Grid

async def main():
"""Show example on getting data from your Pure Energie device."""
async with PureEnergie(
"""Show example on getting data from your NET2GRID device."""
async with Net2Grid(
host="example.com",
) as client:
device = await client.device()
Expand Down Expand Up @@ -142,33 +146,33 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

[pure-energie]: https://pure-energie.nl
[pem]: https://pure-energie.nl/kennisbank/pure-energie-meter
[net2grid]: https://www.net2grid.com
[poetry-install]: https://python-poetry.org/docs/#installation
[poetry]: https://python-poetry.org
[pre-commit]: https://pre-commit.com

<!-- MARKDOWN LINKS & IMAGES -->
[build-shield]: https://github.com/klaasnicolaas/python-pem/actions/workflows/tests.yaml/badge.svg
[build-url]: https://github.com/klaasnicolaas/python-pem/actions/workflows/tests.yaml
[code-quality-shield]: https://img.shields.io/lgtm/grade/python/g/klaasnicolaas/python-pem.svg?logo=lgtm&logoWidth=18
[code-quality]: https://lgtm.com/projects/g/klaasnicolaas/python-pem/context:python
[commits-shield]: https://img.shields.io/github/commit-activity/y/klaasnicolaas/python-pem.svg
[commits-url]: https://github.com/klaasnicolaas/python-pem/commits/main
[codecov-shield]: https://codecov.io/gh/klaasnicolaas/python-pem/branch/main/graph/badge.svg?token=VQTR24YFQ9
[codecov-url]: https://codecov.io/gh/klaasnicolaas/python-pem
[forks-shield]: https://img.shields.io/github/forks/klaasnicolaas/python-pem.svg
[forks-url]: https://github.com/klaasnicolaas/python-pem/network/members
[issues-shield]: https://img.shields.io/github/issues/klaasnicolaas/python-pem.svg
[issues-url]: https://github.com/klaasnicolaas/python-pem/issues
[license-shield]: https://img.shields.io/github/license/klaasnicolaas/python-pem.svg
[last-commit-shield]: https://img.shields.io/github/last-commit/klaasnicolaas/python-pem.svg
[build-shield]: https://github.com/klaasnicolaas/python-net2grid/actions/workflows/tests.yaml/badge.svg
[build-url]: https://github.com/klaasnicolaas/python-net2grid/actions/workflows/tests.yaml
[code-quality-shield]: https://img.shields.io/lgtm/grade/python/g/klaasnicolaas/python-net2grid.svg?logo=lgtm&logoWidth=18
[code-quality]: https://lgtm.com/projects/g/klaasnicolaas/python-net2grid/context:python
[commits-shield]: https://img.shields.io/github/commit-activity/y/klaasnicolaas/python-net2grid.svg
[commits-url]: https://github.com/klaasnicolaas/python-net2grid/commits/main
[codecov-shield]: https://codecov.io/gh/klaasnicolaas/python-net2grid/branch/main/graph/badge.svg?token=VQTR24YFQ9
[codecov-url]: https://codecov.io/gh/klaasnicolaas/python-net2grid
[forks-shield]: https://img.shields.io/github/forks/klaasnicolaas/python-net2grid.svg
[forks-url]: https://github.com/klaasnicolaas/python-net2grid/network/members
[issues-shield]: https://img.shields.io/github/issues/klaasnicolaas/python-net2grid.svg
[issues-url]: https://github.com/klaasnicolaas/python-net2grid/issues
[license-shield]: https://img.shields.io/github/license/klaasnicolaas/python-net2grid.svg
[last-commit-shield]: https://img.shields.io/github/last-commit/klaasnicolaas/python-net2grid.svg
[maintenance-shield]: https://img.shields.io/maintenance/yes/2021.svg
[maintainability-shield]: https://api.codeclimate.com/v1/badges/0b3297077cbc525a837e/maintainability
[maintainability-url]: https://codeclimate.com/github/klaasnicolaas/python-pem/maintainability
[maintainability-url]: https://codeclimate.com/github/klaasnicolaas/python-net2grid/maintainability
[project-stage-shield]: https://img.shields.io/badge/project%20stage-experimental-yellow.svg
[pypi]: https://pypi.org/project/pure_energie/
[python-versions-shield]: https://img.shields.io/pypi/pyversions/pure_energie
[releases-shield]: https://img.shields.io/github/release/klaasnicolaas/python-pem.svg
[releases]: https://github.com/klaasnicolaas/python-pem/releases
[stars-shield]: https://img.shields.io/github/stars/klaasnicolaas/python-pem.svg
[stars-url]: https://github.com/klaasnicolaas/python-pem/stargazers
[pypi]: https://pypi.org/project/net2grid/
[python-versions-shield]: https://img.shields.io/pypi/pyversions/net2grid
[releases-shield]: https://img.shields.io/github/release/klaasnicolaas/python-net2grid.svg
[releases]: https://github.com/klaasnicolaas/python-net2grid/releases
[stars-shield]: https://img.shields.io/github/stars/klaasnicolaas/python-net2grid.svg
[stars-url]: https://github.com/klaasnicolaas/python-net2grid/stargazers
13 changes: 13 additions & 0 deletions net2grid/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Asynchronous Python client for a NED2GRID device."""

from .exceptions import Net2GridConnectionError, Net2GridError
from .models import Device, SmartMeter
from .net2grid import Net2Grid

__all__ = [
"Device",
"Net2Grid",
"Net2GridError",
"Net2GridConnectionError",
"SmartMeter",
]
9 changes: 9 additions & 0 deletions net2grid/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Exceptions for NET2GRID."""


class Net2GridError(Exception):
"""Generic NET2Grid exception."""


class Net2GridConnectionError(Net2GridError):
"""NET2GRID connection exception."""
18 changes: 9 additions & 9 deletions pure_energie/models.py → net2grid/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Models for Pure Energie Meter."""
"""Models for NET2GRID."""
from __future__ import annotations

from dataclasses import dataclass
Expand All @@ -7,18 +7,18 @@

@dataclass
class SmartMeter:
"""Object representing an SmartMeter response from Pure Energie Meter."""
"""Object representing an SmartMeter response from a NET2GRID device."""

power_flow: int
energy_consumption_total: float
energy_production_total: float

@staticmethod
def from_dict(data: dict[str, Any]) -> SmartMeter:
"""Return SmartMeter object from the Pure Energie Meter API response.
"""Return SmartMeter object from the NET2GRID API response.
Args:
data: The data from the Pure Energie Meter API.
data: The data from the NET2GRID API.
Returns:
A SmartMeter object.
Expand Down Expand Up @@ -46,9 +46,9 @@ def convert(value):

@dataclass
class Device:
"""Object representing an Device response from Pure Energie Meter."""
"""Object representing an Device response from NET2GRID device."""

pem_id: str
n2g_id: str
model: str
batch: str
firmware: str
Expand All @@ -57,17 +57,17 @@ class Device:

@staticmethod
def from_dict(data: dict[str, Any]) -> Device:
"""Return Device object from the Pure Energie Meter API response.
"""Return Device object from the NET2GRID API response.
Args:
data: The data from the Pure Energie Meter API.
data: The data from the NET2GRID API.
Returns:
A Device object.
"""

return Device(
pem_id=data.get("id"),
n2g_id=data.get("id"),
model=data.get("model"),
batch=data.get("batch"),
firmware=data.get("fw"),
Expand Down
40 changes: 20 additions & 20 deletions pure_energie/pure_energie.py → net2grid/net2grid.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Asynchronous Python client for Pure Energie Meter API."""
"""Asynchronous Python client for NET2GRID devices."""
from __future__ import annotations

import asyncio
Expand All @@ -13,13 +13,13 @@
from aiohttp.hdrs import METH_GET
from yarl import URL

from .exceptions import PureEnergieMeterConnectionError
from .exceptions import Net2GridConnectionError
from .models import Device, SmartMeter


@dataclass
class PureEnergie:
"""Main class for handling connections with the Pure Energie Meter API."""
class Net2Grid:
"""Main class for handling connections with the NET2GRID devices."""

host: str
request_timeout: int = 10
Expand All @@ -34,26 +34,26 @@ async def request(
method: str = METH_GET,
data: dict | None = None,
) -> dict[str, Any]:
"""Handle a request to a Pure Energie device.
"""Handle a request to a NET2GRID device.
Args:
uri: Request URI, without '/', for example, 'status'
method: HTTP Method to use.
data: Dictionary of data to send to the Pure Energie API.
data: Dictionary of data to send to the NET2GRID API.
Returns:
A Python dictionary (text) with the response from
the Pure Energie device.
a NET2GRID device.
Raises:
PureEnergieMeterConnectionError: An error occurred while
communicating with the Pure Energie device.
Net2GridConnectionError: An error occurred while
communicating with the NET2GRID device.
"""
version = metadata.version(__package__)
url = URL.build(scheme="http", host=self.host, path="/").join(URL(uri))

headers = {
"User-Agent": f"PythonPureEnergie/{version}",
"User-Agent": f"PythonNet2Grid/{version}",
"Accept": "application/json, text/plain, */*",
}

Expand All @@ -71,35 +71,35 @@ async def request(
)
response.raise_for_status()
except asyncio.TimeoutError as exception:
raise PureEnergieMeterConnectionError(
"Timeout occurred while connecting to Pure Energie Meter device"
raise Net2GridConnectionError(
"Timeout occurred while connecting to the NET2GRID device"
) from exception
except (
ClientError,
ClientResponseError,
socket.gaierror,
) as exception:
raise PureEnergieMeterConnectionError(
"Error occurred while communicating with the Pure Energie Meter device"
raise Net2GridConnectionError(
"Error occurred while communicating with the NET2GRID device"
) from exception

return await response.text()

async def device(self) -> Device:
"""Get the latest values from the Pure Energie Meter.
"""Get the latest values from a NET2GRID device.
Returns:
A Device data object from the Pure Energie device API.
A Device data object from the API.
"""
data = await self.request("info")
data = json.loads(data)
return Device.from_dict(data)

async def smartmeter(self) -> SmartMeter:
"""Get the latest values from the Pure Energie Meter.
"""Get the latest values from a NET2GRID device.
Returns:
A SmartMeter data object from the Pure Energie device API.
A SmartMeter data object from the API.
"""
data = await self.request("meter/now")
data = json.loads(data)
Expand All @@ -110,11 +110,11 @@ async def close(self) -> None:
if self.session and self._close_session:
await self.session.close()

async def __aenter__(self) -> PureEnergie:
async def __aenter__(self) -> Net2Grid:
"""Async enter.
Returns:
The Pure Energie Meter object.
The NET2GRID object.
"""
return self

Expand Down
File renamed without changes.
13 changes: 0 additions & 13 deletions pure_energie/__init__.py

This file was deleted.

9 changes: 0 additions & 9 deletions pure_energie/exceptions.py

This file was deleted.

Loading

0 comments on commit b5a4260

Please sign in to comment.