mirror of
https://github.com/home-assistant/core.git
synced 2026-04-02 08:26:41 +01:00
Move nuheat coordinator to separate module (#164833)
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
"""Support for NuHeat thermostats."""
|
||||
|
||||
from datetime import timedelta
|
||||
from http import HTTPStatus
|
||||
import logging
|
||||
|
||||
@@ -11,14 +10,14 @@ from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from .const import CONF_SERIAL_NUMBER, DOMAIN, PLATFORMS
|
||||
from .coordinator import NuHeatCoordinator
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _get_thermostat(api, serial_number):
|
||||
def _get_thermostat(api: nuheat.NuHeat, serial_number: str) -> nuheat.NuHeatThermostat:
|
||||
"""Authenticate and create the thermostat object."""
|
||||
api.authenticate()
|
||||
return api.get_thermostat(serial_number)
|
||||
@@ -29,9 +28,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
|
||||
conf = entry.data
|
||||
|
||||
username = conf[CONF_USERNAME]
|
||||
password = conf[CONF_PASSWORD]
|
||||
serial_number = conf[CONF_SERIAL_NUMBER]
|
||||
username: str = conf[CONF_USERNAME]
|
||||
password: str = conf[CONF_PASSWORD]
|
||||
serial_number: str = conf[CONF_SERIAL_NUMBER]
|
||||
|
||||
api = nuheat.NuHeat(username, password)
|
||||
|
||||
@@ -53,18 +52,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
_LOGGER.error("Failed to login to nuheat: %s", ex)
|
||||
return False
|
||||
|
||||
async def _async_update_data():
|
||||
"""Fetch data from API endpoint."""
|
||||
await hass.async_add_executor_job(thermostat.get_data)
|
||||
|
||||
coordinator = DataUpdateCoordinator(
|
||||
hass,
|
||||
_LOGGER,
|
||||
config_entry=entry,
|
||||
name=f"nuheat {serial_number}",
|
||||
update_method=_async_update_data,
|
||||
update_interval=timedelta(minutes=5),
|
||||
)
|
||||
coordinator = NuHeatCoordinator(hass, entry, thermostat)
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
hass.data[DOMAIN][entry.entry_id] = (thermostat, coordinator)
|
||||
|
||||
@@ -27,6 +27,7 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN, MANUFACTURER, NUHEAT_API_STATE_SHIFT_DELAY
|
||||
from .coordinator import NuHeatCoordinator
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -69,7 +70,7 @@ async def async_setup_entry(
|
||||
async_add_entities([entity], True)
|
||||
|
||||
|
||||
class NuHeatThermostat(CoordinatorEntity, ClimateEntity):
|
||||
class NuHeatThermostat(CoordinatorEntity[NuHeatCoordinator], ClimateEntity):
|
||||
"""Representation of a NuHeat Thermostat."""
|
||||
|
||||
_attr_hvac_modes = OPERATION_LIST
|
||||
|
||||
42
homeassistant/components/nuheat/coordinator.py
Normal file
42
homeassistant/components/nuheat/coordinator.py
Normal file
@@ -0,0 +1,42 @@
|
||||
"""DataUpdateCoordinator for NuHeat thermostats."""
|
||||
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
import nuheat
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from .const import CONF_SERIAL_NUMBER
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
SCAN_INTERVAL = timedelta(minutes=5)
|
||||
|
||||
|
||||
class NuHeatCoordinator(DataUpdateCoordinator[None]):
|
||||
"""Coordinator for NuHeat thermostat data."""
|
||||
|
||||
config_entry: ConfigEntry
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
thermostat: nuheat.NuHeatThermostat,
|
||||
) -> None:
|
||||
"""Initialize the coordinator."""
|
||||
super().__init__(
|
||||
hass,
|
||||
_LOGGER,
|
||||
config_entry=entry,
|
||||
name=f"nuheat {entry.data[CONF_SERIAL_NUMBER]}",
|
||||
update_interval=SCAN_INTERVAL,
|
||||
)
|
||||
self.thermostat = thermostat
|
||||
|
||||
async def _async_update_data(self) -> None:
|
||||
"""Fetch data from API endpoint."""
|
||||
await self.hass.async_add_executor_job(self.thermostat.get_data)
|
||||
Reference in New Issue
Block a user