From 6303ff93bb2dab2ce91a7211cd3cc4b3193a951c Mon Sep 17 00:00:00 2001 From: Andreas <69649604+a529987659852@users.noreply.github.com> Date: Fri, 19 Jan 2024 11:48:35 +0100 Subject: [PATCH] Technical update for HA 2024.1 - no new features --- custom_components/openwbmqtt/__init__.py | 24 ++++++++++++------- custom_components/openwbmqtt/binary_sensor.py | 8 +++---- custom_components/openwbmqtt/common.py | 1 + custom_components/openwbmqtt/config_flow.py | 11 ++++++--- custom_components/openwbmqtt/const.py | 22 ++++++++--------- custom_components/openwbmqtt/number.py | 19 ++++----------- custom_components/openwbmqtt/select.py | 13 ++++++---- custom_components/openwbmqtt/sensor.py | 11 ++++----- custom_components/openwbmqtt/switch.py | 15 ++++++------ 9 files changed, 64 insertions(+), 60 deletions(-) diff --git a/custom_components/openwbmqtt/__init__.py b/custom_components/openwbmqtt/__init__.py index 3360ed7..dd7d73c 100644 --- a/custom_components/openwbmqtt/__init__.py +++ b/custom_components/openwbmqtt/__init__.py @@ -1,4 +1,4 @@ -"""The openwbmqtt component for controlling the openWB wallbox via home assistant / MQTT""" +"""The openwbmqtt component for controlling the openWB wallbox via home assistant / MQTT.""" import logging from homeassistant.config_entries import ConfigEntry @@ -11,15 +11,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: - - # Trigger the creation of sensors. + """Trigger the creation of sensors.""" for platform in PLATFORMS: hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, platform) ) - """Define services that publish data to MQTT. The published data is subscribed by openWB - and the respective settings are changed.""" + # Define services that publish data to MQTT. The published data is subscribed by openWB + # and the respective settings are changed. def fun_enable_disable_cp(call): """Enable or disable charge point # --> set/lp#/ChargePointEnabled [0,1].""" @@ -53,10 +52,11 @@ def fun_change_global_charge_mode(call): def fun_change_charge_limitation_per_cp(call): """If box is in state 'Sofortladen', the charge limitation can be finetuned. + --> config/set/sofort/lp/#/chargeLimitation [0, 1, 2]. If the wallbox shall charge only a limited amount of energy [1] or to a certain SOC [2] --> config/set/sofort/lp/#/energyToCharge [value in kWh] - --> config/set/sofort/lp/#/socToChargeTo [value in %] + --> config/set/sofort/lp/#/socToChargeTo [value in %]. """ topic = f"{call.data.get('mqtt_prefix')}/config/set/sofort/lp/{call.data.get('charge_point_id')}/chargeLimitation" _LOGGER.debug("topic (change_charge_limitation_per_cp): %s", topic) @@ -84,7 +84,7 @@ def fun_change_charge_current_per_cp(call): payload = str(call.data.get("target_current")) hass.components.mqtt.publish(hass, topic, payload) - + def fun_enable_disable_price_based_charging(call): """Enable or disable price-based charging for charge point # --> set/lp#/etBasedCharging [0,1].""" topic = f"{call.data.get('mqtt_prefix')}/set/lp{call.data.get('charge_point_id')}/etBasedCharging" @@ -109,7 +109,11 @@ def fun_enable_disable_price_based_charging(call): hass.services.async_register( DOMAIN, "change_charge_current_per_cp", fun_change_charge_current_per_cp ) - hass.services.async_register(DOMAIN, "enable_disable_price_based_charging", fun_enable_disable_price_based_charging) + hass.services.async_register( + DOMAIN, + "enable_disable_price_based_charging", + fun_enable_disable_price_based_charging, + ) # Return boolean to indicate that initialization was successfully. return True @@ -117,7 +121,9 @@ def fun_enable_disable_price_based_charging(call): async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload all sensor entities and services if integration is removed via UI. - No restart of home assistant is required.""" + + No restart of home assistant is required. + """ hass.services.async_remove(DOMAIN, "enable_disable_cp") hass.services.async_remove(DOMAIN, "change_global_charge_mode") hass.services.async_remove(DOMAIN, "change_charge_limitation_per_cp") diff --git a/custom_components/openwbmqtt/binary_sensor.py b/custom_components/openwbmqtt/binary_sensor.py index 02e554f..45dd2f1 100644 --- a/custom_components/openwbmqtt/binary_sensor.py +++ b/custom_components/openwbmqtt/binary_sensor.py @@ -1,4 +1,4 @@ -"""The openwbmqtt component for controlling the openWB wallbox via home assistant / MQTT""" +"""The openwbmqtt component for controlling the openWB wallbox via home assistant / MQTT.""" from __future__ import annotations import copy @@ -8,7 +8,6 @@ from homeassistant.components.binary_sensor import DOMAIN, BinarySensorEntity from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback -from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.util import slugify @@ -84,9 +83,10 @@ def __init__( nChargePoints: int | None = None, currentChargePoint: int | None = None, ) -> None: - """Initialize the sensor.""" + """Initialize the sensor. - """Initialize the sensor and the openWB device.""" + Initialize the sensor and the openWB device. + """ super().__init__( device_friendly_name=device_friendly_name, mqtt_root=mqtt_root, diff --git a/custom_components/openwbmqtt/common.py b/custom_components/openwbmqtt/common.py index 1da7533..8d1e0c8 100644 --- a/custom_components/openwbmqtt/common.py +++ b/custom_components/openwbmqtt/common.py @@ -1,3 +1,4 @@ +"""The openwbmqtt component for controlling the openWB wallbox via home assistant / MQTT.""" from homeassistant.helpers.entity import DeviceInfo from .const import DOMAIN, MANUFACTURER, MODEL diff --git a/custom_components/openwbmqtt/config_flow.py b/custom_components/openwbmqtt/config_flow.py index c64ff2f..6bb9361 100644 --- a/custom_components/openwbmqtt/config_flow.py +++ b/custom_components/openwbmqtt/config_flow.py @@ -1,4 +1,4 @@ -"""The openwbmqtt component for controlling the openWB wallbox via home assistant / MQTT""" +"""The openwbmqtt component for controlling the openWB wallbox via home assistant / MQTT.""" from __future__ import annotations from homeassistant.config_entries import ConfigFlow @@ -8,10 +8,15 @@ class openwbmqttConfigFlow(ConfigFlow, domain=DOMAIN): - """Configuration flow for the configuration of the openWB integration. When added by the user, he/she - must provide configuration values as defined in DATA_SCHEMA.""" + """Configuration flow for the configuration of the openWB integration. + + When added by the user, he/she + must provide configuration values as defined in DATA_SCHEMA. + """ async def async_step_user(self, user_input=None): + """Return the configuration form.""" + if user_input is None: return self.async_show_form(step_id="user", data_schema=DATA_SCHEMA) diff --git a/custom_components/openwbmqtt/const.py b/custom_components/openwbmqtt/const.py index 532cea5..3881f70 100644 --- a/custom_components/openwbmqtt/const.py +++ b/custom_components/openwbmqtt/const.py @@ -1,8 +1,8 @@ -"""The openwbmqtt component for controlling the openWB wallbox via home assistant / MQTT""" +"""The openwbmqtt component for controlling the openWB wallbox via home assistant / MQTT.""" from __future__ import annotations -from dataclasses import dataclass from collections.abc import Callable +from dataclasses import dataclass import voluptuous as vol @@ -21,20 +21,20 @@ from homeassistant.const import ( PERCENTAGE, Platform, + UnitOfElectricCurrent, + UnitOfElectricPotential, UnitOfEnergy, UnitOfLength, UnitOfPower, - UnitOfElectricPotential, - UnitOfElectricCurrent, ) import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import EntityCategory PLATFORMS = [ - Platform.SELECT, - Platform.SENSOR, Platform.BINARY_SENSOR, Platform.NUMBER, + Platform.SELECT, + Platform.SENSOR, Platform.SWITCH, ] @@ -58,7 +58,7 @@ @dataclass class openwbSensorEntityDescription(SensorEntityDescription): - """Enhance the sensor entity description for openWB""" + """Enhance the sensor entity description for openWB.""" value_fn: Callable | None = None valueMap: dict | None = None @@ -67,7 +67,7 @@ class openwbSensorEntityDescription(SensorEntityDescription): @dataclass class openwbBinarySensorEntityDescription(BinarySensorEntityDescription): - """Enhance the sensor entity description for openWB""" + """Enhance the sensor entity description for openWB.""" state: Callable | None = None mqttTopicCurrentValue: str | None = None @@ -75,7 +75,7 @@ class openwbBinarySensorEntityDescription(BinarySensorEntityDescription): @dataclass class openwbSelectEntityDescription(SelectEntityDescription): - """Enhance the select entity description for openWB""" + """Enhance the select entity description for openWB.""" valueMapCommand: dict | None = None valueMapCurrentValue: dict | None = None @@ -86,7 +86,7 @@ class openwbSelectEntityDescription(SelectEntityDescription): @dataclass class openwbSwitchEntityDescription(SwitchEntityDescription): - """Enhance the select entity description for openWB""" + """Enhance the select entity description for openWB.""" mqttTopicCommand: str | None = None mqttTopicCurrentValue: str | None = None @@ -95,7 +95,7 @@ class openwbSwitchEntityDescription(SwitchEntityDescription): @dataclass class openWBNumberEntityDescription(NumberEntityDescription): - """Enhance the number entity description for openWB""" + """Enhance the number entity description for openWB.""" mqttTopicCommand: str | None = None mqttTopicCurrentValue: str | None = None diff --git a/custom_components/openwbmqtt/number.py b/custom_components/openwbmqtt/number.py index f7c890f..611b371 100644 --- a/custom_components/openwbmqtt/number.py +++ b/custom_components/openwbmqtt/number.py @@ -1,26 +1,15 @@ +"""The openwbmqtt component for controlling the openWB wallbox via home assistant / MQTT.""" from __future__ import annotations import copy -from dataclasses import dataclass import logging -from os import device_encoding, stat - -from sqlalchemy import desc +# from sqlalchemy import desc from homeassistant.components import mqtt from homeassistant.components.number import DOMAIN, NumberEntity, NumberMode from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ( - DEVICE_DEFAULT_NAME, - UnitOfElectricCurrent, - UnitOfEnergy, - EntityCategory, - PERCENTAGE, -) from homeassistant.core import HomeAssistant, callback -from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.util import slugify from .common import OpenWBBaseEntity @@ -87,7 +76,7 @@ async def async_setup_entry( class openWBNumber(OpenWBBaseEntity, NumberEntity): - """Entity representing openWB numbers""" + """Entity representing openWB numbers.""" entity_description: openWBNumberEntityDescription @@ -159,6 +148,7 @@ def message_received(message): async def async_set_native_value(self, value): """Update the current value. + After set_value --> the result is published to MQTT. But the HA sensor shall only change when the MQTT message on the /get/ topic is received. Only then, openWB has changed the setting as well. @@ -168,6 +158,7 @@ async def async_set_native_value(self, value): # self.async_write_ha_state() def publishToMQTT(self): + """Publish data to MQTT.""" topic = f"{self.entity_description.mqttTopicCommand}" _LOGGER.debug("MQTT topic: %s", topic) payload = str(int(self._attr_native_value)) diff --git a/custom_components/openwbmqtt/select.py b/custom_components/openwbmqtt/select.py index 5d8b162..6f9fbcf 100644 --- a/custom_components/openwbmqtt/select.py +++ b/custom_components/openwbmqtt/select.py @@ -1,4 +1,4 @@ -"""OpenWB Selector""" +"""OpenWB Selector.""" from __future__ import annotations import copy @@ -28,6 +28,7 @@ async def async_setup_entry( config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: + """Return selectors.""" integrationUniqueID = config_entry.unique_id mqttRoot = config_entry.data[MQTT_ROOT_TOPIC] @@ -85,7 +86,7 @@ def __init__( device_friendly_name=device_friendly_name, mqtt_root=mqtt_root, ) - """Initialize the inverter operation mode setting entity.""" + # Initialize the inverter operation mode setting entity self.entity_description = description if nChargePoints: @@ -131,16 +132,18 @@ def message_received(message): ) async def async_select_option(self, option: str) -> None: - """Change the selected option.""" - self.publishToMQTT(option) - """After select --> the result is published to MQTT. + """Change the selected option. + + After select --> the result is published to MQTT. But the HA sensor shall only change when the MQTT message on the /get/ topic is received. Only then, openWB has changed the setting as well. """ + self.publishToMQTT(option) # self._attr_current_option = option # self.async_write_ha_state() def publishToMQTT(self, commandValueToPublish): + """Publish data to MQTT.""" topic = f"{self.entity_description.mqttTopicCommand}" _LOGGER.debug("MQTT topic: %s", topic) try: diff --git a/custom_components/openwbmqtt/sensor.py b/custom_components/openwbmqtt/sensor.py index a211abf..e3a20a7 100644 --- a/custom_components/openwbmqtt/sensor.py +++ b/custom_components/openwbmqtt/sensor.py @@ -1,4 +1,4 @@ -"""The openwbmqtt component for controlling the openWB wallbox via home assistant / MQTT""" +"""The openwbmqtt component for controlling the openWB wallbox via home assistant / MQTT.""" from __future__ import annotations import copy @@ -12,8 +12,7 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.device_registry import async_get as async_get_dev_reg from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.typing import StateType -from homeassistant.util import dt, slugify +from homeassistant.util import dt as dt_util, slugify from .common import OpenWBBaseEntity @@ -135,7 +134,7 @@ def message_received(message): # Reformat TimeRemaining --> timestamp. if "TimeRemaining" in self.entity_description.key: - now = dt.utcnow() + now = dt_util.utcnow() if "H" in self._attr_native_value: tmp = self._attr_native_value.split() delta = timedelta(hours=int(tmp[0]), minutes=int(tmp[2])) @@ -173,7 +172,7 @@ def message_received(message): device.id, configuration_url=f"http://{message.payload}/openWB/web/index.php", ) - device_registry.async_update_device + # device_registry.async_update_device # If MQTT message contains version --> set sw_version of the device elif "version" in self.entity_id: device_registry = async_get_dev_reg(self.hass) @@ -183,7 +182,7 @@ def message_received(message): device_registry.async_update_device( device.id, sw_version=message.payload ) - device_registry.async_update_device + # device_registry.async_update_device # Update icon of countPhasesInUse elif "countPhasesInUse" in self.entity_description.key: diff --git a/custom_components/openwbmqtt/switch.py b/custom_components/openwbmqtt/switch.py index 6bc46fd..5a4dc81 100644 --- a/custom_components/openwbmqtt/switch.py +++ b/custom_components/openwbmqtt/switch.py @@ -1,10 +1,11 @@ +"""The openwbmqtt component for controlling the openWB wallbox via home assistant / MQTT.""" from __future__ import annotations import copy import logging from homeassistant.components import mqtt -from homeassistant.components.switch import DOMAIN, SwitchDeviceClass, SwitchEntity +from homeassistant.components.switch import DOMAIN, SwitchEntity from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -26,7 +27,7 @@ async def async_setup_entry( config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: - + """Return switch entities.""" integrationUniqueID = config_entry.unique_id mqttRoot = config_entry.data[MQTT_ROOT_TOPIC] nChargePoints = config_entry.data[CHARGE_POINTS] @@ -77,7 +78,7 @@ def __init__( device_friendly_name=device_friendly_name, mqtt_root=mqtt_root, ) - """Initialize the inverter operation mode setting entity.""" + # Initialize the inverter operation mode setting entity self.entity_description = description if nChargePoints: @@ -117,6 +118,7 @@ def message_received(message): def turn_on(self, **kwargs): """Turn the switch on. + After turn_on --> the result is published to MQTT. But the HA sensor shall only change when the MQTT message on the /get/ topic is received. Only then, openWB has changed the setting as well. @@ -127,6 +129,7 @@ def turn_on(self, **kwargs): def turn_off(self, **kwargs): """Turn the device off. + After turn_off --> the result is published to MQTT. But the HA sensor shall only change when the MQTT message on the /get/ topic is received. Only then, openWB has changed the setting as well. @@ -136,10 +139,6 @@ def turn_off(self, **kwargs): # self.schedule_update_ha_state() def publishToMQTT(self): + """Publish data to MQTT.""" topic = f"{self.entity_description.mqttTopicCommand}" self.hass.components.mqtt.publish(self.hass, topic, str(int(self._attr_is_on))) - """if self._attr_is_on == True: - self.hass.components.mqtt.publish(self.hass, topic, str(1)) - else: - self.hass.components.mqtt.publish(self.hass, topic, str(0)) - """