Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions homeassistant/components/plugwise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import voluptuous as vol

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_SCAN_INTERVAL
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr
Expand All @@ -20,7 +21,7 @@
UpdateFailed,
)

from .const import DOMAIN
from .const import COORDINATOR, DEFAULT_SCAN_INTERVAL, DOMAIN, UNDO_UPDATE_LISTENER

CONFIG_SCHEMA = vol.Schema({DOMAIN: vol.Schema({})}, extra=vol.ALLOW_EXTRA)

Expand All @@ -39,7 +40,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Plugwise Smiles from a config entry."""
websession = async_get_clientsession(hass, verify_ssl=False)
api = Smile(
host=entry.data["host"], password=entry.data["password"], websession=websession
host=entry.data[CONF_HOST],
password=entry.data[CONF_PASSWORD],
websession=websession,
)

try:
Expand All @@ -61,9 +64,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
_LOGGER.error("Timeout while connecting to Smile")
raise ConfigEntryNotReady from err

update_interval = timedelta(seconds=60)
if api.smile_type == "power":
update_interval = timedelta(seconds=10)
update_interval = timedelta(
seconds=entry.options.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL[api.smile_type]
)
)

async def async_update_data():
"""Update data via API endpoint."""
Expand All @@ -89,9 +94,12 @@ async def async_update_data():

api.get_all_devices()

undo_listener = entry.add_update_listener(_update_listener)

hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
"api": api,
"coordinator": coordinator,
COORDINATOR: coordinator,
UNDO_UPDATE_LISTENER: undo_listener,
}

device_registry = await dr.async_get_registry(hass)
Expand All @@ -118,6 +126,14 @@ async def async_update_data():
return True


async def _update_listener(hass: HomeAssistant, entry: ConfigEntry):
"""Handle options update."""
coordinator = hass.data[DOMAIN][entry.entry_id][COORDINATOR]
coordinator.update_interval = timedelta(
seconds=entry.options.get(CONF_SCAN_INTERVAL)
)


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry."""
unload_ok = all(
Expand All @@ -128,6 +144,9 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
]
)
)

hass.data[DOMAIN][entry.entry_id][UNDO_UPDATE_LISTENER]()

if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)

Expand Down
11 changes: 9 additions & 2 deletions homeassistant/components/plugwise/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.core import callback

from .const import DOMAIN, FLAME_ICON, FLOW_OFF_ICON, FLOW_ON_ICON, IDLE_ICON
from .const import (
COORDINATOR,
DOMAIN,
FLAME_ICON,
FLOW_OFF_ICON,
FLOW_ON_ICON,
IDLE_ICON,
)
from .sensor import SmileSensor

BINARY_SENSOR_MAP = {
Expand All @@ -20,7 +27,7 @@
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the Smile binary_sensors from a config entry."""
api = hass.data[DOMAIN][config_entry.entry_id]["api"]
coordinator = hass.data[DOMAIN][config_entry.entry_id]["coordinator"]
coordinator = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR]

entities = []

Expand Down
11 changes: 9 additions & 2 deletions homeassistant/components/plugwise/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@
from homeassistant.core import callback

from . import SmileGateway
from .const import DEFAULT_MAX_TEMP, DEFAULT_MIN_TEMP, DOMAIN, SCHEDULE_OFF, SCHEDULE_ON
from .const import (
COORDINATOR,
DEFAULT_MAX_TEMP,
DEFAULT_MIN_TEMP,
DOMAIN,
SCHEDULE_OFF,
SCHEDULE_ON,
)

HVAC_MODES_HEAT_ONLY = [HVAC_MODE_HEAT, HVAC_MODE_AUTO]
HVAC_MODES_HEAT_COOL = [HVAC_MODE_HEAT_COOL, HVAC_MODE_AUTO]
Expand All @@ -32,7 +39,7 @@
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the Smile Thermostats from a config entry."""
api = hass.data[DOMAIN][config_entry.entry_id]["api"]
coordinator = hass.data[DOMAIN][config_entry.entry_id]["coordinator"]
coordinator = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR]

entities = []
thermostat_classes = [
Expand Down
43 changes: 38 additions & 5 deletions homeassistant/components/plugwise/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import voluptuous as vol

from homeassistant import config_entries, core, exceptions
from homeassistant.const import CONF_HOST, CONF_PASSWORD
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_SCAN_INTERVAL
from homeassistant.core import callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.typing import DiscoveryInfoType

from .const import DOMAIN # pylint:disable=unused-import
from .const import DEFAULT_SCAN_INTERVAL, DOMAIN # pylint:disable=unused-import

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -56,7 +57,7 @@ async def validate_input(hass: core.HomeAssistant, data):
return api


class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
class PlugwiseConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Plugwise Smile."""

VERSION = 1
Expand Down Expand Up @@ -98,7 +99,6 @@ async def async_step_user(self, user_input=None):
try:
api = await validate_input(self.hass, user_input)

return self.async_create_entry(title=api.smile_name, data=user_input)
except CannotConnect:
errors["base"] = "cannot_connect"
except InvalidAuth:
Expand All @@ -109,13 +109,46 @@ async def async_step_user(self, user_input=None):

if not errors:
await self.async_set_unique_id(api.gateway_id)
self._abort_if_unique_id_configured()

return self.async_create_entry(title=api.smile_name, data=user_input)

return self.async_show_form(
step_id="user", data_schema=_base_schema(self.discovery_info), errors=errors
step_id="user",
data_schema=_base_schema(self.discovery_info),
errors=errors or {},
)

@staticmethod
@callback
def async_get_options_flow(config_entry):
"""Get the options flow for this handler."""
return PlugwiseOptionsFlowHandler(config_entry)


class PlugwiseOptionsFlowHandler(config_entries.OptionsFlow):
"""Plugwise option flow."""

def __init__(self, config_entry):
"""Initialize options flow."""
self.config_entry = config_entry

async def async_step_init(self, user_input=None):
"""Manage the Plugwise options."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)

api = self.hass.data[DOMAIN][self.config_entry.entry_id]["api"]
interval = DEFAULT_SCAN_INTERVAL[api.smile_type]
data = {
vol.Optional(
CONF_SCAN_INTERVAL,
default=self.config_entry.options.get(CONF_SCAN_INTERVAL, interval),
): int
}

return self.async_show_form(step_id="init", data_schema=vol.Schema(data))


class CannotConnect(exceptions.HomeAssistantError):
"""Error to indicate we cannot connect."""
Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/plugwise/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@
IDLE_ICON = "mdi:circle-off-outline"
FLOW_OFF_ICON = "mdi:water-pump-off"
FLOW_ON_ICON = "mdi:water-pump"

UNDO_UPDATE_LISTENER = "undo_update_listener"
COORDINATOR = "coordinator"
3 changes: 2 additions & 1 deletion homeassistant/components/plugwise/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from . import SmileGateway
from .const import (
COOL_ICON,
COORDINATOR,
DEVICE_STATE,
DOMAIN,
FLAME_ICON,
Expand Down Expand Up @@ -168,7 +169,7 @@
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the Smile sensors from a config entry."""
api = hass.data[DOMAIN][config_entry.entry_id]["api"]
coordinator = hass.data[DOMAIN][config_entry.entry_id]["coordinator"]
coordinator = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR]

entities = []
all_devices = api.get_all_devices()
Expand Down
10 changes: 10 additions & 0 deletions homeassistant/components/plugwise/strings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
{
"options": {
"step": {
"init": {
"description": "Adjust Plugwise Options",
"data": {
"scan_interval": "Scan Interval (seconds)"
}
}
}
},
"config": {
"step": {
"user": {
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/plugwise/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
from homeassistant.core import callback

from . import SmileGateway
from .const import DOMAIN
from .const import COORDINATOR, DOMAIN

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the Smile switches from a config entry."""
api = hass.data[DOMAIN][config_entry.entry_id]["api"]
coordinator = hass.data[DOMAIN][config_entry.entry_id]["coordinator"]
coordinator = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR]

entities = []
all_devices = api.get_all_devices()
Expand Down
Loading