From 0e674fc59746fb648cc7c3b736e87c2abeb69acd Mon Sep 17 00:00:00 2001 From: Martin Hjelmare Date: Wed, 22 Jun 2022 21:35:26 +0200 Subject: [PATCH] Clean up zwave_js logging and hass.data (#73856) --- homeassistant/components/zwave_js/__init__.py | 26 +++++-------------- .../components/zwave_js/binary_sensor.py | 3 --- homeassistant/components/zwave_js/cover.py | 3 --- homeassistant/components/zwave_js/entity.py | 6 +---- homeassistant/components/zwave_js/light.py | 3 --- homeassistant/components/zwave_js/lock.py | 4 +-- homeassistant/components/zwave_js/sensor.py | 4 +-- homeassistant/components/zwave_js/switch.py | 3 --- 8 files changed, 10 insertions(+), 42 deletions(-) diff --git a/homeassistant/components/zwave_js/__init__.py b/homeassistant/components/zwave_js/__init__.py index 4f5756361c8..fe616e8bdb9 100644 --- a/homeassistant/components/zwave_js/__init__.py +++ b/homeassistant/components/zwave_js/__init__.py @@ -104,8 +104,6 @@ from .services import ZWaveServices CONNECT_TIMEOUT = 10 DATA_CLIENT_LISTEN_TASK = "client_listen_task" DATA_START_PLATFORM_TASK = "start_platform_task" -DATA_CONNECT_FAILED_LOGGED = "connect_failed_logged" -DATA_INVALID_SERVER_VERSION_LOGGED = "invalid_server_version_logged" async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: @@ -170,28 +168,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: await async_ensure_addon_running(hass, entry) client = ZwaveClient(entry.data[CONF_URL], async_get_clientsession(hass)) - entry_hass_data: dict = hass.data[DOMAIN].setdefault(entry.entry_id, {}) # connect and throw error if connection failed try: async with timeout(CONNECT_TIMEOUT): await client.connect() except InvalidServerVersion as err: - if not entry_hass_data.get(DATA_INVALID_SERVER_VERSION_LOGGED): - LOGGER.error("Invalid server version: %s", err) - entry_hass_data[DATA_INVALID_SERVER_VERSION_LOGGED] = True if use_addon: async_ensure_addon_updated(hass) - raise ConfigEntryNotReady from err + raise ConfigEntryNotReady(f"Invalid server version: {err}") from err except (asyncio.TimeoutError, BaseZwaveJSServerError) as err: - if not entry_hass_data.get(DATA_CONNECT_FAILED_LOGGED): - LOGGER.error("Failed to connect: %s", err) - entry_hass_data[DATA_CONNECT_FAILED_LOGGED] = True - raise ConfigEntryNotReady from err + raise ConfigEntryNotReady(f"Failed to connect: {err}") from err else: LOGGER.info("Connected to Zwave JS Server") - entry_hass_data[DATA_CONNECT_FAILED_LOGGED] = False - entry_hass_data[DATA_INVALID_SERVER_VERSION_LOGGED] = False dev_reg = device_registry.async_get(hass) ent_reg = entity_registry.async_get(hass) @@ -202,7 +191,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async_register_api(hass) platform_task = hass.async_create_task(start_platforms(hass, entry, client)) - entry_hass_data[DATA_START_PLATFORM_TASK] = platform_task + hass.data[DOMAIN].setdefault(entry.entry_id, {})[ + DATA_START_PLATFORM_TASK + ] = platform_task return True @@ -635,9 +626,7 @@ async def disconnect_client(hass: HomeAssistant, entry: ConfigEntry) -> None: platform_task: asyncio.Task = data[DATA_START_PLATFORM_TASK] listen_task.cancel() platform_task.cancel() - platform_setup_tasks = ( - hass.data[DOMAIN].get(entry.entry_id, {}).get(DATA_PLATFORM_SETUP, {}).values() - ) + platform_setup_tasks = data.get(DATA_PLATFORM_SETUP, {}).values() for task in platform_setup_tasks: task.cancel() @@ -711,8 +700,7 @@ async def async_ensure_addon_running(hass: HomeAssistant, entry: ConfigEntry) -> try: addon_info = await addon_manager.async_get_addon_info() except AddonError as err: - LOGGER.error(err) - raise ConfigEntryNotReady from err + raise ConfigEntryNotReady(err) from err usb_path: str = entry.data[CONF_USB_PATH] # s0_legacy_key was saved as network_key before s2 was added. diff --git a/homeassistant/components/zwave_js/binary_sensor.py b/homeassistant/components/zwave_js/binary_sensor.py index fb085cffe62..f6480689910 100644 --- a/homeassistant/components/zwave_js/binary_sensor.py +++ b/homeassistant/components/zwave_js/binary_sensor.py @@ -2,7 +2,6 @@ from __future__ import annotations from dataclasses import dataclass -import logging from zwave_js_server.client import Client as ZwaveClient from zwave_js_server.const import CommandClass @@ -30,8 +29,6 @@ from .entity import ZWaveBaseEntity PARALLEL_UPDATES = 0 -LOGGER = logging.getLogger(__name__) - NOTIFICATION_SMOKE_ALARM = "1" NOTIFICATION_CARBON_MONOOXIDE = "2" diff --git a/homeassistant/components/zwave_js/cover.py b/homeassistant/components/zwave_js/cover.py index ee83db4578c..30364d127eb 100644 --- a/homeassistant/components/zwave_js/cover.py +++ b/homeassistant/components/zwave_js/cover.py @@ -1,7 +1,6 @@ """Support for Z-Wave cover devices.""" from __future__ import annotations -import logging from typing import Any, cast from zwave_js_server.client import Client as ZwaveClient @@ -39,8 +38,6 @@ from .entity import ZWaveBaseEntity PARALLEL_UPDATES = 0 -LOGGER = logging.getLogger(__name__) - async def async_setup_entry( hass: HomeAssistant, diff --git a/homeassistant/components/zwave_js/entity.py b/homeassistant/components/zwave_js/entity.py index a4271ac1c02..79dd1d27a4c 100644 --- a/homeassistant/components/zwave_js/entity.py +++ b/homeassistant/components/zwave_js/entity.py @@ -1,8 +1,6 @@ """Generic Z-Wave Entity Class.""" from __future__ import annotations -import logging - from zwave_js_server.const import NodeStatus from zwave_js_server.model.driver import Driver from zwave_js_server.model.value import Value as ZwaveValue, get_value_id @@ -12,12 +10,10 @@ from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity import DeviceInfo, Entity -from .const import DOMAIN +from .const import DOMAIN, LOGGER from .discovery import ZwaveDiscoveryInfo from .helpers import get_device_id, get_unique_id -LOGGER = logging.getLogger(__name__) - EVENT_VALUE_UPDATED = "value updated" EVENT_VALUE_REMOVED = "value removed" EVENT_DEAD = "dead" diff --git a/homeassistant/components/zwave_js/light.py b/homeassistant/components/zwave_js/light.py index 17293e85a21..4c8fe2a3986 100644 --- a/homeassistant/components/zwave_js/light.py +++ b/homeassistant/components/zwave_js/light.py @@ -1,7 +1,6 @@ """Support for Z-Wave lights.""" from __future__ import annotations -import logging from typing import Any, cast from zwave_js_server.client import Client as ZwaveClient @@ -49,8 +48,6 @@ from .entity import ZWaveBaseEntity PARALLEL_UPDATES = 0 -LOGGER = logging.getLogger(__name__) - MULTI_COLOR_MAP = { ColorComponent.WARM_WHITE: COLOR_SWITCH_COMBINED_WARM_WHITE, ColorComponent.COLD_WHITE: COLOR_SWITCH_COMBINED_COLD_WHITE, diff --git a/homeassistant/components/zwave_js/lock.py b/homeassistant/components/zwave_js/lock.py index ffe99373991..efeadb9b6b3 100644 --- a/homeassistant/components/zwave_js/lock.py +++ b/homeassistant/components/zwave_js/lock.py @@ -1,7 +1,6 @@ """Representation of Z-Wave locks.""" from __future__ import annotations -import logging from typing import Any import voluptuous as vol @@ -27,6 +26,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from .const import ( DATA_CLIENT, DOMAIN, + LOGGER, SERVICE_CLEAR_LOCK_USERCODE, SERVICE_SET_LOCK_USERCODE, ) @@ -35,8 +35,6 @@ from .entity import ZWaveBaseEntity PARALLEL_UPDATES = 0 -LOGGER = logging.getLogger(__name__) - STATE_TO_ZWAVE_MAP: dict[int, dict[str, int | bool]] = { CommandClass.DOOR_LOCK: { STATE_UNLOCKED: DoorLockMode.UNSECURED, diff --git a/homeassistant/components/zwave_js/sensor.py b/homeassistant/components/zwave_js/sensor.py index 2b2e2a0de2b..22fbfdab728 100644 --- a/homeassistant/components/zwave_js/sensor.py +++ b/homeassistant/components/zwave_js/sensor.py @@ -2,7 +2,6 @@ from __future__ import annotations from collections.abc import Mapping -import logging from typing import cast import voluptuous as vol @@ -55,6 +54,7 @@ from .const import ( ENTITY_DESC_KEY_TEMPERATURE, ENTITY_DESC_KEY_TOTAL_INCREASING, ENTITY_DESC_KEY_VOLTAGE, + LOGGER, SERVICE_RESET_METER, ) from .discovery import ZwaveDiscoveryInfo @@ -67,8 +67,6 @@ from .helpers import get_device_id, get_valueless_base_unique_id PARALLEL_UPDATES = 0 -LOGGER = logging.getLogger(__name__) - STATUS_ICON: dict[NodeStatus, str] = { NodeStatus.ALIVE: "mdi:heart-pulse", NodeStatus.ASLEEP: "mdi:sleep", diff --git a/homeassistant/components/zwave_js/switch.py b/homeassistant/components/zwave_js/switch.py index 52b8f813326..154106d56f5 100644 --- a/homeassistant/components/zwave_js/switch.py +++ b/homeassistant/components/zwave_js/switch.py @@ -1,7 +1,6 @@ """Representation of Z-Wave switches.""" from __future__ import annotations -import logging from typing import Any from zwave_js_server.client import Client as ZwaveClient @@ -23,8 +22,6 @@ from .entity import ZWaveBaseEntity PARALLEL_UPDATES = 0 -LOGGER = logging.getLogger(__name__) - async def async_setup_entry( hass: HomeAssistant,