Skip to content

Commit

Permalink
Merge pull request #108 from fsaris/powerplay
Browse files Browse the repository at this point in the history
Powerplay
  • Loading branch information
fsaris authored Apr 12, 2024
2 parents 639fe53 + 6a8d8cf commit 40e8f1f
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 12 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ Unofficial integration for Zonneplan | Energie
- Electricity delivery costs today
- Electricity production costs today
- Gas delivery costs today
- Zonneplan ONE (Solar inverter) sensors: _(available when you have a Zonneplan solar inverter)_
- Zonneplan Solar inverter sensors: _(available when you have a Zonneplan solar inverter)_
- Yield total: `kWh`
- First measured: `date` _(default disabled)_
- Last measured value: `W`
- Last measured: `date`
- Powerplay enabled: `on/off` _(default disabled)_
- Powerplay/power limit active: `on/off` _(default disabled)_
- Powerplay total: `` _(default disabled)_
- Powerplay today: `` _(default disabled)_
- Current scenario _(default disabled)_
- Zonneplan Connect (P1 reader) sensors: _(available when there is a P1 reader from Zonneplan)_
- Electricity consumption: `W`
- Electricity production: `W`
Expand Down
77 changes: 70 additions & 7 deletions custom_components/zonneplan_one/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,39 @@
BINARY_SENSORS_TYPES,
CHARGE_POINT,
BATTERY,
PV_INSTALL,
ZonneplanBinarySensorEntityDescription,
)

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(hass: HomeAssistantType, config_entry, async_add_entities):

coordinator: ZonneplanUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id][
"coordinator"
]

entities = []
for uuid, connection in coordinator.connections.items():
pv_installations = coordinator.getConnectionValue(uuid, PV_INSTALL)
charge_point = coordinator.getConnectionValue(uuid, CHARGE_POINT)
battery = coordinator.getConnectionValue(uuid, BATTERY)

_LOGGER.debug("Setup binary sensors for connnection %s", uuid)

if pv_installations:
for install_index in range(len(pv_installations)):
for sensor_key in BINARY_SENSORS_TYPES[PV_INSTALL]:
entities.append(
ZonneplanPvBinarySensor(
uuid,
sensor_key,
coordinator,
install_index,
BINARY_SENSORS_TYPES[PV_INSTALL][sensor_key],
)
)

if charge_point:
for install_index in range(len(charge_point)):
for sensor_key in BINARY_SENSORS_TYPES[CHARGE_POINT]:
Expand Down Expand Up @@ -73,12 +87,12 @@ class ZonneplanBinarySensor(CoordinatorEntity, RestoreEntity, BinarySensorEntity
coordinator: ZonneplanUpdateCoordinator

def __init__(
self,
connection_uuid,
sensor_key: str,
coordinator: ZonneplanUpdateCoordinator,
install_index: Number,
description: ZonneplanBinarySensorEntityDescription,
self,
connection_uuid,
sensor_key: str,
coordinator: ZonneplanUpdateCoordinator,
install_index: Number,
description: ZonneplanBinarySensorEntityDescription,
):
"""Initialize the sensor."""
super().__init__(coordinator)
Expand Down Expand Up @@ -133,6 +147,54 @@ def extra_state_attributes(self):

return attrs


class ZonneplanPvBinarySensor(ZonneplanBinarySensor):
@property
def install_uuid(self) -> str:
"""Return install ID."""
if self._install_index < 0:
return self._connection_uuid
else:
return self.coordinator.getConnectionValue(
self._connection_uuid,
"pv_installation.{install_index}.uuid".format(
install_index=self._install_index
),
)

@property
def device_info(self):
"""Return the device information."""
return {
"identifiers": {(DOMAIN, self.install_uuid)},
"via_device": (DOMAIN, self._connection_uuid),
"manufacturer": "Zonneplan",
"name": self.coordinator.getConnectionValue(
self._connection_uuid,
"pv_installation.{install_index}.meta.name".format(
install_index=self._install_index
),
) + (f" ({self._install_index + 1})" if self._install_index and self._install_index > 0 else ""),
"model": self.coordinator.getConnectionValue(
self._connection_uuid,
"pv_installation.{install_index}.label".format(
install_index=self._install_index
),
) + " " + str(self.coordinator.getConnectionValue(
self._connection_uuid,
"pv_installation.{install_index}.meta.panel_count".format(
install_index=self._install_index
),
)) + " panels",
"serial_number": self.coordinator.getConnectionValue(
self._connection_uuid,
"pv_installation.{install_index}.meta.sgn_serial_number".format(
install_index=self._install_index
),
),
}


class ZonneplanChargePointBinarySensor(ZonneplanBinarySensor):
@property
def install_uuid(self) -> str:
Expand Down Expand Up @@ -174,6 +236,7 @@ def device_info(self):
)
}


class ZonneplanBatteryBinarySensor(ZonneplanBinarySensor):
@property
def install_uuid(self) -> str:
Expand Down
46 changes: 46 additions & 0 deletions custom_components/zonneplan_one/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,40 @@ class ZonneplanButtonEntityDescription(ButtonEntityDescription):
icon="mdi:calendar-clock",
entity_registry_enabled_default=True,
),
"expected_surplus_kwh": ZonneplanSensorEntityDescription(
key="pv_data.contracts.{install_index}.meta.expected_surplus_kwh",
name="Expected surplus",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
value_factor=0.001,
device_class=SensorDeviceClass.ENERGY,
entity_registry_enabled_default=False,
state_class=SensorStateClass.MEASUREMENT,
),
"total_earned": ZonneplanSensorEntityDescription(
key="pv_data.contracts.{install_index}.meta.total_earned",
name="Powerplay total",
value_factor=0.0000001,
device_class=SensorDeviceClass.MONETARY,
native_unit_of_measurement='EUR',
state_class=SensorStateClass.TOTAL,
entity_registry_enabled_default=False,
),
"total_day": ZonneplanSensorEntityDescription(
key="pv_data.contracts.{install_index}.meta.total_day",
name="Powerplay today",
value_factor=0.0000001,
device_class=SensorDeviceClass.MONETARY,
native_unit_of_measurement='EUR',
state_class=SensorStateClass.TOTAL,
last_reset_key="pv_data.measurement_groups.0.date",
entity_registry_enabled_default=False,
),
"current_scenario": ZonneplanSensorEntityDescription(
key="pv_data.contracts.{install_index}.meta.current_scenario",
name="Current scenario",
entity_registry_enabled_default=False,
icon="mdi:message-text-outline",
),
},
"totals": {
"total_today": ZonneplanSensorEntityDescription(
Expand Down Expand Up @@ -590,6 +624,18 @@ class ZonneplanButtonEntityDescription(ButtonEntityDescription):
entity_registry_enabled_default=True,
),
},
PV_INSTALL: {
"dynamic_control_enabled": ZonneplanBinarySensorEntityDescription(
key="pv_data.contracts.{install_index}.meta.dynamic_control_enabled",
name="Powerplay enabled",
entity_registry_enabled_default=False,
),
"power_limit_active": ZonneplanBinarySensorEntityDescription(
key="pv_data.contracts.{install_index}.meta.power_limit_active",
name="Power limit active",
entity_registry_enabled_default=False,
),
},
CHARGE_POINT: {
"connectivity_state": ZonneplanBinarySensorEntityDescription(
key="charge_point_data.state.connectivity_state",
Expand Down
2 changes: 1 addition & 1 deletion custom_components/zonneplan_one/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"issue_tracker": "https://github.com/fsaris/home-assistant-zonneplan-one/issues",
"requirements": [],
"ssdp": [],
"version": "2024.3.0",
"version": "2024.4.0",
"zeroconf": []
}
8 changes: 5 additions & 3 deletions custom_components/zonneplan_one/zonneplan_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import inspect
import os

API_VERSION = "4.7.1"
APP_VERSION = "4.12.1"
API_VERSION = "v2"
LOGIN_REQUEST_URI = "https://app-api.zonneplan.nl/auth/request"
OAUTH2_TOKEN_URI = "https://app-api.zonneplan.nl/oauth/token"

Expand All @@ -19,8 +20,9 @@ def __init__(self, session: aiohttp.ClientSession):
self._session = session
self._request_headers = {
"content-type": "application/json;charset=utf-8",
"x-app-version": API_VERSION,
# "x-app-environment": "production",
"x-app-version": APP_VERSION,
"x-api-version": API_VERSION,
"x-app-environment": "production",
"x-ha-integration": self._get_integration_version(),
}

Expand Down

0 comments on commit 40e8f1f

Please sign in to comment.