Skip to content

Commit

Permalink
Add transition support to scenes, cleanup blocking parameter (home-as…
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored Apr 21, 2020
1 parent 19be31d commit bc5a2da
Show file tree
Hide file tree
Showing 66 changed files with 547 additions and 229 deletions.
21 changes: 17 additions & 4 deletions homeassistant/components/alarm_control_panel/reproduce_state.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Reproduce an Alarm control panel state."""
import asyncio
import logging
from typing import Iterable, Optional
from typing import Any, Dict, Iterable, Optional

from homeassistant.const import (
ATTR_ENTITY_ID,
Expand Down Expand Up @@ -36,7 +36,11 @@


async def _async_reproduce_state(
hass: HomeAssistantType, state: State, context: Optional[Context] = None
hass: HomeAssistantType,
state: State,
*,
context: Optional[Context] = None,
reproduce_options: Optional[Dict[str, Any]] = None,
) -> None:
"""Reproduce a single state."""
cur_state = hass.states.get(state.entity_id)
Expand Down Expand Up @@ -76,9 +80,18 @@ async def _async_reproduce_state(


async def async_reproduce_states(
hass: HomeAssistantType, states: Iterable[State], context: Optional[Context] = None
hass: HomeAssistantType,
states: Iterable[State],
*,
context: Optional[Context] = None,
reproduce_options: Optional[Dict[str, Any]] = None,
) -> None:
"""Reproduce Alarm control panel states."""
await asyncio.gather(
*(_async_reproduce_state(hass, state, context) for state in states)
*(
_async_reproduce_state(
hass, state, context=context, reproduce_options=reproduce_options
)
for state in states
)
)
21 changes: 17 additions & 4 deletions homeassistant/components/automation/reproduce_state.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Reproduce an Automation state."""
import asyncio
import logging
from typing import Iterable, Optional
from typing import Any, Dict, Iterable, Optional

from homeassistant.const import (
ATTR_ENTITY_ID,
Expand All @@ -21,7 +21,11 @@


async def _async_reproduce_state(
hass: HomeAssistantType, state: State, context: Optional[Context] = None
hass: HomeAssistantType,
state: State,
*,
context: Optional[Context] = None,
reproduce_options: Optional[Dict[str, Any]] = None,
) -> None:
"""Reproduce a single state."""
cur_state = hass.states.get(state.entity_id)
Expand Down Expand Up @@ -53,9 +57,18 @@ async def _async_reproduce_state(


async def async_reproduce_states(
hass: HomeAssistantType, states: Iterable[State], context: Optional[Context] = None
hass: HomeAssistantType,
states: Iterable[State],
*,
context: Optional[Context] = None,
reproduce_options: Optional[Dict[str, Any]] = None,
) -> None:
"""Reproduce Automation states."""
await asyncio.gather(
*(_async_reproduce_state(hass, state, context) for state in states)
*(
_async_reproduce_state(
hass, state, context=context, reproduce_options=reproduce_options
)
for state in states
)
)
21 changes: 17 additions & 4 deletions homeassistant/components/climate/reproduce_state.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Module that groups code required to handle state restore for component."""
import asyncio
from typing import Iterable, Optional
from typing import Any, Dict, Iterable, Optional

from homeassistant.const import ATTR_TEMPERATURE
from homeassistant.core import Context, State
Expand All @@ -26,7 +26,11 @@


async def _async_reproduce_states(
hass: HomeAssistantType, state: State, context: Optional[Context] = None
hass: HomeAssistantType,
state: State,
*,
context: Optional[Context] = None,
reproduce_options: Optional[Dict[str, Any]] = None,
) -> None:
"""Reproduce component states."""

Expand Down Expand Up @@ -69,9 +73,18 @@ async def call_service(service: str, keys: Iterable, data=None):


async def async_reproduce_states(
hass: HomeAssistantType, states: Iterable[State], context: Optional[Context] = None
hass: HomeAssistantType,
states: Iterable[State],
*,
context: Optional[Context] = None,
reproduce_options: Optional[Dict[str, Any]] = None,
) -> None:
"""Reproduce component states."""
await asyncio.gather(
*(_async_reproduce_states(hass, state, context) for state in states)
*(
_async_reproduce_states(
hass, state, context=context, reproduce_options=reproduce_options
)
for state in states
)
)
21 changes: 17 additions & 4 deletions homeassistant/components/counter/reproduce_state.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Reproduce an Counter state."""
import asyncio
import logging
from typing import Iterable, Optional
from typing import Any, Dict, Iterable, Optional

from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import Context, State
Expand All @@ -21,7 +21,11 @@


async def _async_reproduce_state(
hass: HomeAssistantType, state: State, context: Optional[Context] = None
hass: HomeAssistantType,
state: State,
*,
context: Optional[Context] = None,
reproduce_options: Optional[Dict[str, Any]] = None,
) -> None:
"""Reproduce a single state."""
cur_state = hass.states.get(state.entity_id)
Expand Down Expand Up @@ -63,9 +67,18 @@ async def _async_reproduce_state(


async def async_reproduce_states(
hass: HomeAssistantType, states: Iterable[State], context: Optional[Context] = None
hass: HomeAssistantType,
states: Iterable[State],
*,
context: Optional[Context] = None,
reproduce_options: Optional[Dict[str, Any]] = None,
) -> None:
"""Reproduce Counter states."""
await asyncio.gather(
*(_async_reproduce_state(hass, state, context) for state in states)
*(
_async_reproduce_state(
hass, state, context=context, reproduce_options=reproduce_options
)
for state in states
)
)
33 changes: 24 additions & 9 deletions homeassistant/components/cover/reproduce_state.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Reproduce an Cover state."""
import asyncio
import logging
from typing import Iterable, Optional
from typing import Any, Dict, Iterable, Optional

from homeassistant.components.cover import (
ATTR_CURRENT_POSITION,
Expand Down Expand Up @@ -33,7 +33,11 @@


async def _async_reproduce_state(
hass: HomeAssistantType, state: State, context: Optional[Context] = None
hass: HomeAssistantType,
state: State,
*,
context: Optional[Context] = None,
reproduce_options: Optional[Dict[str, Any]] = None,
) -> None:
"""Reproduce a single state."""
cur_state = hass.states.get(state.entity_id)
Expand Down Expand Up @@ -61,13 +65,15 @@ async def _async_reproduce_state(
service_data = {ATTR_ENTITY_ID: state.entity_id}
service_data_tilting = {ATTR_ENTITY_ID: state.entity_id}

if cur_state.state != state.state or cur_state.attributes.get(
ATTR_CURRENT_POSITION
) != state.attributes.get(ATTR_CURRENT_POSITION):
if not (
cur_state.state == state.state
and cur_state.attributes.get(ATTR_CURRENT_POSITION)
== state.attributes.get(ATTR_CURRENT_POSITION)
):
# Open/Close
if state.state == STATE_CLOSED or state.state == STATE_CLOSING:
if state.state in [STATE_CLOSED, STATE_CLOSING]:
service = SERVICE_CLOSE_COVER
elif state.state == STATE_OPEN or state.state == STATE_OPENING:
elif state.state in [STATE_OPEN, STATE_OPENING]:
if (
ATTR_CURRENT_POSITION in cur_state.attributes
and ATTR_CURRENT_POSITION in state.attributes
Expand Down Expand Up @@ -108,10 +114,19 @@ async def _async_reproduce_state(


async def async_reproduce_states(
hass: HomeAssistantType, states: Iterable[State], context: Optional[Context] = None
hass: HomeAssistantType,
states: Iterable[State],
*,
context: Optional[Context] = None,
reproduce_options: Optional[Dict[str, Any]] = None,
) -> None:
"""Reproduce Cover states."""
# Reproduce states in parallel.
await asyncio.gather(
*(_async_reproduce_state(hass, state, context) for state in states)
*(
_async_reproduce_state(
hass, state, context=context, reproduce_options=reproduce_options
)
for state in states
)
)
9 changes: 4 additions & 5 deletions homeassistant/components/deconz/scene.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Support for deCONZ scenes."""
from typing import Any

from homeassistant.components.scene import Scene
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
Expand All @@ -18,10 +20,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
@callback
def async_add_scene(scenes):
"""Add scene from deCONZ."""
entities = []

for scene in scenes:
entities.append(DeconzScene(scene, gateway))
entities = [DeconzScene(scene, gateway) for scene in scenes]

async_add_entities(entities)

Expand Down Expand Up @@ -51,7 +50,7 @@ async def async_will_remove_from_hass(self) -> None:
del self.gateway.deconz_ids[self.entity_id]
self._scene = None

async def async_activate(self):
async def async_activate(self, **kwargs: Any) -> None:
"""Activate the scene."""
await self._scene.async_set_state({})

Expand Down
4 changes: 3 additions & 1 deletion homeassistant/components/elkm1/scene.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Support for control of ElkM1 tasks ("macros")."""
from typing import Any

from homeassistant.components.scene import Scene

from . import ElkAttachedEntity, create_elk_entities
Expand All @@ -17,6 +19,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class ElkTask(ElkAttachedEntity, Scene):
"""Elk-M1 task as scene."""

async def async_activate(self):
async def async_activate(self, **kwargs: Any) -> None:
"""Activate the task."""
self._element.activate()
21 changes: 17 additions & 4 deletions homeassistant/components/fan/reproduce_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import asyncio
import logging
from types import MappingProxyType
from typing import Iterable, Optional
from typing import Any, Dict, Iterable, Optional

from homeassistant.const import (
ATTR_ENTITY_ID,
Expand Down Expand Up @@ -35,7 +35,11 @@


async def _async_reproduce_state(
hass: HomeAssistantType, state: State, context: Optional[Context] = None
hass: HomeAssistantType,
state: State,
*,
context: Optional[Context] = None,
reproduce_options: Optional[Dict[str, Any]] = None,
) -> None:
"""Reproduce a single state."""
cur_state = hass.states.get(state.entity_id)
Expand Down Expand Up @@ -85,11 +89,20 @@ async def _async_reproduce_state(


async def async_reproduce_states(
hass: HomeAssistantType, states: Iterable[State], context: Optional[Context] = None
hass: HomeAssistantType,
states: Iterable[State],
*,
context: Optional[Context] = None,
reproduce_options: Optional[Dict[str, Any]] = None,
) -> None:
"""Reproduce Fan states."""
await asyncio.gather(
*(_async_reproduce_state(hass, state, context) for state in states)
*(
_async_reproduce_state(
hass, state, context=context, reproduce_options=reproduce_options
)
for state in states
)
)


Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/fibaro/scene.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Support for Fibaro scenes."""
import logging
from typing import Any

from homeassistant.components.scene import Scene

Expand All @@ -21,6 +22,6 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class FibaroScene(FibaroDevice, Scene):
"""Representation of a Fibaro scene entity."""

def activate(self):
def activate(self, **kwargs: Any) -> None:
"""Activate the scene."""
self.fibaro_device.start()
12 changes: 9 additions & 3 deletions homeassistant/components/group/reproduce_state.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Module that groups code required to handle state restore for component."""
from typing import Iterable, Optional
from typing import Any, Dict, Iterable, Optional

from homeassistant.core import Context, State
from homeassistant.helpers.state import async_reproduce_state
Expand All @@ -9,7 +9,11 @@


async def async_reproduce_states(
hass: HomeAssistantType, states: Iterable[State], context: Optional[Context] = None
hass: HomeAssistantType,
states: Iterable[State],
*,
context: Optional[Context] = None,
reproduce_options: Optional[Dict[str, Any]] = None,
) -> None:
"""Reproduce component states."""

Expand All @@ -27,4 +31,6 @@ async def async_reproduce_states(
context=state.context,
)
)
await async_reproduce_state(hass, states_copy, blocking=True, context=context)
await async_reproduce_state(
hass, states_copy, context=context, reproduce_options=reproduce_options
)
Loading

0 comments on commit bc5a2da

Please sign in to comment.