Skip to content

Commit

Permalink
Merge pull request #83 from mvn23/settime
Browse files Browse the repository at this point in the history
Add set_datetime service
  • Loading branch information
wimb0 authored Apr 8, 2024
2 parents a1e2fec + 6ea9ff4 commit 6f6879d
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 1 deletion.
4 changes: 4 additions & 0 deletions custom_components/saj_modbus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
DOMAIN,
)
from .hub import SAJModbusHub
from .services import async_setup_services

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -60,6 +61,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, component)
)

async_setup_services(hass)

return True


Expand Down
18 changes: 17 additions & 1 deletion custom_components/saj_modbus/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from homeassistant.helpers.typing import HomeAssistantType
import logging
import threading
from datetime import timedelta
from datetime import datetime, timedelta
from homeassistant.core import CALLBACK_TYPE, callback
from homeassistant.helpers import entity_registry
from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN
Expand Down Expand Up @@ -320,6 +320,22 @@ def set_limitpower(self, value: int):
self.limitpower = value
self.hass.add_job(self.async_update_listeners)

def set_date_and_time(self, date_time: datetime = None):
"""Set the time and date on the inverter."""
if date_time is None:
date_time = datetime.now()

values = [
date_time.year,
(date_time.month << 8) + date_time.day,
(date_time.hour << 8) + date_time.minute,
(date_time.second << 8)
]

response = self._write_registers(unit=1, address=0x8020, values=values)
if response.isError():
raise response

def set_value(self, key: str, value: int):
"""Set value matching key."""
if key == "limitpower":
Expand Down
72 changes: 72 additions & 0 deletions custom_components/saj_modbus/services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""SAJ Modbus services."""

from collections.abc import Mapping
from typing import Any

import voluptuous as vol

from homeassistant.const import ATTR_DEVICE_ID
from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.helpers import (
device_registry as dr,
config_validation as cv
)

from .const import DOMAIN as SAJ_DOMAIN

ATTR_DATETIME = "datetime"

SERVICE_SET_DATE_TIME = "set_datetime"

SERVICE_SET_DATE_TIME_SCHEMA = vol.All(
vol.Schema({
vol.Required(ATTR_DEVICE_ID): str,
vol.Optional(ATTR_DATETIME): cv.datetime,
})
)

SUPPORTED_SERVICES = (SERVICE_SET_DATE_TIME,)

SERVICE_TO_SCHEMA = {
SERVICE_SET_DATE_TIME: SERVICE_SET_DATE_TIME_SCHEMA,
}


@callback
def async_setup_services(hass: HomeAssistant) -> None:
"""Set up services for SAJ Modbus integration."""

services = {
SERVICE_SET_DATE_TIME: async_set_date_time,
}

async def async_call_service(service_call: ServiceCall) -> None:
"""Call correct SAJ Modbus service."""
await services[service_call.service](hass, service_call.data)

for service in SUPPORTED_SERVICES:
hass.services.async_register(
SAJ_DOMAIN,
service,
async_call_service,
schema=SERVICE_TO_SCHEMA.get(service),
)


@callback
def async_unload_services(hass: HomeAssistant) -> None:
"""Unload SAJ Modbus services."""
for service in SUPPORTED_SERVICES:
hass.services.async_remove(SAJ_DOMAIN, service)


async def async_set_date_time(hass: HomeAssistant, data: Mapping[str, Any]) -> None:
"""Set the date and time on the inverter."""
device_registry = dr.async_get(hass)
device_entry = device_registry.async_get(data[ATTR_DEVICE_ID])

hub = hass.data[SAJ_DOMAIN][device_entry.name]["hub"]
await hass.async_add_executor_job(
hub.set_date_and_time,
data.get(ATTR_DATETIME, None)
)
12 changes: 12 additions & 0 deletions custom_components/saj_modbus/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
set_datetime:
fields:
device_id:
required: true
selector:
device:
integration: saj_modbus
datetime:
required: false
example: "2024-01-23T12:34:56"
selector:
datetime:
16 changes: 16 additions & 0 deletions custom_components/saj_modbus/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,21 @@
"abort": {
"already_configured": "Device is already configured"
}
},
"services": {
"set_datetime": {
"name": "Set date and time",
"description": "Sets the date and time on the inverter.",
"fields": {
"device_id": {
"name": "Device",
"description": "The inverter on which the date and time will be set."
},
"datetime": {
"name": "Date and time",
"description": "The date and time to be set on the inverter."
}
}
}
}
}
16 changes: 16 additions & 0 deletions custom_components/saj_modbus/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,21 @@
"abort": {
"already_configured": "Device is already configured"
}
},
"services": {
"set_datetime": {
"name": "Set date and time",
"description": "Sets the date and time on the inverter.",
"fields": {
"device_id": {
"name": "Device",
"description": "The inverter on which the date and time will be set."
},
"datetime": {
"name": "Date and time",
"description": "The date and time to be set on the inverter."
}
}
}
}
}
16 changes: 16 additions & 0 deletions custom_components/saj_modbus/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,21 @@
"abort": {
"already_configured": "Apparaat is al geconfigureerd"
}
},
"services": {
"set_datetime": {
"name": "Stel datum en tijd in",
"description": "Stelt de datum en tijd van de omvormer in.",
"fields": {
"device_id": {
"name": "Apparaat",
"description": "De omvormer waarop de datum en tijd ingesteld zullen worden."
},
"datetime": {
"name": "Datum en tijd",
"description": "De datum en tijd die op de omvormer ingesteld zullen worden."
}
}
}
}
}

0 comments on commit 6f6879d

Please sign in to comment.