Skip to content

Commit

Permalink
fix: _LOGGER improvement and device_class fix (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
geertmeersman authored Aug 24, 2023
2 parents c6795e4 + 8a5b148 commit b3e6e2c
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 121 deletions.
21 changes: 7 additions & 14 deletions custom_components/nexxtmove/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,16 @@
from __future__ import annotations

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD
from homeassistant.const import CONF_USERNAME
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.helpers.update_coordinator import UpdateFailed
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from requests.exceptions import ConnectionError

from .client import NexxtmoveClient
from .const import _LOGGER
from .const import COORDINATOR_UPDATE_INTERVAL
from .const import DOMAIN
from .const import PLATFORMS
from .exceptions import NexxtmoveException
from .exceptions import NexxtmoveServiceException
from .const import _LOGGER, COORDINATOR_UPDATE_INTERVAL, DOMAIN, PLATFORMS
from .exceptions import NexxtmoveException, NexxtmoveServiceException
from .models import NexxtmoveItem
from .utils import log_debug


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
Expand Down Expand Up @@ -102,15 +95,15 @@ async def _async_update_data(self) -> dict | None:

if len(items) > 0:
fetched_items = {str(items[item].device_key) for item in items}
log_debug(
_LOGGER.debug(
f"[init|NexxtmoveDataUpdateCoordinator|_async_update_data|fetched_items] {fetched_items}"
)
if stale_items := current_items - fetched_items:
for device_key in stale_items:
if device := self._device_registry.async_get_device(
{(DOMAIN, device_key)}
):
log_debug(
_LOGGER.debug(
f"[init|NexxtmoveDataUpdateCoordinator|_async_update_data|async_remove_device] {device_key}",
True,
)
Expand All @@ -121,7 +114,7 @@ async def _async_update_data(self) -> dict | None:
if self.data and fetched_items - {
str(self.data[item].device_key) for item in self.data
}:
# log_debug(f"[init|NexxtmoveDataUpdateCoordinator|_async_update_data|async_reload] {product.product_name}")
# _LOGGER.debug(f"[init|NexxtmoveDataUpdateCoordinator|_async_update_data|async_reload] {product.product_name}")
self.hass.async_create_task(
self.hass.config_entries.async_reload(self._config_entry_id)
)
Expand Down
49 changes: 27 additions & 22 deletions custom_components/nexxtmove/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from requests import Session

from .const import (
_LOGGER,
BASE_HEADERS,
CONNECTION_RETRY,
DEFAULT_NEXXTMOVE_ENVIRONMENT,
Expand All @@ -15,7 +16,7 @@
)
from .exceptions import BadCredentialsException, NexxtmoveServiceException
from .models import NexxtmoveEnvironment, NexxtmoveItem
from .utils import format_entity_name, log_debug
from .utils import format_entity_name


class NexxtmoveClient:
Expand Down Expand Up @@ -54,25 +55,25 @@ def request(
) -> dict:
"""Send a request to Nexxtmove."""
if data is None:
log_debug(f"{caller} Calling GET {url}")
_LOGGER.debug(f"{caller} Calling GET {url}")
response = self.session.get(
url,
timeout=REQUEST_TIMEOUT,
headers=self._headers | {"Authorize": f"Token {self.token}"},
)
else:
log_debug(f"{caller} Calling POST {url} with {data}")
_LOGGER.debug(f"{caller} Calling POST {url} with {data}")
response = self.session.post(
url,
json=data,
timeout=REQUEST_TIMEOUT,
headers=self._headers | {"Authorize": f"Token {self.token}"},
)
log_debug(
_LOGGER.debug(
f"{caller} http status code = {response.status_code} (expecting {expected})"
)
if log:
log_debug(f"{caller} Response:\n{response.text}")
_LOGGER.debug(f"{caller} Response:\n{response.text}")
if expected is not None and response.status_code != expected:
if response.status_code == 404:
self.request_error = response.json()
Expand All @@ -87,7 +88,7 @@ def request(
raise NexxtmoveServiceException(
f"[{caller}] Expecting HTTP {expected} | Response HTTP {response.status_code}, Response: {response.text}, Url: {response.url}"
)
log_debug(
_LOGGER.debug(
f"[NexxtmoveClient|request] Received a HTTP {response.status_code}, nothing to worry about! We give it another try :-)"
)
self.login()
Expand All @@ -99,7 +100,7 @@ def request(
def login(self) -> dict:
"""Start a new Nexxtmove session with a user & password."""

log_debug("[NexxtmoveClient|login|start]")
_LOGGER.debug("[NexxtmoveClient|login|start]")
"""Login process"""
if self.username is None or self.password is None:
return False
Expand All @@ -112,7 +113,7 @@ def login(self) -> dict:
result = response.json()
try:
self.token = result.get("authToken")
log_debug(f"Setting Token {self.token}")
_LOGGER.debug(f"Setting Token {self.token}")
self.profile = result.get("profile")
except Exception as exception:
raise BadCredentialsException(
Expand Down Expand Up @@ -472,7 +473,7 @@ def fetch_data(self):

"""
tokens = self.charging_device_tokens(charging_device_id)
log_debug(f"Charging tokens: {tokens}", True)
_LOGGER.debug(f"Charging tokens: {tokens}", True)
#Switch will be used when it becomes useful
key = format_entity_name(
Expand Down Expand Up @@ -586,7 +587,7 @@ def price_to_ISO4217(self, unit):

def company(self):
"""Fetch Company info."""
log_debug("[NexxtmoveClient|company] Fetching company info from Nexxtmove")
_LOGGER.debug("[NexxtmoveClient|company] Fetching company info from Nexxtmove")
response = self.request(
f"{self.environment.api_endpoint}/company",
"[NexxtmoveClient|company]",
Expand All @@ -599,7 +600,9 @@ def company(self):

def device_list(self):
"""Fetch Device list."""
log_debug("[NexxtmoveClient|device_list] Fetching device list from Nexxtmove")
_LOGGER.debug(
"[NexxtmoveClient|device_list] Fetching device list from Nexxtmove"
)
response = self.request(
f"{self.environment.api_endpoint}/device/list",
"[NexxtmoveClient|device_list]",
Expand All @@ -612,7 +615,7 @@ def device_list(self):

def device_events(self, device_id):
"""Fetch Device events."""
log_debug(
_LOGGER.debug(
"[NexxtmoveClient|device_events] Fetching device events from Nexxtmove"
)
response = self.request(
Expand All @@ -627,7 +630,7 @@ def device_events(self, device_id):

def device_pin(self, device_id):
"""Fetch Device pin."""
log_debug("[NexxtmoveClient|device_pin] Fetching device pin from Nexxtmove")
_LOGGER.debug("[NexxtmoveClient|device_pin] Fetching device pin from Nexxtmove")
response = self.request(
f"{self.environment.api_endpoint}/device/{device_id}/pin",
"[NexxtmoveClient|device_pin]",
Expand All @@ -640,7 +643,7 @@ def device_pin(self, device_id):

def charging_device_tokens(self, device_id):
"""Fetch Device tokens."""
log_debug(
_LOGGER.debug(
"[NexxtmoveClient|charging_device_tokens] Fetching device tokens from Nexxtmove"
)
response = self.request(
Expand All @@ -655,7 +658,7 @@ def charging_device_tokens(self, device_id):

def charging_device_graph(self, device_id, start_date, end_date):
"""Fetch Charging graph data."""
log_debug(
_LOGGER.debug(
"[NexxtmoveClient|charging_device_graph] Fetching charging graph data from Nexxtmove"
)
response = self.request(
Expand All @@ -670,7 +673,7 @@ def charging_device_graph(self, device_id, start_date, end_date):

def charging_point(self, charging_point_id):
"""Fetch Charging point info."""
log_debug(
_LOGGER.debug(
"[NexxtmoveClient|charging_point] Fetching charging point info from Nexxtmove"
)
response = self.request(
Expand All @@ -685,7 +688,7 @@ def charging_point(self, charging_point_id):

def charging_point_events(self, device_id):
"""Fetch charging point events."""
log_debug(
_LOGGER.debug(
"[NexxtmoveClient|charging_point_events] Fetching charging point events from Nexxtmove"
)
response = self.request(
Expand All @@ -700,7 +703,7 @@ def charging_point_events(self, device_id):

def charge_latest(self):
"""Fetch charges."""
log_debug("[NexxtmoveClient|charge_latest] Fetching charges from Nexxtmove")
_LOGGER.debug("[NexxtmoveClient|charge_latest] Fetching charges from Nexxtmove")
response = self.request(
f"{self.environment.api_endpoint}/charge/latest?maxRows=200&offset=0",
"[NexxtmoveClient|charge_latest]",
Expand All @@ -713,7 +716,9 @@ def charge_latest(self):

def consumption(self):
"""Fetch consumption."""
log_debug("[NexxtmoveClient|consumption] Fetching consumption from Nexxtmove")
_LOGGER.debug(
"[NexxtmoveClient|consumption] Fetching consumption from Nexxtmove"
)
response = self.request(
f"{self.environment.api_endpoint}/charge/consumption",
"[NexxtmoveClient|consumption]",
Expand All @@ -726,7 +731,7 @@ def consumption(self):

def charges(self):
"""Fetch charges."""
log_debug("[NexxtmoveClient|charges] Fetching charges from Nexxtmove")
_LOGGER.debug("[NexxtmoveClient|charges] Fetching charges from Nexxtmove")
response = self.request(
f"{self.environment.api_endpoint}/charge/current?maxRows=100&offset=0",
"[NexxtmoveClient|charges]",
Expand All @@ -739,7 +744,7 @@ def charges(self):

def residential_buildings(self):
"""Fetch residential buildings."""
log_debug(
_LOGGER.debug(
"[NexxtmoveClient|residential_buildings] Fetching residential buildings from Nexxtmove"
)
response = self.request(
Expand All @@ -754,7 +759,7 @@ def residential_buildings(self):

def work_buildings(self):
"""Fetch work buildings."""
log_debug(
_LOGGER.debug(
"[NexxtmoveClient|work_buildings] Fetching work buildings from Nexxtmove"
)
response = self.request(
Expand Down
42 changes: 18 additions & 24 deletions custom_components/nexxtmove/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
"""Config flow to configure the Nexxtmove integration."""
from abc import ABC
from abc import abstractmethod
from abc import ABC, abstractmethod
from typing import Any

import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry
from homeassistant.config_entries import ConfigFlow
from homeassistant.config_entries import OptionsFlow
from homeassistant.const import CONF_PASSWORD
from homeassistant.const import CONF_USERNAME
from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowHandler
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.selector import TextSelector
from homeassistant.helpers.selector import TextSelectorConfig
from homeassistant.helpers.selector import TextSelectorType
from homeassistant.data_entry_flow import FlowHandler, FlowResult
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.selector import (
TextSelector,
TextSelectorConfig,
TextSelectorType,
)
from homeassistant.helpers.typing import UNDEFINED
import voluptuous as vol

from .client import NexxtmoveClient
from .const import DOMAIN
from .const import NAME
from .exceptions import BadCredentialsException
from .exceptions import NexxtmoveServiceException
from .const import _LOGGER, DOMAIN, NAME
from .exceptions import BadCredentialsException, NexxtmoveServiceException
from .models import NexxtmoveConfigEntryData
from .utils import log_debug

DEFAULT_ENTRY_DATA = NexxtmoveConfigEntryData(
username=None,
Expand Down Expand Up @@ -70,15 +64,15 @@ async def async_step_connection_init(
if user_input is not None:
user_input = self.new_data() | user_input
test = await self.test_connection(user_input)
log_debug(f"Test result config flow: {test}")
_LOGGER.debug(f"Test result config flow: {test}")
if not test["errors"]:
self.new_title = test["profile"].get("username")
self.new_entry_data |= user_input
await self.async_set_unique_id(
f"{DOMAIN}_" + test["profile"].get("username")
)
self._abort_if_unique_id_configured()
log_debug(f"New account {self.new_title} added")
_LOGGER.debug(f"New account {self.new_title} added")
return self.finish_flow()
errors = test["errors"]
fields = {
Expand Down Expand Up @@ -108,7 +102,7 @@ async def test_connection(self, user_input: dict | None = None) -> dict:
profile = await self.async_validate_input(user_input)
except AssertionError as exception:
errors["base"] = "cannot_connect"
log_debug(f"[async_step_password|login] AssertionError {exception}")
_LOGGER.debug(f"[async_step_password|login] AssertionError {exception}")
except ConnectionError:
errors["base"] = "cannot_connect"
except NexxtmoveServiceException:
Expand All @@ -117,7 +111,7 @@ async def test_connection(self, user_input: dict | None = None) -> dict:
errors["base"] = "invalid_auth"
except Exception as exception:
errors["base"] = "unknown"
log_debug(exception)
_LOGGER.debug(exception)
return {"profile": profile, "errors": errors}

async def async_step_password(self, user_input: dict | None = None) -> FlowResult:
Expand All @@ -131,7 +125,7 @@ async def async_step_password(self, user_input: dict | None = None) -> FlowResul
self.new_entry_data |= NexxtmoveConfigEntryData(
password=user_input[CONF_PASSWORD],
)
log_debug(f"Password changed for {user_input[CONF_USERNAME]}")
_LOGGER.debug(f"Password changed for {user_input[CONF_USERNAME]}")
return self.finish_flow()

fields = {
Expand Down
17 changes: 7 additions & 10 deletions custom_components/nexxtmove/const.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
"""Constants used by Nexxtmove."""
from datetime import timedelta
import json
import logging
from datetime import timedelta
from pathlib import Path
from typing import Final

from homeassistant.const import Platform

from .models import NexxtmoveEnvironment

SHOW_DEBUG_AS_WARNING = False

_LOGGER = logging.getLogger(__name__)

PLATFORMS: Final = [Platform.SENSOR, Platform.SWITCH]
Expand All @@ -30,6 +28,7 @@
}

GRAPH_START_DATE = "20220101"
_LOGGER = logging.getLogger(__name__)

COORDINATOR_UPDATE_INTERVAL = timedelta(minutes=5)
CONNECTION_RETRY = 5
Expand All @@ -46,14 +45,12 @@
NAME = manifest_data.get("name")
VERSION = manifest_data.get("version")
ISSUEURL = manifest_data.get("issue_tracker")
STARTUP = """
STARTUP = f"""
-------------------------------------------------------------------
{name}
Version: {version}
{NAME}
Version: {VERSION}
This is a custom component
If you have any issues with this you need to open an issue here:
{issueurl}
{ISSUEURL}
-------------------------------------------------------------------
""".format(
name=NAME, version=VERSION, issueurl=ISSUEURL
)
"""
Loading

0 comments on commit b3e6e2c

Please sign in to comment.