mirror of
https://github.com/home-assistant/core
synced 2024-09-03 08:14:07 +02:00
Make imports relative in ZHA component (#20020)
* make imports relative * remove cyclic import
This commit is contained in:
parent
5e73846bcc
commit
8748ace244
@ -12,8 +12,6 @@ import types
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries, const as ha_const
|
||||
|
||||
from homeassistant.components.zha.entities import ZhaDeviceEntity
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.device_registry import CONNECTION_ZIGBEE
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
@ -25,13 +23,17 @@ from . import const as zha_const
|
||||
from .event import ZhaEvent, ZhaRelayEvent
|
||||
from . import api
|
||||
from .helpers import convert_ieee
|
||||
from .entities import ZhaDeviceEntity
|
||||
from .const import (
|
||||
COMPONENTS, CONF_BAUDRATE, CONF_DATABASE, CONF_DEVICE_CONFIG,
|
||||
CONF_RADIO_TYPE, CONF_USB_PATH, DATA_ZHA, DATA_ZHA_BRIDGE_ID,
|
||||
DATA_ZHA_CONFIG, DATA_ZHA_CORE_COMPONENT, DATA_ZHA_DISPATCHERS,
|
||||
DATA_ZHA_RADIO, DEFAULT_BAUDRATE, DEFAULT_DATABASE_NAME,
|
||||
DEFAULT_RADIO_TYPE, DOMAIN, ZHA_DISCOVERY_NEW, RadioType,
|
||||
EVENTABLE_CLUSTERS, DATA_ZHA_CORE_EVENTS, ENABLE_QUIRKS)
|
||||
EVENTABLE_CLUSTERS, DATA_ZHA_CORE_EVENTS, ENABLE_QUIRKS,
|
||||
DEVICE_CLASS, SINGLE_INPUT_CLUSTER_DEVICE_CLASS,
|
||||
SINGLE_OUTPUT_CLUSTER_DEVICE_CLASS, CUSTOM_CLUSTER_MAPPINGS,
|
||||
COMPONENT_CLUSTERS)
|
||||
|
||||
REQUIREMENTS = [
|
||||
'bellows==0.7.0',
|
||||
@ -93,6 +95,7 @@ async def async_setup_entry(hass, config_entry):
|
||||
|
||||
Will automatically load components to support devices found on the network.
|
||||
"""
|
||||
establish_device_mappings()
|
||||
hass.data[DATA_ZHA] = hass.data.get(DATA_ZHA, {})
|
||||
hass.data[DATA_ZHA][DATA_ZHA_DISPATCHERS] = []
|
||||
|
||||
@ -204,6 +207,88 @@ async def async_unload_entry(hass, config_entry):
|
||||
return True
|
||||
|
||||
|
||||
def establish_device_mappings():
|
||||
"""Establish mappings between ZCL objects and HA ZHA objects.
|
||||
|
||||
These cannot be module level, as importing bellows must be done in a
|
||||
in a function.
|
||||
"""
|
||||
from zigpy import zcl, quirks
|
||||
from zigpy.profiles import PROFILES, zha, zll
|
||||
from .sensor import RelativeHumiditySensor
|
||||
|
||||
if zha.PROFILE_ID not in DEVICE_CLASS:
|
||||
DEVICE_CLASS[zha.PROFILE_ID] = {}
|
||||
if zll.PROFILE_ID not in DEVICE_CLASS:
|
||||
DEVICE_CLASS[zll.PROFILE_ID] = {}
|
||||
|
||||
EVENTABLE_CLUSTERS.append(zcl.clusters.general.AnalogInput.cluster_id)
|
||||
EVENTABLE_CLUSTERS.append(zcl.clusters.general.LevelControl.cluster_id)
|
||||
EVENTABLE_CLUSTERS.append(zcl.clusters.general.MultistateInput.cluster_id)
|
||||
EVENTABLE_CLUSTERS.append(zcl.clusters.general.OnOff.cluster_id)
|
||||
|
||||
DEVICE_CLASS[zha.PROFILE_ID].update({
|
||||
zha.DeviceType.ON_OFF_SWITCH: 'binary_sensor',
|
||||
zha.DeviceType.LEVEL_CONTROL_SWITCH: 'binary_sensor',
|
||||
zha.DeviceType.REMOTE_CONTROL: 'binary_sensor',
|
||||
zha.DeviceType.SMART_PLUG: 'switch',
|
||||
zha.DeviceType.LEVEL_CONTROLLABLE_OUTPUT: 'light',
|
||||
zha.DeviceType.ON_OFF_LIGHT: 'light',
|
||||
zha.DeviceType.DIMMABLE_LIGHT: 'light',
|
||||
zha.DeviceType.COLOR_DIMMABLE_LIGHT: 'light',
|
||||
zha.DeviceType.ON_OFF_LIGHT_SWITCH: 'binary_sensor',
|
||||
zha.DeviceType.DIMMER_SWITCH: 'binary_sensor',
|
||||
zha.DeviceType.COLOR_DIMMER_SWITCH: 'binary_sensor',
|
||||
})
|
||||
DEVICE_CLASS[zll.PROFILE_ID].update({
|
||||
zll.DeviceType.ON_OFF_LIGHT: 'light',
|
||||
zll.DeviceType.ON_OFF_PLUGIN_UNIT: 'switch',
|
||||
zll.DeviceType.DIMMABLE_LIGHT: 'light',
|
||||
zll.DeviceType.DIMMABLE_PLUGIN_UNIT: 'light',
|
||||
zll.DeviceType.COLOR_LIGHT: 'light',
|
||||
zll.DeviceType.EXTENDED_COLOR_LIGHT: 'light',
|
||||
zll.DeviceType.COLOR_TEMPERATURE_LIGHT: 'light',
|
||||
zll.DeviceType.COLOR_CONTROLLER: 'binary_sensor',
|
||||
zll.DeviceType.COLOR_SCENE_CONTROLLER: 'binary_sensor',
|
||||
zll.DeviceType.CONTROLLER: 'binary_sensor',
|
||||
zll.DeviceType.SCENE_CONTROLLER: 'binary_sensor',
|
||||
zll.DeviceType.ON_OFF_SENSOR: 'binary_sensor',
|
||||
})
|
||||
|
||||
SINGLE_INPUT_CLUSTER_DEVICE_CLASS.update({
|
||||
zcl.clusters.general.OnOff: 'switch',
|
||||
zcl.clusters.measurement.RelativeHumidity: 'sensor',
|
||||
zcl.clusters.measurement.TemperatureMeasurement: 'sensor',
|
||||
zcl.clusters.measurement.PressureMeasurement: 'sensor',
|
||||
zcl.clusters.measurement.IlluminanceMeasurement: 'sensor',
|
||||
zcl.clusters.smartenergy.Metering: 'sensor',
|
||||
zcl.clusters.homeautomation.ElectricalMeasurement: 'sensor',
|
||||
zcl.clusters.general.PowerConfiguration: 'sensor',
|
||||
zcl.clusters.security.IasZone: 'binary_sensor',
|
||||
zcl.clusters.measurement.OccupancySensing: 'binary_sensor',
|
||||
zcl.clusters.hvac.Fan: 'fan',
|
||||
})
|
||||
SINGLE_OUTPUT_CLUSTER_DEVICE_CLASS.update({
|
||||
zcl.clusters.general.OnOff: 'binary_sensor',
|
||||
})
|
||||
|
||||
# A map of device/cluster to component/sub-component
|
||||
CUSTOM_CLUSTER_MAPPINGS.update({
|
||||
(quirks.smartthings.SmartthingsTemperatureHumiditySensor, 64581):
|
||||
('sensor', RelativeHumiditySensor)
|
||||
})
|
||||
|
||||
# A map of hass components to all Zigbee clusters it could use
|
||||
for profile_id, classes in DEVICE_CLASS.items():
|
||||
profile = PROFILES[profile_id]
|
||||
for device_type, component in classes.items():
|
||||
if component not in COMPONENT_CLUSTERS:
|
||||
COMPONENT_CLUSTERS[component] = (set(), set())
|
||||
clusters = profile.CLUSTERS[device_type]
|
||||
COMPONENT_CLUSTERS[component][0].update(clusters[0])
|
||||
COMPONENT_CLUSTERS[component][1].update(clusters[1])
|
||||
|
||||
|
||||
class ApplicationListener:
|
||||
"""All handlers for events that happen on the ZigBee application."""
|
||||
|
||||
@ -214,7 +299,6 @@ class ApplicationListener:
|
||||
self._component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
self._device_registry = collections.defaultdict(list)
|
||||
self._events = {}
|
||||
zha_const.populate_data()
|
||||
|
||||
for component in COMPONENTS:
|
||||
hass.data[DATA_ZHA][component] = (
|
||||
|
@ -9,9 +9,9 @@ import logging
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import websocket_api
|
||||
from homeassistant.components.zha.entities import ZhaDeviceEntity
|
||||
from homeassistant.const import ATTR_ENTITY_ID
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from .entities import ZhaDeviceEntity
|
||||
from .const import (
|
||||
DOMAIN, ATTR_CLUSTER_ID, ATTR_CLUSTER_TYPE, ATTR_ATTRIBUTE, ATTR_VALUE,
|
||||
ATTR_MANUFACTURER, ATTR_COMMAND, ATTR_COMMAND_TYPE, ATTR_ARGS, IN, OUT,
|
||||
|
@ -7,16 +7,16 @@ at https://home-assistant.io/components/binary_sensor.zha/
|
||||
import logging
|
||||
|
||||
from homeassistant.components.binary_sensor import DOMAIN, BinarySensorDevice
|
||||
from homeassistant.components.zha import helpers
|
||||
from homeassistant.components.zha.const import (
|
||||
DATA_ZHA, DATA_ZHA_DISPATCHERS, REPORT_CONFIG_IMMEDIATE, ZHA_DISCOVERY_NEW)
|
||||
from homeassistant.components.zha.entities import ZhaEntity
|
||||
from homeassistant.const import STATE_ON
|
||||
from homeassistant.components.zha.entities.listeners import (
|
||||
OnOffListener, LevelListener
|
||||
)
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
from . import helpers
|
||||
from .const import (
|
||||
DATA_ZHA, DATA_ZHA_DISPATCHERS, REPORT_CONFIG_IMMEDIATE, ZHA_DISCOVERY_NEW)
|
||||
from .entities import ZhaEntity
|
||||
from .entities.listeners import (
|
||||
OnOffListener, LevelListener
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -92,85 +92,3 @@ REPORT_CONFIG_IMMEDIATE = (REPORT_CONFIG_MIN_INT_IMMEDIATE,
|
||||
REPORT_CONFIG_RPT_CHANGE)
|
||||
REPORT_CONFIG_OP = (REPORT_CONFIG_MIN_INT_OP, REPORT_CONFIG_MAX_INT,
|
||||
REPORT_CONFIG_RPT_CHANGE)
|
||||
|
||||
|
||||
def populate_data():
|
||||
"""Populate data using constants from bellows.
|
||||
|
||||
These cannot be module level, as importing bellows must be done in a
|
||||
in a function.
|
||||
"""
|
||||
from zigpy import zcl, quirks
|
||||
from zigpy.profiles import PROFILES, zha, zll
|
||||
from .sensor import RelativeHumiditySensor
|
||||
|
||||
if zha.PROFILE_ID not in DEVICE_CLASS:
|
||||
DEVICE_CLASS[zha.PROFILE_ID] = {}
|
||||
if zll.PROFILE_ID not in DEVICE_CLASS:
|
||||
DEVICE_CLASS[zll.PROFILE_ID] = {}
|
||||
|
||||
EVENTABLE_CLUSTERS.append(zcl.clusters.general.AnalogInput.cluster_id)
|
||||
EVENTABLE_CLUSTERS.append(zcl.clusters.general.LevelControl.cluster_id)
|
||||
EVENTABLE_CLUSTERS.append(zcl.clusters.general.MultistateInput.cluster_id)
|
||||
EVENTABLE_CLUSTERS.append(zcl.clusters.general.OnOff.cluster_id)
|
||||
|
||||
DEVICE_CLASS[zha.PROFILE_ID].update({
|
||||
zha.DeviceType.ON_OFF_SWITCH: 'binary_sensor',
|
||||
zha.DeviceType.LEVEL_CONTROL_SWITCH: 'binary_sensor',
|
||||
zha.DeviceType.REMOTE_CONTROL: 'binary_sensor',
|
||||
zha.DeviceType.SMART_PLUG: 'switch',
|
||||
zha.DeviceType.LEVEL_CONTROLLABLE_OUTPUT: 'light',
|
||||
zha.DeviceType.ON_OFF_LIGHT: 'light',
|
||||
zha.DeviceType.DIMMABLE_LIGHT: 'light',
|
||||
zha.DeviceType.COLOR_DIMMABLE_LIGHT: 'light',
|
||||
zha.DeviceType.ON_OFF_LIGHT_SWITCH: 'binary_sensor',
|
||||
zha.DeviceType.DIMMER_SWITCH: 'binary_sensor',
|
||||
zha.DeviceType.COLOR_DIMMER_SWITCH: 'binary_sensor',
|
||||
})
|
||||
DEVICE_CLASS[zll.PROFILE_ID].update({
|
||||
zll.DeviceType.ON_OFF_LIGHT: 'light',
|
||||
zll.DeviceType.ON_OFF_PLUGIN_UNIT: 'switch',
|
||||
zll.DeviceType.DIMMABLE_LIGHT: 'light',
|
||||
zll.DeviceType.DIMMABLE_PLUGIN_UNIT: 'light',
|
||||
zll.DeviceType.COLOR_LIGHT: 'light',
|
||||
zll.DeviceType.EXTENDED_COLOR_LIGHT: 'light',
|
||||
zll.DeviceType.COLOR_TEMPERATURE_LIGHT: 'light',
|
||||
zll.DeviceType.COLOR_CONTROLLER: 'binary_sensor',
|
||||
zll.DeviceType.COLOR_SCENE_CONTROLLER: 'binary_sensor',
|
||||
zll.DeviceType.CONTROLLER: 'binary_sensor',
|
||||
zll.DeviceType.SCENE_CONTROLLER: 'binary_sensor',
|
||||
zll.DeviceType.ON_OFF_SENSOR: 'binary_sensor',
|
||||
})
|
||||
|
||||
SINGLE_INPUT_CLUSTER_DEVICE_CLASS.update({
|
||||
zcl.clusters.general.OnOff: 'switch',
|
||||
zcl.clusters.measurement.RelativeHumidity: 'sensor',
|
||||
zcl.clusters.measurement.TemperatureMeasurement: 'sensor',
|
||||
zcl.clusters.measurement.PressureMeasurement: 'sensor',
|
||||
zcl.clusters.measurement.IlluminanceMeasurement: 'sensor',
|
||||
zcl.clusters.smartenergy.Metering: 'sensor',
|
||||
zcl.clusters.homeautomation.ElectricalMeasurement: 'sensor',
|
||||
zcl.clusters.general.PowerConfiguration: 'sensor',
|
||||
zcl.clusters.security.IasZone: 'binary_sensor',
|
||||
zcl.clusters.measurement.OccupancySensing: 'binary_sensor',
|
||||
zcl.clusters.hvac.Fan: 'fan',
|
||||
})
|
||||
SINGLE_OUTPUT_CLUSTER_DEVICE_CLASS.update({
|
||||
zcl.clusters.general.OnOff: 'binary_sensor',
|
||||
})
|
||||
|
||||
# A map of device/cluster to component/sub-component
|
||||
CUSTOM_CLUSTER_MAPPINGS.update({
|
||||
(quirks.smartthings.SmartthingsTemperatureHumiditySensor, 64581):
|
||||
('sensor', RelativeHumiditySensor)
|
||||
})
|
||||
|
||||
# A map of hass components to all Zigbee clusters it could use
|
||||
for profile_id, classes in DEVICE_CLASS.items():
|
||||
profile = PROFILES[profile_id]
|
||||
for device_type, component in classes.items():
|
||||
if component not in COMPONENT_CLUSTERS:
|
||||
COMPONENT_CLUSTERS[component] = (set(), set())
|
||||
clusters = profile.CLUSTERS[device_type]
|
||||
COMPONENT_CLUSTERS[component][0].update(clusters[0])
|
||||
COMPONENT_CLUSTERS[component][1].update(clusters[1])
|
||||
|
@ -8,16 +8,16 @@ import asyncio
|
||||
import logging
|
||||
from random import uniform
|
||||
|
||||
from homeassistant.components.zha.const import (
|
||||
DATA_ZHA, DATA_ZHA_BRIDGE_ID, DOMAIN, ATTR_CLUSTER_ID, ATTR_ATTRIBUTE,
|
||||
ATTR_VALUE, ATTR_MANUFACTURER, ATTR_COMMAND, SERVER, ATTR_COMMAND_TYPE,
|
||||
ATTR_ARGS, IN, OUT, CLIENT_COMMANDS, SERVER_COMMANDS)
|
||||
from homeassistant.components.zha.helpers import bind_configure_reporting
|
||||
from homeassistant.const import ATTR_ENTITY_ID
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers import entity
|
||||
from homeassistant.helpers.device_registry import CONNECTION_ZIGBEE
|
||||
from homeassistant.util import slugify
|
||||
from ..const import (
|
||||
DATA_ZHA, DATA_ZHA_BRIDGE_ID, DOMAIN, ATTR_CLUSTER_ID, ATTR_ATTRIBUTE,
|
||||
ATTR_VALUE, ATTR_MANUFACTURER, ATTR_COMMAND, SERVER, ATTR_COMMAND_TYPE,
|
||||
ATTR_ARGS, IN, OUT, CLIENT_COMMANDS, SERVER_COMMANDS)
|
||||
from ..helpers import bind_configure_reporting
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -9,11 +9,11 @@ import logging
|
||||
from homeassistant.components.fan import (
|
||||
DOMAIN, SPEED_HIGH, SPEED_LOW, SPEED_MEDIUM, SPEED_OFF, SUPPORT_SET_SPEED,
|
||||
FanEntity)
|
||||
from homeassistant.components.zha import helpers
|
||||
from homeassistant.components.zha.const import (
|
||||
DATA_ZHA, DATA_ZHA_DISPATCHERS, REPORT_CONFIG_OP, ZHA_DISCOVERY_NEW)
|
||||
from homeassistant.components.zha.entities import ZhaEntity
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from . import helpers
|
||||
from .const import (
|
||||
DATA_ZHA, DATA_ZHA_DISPATCHERS, REPORT_CONFIG_OP, ZHA_DISCOVERY_NEW)
|
||||
from .entities import ZhaEntity
|
||||
|
||||
DEPENDENCIES = ['zha']
|
||||
|
||||
|
@ -7,16 +7,16 @@ at https://home-assistant.io/components/light.zha/
|
||||
import logging
|
||||
|
||||
from homeassistant.components import light
|
||||
from homeassistant.components.zha import helpers
|
||||
from homeassistant.components.zha.const import (
|
||||
DATA_ZHA, DATA_ZHA_DISPATCHERS, REPORT_CONFIG_ASAP, REPORT_CONFIG_DEFAULT,
|
||||
REPORT_CONFIG_IMMEDIATE, ZHA_DISCOVERY_NEW)
|
||||
from homeassistant.components.zha.entities import ZhaEntity
|
||||
from homeassistant.components.zha.entities.listeners import (
|
||||
OnOffListener, LevelListener
|
||||
)
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
import homeassistant.util.color as color_util
|
||||
from . import helpers
|
||||
from .const import (
|
||||
DATA_ZHA, DATA_ZHA_DISPATCHERS, REPORT_CONFIG_ASAP, REPORT_CONFIG_DEFAULT,
|
||||
REPORT_CONFIG_IMMEDIATE, ZHA_DISCOVERY_NEW)
|
||||
from .entities import ZhaEntity
|
||||
from .entities.listeners import (
|
||||
OnOffListener, LevelListener
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -7,14 +7,14 @@ at https://home-assistant.io/components/sensor.zha/
|
||||
import logging
|
||||
|
||||
from homeassistant.components.sensor import DOMAIN
|
||||
from homeassistant.components.zha import helpers
|
||||
from homeassistant.components.zha.const import (
|
||||
DATA_ZHA, DATA_ZHA_DISPATCHERS, REPORT_CONFIG_MAX_INT,
|
||||
REPORT_CONFIG_MIN_INT, REPORT_CONFIG_RPT_CHANGE, ZHA_DISCOVERY_NEW)
|
||||
from homeassistant.components.zha.entities import ZhaEntity
|
||||
from homeassistant.const import TEMP_CELSIUS
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.util.temperature import convert as convert_temperature
|
||||
from . import helpers
|
||||
from .const import (
|
||||
DATA_ZHA, DATA_ZHA_DISPATCHERS, REPORT_CONFIG_MAX_INT,
|
||||
REPORT_CONFIG_MIN_INT, REPORT_CONFIG_RPT_CHANGE, ZHA_DISCOVERY_NEW)
|
||||
from .entities import ZhaEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -7,11 +7,11 @@ at https://home-assistant.io/components/switch.zha/
|
||||
import logging
|
||||
|
||||
from homeassistant.components.switch import DOMAIN, SwitchDevice
|
||||
from homeassistant.components.zha import helpers
|
||||
from homeassistant.components.zha.const import (
|
||||
DATA_ZHA, DATA_ZHA_DISPATCHERS, REPORT_CONFIG_IMMEDIATE, ZHA_DISCOVERY_NEW)
|
||||
from homeassistant.components.zha.entities import ZhaEntity
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from . import helpers
|
||||
from .const import (
|
||||
DATA_ZHA, DATA_ZHA_DISPATCHERS, REPORT_CONFIG_IMMEDIATE, ZHA_DISCOVERY_NEW)
|
||||
from .entities import ZhaEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user