Skip to content

Commit

Permalink
feat: Signed sensor can be defined as inverted
Browse files Browse the repository at this point in the history
feat: Entity ID can be set manually
  • Loading branch information
davidrapan committed Jul 16, 2024
1 parent 4cae460 commit a9572cb
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ parameters:
"Battery Grid Charging Current",
]

# Battery - The power of battery is S16bit (low 16 bits | L:1W, H:10W)
# Battery - The power of battery is S16bit
- name: "Battery Power"
class: "power"
state_class: "measurement"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ parameters:
"Battery Grid Charging Current",
]

# Battery - The power of battery is S16bit (low 16 bits | L:1W, H:10W)
# Battery - The power of battery is S16bit
- name: "Battery Power"
class: "power"
state_class: "measurement"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ parameters:
"Battery Grid Charging Current",
]

# Battery - The power of battery is S16bit (low 16 bits | L:1W, H:10W)
# Battery - The power of battery is S16bit
- name: "Battery Power"
class: "power"
state_class: "measurement"
Expand All @@ -1006,6 +1006,17 @@ parameters:
rule: 2
registers: [0x024E]

# Battery - The inverted power of battery is S16bit
- name: "Battery Power ∇"
entity_id: "battery_power_inverted"
class: "power"
state_class: "measurement"
uom: "W"
scale: 1
rule: 2
inverted: True
registers: [0x024E]

# Battery - The state of health
- name: "Battery SOH"
class: ""
Expand Down Expand Up @@ -1275,6 +1286,20 @@ parameters:
icon: "mdi:transmission-tower"
attributes: ["Zero Export"]

# Grid - The inverted power is S16bit (low 16 bits) + S16bit (high 16 bits)
- name: "Grid Power ∇"
entity_id: "grid_power_inverted"
realtime:
class: "measurement"
state_class: "measurement"
uom: "W"
scale: 1
rule: 4
inverted: True
registers: [0x0271, 0x02B2]
icon: "mdi:transmission-tower"
attributes: ["Zero Export"]

- group: Inverter
items:
# Inverter output - The voltage of phase A
Expand Down
3 changes: 3 additions & 0 deletions custom_components/solarman/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ def try_parse_signed(self, rawData, definition, start, length):
value = self._read_registers_signed(rawData, definition, start, length)

if value is not None:
if "inverted" in definition and definition["inverted"]:
value = -value

if "validation" in definition:
if not self.do_validate(key, value, definition["validation"]):
return
Expand Down
16 changes: 11 additions & 5 deletions custom_components/solarman/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,24 @@ class SolarmanStatus(SolarmanCoordinatorEntity):
def __init__(self, coordinator, sensor):
super().__init__(coordinator)
self.sensor_name = sensor["name"]
self.sensor_entity_id = sensor["entity_id"] if "entity_id" in sensor else None
self.sensor_unique_id = self.sensor_entity_id if self.sensor_entity_id else self.sensor_name

# Return the category of the sensor.
# Set the category of the sensor.
self._attr_entity_category = (EntityCategory.DIAGNOSTIC)

# Return the icon of the sensor.
# Set the icon of the sensor.
self._attr_icon = "mdi:information"

# Return the name of the sensor.
# Set the name of the sensor.
self._attr_name = "{} {}".format(self.coordinator.inverter.name, self.sensor_name)

# Return a unique_id based on the serial number
self._attr_unique_id = "{}_{}_{}".format(self.coordinator.inverter.name, self.coordinator.inverter.serial, self.sensor_name)
# Set the entity_id of the sensor.
if self.sensor_entity_id:
self.entity_id = "sensor.{}_{}".format(self.coordinator.inverter.name, self.sensor_entity_id)

# Set a unique_id based on the serial number
self._attr_unique_id = "{}_{}_{}".format(self.coordinator.inverter.name, self.coordinator.inverter.serial, self.sensor_unique_id)

@property
def available(self) -> bool:
Expand Down

0 comments on commit a9572cb

Please sign in to comment.