mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Code quality custom service for sensibo (#65496)
This commit is contained in:
parent
63680f0b36
commit
3f57adf475
@ -26,7 +26,6 @@ from homeassistant.components.climate.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID,
|
|
||||||
ATTR_STATE,
|
ATTR_STATE,
|
||||||
ATTR_TEMPERATURE,
|
ATTR_TEMPERATURE,
|
||||||
CONF_API_KEY,
|
CONF_API_KEY,
|
||||||
@ -34,9 +33,9 @@ from homeassistant.const import (
|
|||||||
TEMP_CELSIUS,
|
TEMP_CELSIUS,
|
||||||
TEMP_FAHRENHEIT,
|
TEMP_FAHRENHEIT,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, ServiceCall
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv, entity_platform
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
@ -55,10 +54,6 @@ PLATFORM_SCHEMA = PARENT_PLATFORM_SCHEMA.extend(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
ASSUME_STATE_SCHEMA = vol.Schema(
|
|
||||||
{vol.Optional(ATTR_ENTITY_ID): cv.entity_ids, vol.Required(ATTR_STATE): cv.string}
|
|
||||||
)
|
|
||||||
|
|
||||||
FIELD_TO_FLAG = {
|
FIELD_TO_FLAG = {
|
||||||
"fanLevel": SUPPORT_FAN_MODE,
|
"fanLevel": SUPPORT_FAN_MODE,
|
||||||
"swing": SUPPORT_SWING_MODE,
|
"swing": SUPPORT_SWING_MODE,
|
||||||
@ -112,28 +107,13 @@ async def async_setup_entry(
|
|||||||
|
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
async def async_assume_state(service: ServiceCall) -> None:
|
platform = entity_platform.async_get_current_platform()
|
||||||
"""Set state according to external service call.."""
|
platform.async_register_entity_service(
|
||||||
if entity_ids := service.data.get(ATTR_ENTITY_ID):
|
|
||||||
target_climate = [
|
|
||||||
entity for entity in entities if entity.entity_id in entity_ids
|
|
||||||
]
|
|
||||||
else:
|
|
||||||
target_climate = entities
|
|
||||||
|
|
||||||
update_tasks = []
|
|
||||||
for climate in target_climate:
|
|
||||||
await climate.async_assume_state(service.data.get(ATTR_STATE))
|
|
||||||
update_tasks.append(climate.async_update_ha_state(True))
|
|
||||||
|
|
||||||
if update_tasks:
|
|
||||||
await asyncio.wait(update_tasks)
|
|
||||||
|
|
||||||
hass.services.async_register(
|
|
||||||
DOMAIN,
|
|
||||||
SERVICE_ASSUME_STATE,
|
SERVICE_ASSUME_STATE,
|
||||||
async_assume_state,
|
{
|
||||||
schema=ASSUME_STATE_SCHEMA,
|
vol.Required(ATTR_STATE): vol.In(["on", "off"]),
|
||||||
|
},
|
||||||
|
"async_assume_state",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -364,7 +344,5 @@ class SensiboClimate(CoordinatorEntity, ClimateEntity):
|
|||||||
|
|
||||||
async def async_assume_state(self, state) -> None:
|
async def async_assume_state(self, state) -> None:
|
||||||
"""Sync state with api."""
|
"""Sync state with api."""
|
||||||
if state == self.state or (state == "on" and self.state != HVAC_MODE_OFF):
|
|
||||||
return
|
|
||||||
await self._async_set_ac_state_property("on", state != HVAC_MODE_OFF, True)
|
await self._async_set_ac_state_property("on", state != HVAC_MODE_OFF, True)
|
||||||
await self.coordinator.async_refresh()
|
await self.coordinator.async_refresh()
|
||||||
|
@ -1,14 +1,11 @@
|
|||||||
assume_state:
|
assume_state:
|
||||||
name: Assume state
|
name: Assume state
|
||||||
description: Set Sensibo device to external state.
|
description: Set Sensibo device to external state.
|
||||||
|
target:
|
||||||
|
entity:
|
||||||
|
integration: sensibo
|
||||||
|
domain: climate
|
||||||
fields:
|
fields:
|
||||||
entity_id:
|
|
||||||
name: Entity
|
|
||||||
description: Name(s) of entities to change.
|
|
||||||
selector:
|
|
||||||
entity:
|
|
||||||
integration: sensibo
|
|
||||||
domain: climate
|
|
||||||
state:
|
state:
|
||||||
name: State
|
name: State
|
||||||
description: State to set.
|
description: State to set.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user