Make reproduce state use platform instead of rely on function (#25856)

* Make reproduce state use platform instead of rely on function

* Fix types

* address comment Martin.
This commit is contained in:
Paulus Schoutsen 2019-08-11 20:03:21 -07:00 committed by GitHub
parent ab7db5fbd0
commit cf90e49b50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 26 additions and 21 deletions

View file

@ -70,7 +70,6 @@ from .const import (
SUPPORT_TARGET_TEMPERATURE_RANGE, SUPPORT_TARGET_TEMPERATURE_RANGE,
SUPPORT_TARGET_TEMPERATURE, SUPPORT_TARGET_TEMPERATURE,
) )
from .reproduce_state import async_reproduce_states # noqa
DEFAULT_MIN_TEMP = 7 DEFAULT_MIN_TEMP = 7
DEFAULT_MAX_TEMP = 35 DEFAULT_MAX_TEMP = 35

View file

@ -5,7 +5,6 @@ from typing import Iterable, Optional
from homeassistant.const import ATTR_TEMPERATURE from homeassistant.const import ATTR_TEMPERATURE
from homeassistant.core import Context, State from homeassistant.core import Context, State
from homeassistant.helpers.typing import HomeAssistantType from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.loader import bind_hass
from .const import ( from .const import (
ATTR_AUX_HEAT, ATTR_AUX_HEAT,
@ -69,7 +68,6 @@ async def _async_reproduce_states(
await call_service(SERVICE_SET_HUMIDITY, [ATTR_HUMIDITY]) await call_service(SERVICE_SET_HUMIDITY, [ATTR_HUMIDITY])
@bind_hass
async def async_reproduce_states( async def async_reproduce_states(
hass: HomeAssistantType, states: Iterable[State], context: Optional[Context] = None hass: HomeAssistantType, states: Iterable[State], context: Optional[Context] = None
) -> None: ) -> None:

View file

@ -34,7 +34,6 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.config_validation import ENTITY_SERVICE_SCHEMA from homeassistant.helpers.config_validation import ENTITY_SERVICE_SCHEMA
from homeassistant.util.async_ import run_coroutine_threadsafe from homeassistant.util.async_ import run_coroutine_threadsafe
from .reproduce_state import async_reproduce_states # noqa
DOMAIN = "group" DOMAIN = "group"

View file

@ -3,10 +3,8 @@ from typing import Iterable, Optional
from homeassistant.core import Context, State from homeassistant.core import Context, State
from homeassistant.helpers.typing import HomeAssistantType from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.loader import bind_hass
@bind_hass
async def async_reproduce_states( async def async_reproduce_states(
hass: HomeAssistantType, states: Iterable[State], context: Optional[Context] = None hass: HomeAssistantType, states: Iterable[State], context: Optional[Context] = None
) -> None: ) -> None:

View file

@ -96,7 +96,6 @@ from .const import (
SUPPORT_VOLUME_SET, SUPPORT_VOLUME_SET,
SUPPORT_VOLUME_STEP, SUPPORT_VOLUME_STEP,
) )
from .reproduce_state import async_reproduce_states # noqa
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
_RND = SystemRandom() _RND = SystemRandom()

View file

@ -19,7 +19,6 @@ from homeassistant.const import (
) )
from homeassistant.core import Context, State from homeassistant.core import Context, State
from homeassistant.helpers.typing import HomeAssistantType from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.loader import bind_hass
from .const import ( from .const import (
ATTR_MEDIA_VOLUME_LEVEL, ATTR_MEDIA_VOLUME_LEVEL,
@ -89,7 +88,6 @@ async def _async_reproduce_states(
) )
@bind_hass
async def async_reproduce_states( async def async_reproduce_states(
hass: HomeAssistantType, states: Iterable[State], context: Optional[Context] = None hass: HomeAssistantType, states: Iterable[State], context: Optional[Context] = None
) -> None: ) -> None:

View file

@ -4,7 +4,7 @@ import datetime as dt
import json import json
import logging import logging
from collections import defaultdict from collections import defaultdict
from types import TracebackType from types import ModuleType, TracebackType
from typing import ( # noqa: F401 pylint: disable=unused-import from typing import ( # noqa: F401 pylint: disable=unused-import
Awaitable, Awaitable,
Dict, Dict,
@ -16,7 +16,7 @@ from typing import ( # noqa: F401 pylint: disable=unused-import
Union, Union,
) )
from homeassistant.loader import bind_hass from homeassistant.loader import bind_hass, async_get_integration, IntegrationNotFound
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from homeassistant.components.notify import ATTR_MESSAGE, SERVICE_NOTIFY from homeassistant.components.notify import ATTR_MESSAGE, SERVICE_NOTIFY
from homeassistant.components.sun import STATE_ABOVE_HORIZON, STATE_BELOW_HORIZON from homeassistant.components.sun import STATE_ABOVE_HORIZON, STATE_BELOW_HORIZON
@ -152,13 +152,27 @@ async def async_reproduce_state(
for state in states: for state in states:
to_call[state.domain].append(state) to_call[state.domain].append(state)
async def worker(domain: str, data: List[State]) -> None: async def worker(domain: str, states_by_domain: List[State]) -> None:
component = getattr(hass.components, domain) try:
if hasattr(component, "async_reproduce_states"): integration = await async_get_integration(hass, domain)
await component.async_reproduce_states(data, context=context) except IntegrationNotFound:
_LOGGER.warning(
"Trying to reproduce state for unknown integration: %s", domain
)
return
try:
platform: Optional[ModuleType] = integration.get_platform("reproduce_state")
except ImportError:
platform = None
if platform:
await platform.async_reproduce_states( # type: ignore
hass, states_by_domain, context=context
)
else: else:
await async_reproduce_state_legacy( await async_reproduce_state_legacy(
hass, domain, data, blocking=blocking, context=context hass, domain, states_by_domain, blocking=blocking, context=context
) )
if to_call: if to_call:

View file

@ -2,7 +2,7 @@
import pytest import pytest
from homeassistant.components.climate import async_reproduce_states from homeassistant.components.climate.reproduce_state import async_reproduce_states
from homeassistant.components.climate.const import ( from homeassistant.components.climate.const import (
ATTR_AUX_HEAT, ATTR_AUX_HEAT,
ATTR_HUMIDITY, ATTR_HUMIDITY,

View file

@ -2,7 +2,7 @@
from asyncio import Future from asyncio import Future
from unittest.mock import patch from unittest.mock import patch
from homeassistant.components.group import async_reproduce_states from homeassistant.components.group.reproduce_state import async_reproduce_states
from homeassistant.core import Context, State from homeassistant.core import Context, State

View file

@ -2,7 +2,7 @@
import pytest import pytest
from homeassistant.components.media_player import async_reproduce_states from homeassistant.components.media_player.reproduce_state import async_reproduce_states
from homeassistant.components.media_player.const import ( from homeassistant.components.media_player.const import (
ATTR_INPUT_SOURCE, ATTR_INPUT_SOURCE,
ATTR_MEDIA_CONTENT_ID, ATTR_MEDIA_CONTENT_ID,

View file

@ -53,13 +53,13 @@ def test_async_track_states(hass):
def test_call_to_component(hass): def test_call_to_component(hass):
"""Test calls to components state reproduction functions.""" """Test calls to components state reproduction functions."""
with patch( with patch(
("homeassistant.components.media_player." "async_reproduce_states") ("homeassistant.components.media_player.reproduce_state.async_reproduce_states")
) as media_player_fun: ) as media_player_fun:
media_player_fun.return_value = asyncio.Future() media_player_fun.return_value = asyncio.Future()
media_player_fun.return_value.set_result(None) media_player_fun.return_value.set_result(None)
with patch( with patch(
("homeassistant.components.climate." "async_reproduce_states") ("homeassistant.components.climate.reproduce_state.async_reproduce_states")
) as climate_fun: ) as climate_fun:
climate_fun.return_value = asyncio.Future() climate_fun.return_value = asyncio.Future()
climate_fun.return_value.set_result(None) climate_fun.return_value.set_result(None)