From 60778b5230fc6373f539e99f63f7e8b7367fcfe4 Mon Sep 17 00:00:00 2001 From: Jeroen ter Heerdt Date: Thu, 28 May 2020 10:49:44 -0700 Subject: [PATCH] v0.0.19 - fixing bug introduced in v0.0.18 and adding naming options to component to enable fixing of #21 --- custom_components/smart_irrigation/__init__.py | 13 ++++++++++--- custom_components/smart_irrigation/config_flow.py | 8 +++++++- custom_components/smart_irrigation/const.py | 3 ++- custom_components/smart_irrigation/sensor.py | 6 ++++-- custom_components/smart_irrigation/strings.json | 3 ++- .../smart_irrigation/translations/en.json | 3 ++- 6 files changed, 27 insertions(+), 9 deletions(-) diff --git a/custom_components/smart_irrigation/__init__.py b/custom_components/smart_irrigation/__init__.py index a651c48a..acdc42f5 100644 --- a/custom_components/smart_irrigation/__init__.py +++ b/custom_components/smart_irrigation/__init__.py @@ -62,6 +62,7 @@ CONF_SHOW_UNITS, CONF_AUTO_REFRESH, CONF_AUTO_REFRESH_TIME, + CONF_NAME, ) _LOGGER = logging.getLogger(__name__) @@ -108,6 +109,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): longitude = hass.config.as_dict().get(CONF_LONGITUDE) elevation = hass.config.as_dict().get(CONF_ELEVATION) + name = entry.title + # handle options: lead time, max duration, force_mode_duration, show units, auto refresh, auto refresh time lead_time = entry.options.get(CONF_LEAD_TIME, 0) maximum_duration = entry.options.get(CONF_MAXIMUM_DURATION, -1) @@ -138,6 +141,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): show_units=show_units, auto_refresh=auto_refresh, auto_refresh_time=auto_refresh_time, + name=name, ) await coordinator.async_refresh() @@ -155,16 +159,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): # register the services hass.services.async_register( - DOMAIN, SERVICE_RESET_BUCKET, coordinator.handle_reset_bucket + DOMAIN, f"{name}_{SERVICE_RESET_BUCKET}", coordinator.handle_reset_bucket, ) hass.services.async_register( DOMAIN, - SERVICE_CALCULATE_HOURLY_ADJUSTED_RUN_TIME, + f"{name}_{SERVICE_CALCULATE_HOURLY_ADJUSTED_RUN_TIME}", coordinator.handle_calculate_hourly_adjusted_run_time, ) hass.services.async_register( DOMAIN, - SERVICE_CALCULATE_DAILY_ADJUSTED_RUN_TIME, + f"{name}_{SERVICE_CALCULATE_DAILY_ADJUSTED_RUN_TIME}", coordinator.handle_calculate_daily_adjusted_run_time, ) @@ -224,6 +228,7 @@ def __init__( show_units, auto_refresh, auto_refresh_time, + name, ): """Initialize.""" self.api = OWMClient(api_key=api_key, longitude=longitude, latitude=latitude) @@ -245,11 +250,13 @@ def __init__( self.show_units = show_units self.auto_refresh = auto_refresh self.auto_refresh_time = auto_refresh_time + self.name = name self.platforms = [] self.bucket = 0 self.hass = hass self.entities = {} self.entry_setup_completed = False + # should this be name? or domain? super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL) # last update of the day happens at specified local time if auto_refresh is on diff --git a/custom_components/smart_irrigation/config_flow.py b/custom_components/smart_irrigation/config_flow.py index a1567322..267fcc78 100644 --- a/custom_components/smart_irrigation/config_flow.py +++ b/custom_components/smart_irrigation/config_flow.py @@ -40,6 +40,7 @@ CONF_SHOW_UNITS, CONF_AUTO_REFRESH, CONF_AUTO_REFRESH_TIME, + CONF_NAME, ) @@ -52,6 +53,7 @@ class SmartIrrigationConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN) def __init__(self): """Initialize.""" self._api_key = None + self._name = NAME self._reference_et = {} self._errors = {} @@ -66,7 +68,8 @@ async def async_step_step3(self, user_input=None): if user_input is not None: user_input[CONF_API_KEY] = self._api_key user_input[CONF_REFERENCE_ET] = self._reference_et - return self.async_create_entry(title=NAME, data=user_input) + user_input[CONF_NAME] = self._name + return self.async_create_entry(title=self._name, data=user_input) return await self._show_config_form(user_input) async def async_step_step2(self, user_input=None): @@ -125,6 +128,7 @@ async def async_step_user(self, user_input=None): if valid_api: # store values entered self._api_key = user_input[CONF_API_KEY].strip() + self._name = user_input[CONF_NAME] # show next step return await self._show_step2(user_input) else: @@ -187,6 +191,7 @@ async def _show_config_form(self, user_input): step_id="user", data_schema=vol.Schema( { + vol.Required(CONF_NAME, default=NAME): str, vol.Required(CONF_API_KEY): str, # vol.Required(CONF_REFERENCE_ET_1): vol.Coerce(float), # vol.Required(CONF_REFERENCE_ET_2): vol.Coerce(float), @@ -249,6 +254,7 @@ class SmartIrrigationOptionsFlowHandler(config_entries.OptionsFlow): def __init__(self, config_entry): """Initialize HACS options flow.""" self.config_entry = config_entry + _LOGGER.warning("config_entry: {}".format(config_entry)) self.options = dict(config_entry.options) self._errors = {} diff --git a/custom_components/smart_irrigation/const.py b/custom_components/smart_irrigation/const.py index 0fa5e96f..1e5660ee 100644 --- a/custom_components/smart_irrigation/const.py +++ b/custom_components/smart_irrigation/const.py @@ -3,7 +3,7 @@ DOMAIN = "smart_irrigation" NAME = "Smart Irrigation" DOMAIN_DATA = f"{DOMAIN}_data" -VERSION = "0.0.18" +VERSION = "0.0.19" ISSUE_URL = "https://github.com/jeroenterheerdt/HASmartIrrigation/issues" @@ -51,6 +51,7 @@ CONF_SHOW_UNITS = "show_units" CONF_AUTO_REFRESH = "auto_refresh" CONF_AUTO_REFRESH_TIME = "auto_refresh_time" +CONF_NAME = "name" # Events EVENT_BUCKET_UPDATED = "smart_irrigation_bucket_updated_event" diff --git a/custom_components/smart_irrigation/sensor.py b/custom_components/smart_irrigation/sensor.py index b634bb4d..de47626c 100644 --- a/custom_components/smart_irrigation/sensor.py +++ b/custom_components/smart_irrigation/sensor.py @@ -125,7 +125,9 @@ async def async_added_to_hass(self): try: a_val = state.attributes[a] # no split needed if we don't show units - if self.coordinator.show_units or " " in a_val: + if self.coordinator.show_units or ( + isinstance(a_val, str) and " " in a_val + ): numeric_part = a_val.split(" ")[0] else: numeric_part = float(a_val) @@ -189,7 +191,7 @@ def _bucket_updated(self, ev: Event): @property def name(self): """Return the name of the sensor.""" - return f"{DEFAULT_NAME} {self.type}" + return f"{self.coordinator.name} {self.type}" def update_state(self): """Update the state.""" diff --git a/custom_components/smart_irrigation/strings.json b/custom_components/smart_irrigation/strings.json index 635cfabc..01456a07 100644 --- a/custom_components/smart_irrigation/strings.json +++ b/custom_components/smart_irrigation/strings.json @@ -6,7 +6,8 @@ "title": "Smart Irrigation - Step 1/3: Open Weather Map API", "description": "If you need help with the configuration, please see https://github.com/jeroenterheerdt/HASmartIrrigation", "data": { - "api_key": "API key for Open Weather Map" + "api_key": "API key for Open Weather Map", + "name": "Unique name of the instance" } }, "step2": { diff --git a/custom_components/smart_irrigation/translations/en.json b/custom_components/smart_irrigation/translations/en.json index 18d458e6..70af9154 100644 --- a/custom_components/smart_irrigation/translations/en.json +++ b/custom_components/smart_irrigation/translations/en.json @@ -13,7 +13,8 @@ "title": "Smart Irrigation - Step 1/3: Open Weather Map API", "description": "If you need help with the configuration, please see https://github.com/jeroenterheerdt/HASmartIrrigation", "data": { - "api_key": "API key for Open Weather Map" + "api_key": "API key for Open Weather Map", + "name": "Unique name of the instance" } }, "step2": {