Use Platform enum [mysensors] (#63687)
parent
073a080bb2
commit
8c51303332
|
@ -10,11 +10,9 @@ from mysensors import BaseAsyncGateway
|
|||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.device_tracker import DOMAIN as DEVICE_TRACKER_DOMAIN
|
||||
from homeassistant.components.mqtt import valid_publish_topic, valid_subscribe_topic
|
||||
from homeassistant.components.notify import DOMAIN as NOTIFY_DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_OPTIMISTIC
|
||||
from homeassistant.const import CONF_OPTIMISTIC, Platform
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.discovery import async_load_platform
|
||||
|
@ -209,7 +207,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
# Allow loading device tracker platform via discovery
|
||||
# until refactor to config entry is done.
|
||||
|
||||
for platform in (DEVICE_TRACKER_DOMAIN, NOTIFY_DOMAIN):
|
||||
for platform in (Platform.DEVICE_TRACKER, Platform.NOTIFY):
|
||||
load_discovery_platform = partial(
|
||||
async_load_platform,
|
||||
hass,
|
||||
|
@ -269,7 +267,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
@callback
|
||||
def setup_mysensors_platform(
|
||||
hass: HomeAssistant,
|
||||
domain: str, # hass platform name
|
||||
domain: Platform, # hass platform name
|
||||
discovery_info: DiscoveryInfo,
|
||||
device_class: type[MySensorsDevice] | dict[SensorType, type[MySensorsDevice]],
|
||||
device_args: (
|
||||
|
|
|
@ -4,12 +4,11 @@ from __future__ import annotations
|
|||
from homeassistant.components import mysensors
|
||||
from homeassistant.components.binary_sensor import (
|
||||
DEVICE_CLASSES,
|
||||
DOMAIN,
|
||||
BinarySensorDeviceClass,
|
||||
BinarySensorEntity,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import STATE_ON
|
||||
from homeassistant.const import STATE_ON, Platform
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
@ -41,7 +40,7 @@ async def async_setup_entry(
|
|||
"""Discover and add a MySensors binary_sensor."""
|
||||
mysensors.setup_mysensors_platform(
|
||||
hass,
|
||||
DOMAIN,
|
||||
Platform.BINARY_SENSOR,
|
||||
discovery_info,
|
||||
MySensorsBinarySensor,
|
||||
async_add_entities=async_add_entities,
|
||||
|
@ -52,7 +51,7 @@ async def async_setup_entry(
|
|||
config_entry.entry_id,
|
||||
async_dispatcher_connect(
|
||||
hass,
|
||||
MYSENSORS_DISCOVERY.format(config_entry.entry_id, DOMAIN),
|
||||
MYSENSORS_DISCOVERY.format(config_entry.entry_id, Platform.BINARY_SENSOR),
|
||||
async_discover,
|
||||
),
|
||||
)
|
||||
|
|
|
@ -8,7 +8,6 @@ from homeassistant.components.climate import ClimateEntity
|
|||
from homeassistant.components.climate.const import (
|
||||
ATTR_TARGET_TEMP_HIGH,
|
||||
ATTR_TARGET_TEMP_LOW,
|
||||
DOMAIN,
|
||||
HVAC_MODE_AUTO,
|
||||
HVAC_MODE_COOL,
|
||||
HVAC_MODE_HEAT,
|
||||
|
@ -18,7 +17,12 @@ from homeassistant.components.climate.const import (
|
|||
SUPPORT_TARGET_TEMPERATURE_RANGE,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS, TEMP_FAHRENHEIT
|
||||
from homeassistant.const import (
|
||||
ATTR_TEMPERATURE,
|
||||
TEMP_CELSIUS,
|
||||
TEMP_FAHRENHEIT,
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
@ -54,7 +58,7 @@ async def async_setup_entry(
|
|||
"""Discover and add a MySensors climate."""
|
||||
mysensors.setup_mysensors_platform(
|
||||
hass,
|
||||
DOMAIN,
|
||||
Platform.CLIMATE,
|
||||
discovery_info,
|
||||
MySensorsHVAC,
|
||||
async_add_entities=async_add_entities,
|
||||
|
@ -65,7 +69,7 @@ async def async_setup_entry(
|
|||
config_entry.entry_id,
|
||||
async_dispatcher_connect(
|
||||
hass,
|
||||
MYSENSORS_DISCOVERY.format(config_entry.entry_id, DOMAIN),
|
||||
MYSENSORS_DISCOVERY.format(config_entry.entry_id, Platform.CLIMATE),
|
||||
async_discover,
|
||||
),
|
||||
)
|
||||
|
|
|
@ -163,7 +163,7 @@ FLAT_PLATFORM_TYPES: dict[tuple[str, SensorType], set[ValueType]] = {
|
|||
for s_type_name, v_type_name in platform_types.items()
|
||||
}
|
||||
|
||||
TYPE_TO_PLATFORMS: dict[SensorType, list[str]] = defaultdict(list)
|
||||
TYPE_TO_PLATFORMS: dict[SensorType, list[Platform]] = defaultdict(list)
|
||||
|
||||
for platform, platform_types in PLATFORM_TYPES.items():
|
||||
for s_type_name in platform_types:
|
||||
|
|
|
@ -5,9 +5,9 @@ from enum import Enum, unique
|
|||
from typing import Any
|
||||
|
||||
from homeassistant.components import mysensors
|
||||
from homeassistant.components.cover import ATTR_POSITION, DOMAIN, CoverEntity
|
||||
from homeassistant.components.cover import ATTR_POSITION, CoverEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import STATE_OFF, STATE_ON
|
||||
from homeassistant.const import STATE_OFF, STATE_ON, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
@ -37,7 +37,7 @@ async def async_setup_entry(
|
|||
"""Discover and add a MySensors cover."""
|
||||
mysensors.setup_mysensors_platform(
|
||||
hass,
|
||||
DOMAIN,
|
||||
Platform.COVER,
|
||||
discovery_info,
|
||||
MySensorsCover,
|
||||
async_add_entities=async_add_entities,
|
||||
|
@ -48,7 +48,7 @@ async def async_setup_entry(
|
|||
config_entry.entry_id,
|
||||
async_dispatcher_connect(
|
||||
hass,
|
||||
MYSENSORS_DISCOVERY.format(config_entry.entry_id, DOMAIN),
|
||||
MYSENSORS_DISCOVERY.format(config_entry.entry_id, Platform.COVER),
|
||||
async_discover,
|
||||
),
|
||||
)
|
||||
|
|
|
@ -8,7 +8,7 @@ from typing import Any
|
|||
from mysensors import BaseAsyncGateway, Sensor
|
||||
from mysensors.sensor import ChildSensor
|
||||
|
||||
from homeassistant.const import ATTR_BATTERY_LEVEL, STATE_OFF, STATE_ON
|
||||
from homeassistant.const import ATTR_BATTERY_LEVEL, STATE_OFF, STATE_ON, Platform
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity import DeviceInfo, Entity
|
||||
|
@ -209,7 +209,7 @@ class MySensorsDevice:
|
|||
|
||||
|
||||
def get_mysensors_devices(
|
||||
hass: HomeAssistant, domain: str
|
||||
hass: HomeAssistant, domain: Platform
|
||||
) -> dict[DevId, MySensorsDevice]:
|
||||
"""Return MySensors devices for a hass platform name."""
|
||||
if MYSENSORS_PLATFORM_DEVICES.format(domain) not in hass.data[DOMAIN]:
|
||||
|
|
|
@ -5,7 +5,7 @@ from collections.abc import Callable
|
|||
from typing import Any
|
||||
|
||||
from homeassistant.components import mysensors
|
||||
from homeassistant.components.device_tracker import DOMAIN
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.util import slugify
|
||||
|
@ -26,7 +26,7 @@ async def async_setup_scanner(
|
|||
|
||||
new_devices = mysensors.setup_mysensors_platform(
|
||||
hass,
|
||||
DOMAIN,
|
||||
Platform.DEVICE_TRACKER,
|
||||
discovery_info,
|
||||
MySensorsDeviceScanner,
|
||||
device_args=(hass, async_see),
|
||||
|
|
|
@ -3,6 +3,7 @@ from __future__ import annotations
|
|||
|
||||
from mysensors import Message
|
||||
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.util import decorator
|
||||
|
@ -66,7 +67,7 @@ async def handle_sketch_version(
|
|||
|
||||
@callback
|
||||
def _handle_child_update(
|
||||
hass: HomeAssistant, gateway_id: GatewayId, validated: dict[str, list[DevId]]
|
||||
hass: HomeAssistant, gateway_id: GatewayId, validated: dict[Platform, list[DevId]]
|
||||
) -> None:
|
||||
"""Handle a child update."""
|
||||
signals: list[str] = []
|
||||
|
|
|
@ -10,7 +10,7 @@ from mysensors import BaseAsyncGateway, Message
|
|||
from mysensors.sensor import ChildSensor
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.const import CONF_NAME, Platform
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
|
@ -148,7 +148,9 @@ def invalid_msg(
|
|||
)
|
||||
|
||||
|
||||
def validate_set_msg(gateway_id: GatewayId, msg: Message) -> dict[str, list[DevId]]:
|
||||
def validate_set_msg(
|
||||
gateway_id: GatewayId, msg: Message
|
||||
) -> dict[Platform, list[DevId]]:
|
||||
"""Validate a set message."""
|
||||
if not validate_node(msg.gateway, msg.node_id):
|
||||
return {}
|
||||
|
@ -170,9 +172,9 @@ def validate_child(
|
|||
node_id: int,
|
||||
child: ChildSensor,
|
||||
value_type: int | None = None,
|
||||
) -> defaultdict[str, list[DevId]]:
|
||||
) -> defaultdict[Platform, list[DevId]]:
|
||||
"""Validate a child. Returns a dict mapping hass platform names to list of DevId."""
|
||||
validated: defaultdict[str, list[DevId]] = defaultdict(list)
|
||||
validated: defaultdict[Platform, list[DevId]] = defaultdict(list)
|
||||
pres: type[IntEnum] = gateway.const.Presentation
|
||||
set_req: type[IntEnum] = gateway.const.SetReq
|
||||
child_type_name: SensorType | None = next(
|
||||
|
@ -186,7 +188,7 @@ def validate_child(
|
|||
value_type_names: set[ValueType] = {
|
||||
member.name for member in set_req if member.value in value_types
|
||||
}
|
||||
platforms: list[str] = TYPE_TO_PLATFORMS.get(child_type_name, [])
|
||||
platforms: list[Platform] = TYPE_TO_PLATFORMS.get(child_type_name, [])
|
||||
if not platforms:
|
||||
_LOGGER.warning("Child type %s is not supported", child.type)
|
||||
return validated
|
||||
|
|
|
@ -11,11 +11,10 @@ from homeassistant.components.light import (
|
|||
COLOR_MODE_BRIGHTNESS,
|
||||
COLOR_MODE_RGB,
|
||||
COLOR_MODE_RGBW,
|
||||
DOMAIN,
|
||||
LightEntity,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import STATE_OFF, STATE_ON
|
||||
from homeassistant.const import STATE_OFF, STATE_ON, Platform
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
@ -42,7 +41,7 @@ async def async_setup_entry(
|
|||
"""Discover and add a MySensors light."""
|
||||
mysensors.setup_mysensors_platform(
|
||||
hass,
|
||||
DOMAIN,
|
||||
Platform.LIGHT,
|
||||
discovery_info,
|
||||
device_class_map,
|
||||
async_add_entities=async_add_entities,
|
||||
|
@ -53,7 +52,7 @@ async def async_setup_entry(
|
|||
config_entry.entry_id,
|
||||
async_dispatcher_connect(
|
||||
hass,
|
||||
MYSENSORS_DISCOVERY.format(config_entry.entry_id, DOMAIN),
|
||||
MYSENSORS_DISCOVERY.format(config_entry.entry_id, Platform.LIGHT),
|
||||
async_discover,
|
||||
),
|
||||
)
|
||||
|
|
|
@ -4,7 +4,8 @@ from __future__ import annotations
|
|||
from typing import Any
|
||||
|
||||
from homeassistant.components import mysensors
|
||||
from homeassistant.components.notify import ATTR_TARGET, DOMAIN, BaseNotificationService
|
||||
from homeassistant.components.notify import ATTR_TARGET, BaseNotificationService
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DevId, DiscoveryInfo
|
||||
|
@ -20,7 +21,7 @@ async def async_get_service(
|
|||
return None
|
||||
|
||||
new_devices = mysensors.setup_mysensors_platform(
|
||||
hass, DOMAIN, discovery_info, MySensorsNotificationDevice
|
||||
hass, Platform.NOTIFY, discovery_info, MySensorsNotificationDevice
|
||||
)
|
||||
if not new_devices:
|
||||
return None
|
||||
|
@ -51,7 +52,7 @@ class MySensorsNotificationService(BaseNotificationService):
|
|||
self.devices: dict[
|
||||
DevId, MySensorsNotificationDevice
|
||||
] = mysensors.get_mysensors_devices(
|
||||
hass, DOMAIN
|
||||
hass, Platform.NOTIFY
|
||||
) # type: ignore[assignment]
|
||||
|
||||
async def async_send_message(self, message: str = "", **kwargs: Any) -> None:
|
||||
|
|
|
@ -7,7 +7,6 @@ from awesomeversion import AwesomeVersion
|
|||
|
||||
from homeassistant.components import mysensors
|
||||
from homeassistant.components.sensor import (
|
||||
DOMAIN,
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
|
@ -32,6 +31,7 @@ from homeassistant.const import (
|
|||
TEMP_CELSIUS,
|
||||
TEMP_FAHRENHEIT,
|
||||
VOLUME_CUBIC_METERS,
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
|
@ -197,7 +197,7 @@ async def async_setup_entry(
|
|||
"""Discover and add a MySensors sensor."""
|
||||
mysensors.setup_mysensors_platform(
|
||||
hass,
|
||||
DOMAIN,
|
||||
Platform.SENSOR,
|
||||
discovery_info,
|
||||
MySensorsSensor,
|
||||
async_add_entities=async_add_entities,
|
||||
|
@ -208,7 +208,7 @@ async def async_setup_entry(
|
|||
config_entry.entry_id,
|
||||
async_dispatcher_connect(
|
||||
hass,
|
||||
MYSENSORS_DISCOVERY.format(config_entry.entry_id, DOMAIN),
|
||||
MYSENSORS_DISCOVERY.format(config_entry.entry_id, Platform.SENSOR),
|
||||
async_discover,
|
||||
),
|
||||
)
|
||||
|
|
|
@ -6,9 +6,9 @@ from typing import Any
|
|||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import mysensors
|
||||
from homeassistant.components.switch import DOMAIN, SwitchEntity
|
||||
from homeassistant.components.switch import SwitchEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON
|
||||
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, Platform
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
|
@ -57,7 +57,7 @@ async def async_setup_entry(
|
|||
"""Discover and add a MySensors switch."""
|
||||
mysensors.setup_mysensors_platform(
|
||||
hass,
|
||||
DOMAIN,
|
||||
Platform.SWITCH,
|
||||
discovery_info,
|
||||
device_class_map,
|
||||
async_add_entities=async_add_entities,
|
||||
|
@ -67,7 +67,7 @@ async def async_setup_entry(
|
|||
"""Set IR code as device state attribute."""
|
||||
entity_ids = service.data.get(ATTR_ENTITY_ID)
|
||||
ir_code = service.data.get(ATTR_IR_CODE)
|
||||
devices = mysensors.get_mysensors_devices(hass, DOMAIN)
|
||||
devices = mysensors.get_mysensors_devices(hass, Platform.SWITCH)
|
||||
|
||||
if entity_ids:
|
||||
_devices = [
|
||||
|
@ -99,7 +99,7 @@ async def async_setup_entry(
|
|||
config_entry.entry_id,
|
||||
async_dispatcher_connect(
|
||||
hass,
|
||||
MYSENSORS_DISCOVERY.format(config_entry.entry_id, DOMAIN),
|
||||
MYSENSORS_DISCOVERY.format(config_entry.entry_id, Platform.SWITCH),
|
||||
async_discover,
|
||||
),
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue