Skip to content

Commit

Permalink
Technical update for HA 2024.1 - no new features
Browse files Browse the repository at this point in the history
  • Loading branch information
a529987659852 committed Jan 19, 2024
1 parent 4683509 commit 6303ff9
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 60 deletions.
24 changes: 15 additions & 9 deletions custom_components/openwbmqtt/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]."""
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand All @@ -109,15 +109,21 @@ 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


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")
Expand Down
8 changes: 4 additions & 4 deletions custom_components/openwbmqtt/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions custom_components/openwbmqtt/common.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 8 additions & 3 deletions custom_components/openwbmqtt/config_flow.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand Down
22 changes: 11 additions & 11 deletions custom_components/openwbmqtt/const.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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,
]

Expand All @@ -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
Expand All @@ -67,15 +67,15 @@ 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


@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
Expand All @@ -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
Expand All @@ -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
Expand Down
19 changes: 5 additions & 14 deletions custom_components/openwbmqtt/number.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -87,7 +76,7 @@ async def async_setup_entry(


class openWBNumber(OpenWBBaseEntity, NumberEntity):
"""Entity representing openWB numbers"""
"""Entity representing openWB numbers."""

entity_description: openWBNumberEntityDescription

Expand Down Expand Up @@ -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.
Expand All @@ -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))
Expand Down
13 changes: 8 additions & 5 deletions custom_components/openwbmqtt/select.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""OpenWB Selector"""
"""OpenWB Selector."""
from __future__ import annotations

import copy
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 5 additions & 6 deletions custom_components/openwbmqtt/sensor.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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]))
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down
Loading

0 comments on commit 6303ff9

Please sign in to comment.