Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HMI brightness entity #136

Merged
merged 2 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 0.7.2b1

* Add support for setting status light brightness, #112
* Add retry on 500 server errors from Zaptec cloud, #90
* Added new charger lock "Permanent cable lock" and removed the binary
sensor of the same name #102
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ supported by the API. Use at own risk and they might break at any time.
> * Setting charger min and max current
> * Authorize charging
> * Setting cable lock
> * Setting status light brightness

## Zaptec device concept

Expand Down
14 changes: 14 additions & 0 deletions custom_components/zaptec/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,20 @@ async def set_permanent_cable_lock(self, lock: bool):
f"chargers/{self.id}/localSettings", method="post", data=data
)
return result

async def set_hmi_brightness(self, brightness: float):
"""Set the HMI brightness"""
_LOGGER.debug("Set HMI brightness %s", brightness)
data = {
"Device": {
"HmiBrightness": brightness,
},
}
# NOTE: Undocumented API call
result = await self._account._request(
f"chargers/{self.id}/localSettings", method="post", data=data
)
return result


class Account:
Expand Down
46 changes: 44 additions & 2 deletions custom_components/zaptec/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import ZaptecBaseEntity, ZaptecUpdateCoordinator
from .api import Installation
from .api import Installation, Charger
from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)
Expand All @@ -38,6 +38,7 @@ def _update_from_zaptec(self) -> None:

class ZaptecAvailableCurrentNumber(ZaptecNumber):
zaptec_obj: Installation
entity_description: ZapNumberEntityDescription

def _post_init(self):
# Get the max current rating from the reported max current
Expand All @@ -64,7 +65,8 @@ async def async_set_native_value(self, value: float) -> None:


class ZaptecSettingNumber(ZaptecNumber):
zaptec_obj: Installation
zaptec_obj: Charger
entity_description: ZapNumberEntityDescription

def _post_init(self):
# Get the max current rating from the reported max current
Expand Down Expand Up @@ -93,6 +95,38 @@ async def async_set_native_value(self, value: float) -> None:
await self.coordinator.async_request_refresh()


class ZaptecHmiBrightness(ZaptecNumber):
zaptec_obj: Charger
entity_description: ZapNumberEntityDescription

@callback
def _update_from_zaptec(self) -> None:
try:
self._attr_native_value = float(self._get_zaptec_value()) * 100
self._attr_available = True
self._log_value(self._attr_native_value)
except (KeyError, AttributeError):
self._attr_available = False
self._log_unavailable()

async def async_set_native_value(self, value: float) -> None:
"""Update to Zaptec."""
_LOGGER.debug(
"Set %s to <%s> %s in %s",
self.entity_id,
type(value).__qualname__,
value,
self.zaptec_obj.qual_id,
)

try:
await self.zaptec_obj.set_hmi_brightness(value / 100)
except Exception as exc:
raise HomeAssistantError(f"Set HmiBrightness to {value} failed") from exc

await self.coordinator.async_request_refresh()


@dataclass
class ZapNumberEntityDescription(NumberEntityDescription):
cls: type | None = None
Expand Down Expand Up @@ -139,6 +173,14 @@ class ZapNumberEntityDescription(NumberEntityDescription):
cls=ZaptecSettingNumber,
setting="CurrentInMaximum",
),
ZapNumberEntityDescription(
key="hmi_brightness",
translation_key="hmi_brightness",
entity_category=EntityCategory.DIAGNOSTIC,
icon="mdi:brightness-6",
native_unit_of_measurement=const.PERCENTAGE,
cls=ZaptecHmiBrightness,
),
]


Expand Down
3 changes: 2 additions & 1 deletion custom_components/zaptec/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@
"number": {
"available_current": { "name": "Available current" },
"charger_max_current": { "name": "Charger max current" },
"charger_min_current": { "name": "Charger min current" }
"charger_min_current": { "name": "Charger min current" },
"hmi_brightness": { "name": "Status indicator brightness" }
},
"binary_sensor": {
"authorization_required": {
Expand Down
3 changes: 2 additions & 1 deletion custom_components/zaptec/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@
"number": {
"available_current": { "name": "Beschikbare stroom" },
"charger_max_current": { "name": "Max stroom" },
"charger_min_current": { "name": "Min stroom" }
"charger_min_current": { "name": "Min stroom" },
"hmi_brightness": { "name": "Helderheid statusindicator" }
},
"binary_sensor": {
"authorization_required": {
Expand Down