|
1 |
| -from homeassistant.core import HomeAssistant |
2 |
| -from homeassistant.helpers import config_validation as cv |
| 1 | +from homeassistant.core import HomeAssistant, SupportsResponse |
| 2 | +from homeassistant.helpers import config_validation as cv, entity_registry, entity |
| 3 | +from homeassistant.helpers.entity_component import EntityComponent |
| 4 | +from homeassistant.exceptions import ServiceValidationError |
3 | 5 | import voluptuous as vol
|
4 | 6 | from .const import *
|
5 | 7 | from .solarman import Inverter
|
| 8 | +import logging |
6 | 9 |
|
| 10 | +log = logging.getLogger(__name__) |
7 | 11 |
|
8 |
| -SERVICE_WRITE_REGISTER = 'write_holding_register' |
9 |
| -SERVICE_WRITE_MULTIPLE_REGISTERS = 'write_multiple_holding_registers' |
| 12 | +SERVICE_READ_HOLDING_REGISTER = 'read_holding_register' |
| 13 | +SERVICE_READ_MULTIPLE_HOLDING_REGISTERS = 'read_multiple_holding_registers' |
| 14 | +SERVICE_WRITE_HOLDING_REGISTER = 'write_holding_register' |
| 15 | +SERVICE_WRITE_MULTIPLE_HOLDING_REGISTERS = 'write_multiple_holding_registers' |
| 16 | +PARAM_DEVICE = 'device' |
10 | 17 | PARAM_REGISTER = 'register'
|
11 |
| -PARAM_VALUE = 'value' |
12 |
| -PARAM_VALUES = 'values' |
13 |
| - |
| 18 | +PARAM_COUNT = 'count' |
| 19 | +PARAM_VALUE = 'value' |
| 20 | +PARAM_VALUES = 'values' |
14 | 21 |
|
15 | 22 |
|
16 | 23 | # Register the services one can invoke on the inverter.
|
17 | 24 | # Apart from this, it also need to be defined in the file
|
18 | 25 | # services.yaml for the Home Assistant UI in "Developer Tools"
|
19 | 26 |
|
| 27 | +SERVICE_READ_REGISTER_SCHEMA = vol.Schema( |
| 28 | + { |
| 29 | + vol.Required(PARAM_DEVICE): vol.All(vol.Coerce(str)), |
| 30 | + vol.Required(PARAM_REGISTER): vol.All(vol.Coerce(int), vol.Range(min=0, max=65535)) |
| 31 | + } |
| 32 | +) |
| 33 | + |
| 34 | +SERVICE_READ_MULTIPLE_REGISTERS_SCHEMA = vol.Schema( |
| 35 | + { |
| 36 | + vol.Required(PARAM_DEVICE): vol.All(vol.Coerce(str)), |
| 37 | + vol.Required(PARAM_REGISTER): vol.All(vol.Coerce(int), vol.Range(min=0, max=65535)), |
| 38 | + vol.Required(PARAM_COUNT): vol.All(vol.Coerce(int), vol.Range(min=0, max=65535)) |
| 39 | + } |
| 40 | +) |
20 | 41 |
|
21 | 42 | SERVICE_WRITE_REGISTER_SCHEMA = vol.Schema(
|
22 | 43 | {
|
| 44 | + vol.Required(PARAM_DEVICE): vol.All(vol.Coerce(str)), |
23 | 45 | vol.Required(PARAM_REGISTER): vol.All(vol.Coerce(int), vol.Range(min=0, max=65535)),
|
24 | 46 | vol.Required(PARAM_VALUE): vol.All(vol.Coerce(int), vol.Range(min=0, max=65535)),
|
25 | 47 | }
|
26 | 48 | )
|
27 | 49 |
|
28 | 50 | SERVICE_WRITE_MULTIPLE_REGISTERS_SCHEMA = vol.Schema(
|
29 | 51 | {
|
| 52 | + vol.Required(PARAM_DEVICE): vol.All(vol.Coerce(str)), |
30 | 53 | vol.Required(PARAM_REGISTER): vol.All(vol.Coerce(int), vol.Range(min=0, max=65535)),
|
31 | 54 | vol.Required(PARAM_VALUES): vol.All(cv.ensure_list, [vol.All(vol.Coerce(int), vol.Range(min=0, max=65535))]),
|
32 | 55 | }
|
33 | 56 | )
|
34 | 57 |
|
35 |
| -def register_services (hass: HomeAssistant, inverter: Inverter ): |
| 58 | +def register_services (hass: HomeAssistant ): |
| 59 | + |
| 60 | + def getInverter(device_id): |
| 61 | + inverter: Inverter | None |
| 62 | + entity_comp: EntityComponent[entity.Entity] | None |
| 63 | + registry = entity_registry.async_get(hass) |
| 64 | + entries = entity_registry.async_entries_for_device(registry, device_id) |
| 65 | + for entity_reg in entries: |
| 66 | + entity_id = entity_reg.entity_id |
| 67 | + domain = entity_id.partition(".")[0] |
| 68 | + entity_comp = hass.data.get("entity_components", {}).get(domain) |
| 69 | + if entity_comp is None: |
| 70 | + log.info(f'read_holding_register: Component for {entity_id} not loaded') |
| 71 | + continue |
| 72 | + |
| 73 | + if (entity_obj := entity_comp.get_entity(entity_id)) is None: |
| 74 | + log.info(f'read_holding_register: Entity {entity_id} not found') |
| 75 | + continue |
| 76 | + |
| 77 | + if (inverter := entity_obj.inverter) is None: |
| 78 | + log.info(f'read_holding_register: Entity {entity_id} has no inverter') |
| 79 | + continue |
| 80 | + |
| 81 | + break |
| 82 | + |
| 83 | + return inverter |
| 84 | + |
| 85 | + |
| 86 | + async def read_holding_register(call) -> int: |
| 87 | + if (inverter := getInverter(call.data.get(PARAM_DEVICE))) is None: |
| 88 | + raise ServiceValidationError( |
| 89 | + "No communication interface for device found", |
| 90 | + translation_domain=DOMAIN, |
| 91 | + translation_key="no_interface_found" |
| 92 | + ) |
| 93 | + |
| 94 | + try: |
| 95 | + response = inverter.service_read_holding_register( register=call.data.get(PARAM_REGISTER) ) |
| 96 | + except Exception as e: |
| 97 | + raise ServiceValidationError( |
| 98 | + e, |
| 99 | + translation_domain=DOMAIN, |
| 100 | + translation_key="call_failed" |
| 101 | + ) |
| 102 | + |
| 103 | + result = {call.data.get(PARAM_REGISTER): response[0]} |
| 104 | + return result |
| 105 | + |
| 106 | + async def read_multiple_holding_registers(call) -> int: |
| 107 | + if (inverter := getInverter(call.data.get(PARAM_DEVICE))) is None: |
| 108 | + raise ServiceValidationError( |
| 109 | + "No communication interface for device found", |
| 110 | + translation_domain=DOMAIN, |
| 111 | + translation_key="no_interface_found" |
| 112 | + ) |
| 113 | + |
| 114 | + try: |
| 115 | + response = inverter.service_read_multiple_holding_registers( |
| 116 | + register=call.data.get(PARAM_REGISTER), |
| 117 | + count=call.data.get(PARAM_COUNT) ) |
| 118 | + except Exception as e: |
| 119 | + raise ServiceValidationError( |
| 120 | + e, |
| 121 | + translation_domain=DOMAIN, |
| 122 | + translation_key="call_failed" |
| 123 | + ) |
| 124 | + |
| 125 | + result = {} |
| 126 | + register=call.data.get(PARAM_REGISTER) |
| 127 | + for i in range(0,call.data.get(PARAM_COUNT)): |
| 128 | + result[register+i] = response[i] |
| 129 | + return result |
36 | 130 |
|
37 | 131 | async def write_holding_register(call) -> None:
|
38 |
| - inverter.service_write_holding_register( |
39 |
| - register=call.data.get(PARAM_REGISTER), |
40 |
| - value=call.data.get(PARAM_VALUE)) |
| 132 | + log.debug(f'write_holding_register: call={call}') |
| 133 | + if (inverter := getInverter(call.data.get(PARAM_DEVICE))) is None: |
| 134 | + raise ServiceValidationError( |
| 135 | + "No communication interface for device found", |
| 136 | + translation_domain=DOMAIN, |
| 137 | + translation_key="no_interface_found", |
| 138 | + ) |
| 139 | + |
| 140 | + try: |
| 141 | + inverter.service_write_holding_register( |
| 142 | + register=call.data.get(PARAM_REGISTER), |
| 143 | + value=call.data.get(PARAM_VALUE)) |
| 144 | + except Exception as e: |
| 145 | + raise ServiceValidationError( |
| 146 | + e, |
| 147 | + translation_domain=DOMAIN, |
| 148 | + translation_key="call_failed" |
| 149 | + ) |
| 150 | + |
41 | 151 | return
|
42 | 152 |
|
43 | 153 | async def write_multiple_holding_registers(call) -> None:
|
44 |
| - inverter.service_write_multiple_holding_registers( |
45 |
| - register=call.data.get(PARAM_REGISTER), |
46 |
| - values=call.data.get(PARAM_VALUES)) |
| 154 | + log.debug(f'write_holding_register: call={call}') |
| 155 | + if (inverter := getInverter(call.data.get(PARAM_DEVICE))) is None: |
| 156 | + raise ServiceValidationError( |
| 157 | + "No communication interface for device found", |
| 158 | + translation_domain=DOMAIN, |
| 159 | + translation_key="no_interface_found", |
| 160 | + ) |
| 161 | + |
| 162 | + try: |
| 163 | + inverter.service_write_multiple_holding_registers( |
| 164 | + register=call.data.get(PARAM_REGISTER), |
| 165 | + values=call.data.get(PARAM_VALUES)) |
| 166 | + except Exception as e: |
| 167 | + raise ServiceValidationError( |
| 168 | + e, |
| 169 | + translation_domain=DOMAIN, |
| 170 | + translation_key="call_failed" |
| 171 | + ) |
| 172 | + |
47 | 173 | return
|
48 | 174 |
|
49 | 175 | hass.services.async_register(
|
50 |
| - DOMAIN, SERVICE_WRITE_REGISTER, write_holding_register, schema=SERVICE_WRITE_REGISTER_SCHEMA |
| 176 | + DOMAIN, SERVICE_READ_HOLDING_REGISTER, read_holding_register, schema=SERVICE_READ_REGISTER_SCHEMA, supports_response=SupportsResponse.OPTIONAL |
| 177 | + ) |
| 178 | + |
| 179 | + hass.services.async_register( |
| 180 | + DOMAIN, SERVICE_READ_MULTIPLE_HOLDING_REGISTERS, read_multiple_holding_registers, schema=SERVICE_READ_MULTIPLE_REGISTERS_SCHEMA, supports_response=SupportsResponse.OPTIONAL |
| 181 | + ) |
| 182 | + |
| 183 | + hass.services.async_register( |
| 184 | + DOMAIN, SERVICE_WRITE_HOLDING_REGISTER, write_holding_register, schema=SERVICE_WRITE_REGISTER_SCHEMA |
51 | 185 | )
|
52 | 186 |
|
53 | 187 | hass.services.async_register(
|
54 |
| - DOMAIN, SERVICE_WRITE_MULTIPLE_REGISTERS, write_multiple_holding_registers, schema=SERVICE_WRITE_MULTIPLE_REGISTERS_SCHEMA |
| 188 | + DOMAIN, SERVICE_WRITE_MULTIPLE_HOLDING_REGISTERS, write_multiple_holding_registers, schema=SERVICE_WRITE_MULTIPLE_REGISTERS_SCHEMA |
55 | 189 | )
|
56 | 190 | return
|
0 commit comments