diff --git a/homeassistant/components/ads/sensor.py b/homeassistant/components/ads/sensor.py index 172f8ee70df..76d73f75a8b 100644 --- a/homeassistant/components/ads/sensor.py +++ b/homeassistant/components/ads/sensor.py @@ -3,7 +3,6 @@ from __future__ import annotations import voluptuous as vol -from homeassistant.components import ads from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity from homeassistant.const import CONF_NAME, CONF_UNIT_OF_MEASUREMENT from homeassistant.core import HomeAssistant @@ -19,6 +18,7 @@ from . import ( STATE_KEY_STATE, AdsEntity, ) +from .. import ads DEFAULT_NAME = "ADS sensor" PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( diff --git a/homeassistant/components/ios/notify.py b/homeassistant/components/ios/notify.py index 2e27271841a..12fe5ba5f5e 100644 --- a/homeassistant/components/ios/notify.py +++ b/homeassistant/components/ios/notify.py @@ -4,7 +4,6 @@ import logging import requests -from homeassistant.components import ios from homeassistant.components.notify import ( ATTR_DATA, ATTR_MESSAGE, @@ -15,6 +14,8 @@ from homeassistant.components.notify import ( ) import homeassistant.util.dt as dt_util +from .. import ios + _LOGGER = logging.getLogger(__name__) PUSH_URL = "https://ios-push.home-assistant.io/push" diff --git a/homeassistant/components/ios/sensor.py b/homeassistant/components/ios/sensor.py index a97510d364f..397d829f36d 100644 --- a/homeassistant/components/ios/sensor.py +++ b/homeassistant/components/ios/sensor.py @@ -1,7 +1,6 @@ """Support for Home Assistant iOS app sensors.""" from __future__ import annotations -from homeassistant.components import ios from homeassistant.components.sensor import SensorEntity, SensorEntityDescription from homeassistant.config_entries import ConfigEntry from homeassistant.const import PERCENTAGE @@ -12,6 +11,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.icon import icon_for_battery_level from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType +from .. import ios from .const import DOMAIN SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( diff --git a/homeassistant/components/mysensors/binary_sensor.py b/homeassistant/components/mysensors/binary_sensor.py index fc924aa4873..07d03c3debd 100644 --- a/homeassistant/components/mysensors/binary_sensor.py +++ b/homeassistant/components/mysensors/binary_sensor.py @@ -1,7 +1,6 @@ """Support for MySensors binary sensors.""" from __future__ import annotations -from homeassistant.components import mysensors from homeassistant.components.binary_sensor import ( DEVICE_CLASSES, BinarySensorDeviceClass, @@ -13,6 +12,7 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback +from .. import mysensors from .const import MYSENSORS_DISCOVERY, DiscoveryInfo from .helpers import on_unload diff --git a/homeassistant/components/mysensors/climate.py b/homeassistant/components/mysensors/climate.py index 6a6640f7bdd..3e540bd5714 100644 --- a/homeassistant/components/mysensors/climate.py +++ b/homeassistant/components/mysensors/climate.py @@ -3,7 +3,6 @@ from __future__ import annotations from typing import Any -from homeassistant.components import mysensors from homeassistant.components.climate import ClimateEntity from homeassistant.components.climate.const import ( ATTR_TARGET_TEMP_HIGH, @@ -22,6 +21,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback +from .. import mysensors from .const import MYSENSORS_DISCOVERY, DiscoveryInfo from .helpers import on_unload diff --git a/homeassistant/components/mysensors/cover.py b/homeassistant/components/mysensors/cover.py index f1d7a4cdbf4..a1b2cb303ed 100644 --- a/homeassistant/components/mysensors/cover.py +++ b/homeassistant/components/mysensors/cover.py @@ -4,7 +4,6 @@ from __future__ import annotations from enum import Enum, unique from typing import Any -from homeassistant.components import mysensors from homeassistant.components.cover import ATTR_POSITION, CoverEntity from homeassistant.config_entries import ConfigEntry from homeassistant.const import STATE_OFF, STATE_ON, Platform @@ -12,6 +11,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback +from .. import mysensors from .const import MYSENSORS_DISCOVERY, DiscoveryInfo from .helpers import on_unload diff --git a/homeassistant/components/mysensors/device_tracker.py b/homeassistant/components/mysensors/device_tracker.py index 3c204776b7d..a3fd86d1b44 100644 --- a/homeassistant/components/mysensors/device_tracker.py +++ b/homeassistant/components/mysensors/device_tracker.py @@ -3,7 +3,6 @@ from __future__ import annotations from typing import Any, cast -from homeassistant.components import mysensors from homeassistant.components.device_tracker import AsyncSeeCallback from homeassistant.const import Platform from homeassistant.core import HomeAssistant @@ -11,6 +10,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.util import slugify +from .. import mysensors from .const import ATTR_GATEWAY_ID, DevId, DiscoveryInfo, GatewayId from .helpers import on_unload diff --git a/homeassistant/components/mysensors/light.py b/homeassistant/components/mysensors/light.py index e2c676220fa..e7ccdf8c569 100644 --- a/homeassistant/components/mysensors/light.py +++ b/homeassistant/components/mysensors/light.py @@ -3,7 +3,6 @@ from __future__ import annotations from typing import Any, cast -from homeassistant.components import mysensors from homeassistant.components.light import ( ATTR_BRIGHTNESS, ATTR_RGB_COLOR, @@ -18,6 +17,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.util.color import rgb_hex_to_rgb_list +from .. import mysensors from .const import MYSENSORS_DISCOVERY, DiscoveryInfo, SensorType from .device import MySensorsDevice from .helpers import on_unload diff --git a/homeassistant/components/mysensors/notify.py b/homeassistant/components/mysensors/notify.py index 042abe1515a..43f4779604d 100644 --- a/homeassistant/components/mysensors/notify.py +++ b/homeassistant/components/mysensors/notify.py @@ -3,11 +3,11 @@ from __future__ import annotations from typing import Any -from homeassistant.components import mysensors from homeassistant.components.notify import ATTR_TARGET, BaseNotificationService from homeassistant.const import Platform from homeassistant.core import HomeAssistant +from .. import mysensors from .const import DevId, DiscoveryInfo diff --git a/homeassistant/components/mysensors/sensor.py b/homeassistant/components/mysensors/sensor.py index ecc42c5a0bf..6f940c5d625 100644 --- a/homeassistant/components/mysensors/sensor.py +++ b/homeassistant/components/mysensors/sensor.py @@ -5,7 +5,6 @@ from typing import Any from awesomeversion import AwesomeVersion -from homeassistant.components import mysensors from homeassistant.components.sensor import ( SensorDeviceClass, SensorEntity, @@ -37,6 +36,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback +from .. import mysensors from .const import MYSENSORS_DISCOVERY, DiscoveryInfo from .helpers import on_unload diff --git a/homeassistant/components/mysensors/switch.py b/homeassistant/components/mysensors/switch.py index d8492e5e4a6..dd79c6819ec 100644 --- a/homeassistant/components/mysensors/switch.py +++ b/homeassistant/components/mysensors/switch.py @@ -5,7 +5,6 @@ from typing import Any import voluptuous as vol -from homeassistant.components import mysensors from homeassistant.components.switch import SwitchEntity from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, Platform @@ -14,6 +13,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback +from .. import mysensors from .const import ( DOMAIN as MYSENSORS_DOMAIN, MYSENSORS_DISCOVERY, diff --git a/homeassistant/components/pilight/binary_sensor.py b/homeassistant/components/pilight/binary_sensor.py index 0ee6ff1b38e..303c755a035 100644 --- a/homeassistant/components/pilight/binary_sensor.py +++ b/homeassistant/components/pilight/binary_sensor.py @@ -5,7 +5,6 @@ import datetime import voluptuous as vol -from homeassistant.components import pilight from homeassistant.components.binary_sensor import PLATFORM_SCHEMA, BinarySensorEntity from homeassistant.const import ( CONF_DISARM_AFTER_TRIGGER, @@ -21,6 +20,8 @@ from homeassistant.helpers.event import track_point_in_time from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.util import dt as dt_util +from .. import pilight + CONF_VARIABLE = "variable" CONF_RESET_DELAY_SEC = "reset_delay_sec" diff --git a/homeassistant/components/pilight/sensor.py b/homeassistant/components/pilight/sensor.py index 0f707489e9a..21036a94210 100644 --- a/homeassistant/components/pilight/sensor.py +++ b/homeassistant/components/pilight/sensor.py @@ -5,7 +5,6 @@ import logging import voluptuous as vol -from homeassistant.components import pilight from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity from homeassistant.const import CONF_NAME, CONF_PAYLOAD, CONF_UNIT_OF_MEASUREMENT from homeassistant.core import HomeAssistant @@ -13,6 +12,8 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType +from .. import pilight + _LOGGER = logging.getLogger(__name__) CONF_VARIABLE = "variable" diff --git a/homeassistant/components/recorder/history.py b/homeassistant/components/recorder/history.py index e1eca282a3a..7e875a5ff93 100644 --- a/homeassistant/components/recorder/history.py +++ b/homeassistant/components/recorder/history.py @@ -17,7 +17,6 @@ from sqlalchemy.sql.expression import literal from sqlalchemy.sql.lambdas import StatementLambdaElement from sqlalchemy.sql.selectable import Subquery -from homeassistant.components import recorder from homeassistant.components.websocket_api.const import ( COMPRESSED_STATE_LAST_UPDATED, COMPRESSED_STATE_STATE, @@ -25,6 +24,7 @@ from homeassistant.components.websocket_api.const import ( from homeassistant.core import HomeAssistant, State, split_entity_id import homeassistant.util.dt as dt_util +from .. import recorder from .db_schema import RecorderRuns, StateAttributes, States from .filters import Filters from .models import ( diff --git a/homeassistant/components/tellduslive/binary_sensor.py b/homeassistant/components/tellduslive/binary_sensor.py index e8e24f05c89..1e7a30d6174 100644 --- a/homeassistant/components/tellduslive/binary_sensor.py +++ b/homeassistant/components/tellduslive/binary_sensor.py @@ -1,11 +1,12 @@ """Support for binary sensors using Tellstick Net.""" -from homeassistant.components import binary_sensor, tellduslive +from homeassistant.components import binary_sensor from homeassistant.components.binary_sensor import BinarySensorEntity from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback +from .. import tellduslive from .entry import TelldusLiveEntity diff --git a/homeassistant/components/tellduslive/cover.py b/homeassistant/components/tellduslive/cover.py index 829478fc990..57da852a356 100644 --- a/homeassistant/components/tellduslive/cover.py +++ b/homeassistant/components/tellduslive/cover.py @@ -1,7 +1,7 @@ """Support for Tellstick covers using Tellstick Net.""" from typing import Any -from homeassistant.components import cover, tellduslive +from homeassistant.components import cover from homeassistant.components.cover import CoverEntity from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant @@ -9,6 +9,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import TelldusLiveClient +from .. import tellduslive from .entry import TelldusLiveEntity diff --git a/homeassistant/components/tellduslive/light.py b/homeassistant/components/tellduslive/light.py index 205fff840d6..3b69b58966c 100644 --- a/homeassistant/components/tellduslive/light.py +++ b/homeassistant/components/tellduslive/light.py @@ -2,13 +2,14 @@ import logging from typing import Any -from homeassistant.components import light, tellduslive +from homeassistant.components import light from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback +from .. import tellduslive from .entry import TelldusLiveEntity _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/tellduslive/sensor.py b/homeassistant/components/tellduslive/sensor.py index 00949fc41b8..8d02763d428 100644 --- a/homeassistant/components/tellduslive/sensor.py +++ b/homeassistant/components/tellduslive/sensor.py @@ -1,7 +1,7 @@ """Support for Tellstick Net/Telstick Live sensors.""" from __future__ import annotations -from homeassistant.components import sensor, tellduslive +from homeassistant.components import sensor from homeassistant.components.sensor import ( SensorDeviceClass, SensorEntity, @@ -23,6 +23,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback +from .. import tellduslive from .entry import TelldusLiveEntity SENSOR_TYPE_TEMPERATURE = "temp" diff --git a/homeassistant/components/tellduslive/switch.py b/homeassistant/components/tellduslive/switch.py index 78b31e8fea7..a1bb2ffc9e1 100644 --- a/homeassistant/components/tellduslive/switch.py +++ b/homeassistant/components/tellduslive/switch.py @@ -1,11 +1,12 @@ """Support for Tellstick switches using Tellstick Net.""" -from homeassistant.components import switch, tellduslive +from homeassistant.components import switch from homeassistant.components.switch import SwitchEntity from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback +from .. import tellduslive from .entry import TelldusLiveEntity diff --git a/homeassistant/components/trace/websocket_api.py b/homeassistant/components/trace/websocket_api.py index d45265c2989..fb9357cc067 100644 --- a/homeassistant/components/trace/websocket_api.py +++ b/homeassistant/components/trace/websocket_api.py @@ -3,7 +3,7 @@ import json import voluptuous as vol -from homeassistant.components import trace, websocket_api +from homeassistant.components import websocket_api from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.dispatcher import ( @@ -24,6 +24,8 @@ from homeassistant.helpers.script import ( debug_stop, ) +from .. import trace + # mypy: allow-untyped-calls, allow-untyped-defs TRACE_DOMAINS = ("automation", "script") diff --git a/homeassistant/components/zabbix/sensor.py b/homeassistant/components/zabbix/sensor.py index 6d1b0b186d1..3e72e4d71f1 100644 --- a/homeassistant/components/zabbix/sensor.py +++ b/homeassistant/components/zabbix/sensor.py @@ -5,7 +5,6 @@ import logging import voluptuous as vol -from homeassistant.components import zabbix from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity from homeassistant.const import CONF_NAME from homeassistant.core import HomeAssistant @@ -13,6 +12,8 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType +from .. import zabbix + _LOGGER = logging.getLogger(__name__) _CONF_TRIGGERS = "triggers" diff --git a/pylint/plugins/hass_imports.py b/pylint/plugins/hass_imports.py index 62fd262eacc..6068e1c2baf 100644 --- a/pylint/plugins/hass_imports.py +++ b/pylint/plugins/hass_imports.py @@ -329,7 +329,13 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc] f"{self.current_package}." ): self.add_message("hass-relative-import", node=node) - elif obsolete_imports := _OBSOLETE_IMPORT.get(node.modname): + return + if self.current_package.startswith("homeassistant.components") and node.modname == "homeassistant.components": + for name in node.names: + if name[0] == self.current_package.split(".")[2]: + self.add_message("hass-relative-import", node=node) + return + if obsolete_imports := _OBSOLETE_IMPORT.get(node.modname): for name_tuple in node.names: for obsolete_import in obsolete_imports: if import_match := obsolete_import.constant.match(name_tuple[0]): diff --git a/tests/pylint/test_imports.py b/tests/pylint/test_imports.py index 6367427eea7..d1ba7fe4a7f 100644 --- a/tests/pylint/test_imports.py +++ b/tests/pylint/test_imports.py @@ -84,6 +84,12 @@ def test_good_import( "CONSTANT", "hass-absolute-import", ), + ( + "homeassistant.components.pylint_test.api.hub", + "homeassistant.components", + "pylint_test", + "hass-relative-import", + ), ], ) def test_bad_import( @@ -111,7 +117,7 @@ def test_bad_import( line=1, col_offset=0, end_line=1, - end_col_offset=len(import_from) + 21, + end_col_offset=len(import_from) + len(import_what) + 13, ), ): imports_checker.visit_importfrom(import_node)