mirror of
https://github.com/home-assistant/core.git
synced 2025-07-30 16:57:19 +00:00
Migrate modbus to use HassKey (#136379)
This commit is contained in:
parent
5e34babc39
commit
a12255ea5d
@ -3,7 +3,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from typing import cast
|
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -143,7 +142,7 @@ from .const import (
|
|||||||
UDP,
|
UDP,
|
||||||
DataType,
|
DataType,
|
||||||
)
|
)
|
||||||
from .modbus import ModbusHub, async_modbus_setup
|
from .modbus import DATA_MODBUS_HUBS, ModbusHub, async_modbus_setup
|
||||||
from .validators import (
|
from .validators import (
|
||||||
duplicate_fan_mode_validator,
|
duplicate_fan_mode_validator,
|
||||||
duplicate_swing_mode_validator,
|
duplicate_swing_mode_validator,
|
||||||
@ -458,7 +457,7 @@ CONFIG_SCHEMA = vol.Schema(
|
|||||||
|
|
||||||
def get_hub(hass: HomeAssistant, name: str) -> ModbusHub:
|
def get_hub(hass: HomeAssistant, name: str) -> ModbusHub:
|
||||||
"""Return modbus hub with name."""
|
"""Return modbus hub with name."""
|
||||||
return cast(ModbusHub, hass.data[DOMAIN][name])
|
return hass.data[DATA_MODBUS_HUBS][name]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
@ -468,12 +467,12 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
|
|
||||||
async def _reload_config(call: Event | ServiceCall) -> None:
|
async def _reload_config(call: Event | ServiceCall) -> None:
|
||||||
"""Reload Modbus."""
|
"""Reload Modbus."""
|
||||||
if DOMAIN not in hass.data:
|
if DATA_MODBUS_HUBS not in hass.data:
|
||||||
_LOGGER.error("Modbus cannot reload, because it was never loaded")
|
_LOGGER.error("Modbus cannot reload, because it was never loaded")
|
||||||
return
|
return
|
||||||
hubs = hass.data[DOMAIN]
|
hubs = hass.data[DATA_MODBUS_HUBS]
|
||||||
for name in hubs:
|
for hub in hubs.values():
|
||||||
await hubs[name].async_close()
|
await hub.async_close()
|
||||||
reset_platforms = async_get_platforms(hass, DOMAIN)
|
reset_platforms = async_get_platforms(hass, DOMAIN)
|
||||||
for reset_platform in reset_platforms:
|
for reset_platform in reset_platforms:
|
||||||
_LOGGER.debug("Reload modbus resetting platform: %s", reset_platform.domain)
|
_LOGGER.debug("Reload modbus resetting platform: %s", reset_platform.domain)
|
||||||
@ -487,7 +486,4 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
|
|
||||||
async_register_admin_service(hass, DOMAIN, SERVICE_RELOAD, _reload_config)
|
async_register_admin_service(hass, DOMAIN, SERVICE_RELOAD, _reload_config)
|
||||||
|
|
||||||
return await async_modbus_setup(
|
return await async_modbus_setup(hass, config)
|
||||||
hass,
|
|
||||||
config,
|
|
||||||
)
|
|
||||||
|
@ -35,6 +35,7 @@ from homeassistant.helpers.discovery import async_load_platform
|
|||||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
from homeassistant.helpers.event import async_call_later
|
from homeassistant.helpers.event import async_call_later
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
from homeassistant.util.hass_dict import HassKey
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_ADDRESS,
|
ATTR_ADDRESS,
|
||||||
@ -70,6 +71,7 @@ from .const import (
|
|||||||
from .validators import check_config
|
from .validators import check_config
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
DATA_MODBUS_HUBS: HassKey[dict[str, ModbusHub]] = HassKey(DOMAIN)
|
||||||
|
|
||||||
|
|
||||||
ConfEntry = namedtuple("ConfEntry", "call_type attr func_name value_attr_name") # noqa: PYI024
|
ConfEntry = namedtuple("ConfEntry", "call_type attr func_name value_attr_name") # noqa: PYI024
|
||||||
@ -136,14 +138,14 @@ async def async_modbus_setup(
|
|||||||
config[DOMAIN] = check_config(hass, config[DOMAIN])
|
config[DOMAIN] = check_config(hass, config[DOMAIN])
|
||||||
if not config[DOMAIN]:
|
if not config[DOMAIN]:
|
||||||
return False
|
return False
|
||||||
if DOMAIN in hass.data and config[DOMAIN] == []:
|
if DATA_MODBUS_HUBS in hass.data and config[DOMAIN] == []:
|
||||||
hubs = hass.data[DOMAIN]
|
hubs = hass.data[DATA_MODBUS_HUBS]
|
||||||
for name in hubs:
|
for hub in hubs.values():
|
||||||
if not await hubs[name].async_setup():
|
if not await hub.async_setup():
|
||||||
return False
|
return False
|
||||||
hub_collect = hass.data[DOMAIN]
|
hub_collect = hass.data[DATA_MODBUS_HUBS]
|
||||||
else:
|
else:
|
||||||
hass.data[DOMAIN] = hub_collect = {}
|
hass.data[DATA_MODBUS_HUBS] = hub_collect = {}
|
||||||
|
|
||||||
for conf_hub in config[DOMAIN]:
|
for conf_hub in config[DOMAIN]:
|
||||||
my_hub = ModbusHub(hass, conf_hub)
|
my_hub = ModbusHub(hass, conf_hub)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user