Skip to content

Commit

Permalink
feat: allow to disable Ph sensor #26
Browse files Browse the repository at this point in the history
  • Loading branch information
tdragon committed Apr 18, 2024
1 parent 78209b0 commit fffa7d8
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 47 deletions.
24 changes: 13 additions & 11 deletions custom_components/reef_pi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .async_api import CannotConnect, InvalidAuth, ReefApi
from .const import (
_LOGGER,
DISABLE_PH,
DOMAIN,
HOST,
MANUFACTURER,
Expand Down Expand Up @@ -98,14 +99,14 @@ def __init__(self, hass, session, config_entry):
self.unique_id = config_entry.data[HOST]
self.hass = hass

if UPDATE_INTERVAL_CFG in config_entry.data.keys():
self.update_interval = timedelta(
seconds=config_entry.data[UPDATE_INTERVAL_CFG]
)
else:
self.update_interval = UPDATE_INTERVAL_MIN
self.update_interval = UPDATE_INTERVAL_MIN
update_interval = config_entry.options.get(
UPDATE_INTERVAL_CFG
) or config_entry.data.get(UPDATE_INTERVAL_CFG)
if update_interval is not None:
self.update_interval = timedelta(seconds=update_interval)

_LOGGER.debug(f"Update interval {self.update_interval.total_seconds()} seconds")
self.disable_ph = config_entry.options.get(DISABLE_PH) or False

self.has_temperature = False
self.has_equipment = False
Expand Down Expand Up @@ -147,14 +148,15 @@ def device_info(self) -> DeviceInfo:

async def update_capabilities(self):
_LOGGER.debug("Fetching capabilities")

def get_cabability(n):
return n in self.capabilities.keys() and self.capabilities[n]

self.capabilities = await self.api.capabilities()
if self.capabilities:
get_cabability = (
lambda n: n in self.capabilities.keys() and self.capabilities[n]
)
self.has_temperature = get_cabability("temperature")
self.has_equipment = get_cabability("equipment")
self.has_ph = get_cabability("ph")
self.has_ph = get_cabability("ph") and not self.disable_ph
self.has_pumps = get_cabability("doser")
self.has_ato = get_cabability("ato")
self.has_timers = get_cabability("timers")
Expand Down
70 changes: 61 additions & 9 deletions custom_components/reef_pi/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,33 @@

import voluptuous as vol
from homeassistant import config_entries
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import FlowResult

from .async_api import CannotConnect, InvalidAuth, ReefApi
from .const import DOMAIN, UPDATE_INTERVAL_MIN
from .const import (
DISABLE_PH,
DOMAIN,
HOST,
PASSWORD,
UPDATE_INTERVAL_CFG,
UPDATE_INTERVAL_MIN,
USER,
VERIFY_TLS,
)

_LOGGER = logging.getLogger(__name__)

STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required("host", default="https://127.0.0.1"): str, # type: ignore
vol.Required("username", default="reef-pi"): str, # type: ignore
vol.Required("password", default=""): str, # type: ignore
vol.Optional("verify", default=False): bool, # type: ignore
vol.Required(HOST, default="https://127.0.0.1"): str, # type: ignore
vol.Required(USER, default="reef-pi"): str, # type: ignore
vol.Required(PASSWORD, default=""): str, # type: ignore
vol.Optional(VERIFY_TLS, default=False): bool, # type: ignore
vol.Optional(
"update_interval", default=UPDATE_INTERVAL_MIN.total_seconds() # type: ignore
): int,
UPDATE_INTERVAL_CFG,
default=UPDATE_INTERVAL_MIN.total_seconds(), # type: ignore
): int,
}
)

Expand All @@ -49,7 +59,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):

async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> config_entries.ConfigFlowResult:
"""Handle the initial step."""
if user_input is None:
return self.async_show_form(
Expand All @@ -76,3 +86,45 @@ async def async_step_user(
return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
)

@staticmethod
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> ReefPiConfigFlowHandler:
return ReefPiConfigFlowHandler(config_entry)


class ReefPiConfigFlowHandler(config_entries.OptionsFlow):
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry

async def async_step_init(self, _user_input=None):
"""Manage the options."""
return await self.async_step_user()

async def async_step_user(self, user_input=None) -> config_entries.ConfigFlowResult:
"""Handle a flow initialized by the user."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)

update_interval = self.config_entry.options.get(UPDATE_INTERVAL_CFG)
if update_interval is None:
update_interval = self.config_entry.data.get(UPDATE_INTERVAL_CFG)

return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Optional(
UPDATE_INTERVAL_CFG,
default=update_interval, # type: ignore
): int,
vol.Optional(
DISABLE_PH,
default=self.config_entry.options.get(DISABLE_PH), # type: ignore
): bool,
}
),
)
1 change: 1 addition & 0 deletions custom_components/reef_pi/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
PASSWORD = "password"
VERIFY_TLS = "verify"
UPDATE_INTERVAL_CFG="update_interval"
DISABLE_PH = "disable_ph"
UPDATE_INTERVAL_MIN = timedelta(minutes=1)
TIMEOUT_API_SEC = 1

Expand Down
4 changes: 2 additions & 2 deletions custom_components/reef_pi/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"domain": "reef_pi",
"version": "0.3.7",
"version": "0.3.8",
"name": "Reef PI Integration",
"config_flow": true,
"documentation": "https://github.com/tdragon/reef-pi-hass-custom",
Expand All @@ -17,4 +17,4 @@
"@alex255"
],
"iot_class": "local_polling"
}
}
23 changes: 0 additions & 23 deletions custom_components/reef_pi/strings.json

This file was deleted.

18 changes: 17 additions & 1 deletion custom_components/reef_pi/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,26 @@
"host": "Host",
"password": "Password",
"username": "Username",
"verify": "Verify TLS certificate"
"verify": "Verify TLS certificate",
"update_interval": "Update interval"
}
}
}
},
"options": {
"step": {
"user": {
"data": {
"host": "Host",
"password": "Password",
"username": "Username",
"verify": "Verify TLS certificate",
"update_interval": "Update interval",
"disable_ph": "Disable pH sensor"
}
}
}

},
"title": "Reef-PI integration"
}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "reef-pi-hass-custom"
version = "0.3.7"
version = "0.3.8"
description = "HomeAssistant Reef PI Integration"
authors = [
{name = "tdragon", email = "[email protected]"},
Expand Down

0 comments on commit fffa7d8

Please sign in to comment.