Skip to content

Commit

Permalink
Merge pull request #37 from hasl-sensor/dev
Browse files Browse the repository at this point in the history
Dev to main 3.0.5
  • Loading branch information
DSorlov authored Mar 17, 2022
2 parents dc670b8 + 64a78f2 commit 1e14aa8
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 89 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.0.5] (2022-03-17)

### Fixes
- Fixes [hasl-sensor/integration#36](https://github.com/hasl-sensor/integration/issues/36) introduced in 3.0.3
- Fixes [hasl-sensor/integration#35](https://github.com/hasl-sensor/integration/issues/35) anoying log message due to stupidity

### Changes
- Sensor will now show unavailable until sensor data is initiated
- Configuration now stored in data and not options inside of configuration object
- Implemented separate configuration schema versioning (restarting with schema version 2. Everyting earlier is to be treated as version 1)
- Now configures integration in one step when adding new integration
- Harmonizing version settings between slapi and core as theese will never differ anymore

## [3.0.4] (2022-03-07)

### Fixes
Expand Down Expand Up @@ -341,6 +354,8 @@ Forked from 2.2.3 but changes from later versions are implemented as needed.
- This is a great day indeed.

[keep-a-changelog]: http://keepachangelog.com/en/1.0.0/
[3.0.5]: https://github.com/hasl-sensor/integration/compare/3.0.4...3.0.5
[3.0.4]: https://github.com/hasl-sensor/integration/compare/3.0.3...3.0.4
[3.0.3]: https://github.com/hasl-sensor/integration/compare/3.0.2...3.0.3
[3.0.2]: https://github.com/hasl-sensor/integration/compare/3.0.1...3.0.2
[3.0.1]: https://github.com/hasl-sensor/integration/compare/3.0.0...3.0.1
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
![maintained](https://img.shields.io/maintenance/yes/2022.svg)
[![hacs_badge](https://img.shields.io/badge/hacs-default-green.svg)](https://github.com/custom-components/hacs)
[![ha_version](https://img.shields.io/badge/home%20assistant-2021.12%2B-green.svg)](https://www.home-assistant.io)
![version](https://img.shields.io/badge/version-3.0.4-green.svg)
![version](https://img.shields.io/badge/version-3.0.5-green.svg)
![stability-alpha](https://img.shields.io/badge/stability-stable-green.svg)
[![maintainer](https://img.shields.io/badge/maintainer-dsorlov-blue.svg)](https://github.com/DSorlov)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
Expand Down
16 changes: 13 additions & 3 deletions custom_components/hasl3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .const import (
DOMAIN,
HASL_VERSION,
SCHEMA_VERSION,
DEVICE_NAME,
DEVICE_MANUFACTURER,
DEVICE_MODEL,
Expand Down Expand Up @@ -200,12 +201,21 @@ async def eventListener(service):
async def async_migrate_entry(hass, config_entry: ConfigEntry):
logger.debug("[migrate_entry] Entered")

logger.debug("[migrate_entry] Nothing to do from version %s to version %s", config_entry.version, HASL_VERSION)
logger.debug("[migrate_entry] Migrating configuration from schema version %s to version %s", config_entry.version, SCHEMA_VERSION)

logger.debug("[migrate_entry] Completed")
data = {**config_entry.data}
for option in config_entry.options:
logger.debug(f"[migrate_entry] set {option} = {config_entry.options[option]}")
data[option]=config_entry.options[option]

return True
try:
hass.config_entries.async_update_entry(config_entry, data=data)
logger.debug("[migrate_entry] Completed")
except Exception as e:
logger.error(f"[migrate_entry] Failed: {str(e)}")
return False

return True

async def reload_entry(hass, entry):
"""Reload HASL."""
Expand Down
23 changes: 14 additions & 9 deletions custom_components/hasl3/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ async def setup_hasl_sensor(hass, config):

logger.debug("[setup_binary_sensor] Processing sensors")
if config.data[CONF_INTEGRATION_TYPE] == SENSOR_STATUS:
if not config.options[CONF_ANALOG_SENSORS]:
if CONF_TL2_KEY in config.options:
await hass.data[DOMAIN]["worker"].assert_tl2(config.options[CONF_TL2_KEY])
if not CONF_ANALOG_SENSORS in config.data:
if CONF_TL2_KEY in config.data:
await hass.data[DOMAIN]["worker"].assert_tl2(config.data[CONF_TL2_KEY])
for sensortype in CONF_TRANSPORT_MODE_LIST:
if sensortype in config.options and config.options[sensortype]:
if sensortype in config.data and config.data[sensortype]:
logger.debug("[setup_binary_sensor] Setting up binary problem sensor..")
try:
sensors.append(HASLTrafficProblemSensor(hass, config, sensortype))
Expand Down Expand Up @@ -86,20 +86,20 @@ def __init__(self, hass, config, sensortype):
self._hass = hass
self._config = config
self._sensortype = sensortype
self._enabled_sensor = config.options[CONF_SENSOR]
self._enabled_sensor = config.data[CONF_SENSOR]
self._name = f"SL {self._sensortype.capitalize()} Problem Sensor ({self._config.title})"
self._sensordata = []
self._scan_interval = self._config.options[CONF_SCAN_INTERVAL] or 300
self._scan_interval = self._config.data[CONF_SCAN_INTERVAL] or 300
self._worker = hass.data[DOMAIN]["worker"]

async def async_update(self):
"""Update the sensor."""

logger.debug("[async_update] Entered")
logger.debug(f"[async_update] Processing {self._name}")
if self._worker.data.tl2[self._config.options[CONF_TL2_KEY]]["api_lastrun"]:
if self._worker.data.tl2[self._config.data[CONF_TL2_KEY]]["api_lastrun"]:
if self._worker.checksensorstate(self._enabled_sensor, STATE_ON):
if self._worker.getminutesdiff(now().strftime('%Y-%m-%d %H:%M:%S'), self._worker.data.tl2[self._config.options[CONF_TL2_KEY]]["api_lastrun"]) > self._config.options[CONF_SCAN_INTERVAL]:
if self._worker.getminutesdiff(now().strftime('%Y-%m-%d %H:%M:%S'), self._worker.data.tl2[self._config.data[CONF_TL2_KEY]]["api_lastrun"]) > self._config.data[CONF_SCAN_INTERVAL]:
try:
await self._worker.process_tl2()
logger.debug("[async_update] Update processed")
Expand All @@ -108,7 +108,7 @@ async def async_update(self):
else:
logger.debug("[async_update] Not due for update, skipping")

self._sensordata = self._worker.data.tl2[self._config.options[CONF_TL2_KEY]]
self._sensordata = self._worker.data.tl2[self._config.data[CONF_TL2_KEY]]
logger.debug("[async_update] Completed")

@property
Expand Down Expand Up @@ -170,6 +170,11 @@ def scan_interval(self):
"""Return the unique id."""
return self._scan_interval

@property
def available(self):
"""Return true if value is valid."""
return self._sensordata != []

@property
def extra_state_attributes(self):
"""Attributes."""
Expand Down
87 changes: 70 additions & 17 deletions custom_components/hasl3/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from .const import (
DOMAIN,
HASL_VERSION,
SCHEMA_VERSION,
CONF_NAME,
SENSOR_STANDARD,
SENSOR_STATUS,
Expand All @@ -36,7 +36,7 @@
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for HASL."""

VERSION = HASL_VERSION
VERSION = SCHEMA_VERSION
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL

# FIXME: DOES NOT ACTUALLY VALIDATE ANYTHING! WE NEED THIS! =)
Expand All @@ -48,6 +48,11 @@ async def validate_input(self, data):

return data

async def validate_config(self, data):
"""Validate input in step config"""

return data

async def async_step_user(self, user_input):
"""Handle the initial step."""
logger.debug("[setup_integration] Entered")
Expand Down Expand Up @@ -75,18 +80,66 @@ async def async_step_user(self, user_input):
id = str(uuid.uuid4())
await self.async_set_unique_id(id)
user_input[CONF_INTEGRATION_ID] = id
self._userdata = user_input

if user_input[CONF_INTEGRATION_TYPE] == SENSOR_STANDARD:
schema = standard_config_option_schema()
if user_input[CONF_INTEGRATION_TYPE] == SENSOR_STATUS:
schema = status_config_option_schema()
if user_input[CONF_INTEGRATION_TYPE] == SENSOR_VEHICLE_LOCATION:
schema = vehiclelocation_config_option_schema()
if user_input[CONF_INTEGRATION_TYPE] == SENSOR_DEVIATION:
schema = deviation_config_option_schema()
if user_input[CONF_INTEGRATION_TYPE] == SENSOR_ROUTE:
schema = route_config_option_schema()

return self.async_show_form(step_id="config", data_schema=voluptuous.Schema(schema), errors=errors)

async def async_step_config(self, user_input):
"""Handle a flow initialized by the user."""
logger.debug("[setup_integration_config] Entered")
errors = {}

name = user_input[CONF_NAME]
del user_input[CONF_NAME]
if self._userdata[CONF_INTEGRATION_TYPE] == SENSOR_STANDARD:
schema = standard_config_option_schema(user_input)
if self._userdata[CONF_INTEGRATION_TYPE] == SENSOR_STATUS:
schema = status_config_option_schema(user_input)
if self._userdata[CONF_INTEGRATION_TYPE] == SENSOR_VEHICLE_LOCATION:
schema = vehiclelocation_config_option_schema(user_input)
if self._userdata[CONF_INTEGRATION_TYPE] == SENSOR_DEVIATION:
schema = deviation_config_option_schema(user_input)
if self._userdata[CONF_INTEGRATION_TYPE] == SENSOR_ROUTE:
schema = route_config_option_schema(user_input)

logger.debug(f"[setup_integration] Creating entry '{name}' with id {id}")
try:
tempResult = self.async_create_entry(title=name, data=user_input)
logger.debug("[setup_integration] Entry creating succeeded")
return tempResult
except:
logger.error(f"[setup_integration] Entry creation failed for '{name}' with id {id}")
return self.async_abort(reason="not_supported")
logger.debug(f"[setup_integration_config] Schema is {self._userdata[CONF_INTEGRATION_TYPE]}")

# FIXME: DOES NOT ACTUALLY VALIDATE ANYTHING! WE NEED THIS! =)
if user_input is not None:
try:
user_input = await self.validate_config(user_input)
except Exception: # pylint: disable=broad-except
errors["base"] = "unknown_exception"
logger.debug("[setup_integration_config(validate)] Unknown exception occured")
else:
try:
name = self._userdata[CONF_NAME]
del self._userdata[CONF_NAME]
logger.debug(f"[setup_integration_config] Creating entry '{name}' with id {self._userdata[CONF_INTEGRATION_ID]}")

self._userdata.update(user_input)

tempresult = self.async_create_entry(title=name, data=self._userdata)
logger.debug("[setup_integration_config] Entry creating succeeded")
return tempresult
except:
logger.error(f"[setup_integration] Entry creation failed for '{name}' with id {self._userdata[CONF_INTEGRATION_ID]}")
return self.async_abort(reason="not_supported")

logger.debug("[setup_integration_config] Validation errors encountered so showing options form again")
return self.async_show_form(step_id="config", data_schema=voluptuous.Schema(schema), errors=errors)

logger.debug("[setup_integration_config] No user input so showing options form")
return self.async_show_form(step_id="config", data_schema=voluptuous.Schema(schema))

@staticmethod
@callback
Expand Down Expand Up @@ -117,15 +170,15 @@ async def async_step_user(self, user_input):
errors = {}

if self.config_entry.data[CONF_INTEGRATION_TYPE] == SENSOR_STANDARD:
schema = standard_config_option_schema(self.config_entry.options)
schema = standard_config_option_schema(self.config_entry.data)
if self.config_entry.data[CONF_INTEGRATION_TYPE] == SENSOR_STATUS:
schema = status_config_option_schema(self.config_entry.options)
schema = status_config_option_schema(self.config_entry.data)
if self.config_entry.data[CONF_INTEGRATION_TYPE] == SENSOR_VEHICLE_LOCATION:
schema = vehiclelocation_config_option_schema(self.config_entry.options)
schema = vehiclelocation_config_option_schema(self.config_entry.data)
if self.config_entry.data[CONF_INTEGRATION_TYPE] == SENSOR_DEVIATION:
schema = deviation_config_option_schema(self.config_entry.options)
schema = deviation_config_option_schema(self.config_entry.data)
if self.config_entry.data[CONF_INTEGRATION_TYPE] == SENSOR_ROUTE:
schema = route_config_option_schema(self.config_entry.options)
schema = route_config_option_schema(self.config_entry.data)

logger.debug(f"[integration_options] Schema is {self.config_entry.data[CONF_INTEGRATION_TYPE]}")

Expand Down
3 changes: 2 additions & 1 deletion custom_components/hasl3/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
STATE_OFF
)

HASL_VERSION = "3.0.4"
HASL_VERSION = "3.0.5"
SCHEMA_VERSION = "2"
DOMAIN = "hasl3"
NAME = "SL Integration (HASL)"

Expand Down
2 changes: 1 addition & 1 deletion custom_components/hasl3/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dependencies": [],
"after_dependencies": [],
"config_flow": true,
"version": "3.0.4",
"version": "3.0.5",
"iot_class": "cloud_polling",
"quality_scale": "silver",
"requirements": [
Expand Down
Loading

0 comments on commit 1e14aa8

Please sign in to comment.