Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
unl0ck committed May 14, 2024
2 parents 2a7065c + 9048bdc commit 6ce54df
Show file tree
Hide file tree
Showing 46 changed files with 1,306 additions and 216 deletions.
8 changes: 5 additions & 3 deletions .bumpversion-dev.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.bumpversion]
current_version = "1.9.11"
current_version = "1.10.2"
parse = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)"
serialize = ["{major}.{minor}.{patch}"]
search = "{current_version}"
Expand All @@ -11,11 +11,13 @@ tag = false
sign_tags = false
tag_name = "{new_version}"
tag_message = "Bump version: {current_version} → {new_version}"
allow_dirty = false
allow_dirty = true
commit = false
message = "Bump version: {current_version} → {new_version}"
commit_args = ""
[[tool.bumpversion.files]]
filename="./GridboxConnectorAddon-dev/config.yaml"
[[tool.bumpversion.files]]
filename="./GridboxConnectorAddon-dev/cloudSettings.json"
filename="./GridboxConnectorAddon-dev/cloudSettings.json"
[[tool.bumpversion.files]]
filename="./GridboxConnectorAddon-dev/rootfs/share/cloudSettings.json"
4 changes: 2 additions & 2 deletions .bumpversion.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ commit_args = ""
[[tool.bumpversion.files]]
filename="./GridboxConnectorAddon/config.yaml"
[[tool.bumpversion.files]]
filename="./GridboxConnectorAddon/cloudSettings.json"
filename="./GridboxConnectorAddon/rootfs/share/cloudSettings.json"
[[tool.bumpversion.files]]
filename="./GridboxConnectorAddon-dev/config.yaml"
[[tool.bumpversion.files]]
filename="./GridboxConnectorAddon-dev/cloudSettings.json"
filename="./GridboxConnectorAddon-dev/rootfs/share/cloudSettings.json"
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__
.pytest_cache
.history
20 changes: 20 additions & 0 deletions GridboxConnectorAddon-dev/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
<!-- https://developers.home-assistant.io/docs/add-ons/presentation#keeping-a-changelog -->

## 1.10.2

## Added

- Added new sensor `directConsumptionHousehold` and `directConsumptionHeatPump` to show the direct consumption of the household
- Added new sensor `totalConsumption` to show the total consumption of the household
- Added Classes for the sensor to start with unit tests

## Fixed

- Fixed the issue with the battery sum sensor
- Fixed the issue with the battery sum appliance id
- Fixed the issue with each battery show now correct values

## 1.9.12

## Fixed

- Fixed Battery Sum set_state

## 1.9.11

## Fixed
Expand Down
34 changes: 0 additions & 34 deletions GridboxConnectorAddon-dev/GridboxConnector/Battery.py

This file was deleted.

135 changes: 11 additions & 124 deletions GridboxConnectorAddon-dev/GridboxConnector/__main__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import os
import json
import time
from ha_mqtt_discoverable import Settings, DeviceInfo
from ha_mqtt_discoverable.sensors import Sensor, SensorInfo
from gridbox_connector import GridboxConnector
from Battery import Battery
from ha_mqtt_discoverable import Settings
from ha_viessmann_gridbox_connector import HAViessmannGridboxConnector

if __name__ == '__main__':
f = open('/build/cloudSettings.json')
Expand All @@ -13,142 +12,30 @@
data = json.load(f)
f.close()
print("Start Viessmann Connector")
#print(f"====Version {data["version"]}====")
# print(f"====Version {data["version"]}====")

options_file = open('/data/options.json')
options_json = json.load(options_file)
WAIT = int(options_json["wait_time"])
USER = os.getenv('USERNAME')
PASSWORD = os.environ.get('PASSWORD')
mqtt_user = os.getenv('MqttUser')
mqtt_pw = os.getenv('MqttPw')
mqtt_server = os.getenv('MqttServer')
mqtt_port = os.getenv('MqttPort')
mqtt_user = os.getenv('MqttUser', "")
mqtt_pw = os.getenv('MqttPw', "")
mqtt_server = os.getenv('MqttServer', "")
mqtt_port = os.getenv('MqttPort', "")
data["login"]["username"] = USER
data["login"]["password"] = PASSWORD
print(data["login"])
one_time_print = True

battery_dict = {}

mqtt_settings = Settings.MQTT(host=mqtt_server, username=mqtt_user, password=mqtt_pw)

device_info = DeviceInfo(name="Viessmann Gridbox", identifiers="viessmann_gridbox", manufacturer="Viessmann", model="Vitocharge 2.0")

production_sensor_info = SensorInfo(name="Production", device_class="power", unique_id="gridbox_production", device=device_info, unit_of_measurement="W")
production_settings = Settings(mqtt=mqtt_settings, entity=production_sensor_info)

grid_sensor_info = SensorInfo(name="Grid", device_class="power", unique_id="gridbox_grid", device=device_info, unit_of_measurement="W")
grid_settings = Settings(mqtt=mqtt_settings, entity=grid_sensor_info)

photovoltaic_sensor_info = SensorInfo(name="Photovoltaic", device_class="power", unique_id="gridbox_photovoltaic", device=device_info, unit_of_measurement="W")
photovoltaic_settings = Settings(mqtt=mqtt_settings, entity=photovoltaic_sensor_info)

# consumption
consumption_household_sensor_info = SensorInfo(name="Consumption", device_class="power", unique_id="gridbox_consumption_household", device=device_info, unit_of_measurement="W")
consumption_household_settings = Settings(mqtt=mqtt_settings, entity=consumption_household_sensor_info)

total_consumption_household_sensor_info = SensorInfo(name="Total Consumption", device_class="power", unique_id="total_consumption_household", device=device_info, unit_of_measurement="W")
total_consumption_household_settings = Settings(mqtt=mqtt_settings, entity=total_consumption_household_sensor_info)

# Direct Consumption
direct_consumption_household_sensor_info = SensorInfo(name="DirectConsumptionHousehold", device_class="power", unique_id="gridbox_direct_consumption_household", device=device_info, unit_of_measurement="W")
direct_consumption_household_settings = Settings(mqtt=mqtt_settings, entity=direct_consumption_household_sensor_info)

direct_consumption_heatpump_sensor_info = SensorInfo(name="DirectConsumptionHeatPump", device_class="power", unique_id="gridbox_direct_consumption_heatpump", device=device_info, unit_of_measurement="W")
direct_consumption_heatpump_settings = Settings(mqtt=mqtt_settings, entity=direct_consumption_heatpump_sensor_info)

direct_consumption_rate_sensor_info = SensorInfo(name="DirectConsumptionRate", device_class="power_factor", unique_id="gridbox_direct_consumption_rate", device=device_info, unit_of_measurement="%")
direct_consumption_rate_settings = Settings(mqtt=mqtt_settings, entity=direct_consumption_rate_sensor_info)


# Self Consumption
self_supply_sensor_info = SensorInfo(name="SelfSupply", device_class="power", unique_id="gridbox_self_supply", device=device_info, unit_of_measurement="W")
self_supply_settings = Settings(mqtt=mqtt_settings, entity=self_supply_sensor_info)

self_consumption_rate_sensor_info = SensorInfo(name="SelfConsumptionRate", device_class="power_factor", unique_id="gridbox_self_consumption_rate", device=device_info, unit_of_measurement="%")
self_consumption_rate_settings = Settings(mqtt=mqtt_settings, entity=self_consumption_rate_sensor_info)

self_sufficiency_rate_sensor_info = SensorInfo(name="SelfSufficiencyRate", device_class="power_factor", unique_id="gridbox_self_sufficiency_rate", device=device_info, unit_of_measurement="%")
self_sufficiency_rate_settings = Settings(mqtt=mqtt_settings, entity=self_sufficiency_rate_sensor_info)


# Battery Section
battery_sensor_info_sum = SensorInfo(name="Battery Sum Level", device_class="battery", unique_id="gridbox_battery_sum", device=device_info, unit_of_measurement="%")
battery_settings_sum = Settings(mqtt=mqtt_settings, entity=battery_sensor_info_sum)

battery_sensor_capacity_sum = SensorInfo(name="Battery Sum Capacity", device_class="energy", unique_id="gridbox_battery_level_sum", device=device_info, unit_of_measurement="Wh")
battery_settings_capacity_sum = Settings(mqtt=mqtt_settings, entity=battery_sensor_capacity_sum)

battery_sensor_power_sum = SensorInfo(name="Battery Sum Power", device_class="battery", unique_id="gridbox_battery_power_sum", device=device_info, unit_of_measurement="W")
battery_settings_power_sum = Settings(mqtt=mqtt_settings, entity=battery_sensor_power_sum)

# Instantiate the sensor
production_sensor = Sensor(production_settings)
grid_sensor = Sensor(grid_settings)
photovoltaic_sensor = Sensor(photovoltaic_settings)

# Battery sum
battery_sum = Battery(mqtt_settings, device_info, "sum", "")

# Consumption
consumption_household_sensor = Sensor(consumption_household_settings)
total_consumption_household_sensor = Sensor(total_consumption_household_settings)
direct_consumption_household_sensor = Sensor(direct_consumption_household_settings)
direct_consumtion_heatpump_sensor = Sensor(direct_consumption_heatpump_settings)
direct_consumtion_rate_sensor = Sensor(direct_consumption_rate_settings)

self_supply_sensor = Sensor(self_supply_settings)
self_consumtion_rate_sensor = Sensor(self_consumption_rate_settings)

mqtt_settings = Settings.MQTT(
host=mqtt_server, username=mqtt_user, password=mqtt_pw)
viessmann_gridbox_connector = HAViessmannGridboxConnector(mqtt_settings)
gridboxConnector = GridboxConnector(data)
while True:
measurement = gridboxConnector.retrieve_live_data()
viessmann_gridbox_connector.update_sensors(measurement)
if one_time_print:
print(measurement)
one_time_print = False
if "production" in measurement:
production_sensor.set_state(measurement["production"])
if "grid" in measurement:
grid_sensor.set_state(measurement["grid"])
if "photovoltaic" in measurement:
photovoltaic_sensor.set_state(measurement["photovoltaic"])
if "consumption" in measurement:
consumption_household_sensor.set_state(measurement["consumption"])
if "totalConsumption" in measurement:
total_consumption_household_sensor.set_state(measurement["totalConsumption"])
if "directConsumptionHousehold" in measurement:
direct_consumption_household_sensor.set_state(float(measurement["directConsumptionHousehold"]))
if "directConsumptionHeatPump" in measurement:
direct_consumtion_heatpump_sensor.set_state(float(measurement["directConsumptionHeatPump"]))
if "directConsumptionRate" in measurement:
direct_consumtion_rate_sensor.set_state(float(measurement["directConsumptionRate"])*100)


if "selfSupply" in measurement:
self_supply_sensor.set_state(float(measurement["selfSupply"]))
if "selfConsumptionRate" in measurement:
self_consumtion_rate_sensor.set_state(float(measurement["selfConsumptionRate"])*100)
if "selfSufficiencyRate" in measurement:
self_consumtion_rate_sensor.set_state(float(measurement["selfSufficiencyRate"])*100)


if "battery" in measurement:
state_of_charge = float(measurement["battery"]["stateOfCharge"])*100
capacity = float(measurement["battery"]["capacity"])
power = float(measurement["battery"]["power"])
remaining_charge = float(measurement["battery"]["remainingCharge"])
battery_sum.set_values(state_of_charge, capacity, power, remaining_charge)
if "batteries" in measurement:
for index, battery in enumerate(measurement["batteries"]):
appliance_id = battery["applianceID"]
if appliance_id not in battery_dict:
battery_dict[appliance_id] = Battery(mqtt_settings, device_info, f"{index}", appliance_id)
battery = battery_dict[appliance_id]
state_of_charge = float(measurement["battery"]["stateOfCharge"])*100
capacity = float(measurement["battery"]["capacity"])
power = float(measurement["battery"]["power"])
remaining_charge = float(measurement["battery"]["remainingCharge"])
battery.set_values(state_of_charge, capacity, power, remaining_charge)
# Wait until fetch new values in seconds
time.sleep(WAIT)
62 changes: 62 additions & 0 deletions GridboxConnectorAddon-dev/GridboxConnector/ha_viessmann_battery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from ha_mqtt_discoverable.sensors import Sensor, SensorInfo
from ha_mqtt_discoverable import Settings, DeviceInfo


class HAViessmannBattery:
"""
The Battery class represents a battery with associated sensors for level, capacity, power, and remaining charge. Each sensor is created as an MQTT entity with its own SensorInfo and Settings.
Attributes:
id (str): The unique ID of the battery.
name (str): The name of the battery.
device_info (DeviceInfo): The device information of the battery.
mqtt_settings (Settings.MQTT): The MQTT settings for the sensors.
battery_level (Sensor): The sensor for the battery level.
battery_capacity (Sensor): The sensor for the battery capacity.
battery_power (Sensor): The sensor for the battery power.
battery_remaining_charge (Sensor): The sensor for the remaining battery charge.
Methods:
get_name(): Returns the name of the battery.
set_states(level, capacity, power, remaining_charge): Sets the states of the four sensors.
"""

def __init__(self, mqtt_settings, device_info, name, id):
self.id: str = id
self.name: str = name
self.device_info: DeviceInfo = device_info
self.mqtt_settings: Settings.MQTT = mqtt_settings

self.battery_sensor_info = SensorInfo(
name=f"Battery {name} Level", device_class="battery", unique_id=f"gridbox_battery_{name}", device=device_info, unit_of_measurement="%")
self.battery_settings = Settings(
mqtt=mqtt_settings, entity=self.battery_sensor_info)
self.battery_level = Sensor(self.battery_settings)

self.battery_sensor_capacity = SensorInfo(
name=f"Battery {name} Capacity", device_class="energy", unique_id=f"gridbox_battery_level_{name}", device=device_info, unit_of_measurement="Wh")
self.battery_settings_capacity = Settings(
mqtt=mqtt_settings, entity=self.battery_sensor_capacity)
self.battery_capacity = Sensor(self.battery_settings_capacity)

self.battery_sensor_power = SensorInfo(
name=f"Battery {name} Power", device_class="battery", unique_id=f"gridbox_battery_power_{name}", device=device_info, unit_of_measurement="W")
self.battery_settings_power = Settings(
mqtt=mqtt_settings, entity=self.battery_sensor_power)
self.battery_power = Sensor(self.battery_settings_power)

self.battery_sensor_remaining_charge = SensorInfo(
name=f"Battery {name} Remaining Charge", device_class="energy", unique_id=f"gridbox_remaining_charge_{name}", device=device_info, unit_of_measurement="Wh")
self.battery_settings_remaining_charge = Settings(
mqtt=mqtt_settings, entity=self.battery_sensor_remaining_charge)
self.battery_remaining_charge = Sensor(
self.battery_settings_remaining_charge)

def get_name(self):
return self.name

def set_states(self, level, capacity, power, remaining_charge):
self.battery_level.set_state(level)
self.battery_capacity.set_state(capacity)
self.battery_power.set_state(power)
self.battery_remaining_charge.set_state(remaining_charge)
Loading

0 comments on commit 6ce54df

Please sign in to comment.