Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ctek pypi #5

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 0 additions & 54 deletions homeassistant/components/nanogrid_air/api.py

This file was deleted.

21 changes: 9 additions & 12 deletions homeassistant/components/nanogrid_air/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

from __future__ import annotations

from ctek import NanogridAir
import voluptuous as vol

from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_URL

from .api import fetch_mac, get_ip
from .const import DOMAIN

API_DEFAULT = "http://ctek-ng-air.local/meter/"
TITLE = "Nanogrid Air"
USER_DESC = "description"

Expand All @@ -20,32 +19,29 @@ class NanogridAirConfigFlow(ConfigFlow, domain=DOMAIN):

VERSION = 1

def __init__(self) -> None:
"""Initialize flow."""
self._url = API_DEFAULT

async def async_step_user(self, user_input=None) -> ConfigFlowResult:
"""Handle a flow initiated by the user."""
self._async_abort_entries_match()

errors = {}

data_schema = vol.Schema(
{vol.Required(CONF_URL, default=self._url, description=USER_DESC): str}
{vol.Required(CONF_URL, default="", description=USER_DESC): str}
)

# Attempt to automatically detect the device
if user_input is None:
try:
if await get_ip():
mac_address = await fetch_mac()
if await NanogridAir().get_ip():
status = await NanogridAir().fetch_status()
mac_address = status.device_info.mac
if mac_address:
unique_id = mac_address
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=TITLE,
data={CONF_URL: self._url},
data={CONF_URL: ""},
)
errors["base"] = "invalid_auth"
else:
Expand All @@ -61,8 +57,9 @@ async def async_step_user(self, user_input=None) -> ConfigFlowResult:

# Handle user input
try:
if await get_ip(user_input[CONF_URL]):
mac_address = await fetch_mac()
if await NanogridAir(device_ip=user_input[CONF_URL]).get_ip():
status = await NanogridAir().fetch_status()
mac_address = status.device_info.mac
if mac_address:
unique_id = mac_address
await self.async_set_unique_id(unique_id)
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/nanogrid_air/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/nanogrid_air",
"integration_type": "device",
"iot_class": "local_polling"
"iot_class": "local_polling",
"requirements": ["ctek==0.2.0"]
}
20 changes: 10 additions & 10 deletions homeassistant/components/nanogrid_air/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from datetime import timedelta

from ctek import NanogridAir

from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
Expand All @@ -24,8 +26,6 @@
DataUpdateCoordinator,
)

from .api import fetch_meter_data

SENSOR = {
"current_0": SensorEntityDescription(
key="current_0",
Expand Down Expand Up @@ -106,7 +106,7 @@ async def async_setup_entry(
"""Set up sensor entities for the integration entry."""

async def update_data():
return await fetch_meter_data()
return await NanogridAir().fetch_meter_data()

coordinator = DataUpdateCoordinator(
hass,
Expand Down Expand Up @@ -149,23 +149,23 @@ def __init__(
def native_value(self) -> str | None:
"""Return the state of the sensor."""
data = self.coordinator.data
if data is None:
if data is None or isinstance(data, dict):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why filter out dicts?

return None

if self._sensor_id.startswith("current_"):
index = int(self._sensor_id.split("_")[-1])
return data.get("current", [None, None, None])[index]
return getattr(data, "current", [None])[index]
if self._sensor_id.startswith("voltage_"):
index = int(self._sensor_id.split("_")[-1])
return data.get("voltage", [None, None, None])[index]
return getattr(data, "voltage", [None])[index]
if self._sensor_id == "power_in":
return data.get("activePowerIn", None)
return getattr(data, "active_power_in", None)
if self._sensor_id == "power_out":
return data.get("activePowerOut", None)
return getattr(data, "active_power_out", None)
if self._sensor_id == "total_energy_import":
return data.get("totalEnergyActiveImport", None)
return getattr(data, "total_energy_active_import", None)
if self._sensor_id == "total_energy_export":
return data.get("totalEnergyActiveExport", None)
return getattr(data, "total_energy_active_export", None)
return None

@property
Expand Down
3 changes: 3 additions & 0 deletions requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,9 @@ crownstone-sse==2.0.4
# homeassistant.components.crownstone
crownstone-uart==2.1.0

# homeassistant.components.nanogrid_air
ctek==0.2.0

# homeassistant.components.datadog
datadog==0.15.0

Expand Down
3 changes: 3 additions & 0 deletions requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,9 @@ crownstone-sse==2.0.4
# homeassistant.components.crownstone
crownstone-uart==2.1.0

# homeassistant.components.nanogrid_air
ctek==0.2.0

# homeassistant.components.datadog
datadog==0.15.0

Expand Down
Loading