Skip to content

Commit

Permalink
Add options for setting the thermostat defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
robbinjanssen committed Jan 28, 2023
1 parent da8e6da commit 720152b
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 50 deletions.
40 changes: 33 additions & 7 deletions custom_components/ojmicroline_thermostat/climate.py
@@ -1,5 +1,7 @@
"""Climate sensors for OJMicroline."""
import logging
from collections.abc import Mapping
from typing import Any

from homeassistant.components.climate import (
ClimateEntity,
Expand Down Expand Up @@ -30,7 +32,14 @@
REGULATION_VACATION,
)

from .const import DOMAIN, MANUFACTURER, PRESET_FROST_PROTECTION, PRESET_VACATION
from .const import (
CONF_COMFORT_MODE_DURATION,
CONF_USE_COMFORT_MODE,
DOMAIN,
MANUFACTURER,
PRESET_FROST_PROTECTION,
PRESET_VACATION,
)
from .coordinator import OJMicrolineDataUpdateCoordinator

MODE_LIST = [HVACMode.HEAT, HVACMode.AUTO]
Expand Down Expand Up @@ -72,7 +81,11 @@ async def async_setup_entry(
coordinator = hass.data[DOMAIN][entry.entry_id]
entities = []
for idx, _ in coordinator.data.items():
entities.append(OJMicrolineThermostat(coordinator, idx))
entities.append(
OJMicrolineThermostat(
coordinator=coordinator, idx=idx, options=entry.options
)
)
async_add_entities(entities)


Expand All @@ -90,17 +103,25 @@ class OJMicrolineThermostat(
_attr_has_entity_name = True

idx = str

def __init__(self, coordinator: OJMicrolineDataUpdateCoordinator, idx: str) -> None:
options = (Mapping[str, Any],)

def __init__(
self,
coordinator: OJMicrolineDataUpdateCoordinator,
idx: str,
options: Mapping[str, Any],
) -> None:
"""
Initialise the entity.
Args:
coordinator: The data coordinator updating the models.
idx: The identifier for this entity.
options: The options provided by the user.
"""
super().__init__(coordinator)
self.idx = idx
self.options = options
self._attr_unique_id = self.idx

@property
Expand Down Expand Up @@ -215,10 +236,15 @@ async def async_set_temperature(self, **kwargs) -> None:
if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
return

regulation_mode = REGULATION_MANUAL
if self.options.get(CONF_USE_COMFORT_MODE) is True:
regulation_mode = REGULATION_COMFORT

await self.coordinator.api.set_regulation_mode(
self.coordinator.data[self.unique_id],
REGULATION_COMFORT,
int(temperature * 100),
resource=self.coordinator.data[self.unique_id],
regulation_mode=regulation_mode,
temperature=int(temperature * 100),
duration=self.options.get(CONF_COMFORT_MODE_DURATION),
)
await self.coordinator.async_request_refresh()

Expand Down
76 changes: 73 additions & 3 deletions custom_components/ojmicroline_thermostat/config_flow.py
Expand Up @@ -2,8 +2,10 @@
from typing import Any, Optional

import voluptuous as vol
from homeassistant import config_entries
from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from ojmicroline_thermostat import (
OJMicroline,
Expand All @@ -12,11 +14,15 @@
OJMicrolineException,
OJMicrolineTimeoutException,
)
from ojmicroline_thermostat.const import COMFORT_DURATION

from .const import (
CONF_COMFORT_MODE_DURATION,
CONF_CUSTOMER_ID,
CONF_DEFAULT_CUSTOMER_ID,
CONF_DEFAULT_HOST,
CONF_USE_COMFORT_MODE,
CONFIG_FLOW_VERSION,
DOMAIN,
INTEGRATION_NAME,
)
Expand All @@ -32,10 +38,26 @@
)


class OJMicrolineFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
class OJMicrolineFlowHandler(ConfigFlow, domain=DOMAIN):
"""Handle an OJ Microline config flow."""

VERSION = 1
VERSION = CONFIG_FLOW_VERSION

@staticmethod
@callback
def async_get_options_flow(
config_entry: ConfigEntry,
) -> OptionsFlow:
"""
Get the options flow for this handler.
Args:
config_entry: The ConfigEntry instance.
Returns:
The created config flow.
"""
return OJMicrolineOptionsFlowHandler(config_entry)

async def async_step_user(self, user_input: Optional[dict[str, Any]] = None) -> Any:
"""
Expand Down Expand Up @@ -86,3 +108,51 @@ async def async_step_user(self, user_input: Optional[dict[str, Any]] = None) ->
return self.async_show_form(
step_id="user", data_schema=DATA_SCHEMA, errors=errors
)


class OJMicrolineOptionsFlowHandler(OptionsFlow):
"""Handle options."""

def __init__(self, config_entry: ConfigEntry) -> None:
"""
Initialize options flow.
Args:
config_entry: The ConfigEntry instance.
"""
self.config_entry = config_entry

async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""
Handle a flow initialized by the user.
Args:
user_input: The input received from the user or none.
Returns:
The created config entry.
"""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)

return self.async_show_form(
step_id="init",
data_schema=vol.Schema(
{
vol.Optional(
CONF_USE_COMFORT_MODE,
default=self.config_entry.options.get(
CONF_USE_COMFORT_MODE, False
),
): bool,
vol.Optional(
CONF_COMFORT_MODE_DURATION,
default=self.config_entry.options.get(
CONF_COMFORT_MODE_DURATION, COMFORT_DURATION
),
): vol.All(vol.Coerce(int), vol.Range(min=1)),
}
),
)
3 changes: 3 additions & 0 deletions custom_components/ojmicroline_thermostat/const.py
Expand Up @@ -2,13 +2,16 @@
DOMAIN = "ojmicroline_thermostat"
MANUFACTURER = "OJ Electronics"
INTEGRATION_NAME = "OJ Microline Thermostat"
CONFIG_FLOW_VERSION = 2

API_TIMEOUT = 30
UPDATE_INTERVAL = 60

CONF_CUSTOMER_ID = "customer_id"
CONF_DEFAULT_HOST = "ocd5.azurewebsites.net"
CONF_DEFAULT_CUSTOMER_ID = 99
CONF_USE_COMFORT_MODE = "use_comfort_mode"
CONF_COMFORT_MODE_DURATION = "comfort_mode_duration"

PRESET_VACATION = "Vacation"
PRESET_FROST_PROTECTION = "Frost Protection"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ojmicroline_thermostat/manifest.json
Expand Up @@ -6,7 +6,7 @@
"documentation": "https://github.com/robbinjanssen/home-assistant-ojmicroline-thermostat",
"issue_tracker": "https://github.com/robbinjanssen/home-assistant-ojmicroline-thermostat/issues",
"requirements": [
"ojmicroline-thermostat==1.0.0"
"ojmicroline-thermostat==1.1.0"
],
"dependencies": [],
"codeowners": [
Expand Down
11 changes: 11 additions & 0 deletions custom_components/ojmicroline_thermostat/strings.json
Expand Up @@ -22,5 +22,16 @@
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_service%]"
}
},
"options": {
"step": {
"init": {
"description": "Set default options when changing the thermostat temperature.",
"data": {
"use_comfort_mode": "Set the regulation to comfort mode when changing the temperature.",
"comfort_mode_duration": "The duration in minutes the comfort mode should be enabled."
}
}
}
}
}
11 changes: 11 additions & 0 deletions custom_components/ojmicroline_thermostat/translations/en.json
Expand Up @@ -22,5 +22,16 @@
"abort": {
"already_configured": "Your credentials are already configured."
}
},
"options": {
"step": {
"init": {
"description": "Set default options when changing the thermostat temperature.",
"data": {
"use_comfort_mode": "Set the regulation to comfort mode when changing the temperature.",
"comfort_mode_duration": "The duration in minutes the comfort mode should be enabled."
}
}
}
}
}
11 changes: 11 additions & 0 deletions custom_components/ojmicroline_thermostat/translations/nl.json
Expand Up @@ -22,5 +22,16 @@
"abort": {
"already_configured": "Deze combinatie is al geconfigureerd."
}
},
"options": {
"step": {
"init": {
"description": "Stel de standaard opties in wanneer de temperatuur van de thermostaat wordt gewijzigd.",
"data": {
"use_comfort_mode": "Zet de modus naar comfort wanneer de temperatuur wijzigd.",
"comfort_mode_duration": "De totale tijd in minuten dat de comfort mode aan moet staan."
}
}
}
}
}

0 comments on commit 720152b

Please sign in to comment.