Skip to content

Commit

Permalink
Move modern_forms coordinator to separate module (#117610)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored May 17, 2024
1 parent 658c1f3 commit 081bf1c
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 75 deletions.
48 changes: 3 additions & 45 deletions homeassistant/components/modern_forms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,25 @@
from __future__ import annotations

from collections.abc import Callable, Coroutine
from datetime import timedelta
import logging
from typing import Any, Concatenate, ParamSpec, TypeVar

from aiomodernforms import (
ModernFormsConnectionError,
ModernFormsDevice,
ModernFormsError,
)
from aiomodernforms.models import Device as ModernFormsDeviceState
from aiomodernforms import ModernFormsConnectionError, ModernFormsError

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
UpdateFailed,
)
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN
from .coordinator import ModernFormsDataUpdateCoordinator

_ModernFormsDeviceEntityT = TypeVar(
"_ModernFormsDeviceEntityT", bound="ModernFormsDeviceEntity"
)
_P = ParamSpec("_P")

SCAN_INTERVAL = timedelta(seconds=5)
PLATFORMS = [
Platform.BINARY_SENSOR,
Platform.FAN,
Expand Down Expand Up @@ -99,37 +88,6 @@ async def handler(
return handler


class ModernFormsDataUpdateCoordinator(DataUpdateCoordinator[ModernFormsDeviceState]): # pylint: disable=hass-enforce-coordinator-module
"""Class to manage fetching Modern Forms data from single endpoint."""

def __init__(
self,
hass: HomeAssistant,
*,
host: str,
) -> None:
"""Initialize global Modern Forms data updater."""
self.modern_forms = ModernFormsDevice(
host, session=async_get_clientsession(hass)
)

super().__init__(
hass,
_LOGGER,
name=DOMAIN,
update_interval=SCAN_INTERVAL,
)

async def _async_update_data(self) -> ModernFormsDevice:
"""Fetch data from Modern Forms."""
try:
return await self.modern_forms.update(
full_update=not self.last_update_success
)
except ModernFormsError as error:
raise UpdateFailed(f"Invalid response from API: {error}") from error


class ModernFormsDeviceEntity(CoordinatorEntity[ModernFormsDataUpdateCoordinator]):
"""Defines a Modern Forms device entity."""

Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/modern_forms/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import dt as dt_util

from . import ModernFormsDataUpdateCoordinator, ModernFormsDeviceEntity
from . import ModernFormsDeviceEntity
from .const import CLEAR_TIMER, DOMAIN
from .coordinator import ModernFormsDataUpdateCoordinator


async def async_setup_entry(
Expand Down
49 changes: 49 additions & 0 deletions homeassistant/components/modern_forms/coordinator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Coordinator for the Modern Forms integration."""

from __future__ import annotations

from datetime import timedelta
import logging

from aiomodernforms import ModernFormsDevice, ModernFormsError
from aiomodernforms.models import Device as ModernFormsDeviceState

from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import DOMAIN

SCAN_INTERVAL = timedelta(seconds=5)
_LOGGER = logging.getLogger(__name__)


class ModernFormsDataUpdateCoordinator(DataUpdateCoordinator[ModernFormsDeviceState]):
"""Class to manage fetching Modern Forms data from single endpoint."""

def __init__(
self,
hass: HomeAssistant,
*,
host: str,
) -> None:
"""Initialize global Modern Forms data updater."""
self.modern_forms = ModernFormsDevice(
host, session=async_get_clientsession(hass)
)

super().__init__(
hass,
_LOGGER,
name=DOMAIN,
update_interval=SCAN_INTERVAL,
)

async def _async_update_data(self) -> ModernFormsDevice:
"""Fetch data from Modern Forms."""
try:
return await self.modern_forms.update(
full_update=not self.last_update_success
)
except ModernFormsError as error:
raise UpdateFailed(f"Invalid response from API: {error}") from error
7 changes: 2 additions & 5 deletions homeassistant/components/modern_forms/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
)
from homeassistant.util.scaling import int_states_in_range

from . import (
ModernFormsDataUpdateCoordinator,
ModernFormsDeviceEntity,
modernforms_exception_handler,
)
from . import ModernFormsDeviceEntity, modernforms_exception_handler
from .const import (
ATTR_SLEEP_TIME,
CLEAR_TIMER,
Expand All @@ -32,6 +28,7 @@
SERVICE_CLEAR_FAN_SLEEP_TIMER,
SERVICE_SET_FAN_SLEEP_TIMER,
)
from .coordinator import ModernFormsDataUpdateCoordinator


async def async_setup_entry(
Expand Down
7 changes: 2 additions & 5 deletions homeassistant/components/modern_forms/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
ranged_value_to_percentage,
)

from . import (
ModernFormsDataUpdateCoordinator,
ModernFormsDeviceEntity,
modernforms_exception_handler,
)
from . import ModernFormsDeviceEntity, modernforms_exception_handler
from .const import (
ATTR_SLEEP_TIME,
CLEAR_TIMER,
Expand All @@ -31,6 +27,7 @@
SERVICE_CLEAR_LIGHT_SLEEP_TIMER,
SERVICE_SET_LIGHT_SLEEP_TIMER,
)
from .coordinator import ModernFormsDataUpdateCoordinator

BRIGHTNESS_RANGE = (1, 255)

Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/modern_forms/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
from homeassistant.helpers.typing import StateType
from homeassistant.util import dt as dt_util

from . import ModernFormsDataUpdateCoordinator, ModernFormsDeviceEntity
from . import ModernFormsDeviceEntity
from .const import CLEAR_TIMER, DOMAIN
from .coordinator import ModernFormsDataUpdateCoordinator


async def async_setup_entry(
Expand Down
7 changes: 2 additions & 5 deletions homeassistant/components/modern_forms/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import (
ModernFormsDataUpdateCoordinator,
ModernFormsDeviceEntity,
modernforms_exception_handler,
)
from . import ModernFormsDeviceEntity, modernforms_exception_handler
from .const import DOMAIN
from .coordinator import ModernFormsDataUpdateCoordinator


async def async_setup_entry(
Expand Down
6 changes: 3 additions & 3 deletions tests/components/modern_forms/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ async def test_full_zeroconf_flow_implementation(


@patch(
"homeassistant.components.modern_forms.ModernFormsDevice.update",
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.update",
side_effect=ModernFormsConnectionError,
)
async def test_connection_error(
Expand All @@ -123,7 +123,7 @@ async def test_connection_error(


@patch(
"homeassistant.components.modern_forms.ModernFormsDevice.update",
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.update",
side_effect=ModernFormsConnectionError,
)
async def test_zeroconf_connection_error(
Expand Down Expand Up @@ -151,7 +151,7 @@ async def test_zeroconf_connection_error(


@patch(
"homeassistant.components.modern_forms.ModernFormsDevice.update",
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.update",
side_effect=ModernFormsConnectionError,
)
async def test_zeroconf_confirm_connection_error(
Expand Down
10 changes: 7 additions & 3 deletions tests/components/modern_forms/test_fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ async def test_fan_error(

aioclient_mock.post("http://192.168.1.123:80/mf", text="", status=400)

with patch("homeassistant.components.modern_forms.ModernFormsDevice.update"):
with patch(
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.update"
):
await hass.services.async_call(
FAN_DOMAIN,
SERVICE_TURN_OFF,
Expand All @@ -211,9 +213,11 @@ async def test_fan_connection_error(
await init_integration(hass, aioclient_mock)

with (
patch("homeassistant.components.modern_forms.ModernFormsDevice.update"),
patch(
"homeassistant.components.modern_forms.ModernFormsDevice.fan",
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.update"
),
patch(
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.fan",
side_effect=ModernFormsConnectionError,
),
):
Expand Down
2 changes: 1 addition & 1 deletion tests/components/modern_forms/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


@patch(
"homeassistant.components.modern_forms.ModernFormsDevice.update",
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.update",
side_effect=ModernFormsConnectionError,
)
async def test_config_entry_not_ready(
Expand Down
10 changes: 7 additions & 3 deletions tests/components/modern_forms/test_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ async def test_light_error(

aioclient_mock.post("http://192.168.1.123:80/mf", text="", status=400)

with patch("homeassistant.components.modern_forms.ModernFormsDevice.update"):
with patch(
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.update"
):
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
Expand All @@ -139,9 +141,11 @@ async def test_light_connection_error(
await init_integration(hass, aioclient_mock)

with (
patch("homeassistant.components.modern_forms.ModernFormsDevice.update"),
patch(
"homeassistant.components.modern_forms.ModernFormsDevice.light",
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.update"
),
patch(
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.light",
side_effect=ModernFormsConnectionError,
),
):
Expand Down
10 changes: 7 additions & 3 deletions tests/components/modern_forms/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ async def test_switch_error(
aioclient_mock.clear_requests()
aioclient_mock.post("http://192.168.1.123:80/mf", text="", status=400)

with patch("homeassistant.components.modern_forms.ModernFormsDevice.update"):
with patch(
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.update"
):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
Expand All @@ -131,9 +133,11 @@ async def test_switch_connection_error(
await init_integration(hass, aioclient_mock)

with (
patch("homeassistant.components.modern_forms.ModernFormsDevice.update"),
patch(
"homeassistant.components.modern_forms.ModernFormsDevice.away",
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.update"
),
patch(
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.away",
side_effect=ModernFormsConnectionError,
),
):
Expand Down

0 comments on commit 081bf1c

Please sign in to comment.