Skip to content

Commit

Permalink
Add inverter_serial_number to InverterResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
Darsstar committed Mar 29, 2024
1 parent e3f462a commit 420eeb5
Show file tree
Hide file tree
Showing 18 changed files with 192 additions and 59 deletions.
2 changes: 2 additions & 0 deletions solax/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from solax.inverter import Inverter
from solax.inverter_http_client import InverterHttpClient

__all__ = ("discover", "DiscoveryKeywords", "DiscoveryError")

if sys.version_info >= (3, 10):
from importlib.metadata import entry_points
else:
Expand Down
21 changes: 19 additions & 2 deletions solax/inverter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Dict, Tuple
from abc import abstractmethod
from typing import Any, Dict, Optional, Tuple

import aiohttp
import voluptuous as vol
Expand Down Expand Up @@ -33,7 +34,14 @@ def __init__(self, http_client: InverterHttpClient):

schema = type(self).schema()
response_decoder = type(self).response_decoder()
self.response_parser = ResponseParser(schema, response_decoder)
dongle_serial_number_getter = type(self).dongle_serial_number_getter
inverter_serial_number_getter = type(self).inverter_serial_number_getter
self.response_parser = ResponseParser(
schema,
response_decoder,
dongle_serial_number_getter,
inverter_serial_number_getter,
)

@classmethod
def _build(cls, host, port, pwd="", params_in_query=True):
Expand Down Expand Up @@ -103,5 +111,14 @@ def schema(cls) -> vol.Schema:
"""
return cls._schema

@classmethod
def dongle_serial_number_getter(cls, response: Dict[str, Any]) -> Optional[str]:
return response["sn"]

@classmethod
@abstractmethod
def inverter_serial_number_getter(cls, response: Dict[str, Any]) -> Optional[str]:
raise NotImplementedError # pragma: no cover

def __str__(self) -> str:
return f"{self.__class__.__name__}::{self.http_client}"
2 changes: 2 additions & 0 deletions solax/inverter_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import aiohttp

__all__ = ("InverterHttpClient", "Method")

if sys.version_info >= (3, 10):
from dataclasses import KW_ONLY

Expand Down
10 changes: 8 additions & 2 deletions solax/inverters/qvolt_hyb_g3_3p.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any, Dict, Optional

import voluptuous as vol

from solax.inverter import Inverter, InverterHttpClient
Expand Down Expand Up @@ -50,13 +52,13 @@ def __init__(self, http_client: InverterHttpClient, *args, **kwargs):
vol.Required("type"): vol.All(int, 14),
vol.Required("sn"): str,
vol.Required("ver"): str,
vol.Required("Data"): vol.Schema(
vol.Required("data"): vol.Schema(
vol.All(
[vol.Coerce(float)],
vol.Length(min=200, max=200),
)
),
vol.Required("Information"): vol.Schema(
vol.Required("information"): vol.Schema(
vol.All(vol.Length(min=10, max=10))
),
},
Expand Down Expand Up @@ -161,6 +163,10 @@ def response_decoder(cls):
# 170-199: always 0
}

@classmethod
def inverter_serial_number_getter(cls, response: Dict[str, Any]) -> Optional[str]:
return response["information"][2]

@classmethod
def build_all_variants(cls, host, port, pwd=""):
versions = [cls._build(host, port, pwd, False)]
Expand Down
12 changes: 9 additions & 3 deletions solax/inverters/x1.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any, Dict, Optional

import voluptuous as vol

from solax.inverter import Inverter
Expand All @@ -10,9 +12,9 @@ class X1(Inverter):
_schema = vol.Schema(
{
vol.Required("type"): vol.All(str, startswith("X1-")),
vol.Required("SN"): str,
vol.Required("sn"): str,
vol.Required("ver"): str,
vol.Required("Data"): vol.Schema(
vol.Required("data"): vol.Schema(
vol.All(
[vol.Coerce(float)],
vol.Any(
Expand All @@ -22,7 +24,7 @@ class X1(Inverter):
),
)
),
vol.Required("Information"): vol.Schema(vol.All(vol.Length(min=9, max=9))),
vol.Required("information"): vol.Schema(vol.All(vol.Length(min=9, max=9))),
},
extra=vol.REMOVE_EXTRA,
)
Expand Down Expand Up @@ -59,3 +61,7 @@ def response_decoder(cls):
}

# pylint: enable=duplicate-code

@classmethod
def inverter_serial_number_getter(cls, response: Dict[str, Any]) -> Optional[str]:
return response["information"][3]
10 changes: 8 additions & 2 deletions solax/inverters/x1_boost.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any, Dict, Optional

import voluptuous as vol

from solax.inverter import Inverter
Expand All @@ -19,13 +21,13 @@ class X1Boost(Inverter):
"sn",
): str,
vol.Required("ver"): str,
vol.Required("Data"): vol.Schema(
vol.Required("data"): vol.Schema(
vol.All(
[vol.Coerce(float)],
vol.Length(min=200, max=200),
)
),
vol.Required("Information"): vol.Schema(
vol.Required("information"): vol.Schema(
vol.All(vol.Length(min=10, max=10))
),
},
Expand Down Expand Up @@ -53,6 +55,10 @@ def response_decoder(cls):
"Total Import Energy": (pack_u16(52, 53), Total(Units.KWH), div100),
}

@classmethod
def inverter_serial_number_getter(cls, response: Dict[str, Any]) -> Optional[str]:
return response["information"][2]

@classmethod
def build_all_variants(cls, host, port, pwd=""):
versions = [
Expand Down
10 changes: 8 additions & 2 deletions solax/inverters/x1_hybrid_gen4.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any, Dict, Optional

import voluptuous as vol

from solax.inverter import Inverter
Expand All @@ -14,13 +16,13 @@ class X1HybridGen4(Inverter):
"sn",
): str,
vol.Required("ver"): str,
vol.Required("Data"): vol.Schema(
vol.Required("data"): vol.Schema(
vol.All(
[vol.Coerce(float)],
vol.Length(min=200, max=200),
)
),
vol.Required("Information"): vol.Schema(vol.All(vol.Length(min=9, max=10))),
vol.Required("information"): vol.Schema(vol.All(vol.Length(min=9, max=10))),
},
extra=vol.REMOVE_EXTRA,
)
Expand Down Expand Up @@ -54,3 +56,7 @@ def response_decoder(cls):
"Total feed-in energy": (pack_u16(34, 35), Total(Units.KWH), div100),
"Total consumption": (pack_u16(36, 37), Total(Units.KWH), div100),
}

@classmethod
def inverter_serial_number_getter(cls, response: Dict[str, Any]) -> Optional[str]:
return response["information"][2]
12 changes: 9 additions & 3 deletions solax/inverters/x1_mini.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any, Dict, Optional

import voluptuous as vol

from solax.inverter import Inverter
Expand All @@ -10,15 +12,15 @@ class X1Mini(Inverter):
_schema = vol.Schema(
{
vol.Required("type"): vol.All(str, startswith("X1-")),
vol.Required("SN"): str,
vol.Required("sn"): str,
vol.Required("ver"): str,
vol.Required("Data"): vol.Schema(
vol.Required("data"): vol.Schema(
vol.All(
[vol.Coerce(float)],
vol.Length(min=69, max=69),
)
),
vol.Required("Information"): vol.Schema(vol.All(vol.Length(min=9, max=9))),
vol.Required("information"): vol.Schema(vol.All(vol.Length(min=9, max=9))),
},
extra=vol.REMOVE_EXTRA,
)
Expand Down Expand Up @@ -46,3 +48,7 @@ def response_decoder(cls):
}

# pylint: enable=duplicate-code

@classmethod
def inverter_serial_number_getter(cls, response: Dict[str, Any]) -> Optional[str]:
return response["information"][3]
10 changes: 8 additions & 2 deletions solax/inverters/x1_mini_v34.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any, Dict, Optional

import voluptuous as vol

from solax.inverter import Inverter
Expand All @@ -22,7 +24,7 @@ class X1MiniV34(Inverter):
"sn",
): str,
vol.Required("ver"): str,
vol.Required("Data"): vol.Schema(
vol.Required("data"): vol.Schema(
vol.All(
[vol.Coerce(float)],
vol.Any(
Expand All @@ -32,7 +34,7 @@ class X1MiniV34(Inverter):
),
)
),
vol.Required("Information"): vol.Schema(
vol.Required("information"): vol.Schema(
vol.Any(vol.Length(min=9, max=9), vol.Length(min=10, max=10))
),
},
Expand Down Expand Up @@ -61,3 +63,7 @@ def response_decoder(cls):
}

# pylint: enable=duplicate-code

@classmethod
def inverter_serial_number_getter(cls, response: Dict[str, Any]) -> Optional[str]:
return response["information"][2]
10 changes: 8 additions & 2 deletions solax/inverters/x1_smart.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any, Dict, Optional

import voluptuous as vol

from solax.inverter import Inverter
Expand All @@ -19,13 +21,13 @@ class X1Smart(Inverter):
"sn",
): str,
vol.Required("ver"): str,
vol.Required("Data"): vol.Schema(
vol.Required("data"): vol.Schema(
vol.All(
[vol.Coerce(float)],
vol.Length(min=200, max=200),
)
),
vol.Required("Information"): vol.Schema(vol.All(vol.Length(min=8, max=8))),
vol.Required("information"): vol.Schema(vol.All(vol.Length(min=8, max=8))),
},
extra=vol.REMOVE_EXTRA,
)
Expand All @@ -51,6 +53,10 @@ def response_decoder(cls):
"Total Consumption": (52, Total(Units.KWH), div100),
}

@classmethod
def inverter_serial_number_getter(cls, response: Dict[str, Any]) -> Optional[str]:
return response["information"][2]

@classmethod
def build_all_variants(cls, host, port, pwd=""):
versions = [
Expand Down
14 changes: 10 additions & 4 deletions solax/inverters/x3.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any, Dict, Optional

import voluptuous as vol

from solax.inverter import Inverter
Expand All @@ -6,23 +8,23 @@


class X3(Inverter):
# pylint: disable=duplicate-code
_schema = vol.Schema(
{
vol.Required("type"): vol.All(str, startswith("X3-")),
vol.Required("SN"): str,
vol.Required("sn"): str,
vol.Required("ver"): str,
vol.Required("Data"): vol.Schema(
vol.Required("data"): vol.Schema(
vol.All(
[vol.Coerce(float)],
vol.Any(vol.Length(min=102, max=103), vol.Length(min=107, max=107)),
)
),
vol.Required("Information"): vol.Schema(vol.All(vol.Length(min=9, max=9))),
vol.Required("information"): vol.Schema(vol.All(vol.Length(min=9, max=9))),
},
extra=vol.REMOVE_EXTRA,
)

# pylint: disable=duplicate-code
@classmethod
def response_decoder(cls):
return {
Expand Down Expand Up @@ -61,3 +63,7 @@ def response_decoder(cls):
"EPS Power": (55, Units.W),
"EPS Frequency": (56, Units.HZ),
}

@classmethod
def inverter_serial_number_getter(cls, response: Dict[str, Any]) -> Optional[str]:
return response["information"][3]
10 changes: 8 additions & 2 deletions solax/inverters/x3_hybrid_g4.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any, Dict, Optional

import voluptuous as vol

from solax.inverter import Inverter
Expand All @@ -22,13 +24,13 @@ class X3HybridG4(Inverter):
vol.Required("type"): vol.All(int, 14),
vol.Required("sn"): str,
vol.Required("ver"): str,
vol.Required("Data"): vol.Schema(
vol.Required("data"): vol.Schema(
vol.All(
[vol.Coerce(float)],
vol.Length(min=300, max=300),
)
),
vol.Required("Information"): vol.Schema(
vol.Required("information"): vol.Schema(
vol.All(vol.Length(min=10, max=10))
),
},
Expand Down Expand Up @@ -133,3 +135,7 @@ def response_decoder(cls):
}

# pylint: enable=duplicate-code

@classmethod
def inverter_serial_number_getter(cls, response: Dict[str, Any]) -> Optional[str]:
return response["information"][2]
10 changes: 8 additions & 2 deletions solax/inverters/x3_mic_pro_g2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any, Dict, Optional

import voluptuous as vol

from solax.inverter import Inverter
Expand All @@ -14,13 +16,13 @@ class X3MicProG2(Inverter):
vol.Required("type"): vol.All(int, 16),
vol.Required("sn"): str,
vol.Required("ver"): str,
vol.Required("Data"): vol.Schema(
vol.Required("data"): vol.Schema(
vol.All(
[vol.Coerce(float)],
vol.Length(min=100, max=100),
)
),
vol.Required("Information"): vol.Schema(
vol.Required("information"): vol.Schema(
vol.All(vol.Length(min=10, max=10))
),
},
Expand Down Expand Up @@ -76,3 +78,7 @@ def response_decoder(cls):
}

# pylint: enable=duplicate-code

@classmethod
def inverter_serial_number_getter(cls, response: Dict[str, Any]) -> Optional[str]:
return response["information"][2]
Loading

0 comments on commit 420eeb5

Please sign in to comment.