add constant CONF_SLEEP_PERIOD (#59195)

This commit is contained in:
Michael 2021-11-06 16:32:58 +01:00 committed by GitHub
parent a9c5f68d64
commit 6a149706ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 15 deletions

View File

@ -36,6 +36,7 @@ from .const import (
BATTERY_DEVICES_WITH_PERMANENT_CONNECTION, BATTERY_DEVICES_WITH_PERMANENT_CONNECTION,
BLOCK, BLOCK,
CONF_COAP_PORT, CONF_COAP_PORT,
CONF_SLEEP_PERIOD,
DATA_CONFIG_ENTRY, DATA_CONFIG_ENTRY,
DEFAULT_COAP_PORT, DEFAULT_COAP_PORT,
DEVICE, DEVICE,
@ -143,7 +144,7 @@ async def async_setup_block_entry(hass: HomeAssistant, entry: ConfigEntry) -> bo
if device_entry and entry.entry_id not in device_entry.config_entries: if device_entry and entry.entry_id not in device_entry.config_entries:
device_entry = None device_entry = None
sleep_period = entry.data.get("sleep_period") sleep_period = entry.data.get(CONF_SLEEP_PERIOD)
@callback @callback
def _async_device_online(_: Any) -> None: def _async_device_online(_: Any) -> None:
@ -152,7 +153,7 @@ async def async_setup_block_entry(hass: HomeAssistant, entry: ConfigEntry) -> bo
if sleep_period is None: if sleep_period is None:
data = {**entry.data} data = {**entry.data}
data["sleep_period"] = get_block_device_sleep_period(device.settings) data[CONF_SLEEP_PERIOD] = get_block_device_sleep_period(device.settings)
data["model"] = device.settings["device"]["type"] data["model"] = device.settings["device"]["type"]
hass.config_entries.async_update_entry(entry, data=data) hass.config_entries.async_update_entry(entry, data=data)
@ -194,7 +195,7 @@ async def async_block_device_setup(
platforms = BLOCK_SLEEPING_PLATFORMS platforms = BLOCK_SLEEPING_PLATFORMS
if not entry.data.get("sleep_period"): if not entry.data.get(CONF_SLEEP_PERIOD):
hass.data[DOMAIN][DATA_CONFIG_ENTRY][entry.entry_id][ hass.data[DOMAIN][DATA_CONFIG_ENTRY][entry.entry_id][
REST REST
] = ShellyDeviceRestWrapper(hass, device) ] = ShellyDeviceRestWrapper(hass, device)
@ -239,7 +240,7 @@ class BlockDeviceWrapper(update_coordinator.DataUpdateCoordinator):
"""Initialize the Shelly device wrapper.""" """Initialize the Shelly device wrapper."""
self.device_id: str | None = None self.device_id: str | None = None
if sleep_period := entry.data["sleep_period"]: if sleep_period := entry.data[CONF_SLEEP_PERIOD]:
update_interval = SLEEP_PERIOD_MULTIPLIER * sleep_period update_interval = SLEEP_PERIOD_MULTIPLIER * sleep_period
else: else:
update_interval = ( update_interval = (
@ -369,7 +370,7 @@ class BlockDeviceWrapper(update_coordinator.DataUpdateCoordinator):
async def _async_update_data(self) -> None: async def _async_update_data(self) -> None:
"""Fetch data.""" """Fetch data."""
if sleep_period := self.entry.data.get("sleep_period"): if sleep_period := self.entry.data.get(CONF_SLEEP_PERIOD):
# Sleeping device, no point polling it, just mark it unavailable # Sleeping device, no point polling it, just mark it unavailable
raise update_coordinator.UpdateFailed( raise update_coordinator.UpdateFailed(
f"Sleeping device did not update within {sleep_period} seconds interval" f"Sleeping device did not update within {sleep_period} seconds interval"
@ -477,7 +478,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
platforms = BLOCK_SLEEPING_PLATFORMS platforms = BLOCK_SLEEPING_PLATFORMS
if not entry.data.get("sleep_period"): if not entry.data.get(CONF_SLEEP_PERIOD):
hass.data[DOMAIN][DATA_CONFIG_ENTRY][entry.entry_id][REST] = None hass.data[DOMAIN][DATA_CONFIG_ENTRY][entry.entry_id][REST] = None
platforms = BLOCK_PLATFORMS platforms = BLOCK_PLATFORMS

View File

@ -17,6 +17,7 @@ from homeassistant.components.binary_sensor import (
STATE_ON, STATE_ON,
BinarySensorEntity, BinarySensorEntity,
) )
from homeassistant.components.shelly.const import CONF_SLEEP_PERIOD
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ENTITY_CATEGORY_DIAGNOSTIC from homeassistant.const import ENTITY_CATEGORY_DIAGNOSTIC
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -174,7 +175,7 @@ async def async_setup_entry(
hass, config_entry, async_add_entities, RPC_SENSORS, RpcBinarySensor hass, config_entry, async_add_entities, RPC_SENSORS, RpcBinarySensor
) )
if config_entry.data["sleep_period"]: if config_entry.data[CONF_SLEEP_PERIOD]:
await async_setup_entry_attribute_entities( await async_setup_entry_attribute_entities(
hass, hass,
config_entry, config_entry,

View File

@ -20,7 +20,7 @@ from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import aiohttp_client from homeassistant.helpers import aiohttp_client
from homeassistant.helpers.typing import DiscoveryInfoType from homeassistant.helpers.typing import DiscoveryInfoType
from .const import AIOSHELLY_DEVICE_TIMEOUT_SEC, DOMAIN from .const import AIOSHELLY_DEVICE_TIMEOUT_SEC, CONF_SLEEP_PERIOD, DOMAIN
from .utils import ( from .utils import (
get_block_device_name, get_block_device_name,
get_block_device_sleep_period, get_block_device_sleep_period,
@ -63,7 +63,7 @@ async def validate_input(
await rpc_device.shutdown() await rpc_device.shutdown()
return { return {
"title": get_rpc_device_name(rpc_device), "title": get_rpc_device_name(rpc_device),
"sleep_period": 0, CONF_SLEEP_PERIOD: 0,
"model": rpc_device.model, "model": rpc_device.model,
"gen": 2, "gen": 2,
} }
@ -78,7 +78,7 @@ async def validate_input(
block_device.shutdown() block_device.shutdown()
return { return {
"title": get_block_device_name(block_device), "title": get_block_device_name(block_device),
"sleep_period": get_block_device_sleep_period(block_device.settings), CONF_SLEEP_PERIOD: get_block_device_sleep_period(block_device.settings),
"model": block_device.model, "model": block_device.model,
"gen": 1, "gen": 1,
} }
@ -130,7 +130,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
title=device_info["title"], title=device_info["title"],
data={ data={
**user_input, **user_input,
"sleep_period": device_info["sleep_period"], CONF_SLEEP_PERIOD: device_info[CONF_SLEEP_PERIOD],
"model": device_info["model"], "model": device_info["model"],
"gen": device_info["gen"], "gen": device_info["gen"],
}, },
@ -166,7 +166,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
data={ data={
**user_input, **user_input,
CONF_HOST: self.host, CONF_HOST: self.host,
"sleep_period": device_info["sleep_period"], CONF_SLEEP_PERIOD: device_info[CONF_SLEEP_PERIOD],
"model": device_info["model"], "model": device_info["model"],
"gen": device_info["gen"], "gen": device_info["gen"],
}, },
@ -224,7 +224,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
title=self.device_info["title"], title=self.device_info["title"],
data={ data={
"host": self.host, "host": self.host,
"sleep_period": self.device_info["sleep_period"], CONF_SLEEP_PERIOD: self.device_info[CONF_SLEEP_PERIOD],
"model": self.device_info["model"], "model": self.device_info["model"],
"gen": self.device_info["gen"], "gen": self.device_info["gen"],
}, },

View File

@ -54,6 +54,7 @@ AIOSHELLY_DEVICE_TIMEOUT_SEC: Final = 10
# Multiplier used to calculate the "update_interval" for sleeping devices. # Multiplier used to calculate the "update_interval" for sleeping devices.
SLEEP_PERIOD_MULTIPLIER: Final = 1.2 SLEEP_PERIOD_MULTIPLIER: Final = 1.2
CONF_SLEEP_PERIOD: Final = "sleep_period"
# Multiplier used to calculate the "update_interval" for non-sleeping devices. # Multiplier used to calculate the "update_interval" for non-sleeping devices.
UPDATE_PERIOD_MULTIPLIER: Final = 2.2 UPDATE_PERIOD_MULTIPLIER: Final = 2.2

View File

@ -23,7 +23,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType from homeassistant.helpers.typing import StateType
from .const import SHAIR_MAX_WORK_HOURS from .const import CONF_SLEEP_PERIOD, SHAIR_MAX_WORK_HOURS
from .entity import ( from .entity import (
BlockAttributeDescription, BlockAttributeDescription,
RestAttributeDescription, RestAttributeDescription,
@ -317,7 +317,7 @@ async def async_setup_entry(
hass, config_entry, async_add_entities, RPC_SENSORS, RpcSensor hass, config_entry, async_add_entities, RPC_SENSORS, RpcSensor
) )
if config_entry.data["sleep_period"]: if config_entry.data[CONF_SLEEP_PERIOD]:
await async_setup_entry_attribute_entities( await async_setup_entry_attribute_entities(
hass, config_entry, async_add_entities, SENSORS, BlockSleepingSensor hass, config_entry, async_add_entities, SENSORS, BlockSleepingSensor
) )