mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Some code cleanups for ESPHome (#46367)
This commit is contained in:
parent
379f5455e5
commit
29d8b8a22f
@ -40,8 +40,7 @@ from homeassistant.helpers.template import Template
|
|||||||
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
||||||
|
|
||||||
# Import config flow so that it's added to the registry
|
# Import config flow so that it's added to the registry
|
||||||
from .config_flow import EsphomeFlowHandler # noqa: F401
|
from .entry_data import RuntimeEntryData
|
||||||
from .entry_data import DATA_KEY, RuntimeEntryData
|
|
||||||
|
|
||||||
DOMAIN = "esphome"
|
DOMAIN = "esphome"
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -62,7 +61,7 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
|
|||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
|
||||||
"""Set up the esphome component."""
|
"""Set up the esphome component."""
|
||||||
hass.data.setdefault(DATA_KEY, {})
|
hass.data.setdefault(DOMAIN, {})
|
||||||
|
|
||||||
host = entry.data[CONF_HOST]
|
host = entry.data[CONF_HOST]
|
||||||
port = entry.data[CONF_PORT]
|
port = entry.data[CONF_PORT]
|
||||||
@ -84,7 +83,7 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool
|
|||||||
store = Store(
|
store = Store(
|
||||||
hass, STORAGE_VERSION, f"esphome.{entry.entry_id}", encoder=JSONEncoder
|
hass, STORAGE_VERSION, f"esphome.{entry.entry_id}", encoder=JSONEncoder
|
||||||
)
|
)
|
||||||
entry_data = hass.data[DATA_KEY][entry.entry_id] = RuntimeEntryData(
|
entry_data = hass.data[DOMAIN][entry.entry_id] = RuntimeEntryData(
|
||||||
client=cli, entry_id=entry.entry_id, store=store
|
client=cli, entry_id=entry.entry_id, store=store
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -363,7 +362,7 @@ async def _cleanup_instance(
|
|||||||
hass: HomeAssistantType, entry: ConfigEntry
|
hass: HomeAssistantType, entry: ConfigEntry
|
||||||
) -> RuntimeEntryData:
|
) -> RuntimeEntryData:
|
||||||
"""Cleanup the esphome client if it exists."""
|
"""Cleanup the esphome client if it exists."""
|
||||||
data: RuntimeEntryData = hass.data[DATA_KEY].pop(entry.entry_id)
|
data: RuntimeEntryData = hass.data[DOMAIN].pop(entry.entry_id)
|
||||||
if data.reconnect_task is not None:
|
if data.reconnect_task is not None:
|
||||||
data.reconnect_task.cancel()
|
data.reconnect_task.cancel()
|
||||||
for disconnect_cb in data.disconnect_callbacks:
|
for disconnect_cb in data.disconnect_callbacks:
|
||||||
@ -545,7 +544,7 @@ class EsphomeBaseEntity(Entity):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def _entry_data(self) -> RuntimeEntryData:
|
def _entry_data(self) -> RuntimeEntryData:
|
||||||
return self.hass.data[DATA_KEY][self._entry_id]
|
return self.hass.data[DOMAIN][self._entry_id]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _static_info(self) -> EntityInfo:
|
def _static_info(self) -> EntityInfo:
|
||||||
|
@ -11,9 +11,8 @@ from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_PORT
|
|||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
|
|
||||||
from .entry_data import DATA_KEY, RuntimeEntryData
|
from . import DOMAIN
|
||||||
|
from .entry_data import RuntimeEntryData
|
||||||
DOMAIN = "esphome"
|
|
||||||
|
|
||||||
|
|
||||||
class EsphomeFlowHandler(ConfigFlow, domain=DOMAIN):
|
class EsphomeFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||||
@ -107,9 +106,9 @@ class EsphomeFlowHandler(ConfigFlow, domain=DOMAIN):
|
|||||||
]:
|
]:
|
||||||
# Is this address or IP address already configured?
|
# Is this address or IP address already configured?
|
||||||
already_configured = True
|
already_configured = True
|
||||||
elif entry.entry_id in self.hass.data.get(DATA_KEY, {}):
|
elif entry.entry_id in self.hass.data.get(DOMAIN, {}):
|
||||||
# Does a config entry with this name already exist?
|
# Does a config entry with this name already exist?
|
||||||
data: RuntimeEntryData = self.hass.data[DATA_KEY][entry.entry_id]
|
data: RuntimeEntryData = self.hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
# Node names are unique in the network
|
# Node names are unique in the network
|
||||||
if data.device_info is not None:
|
if data.device_info is not None:
|
||||||
|
@ -29,8 +29,6 @@ from homeassistant.helpers.typing import HomeAssistantType
|
|||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from . import APIClient
|
from . import APIClient
|
||||||
|
|
||||||
DATA_KEY = "esphome"
|
|
||||||
|
|
||||||
SAVE_DELAY = 120
|
SAVE_DELAY = 120
|
||||||
|
|
||||||
# Mapping from ESPHome info type to HA platform
|
# Mapping from ESPHome info type to HA platform
|
||||||
|
@ -4,7 +4,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.esphome import DATA_KEY
|
from homeassistant.components.esphome import DOMAIN
|
||||||
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT
|
||||||
from homeassistant.data_entry_flow import (
|
from homeassistant.data_entry_flow import (
|
||||||
RESULT_TYPE_ABORT,
|
RESULT_TYPE_ABORT,
|
||||||
@ -207,7 +207,7 @@ async def test_discovery_initiation(hass, mock_client):
|
|||||||
async def test_discovery_already_configured_hostname(hass, mock_client):
|
async def test_discovery_already_configured_hostname(hass, mock_client):
|
||||||
"""Test discovery aborts if already configured via hostname."""
|
"""Test discovery aborts if already configured via hostname."""
|
||||||
entry = MockConfigEntry(
|
entry = MockConfigEntry(
|
||||||
domain="esphome",
|
domain=DOMAIN,
|
||||||
data={CONF_HOST: "test8266.local", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
data={CONF_HOST: "test8266.local", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -232,7 +232,7 @@ async def test_discovery_already_configured_hostname(hass, mock_client):
|
|||||||
async def test_discovery_already_configured_ip(hass, mock_client):
|
async def test_discovery_already_configured_ip(hass, mock_client):
|
||||||
"""Test discovery aborts if already configured via static IP."""
|
"""Test discovery aborts if already configured via static IP."""
|
||||||
entry = MockConfigEntry(
|
entry = MockConfigEntry(
|
||||||
domain="esphome",
|
domain=DOMAIN,
|
||||||
data={CONF_HOST: "192.168.43.183", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
data={CONF_HOST: "192.168.43.183", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -257,14 +257,14 @@ async def test_discovery_already_configured_ip(hass, mock_client):
|
|||||||
async def test_discovery_already_configured_name(hass, mock_client):
|
async def test_discovery_already_configured_name(hass, mock_client):
|
||||||
"""Test discovery aborts if already configured via name."""
|
"""Test discovery aborts if already configured via name."""
|
||||||
entry = MockConfigEntry(
|
entry = MockConfigEntry(
|
||||||
domain="esphome",
|
domain=DOMAIN,
|
||||||
data={CONF_HOST: "192.168.43.183", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
data={CONF_HOST: "192.168.43.183", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
mock_entry_data = MagicMock()
|
mock_entry_data = MagicMock()
|
||||||
mock_entry_data.device_info.name = "test8266"
|
mock_entry_data.device_info.name = "test8266"
|
||||||
hass.data[DATA_KEY] = {entry.entry_id: mock_entry_data}
|
hass.data[DOMAIN] = {entry.entry_id: mock_entry_data}
|
||||||
|
|
||||||
service_info = {
|
service_info = {
|
||||||
"host": "192.168.43.184",
|
"host": "192.168.43.184",
|
||||||
@ -310,7 +310,7 @@ async def test_discovery_duplicate_data(hass, mock_client):
|
|||||||
async def test_discovery_updates_unique_id(hass, mock_client):
|
async def test_discovery_updates_unique_id(hass, mock_client):
|
||||||
"""Test a duplicate discovery host aborts and updates existing entry."""
|
"""Test a duplicate discovery host aborts and updates existing entry."""
|
||||||
entry = MockConfigEntry(
|
entry = MockConfigEntry(
|
||||||
domain="esphome",
|
domain=DOMAIN,
|
||||||
data={CONF_HOST: "192.168.43.183", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
data={CONF_HOST: "192.168.43.183", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user