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

Add vesync debug mode in library #134571

Merged
merged 12 commits into from
Feb 24, 2025
31 changes: 27 additions & 4 deletions homeassistant/components/vesync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
from pyvesync import VeSync

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.const import (
CONF_PASSWORD,
CONF_USERNAME,
EVENT_LOGGING_CHANGED,
Platform,
)
from homeassistant.core import Event, HomeAssistant, ServiceCall, callback
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.dispatcher import async_dispatcher_send

Expand All @@ -17,6 +22,7 @@
VS_COORDINATOR,
VS_DEVICES,
VS_DISCOVERY,
VS_LISTENERS,
VS_MANAGER,
)
from .coordinator import VeSyncDataCoordinator
Expand All @@ -42,7 +48,13 @@

time_zone = str(hass.config.time_zone)

manager = VeSync(username, password, time_zone)
manager = VeSync(
username=username,
password=password,
time_zone=time_zone,
debug=logging.getLogger("pyvesync.vesync").level == logging.DEBUG,
redact=True,
)

login = await hass.async_add_executor_job(manager.login)

Expand All @@ -62,6 +74,17 @@

await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)

@callback
def _async_handle_logging_changed(_event: Event) -> None:
"""Handle when the logging level changes."""
manager.debug = logging.getLogger("pyvesync.vesync").level == logging.DEBUG

Check warning on line 80 in homeassistant/components/vesync/__init__.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/vesync/__init__.py#L80

Added line #L80 was not covered by tests
Copy link
Member

@MartinHjelmare MartinHjelmare Feb 22, 2025

Choose a reason for hiding this comment

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

Why doesn't the library use the standard Python logging mechanism and best practice with just using a logger per module or package? Why does it need this attribute on the manager?

Eg:

LOGGER = logging.getLogger(__name__)

LOGGER.debug("bla bla")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just how it was written - as webdjoe mentioned a re-write of the library is coming that will solve this. If we merge this current code it gives us debug ability until that happens.

It would get removed on the re-write update.


cleanup = hass.bus.async_listen(
EVENT_LOGGING_CHANGED, _async_handle_logging_changed
)

hass.data[DOMAIN][VS_LISTENERS] = cleanup

async def async_new_device_discovery(service: ServiceCall) -> None:
"""Discover if new devices should be added."""
manager = hass.data[DOMAIN][VS_MANAGER]
Expand All @@ -87,7 +110,7 @@

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""

hass.data[DOMAIN][VS_LISTENERS]()
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data.pop(DOMAIN)
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/vesync/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
VS_DEVICES = "devices"
VS_COORDINATOR = "coordinator"
VS_MANAGER = "manager"
VS_LISTENERS = "listeners"
VS_NUMBERS = "numbers"

VS_HUMIDIFIER_MODE_AUTO = "auto"
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/vesync/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/vesync",
"iot_class": "cloud_polling",
"loggers": ["pyvesync"],
"loggers": ["pyvesync.vesync"],
"requirements": ["pyvesync==2.1.18"]
}