mirror of
https://github.com/home-assistant/core.git
synced 2025-07-29 16:17:20 +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
|
||||
|
||||
import logging
|
||||
from typing import cast
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
@ -143,7 +142,7 @@ from .const import (
|
||||
UDP,
|
||||
DataType,
|
||||
)
|
||||
from .modbus import ModbusHub, async_modbus_setup
|
||||
from .modbus import DATA_MODBUS_HUBS, ModbusHub, async_modbus_setup
|
||||
from .validators import (
|
||||
duplicate_fan_mode_validator,
|
||||
duplicate_swing_mode_validator,
|
||||
@ -458,7 +457,7 @@ CONFIG_SCHEMA = vol.Schema(
|
||||
|
||||
def get_hub(hass: HomeAssistant, name: str) -> ModbusHub:
|
||||
"""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:
|
||||
@ -468,12 +467,12 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
|
||||
async def _reload_config(call: Event | ServiceCall) -> None:
|
||||
"""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")
|
||||
return
|
||||
hubs = hass.data[DOMAIN]
|
||||
for name in hubs:
|
||||
await hubs[name].async_close()
|
||||
hubs = hass.data[DATA_MODBUS_HUBS]
|
||||
for hub in hubs.values():
|
||||
await hub.async_close()
|
||||
reset_platforms = async_get_platforms(hass, DOMAIN)
|
||||
for reset_platform in reset_platforms:
|
||||
_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)
|
||||
|
||||
return await async_modbus_setup(
|
||||
hass,
|
||||
config,
|
||||
)
|
||||
return await async_modbus_setup(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.event import async_call_later
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
from homeassistant.util.hass_dict import HassKey
|
||||
|
||||
from .const import (
|
||||
ATTR_ADDRESS,
|
||||
@ -70,6 +71,7 @@ from .const import (
|
||||
from .validators import check_config
|
||||
|
||||
_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
|
||||
@ -136,14 +138,14 @@ async def async_modbus_setup(
|
||||
config[DOMAIN] = check_config(hass, config[DOMAIN])
|
||||
if not config[DOMAIN]:
|
||||
return False
|
||||
if DOMAIN in hass.data and config[DOMAIN] == []:
|
||||
hubs = hass.data[DOMAIN]
|
||||
for name in hubs:
|
||||
if not await hubs[name].async_setup():
|
||||
if DATA_MODBUS_HUBS in hass.data and config[DOMAIN] == []:
|
||||
hubs = hass.data[DATA_MODBUS_HUBS]
|
||||
for hub in hubs.values():
|
||||
if not await hub.async_setup():
|
||||
return False
|
||||
hub_collect = hass.data[DOMAIN]
|
||||
hub_collect = hass.data[DATA_MODBUS_HUBS]
|
||||
else:
|
||||
hass.data[DOMAIN] = hub_collect = {}
|
||||
hass.data[DATA_MODBUS_HUBS] = hub_collect = {}
|
||||
|
||||
for conf_hub in config[DOMAIN]:
|
||||
my_hub = ModbusHub(hass, conf_hub)
|
||||
|
Loading…
x
Reference in New Issue
Block a user