Skip to content

Commit da944a1

Browse files
committed
Add equipment sensor
Schneider develop some "energy management" features, all these feature are based on équipment. that why I create equipment sensors
1 parent c9a1cb5 commit da944a1

File tree

2 files changed

+218
-8
lines changed

2 files changed

+218
-8
lines changed

custom_components/wiser/helpers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,17 @@ def get_config_entry_id_by_name(hass: HomeAssistant, name) -> str or None:
153153
if entry:
154154
return entry[0].entry_id
155155
return None
156+
157+
# added by LGO for equipment management
158+
def get_equipment_name(data, equipment_id):
159+
"""Get the name of the equipment based on its ID."""
160+
equipment = data.wiserhub.equipments.get_equip_by_id(equipment_id)
161+
if equipment:
162+
return f"{ENTITY_PREFIX} {equipment.name}"
163+
return f"{ENTITY_PREFIX} Equipment {equipment_id}" # Fallback if no name found
164+
165+
def get_equipment_identifier(data, equipment_id, device_type):
166+
return (
167+
f"{data.wiserhub.system.name} {get_device_name(data, equipment_id, device_type)}"
168+
)
169+

custom_components/wiser/sensor.py

Lines changed: 204 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@
3838
DOMAIN,
3939
HOT_WATER,
4040
MANUFACTURER,
41+
MANUFACTURER_SCHNEIDER,
4142
SIGNAL_STRENGTH_ICONS,
4243
VERSION,
4344
)
44-
from .helpers import get_device_name, get_unique_id, get_identifier
45+
from .helpers import get_device_name, get_unique_id, get_identifier, get_equipment_identifier,get_equipment_name
4546

4647
_LOGGER = logging.getLogger(__name__)
4748

@@ -126,12 +127,29 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie
126127
if data.wiserhub.devices.smartplugs:
127128
_LOGGER.debug("Setting up Smart Plug power sensors")
128129
for smartplug in data.wiserhub.devices.smartplugs.all:
129-
wiser_sensors.extend(
130+
# Add a sensor equipment for smartplugs
131+
# Hub V2 features
132+
if smartplug.equipment_id > 0:
133+
wiser_sensors.extend(
130134
[
135+
WiserLTSPowerSensor(data, smartplug.id, sensor_type="Power", name="Equipment Power"),
136+
WiserLTSPowerSensor(data, smartplug.id, sensor_type="Energy", name="Equipment Energy Delivered"),
137+
WiserLTSPowerSensor(data, smartplug.id, sensor_type="Energy", name="Equipment Total Energy"),
138+
WiserCurrentVoltageSensor(data, smartplug.id, sensor_type="Current"),
139+
# to preserve the backward compatility
131140
WiserSmartplugPower(data, smartplug.id, sensor_type="Power"),
132-
WiserSmartplugPower(data, smartplug.id, sensor_type="Total Power"),
141+
WiserSmartplugPower(data, smartplug.id, sensor_type="Total Power"),
142+
133143
]
134-
)
144+
)
145+
else:
146+
# Hub V1 features
147+
wiser_sensors.extend(
148+
[
149+
WiserSmartplugPower(data, smartplug.id, sensor_type="Power"),
150+
WiserSmartplugPower(data, smartplug.id, sensor_type="Total Power"),
151+
]
152+
)
135153

136154
# Add power sensors for PTE (v2Hub)
137155
if data.wiserhub.devices.power_tags:
@@ -159,7 +177,12 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie
159177
WiserCurrentVoltageSensor(
160178
data, power_tag.id, sensor_type="Current"
161179
),
162-
]
180+
WiserLTSPowerSensor(
181+
data,
182+
power_tag.id,
183+
sensor_type="Energy",
184+
name="Total Energy"),
185+
]
163186
)
164187

165188
# Add LTS sensors - for room temp and target temp
@@ -189,18 +212,35 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie
189212
for device in data.wiserhub.devices.smokealarms.all
190213
)
191214

192-
# Add LTS sensors - for room Power and Energy for heating actuators
215+
# Add LTS sensors - for Power and Energy for heating actuators
193216
if data.wiserhub.devices.heating_actuators:
194217
_LOGGER.debug("Setting up Heating Actuator LTS sensors")
195218
for heating_actuator in data.wiserhub.devices.heating_actuators.all:
196-
wiser_sensors.extend(
219+
# Add a sensor equipment for heating actuators
220+
# Hub V2 features
221+
if heating_actuator.equipment_id > 0:
222+
wiser_sensors.extend(
223+
[
224+
WiserLTSPowerSensor(data, heating_actuator.id, sensor_type="Power", name="Equipment Power"),
225+
WiserLTSPowerSensor(data, heating_actuator.id, sensor_type="Energy", name="Equipment Energy Delivered"),
226+
WiserLTSPowerSensor(data, heating_actuator.id, sensor_type="Energy", name="Equipment Total Energy"),
227+
# to preserve the backward compatility
228+
WiserLTSPowerSensor(data, heating_actuator.id, sensor_type="Power"),
229+
WiserLTSPowerSensor(data, heating_actuator.id, sensor_type="Energy"),
230+
]
231+
)
232+
else:
233+
# Hub V1 features
234+
wiser_sensors.extend(
197235
[
198236
WiserLTSPowerSensor(data, heating_actuator.id, sensor_type="Power"),
199237
WiserLTSPowerSensor(
200238
data, heating_actuator.id, sensor_type="Energy"
201239
),
202240
]
203-
)
241+
)
242+
# Add a sensor floor temperature
243+
204244
if (
205245
heating_actuator.floor_temperature_sensor
206246
and heating_actuator.floor_temperature_sensor.sensor_type
@@ -213,6 +253,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie
213253
)
214254
)
215255

256+
216257
# Add heating channels demand
217258
for channel in data.wiserhub.heating_channels.all:
218259
_LOGGER.debug("Setting up Heating Demand LTS sensors")
@@ -237,6 +278,13 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie
237278
),
238279
]
239280
)
281+
# Add Equipment sensors
282+
if data.wiserhub.equipments:
283+
_LOGGER.debug(f"Wiserhub Equipment Collection NB {data.wiserhub.equipments.count} ")
284+
for equipment in data.wiserhub.equipments.all:
285+
if equipment.name :
286+
wiser_sensors.append( WiserEquipmentSensor(data, equipment.id,equipment.name) )
287+
240288

241289
async_add_entities(wiser_sensors, True)
242290

@@ -1617,3 +1665,151 @@ def name(self):
16171665
def native_unit_of_measurement(self) -> str:
16181666
"""Return the unit this state is expressed in."""
16191667
return PERCENTAGE
1668+
1669+
1670+
class WiserEquipmentSensor(WiserSensor):
1671+
"""Definition of Wiser Equipment Sensor."""
1672+
1673+
def __init__(self, data, equipment_id=0, sensor_type="") -> None:
1674+
"""Initialise the device sensor."""
1675+
self._equipment_id = equipment_id
1676+
self._sensor_type = sensor_type
1677+
super().__init__(data, equipment_id, sensor_type)
1678+
if self._equipment_id == 0:
1679+
self._equipment = self._data.wiserhub.system
1680+
else:
1681+
self._equipment = self._data.wiserhub.equipments.get_equip_by_id(self._equipment_id)
1682+
1683+
@callback
1684+
def _handle_coordinator_update(self) -> None:
1685+
"""Fetch new state data for the sensor."""
1686+
super()._handle_coordinator_update()
1687+
if self._equipment_id == 0:
1688+
self._equipment = self._data.wiserhub.system
1689+
else:
1690+
self._equipment = self._data.wiserhub.equipments.get_equip_by_id(self._equipment_id)
1691+
self._state = self._equipment.equipment.power.total_active_power
1692+
self.async_write_ha_state()
1693+
1694+
async def async_update(self) -> None:
1695+
"""Fetch new state data for the sensor."""
1696+
await super().async_update()
1697+
1698+
@property
1699+
def name(self):
1700+
"""Return the name of the sensor."""
1701+
return f"{get_equipment_name(self._data, self._equipment_id)} Equipment"
1702+
1703+
1704+
@property
1705+
def icon(self):
1706+
"""Return icon."""
1707+
return "mdi:home-lightning-bolt"
1708+
1709+
@property
1710+
def state(self) -> float:
1711+
"""Return the state of the entity."""
1712+
return self._equipment.equipment.power.total_active_power
1713+
1714+
@property
1715+
def native_unit_of_measurement(self) -> str:
1716+
"""Return the unit this state is expressed in."""
1717+
return UnitOfPower.WATT
1718+
1719+
@property
1720+
def device_info(self):
1721+
"""Return device specific attributes."""
1722+
return {
1723+
"name": get_device_name(self._data, self._equipment.equipment.device_id),
1724+
"identifiers": {(DOMAIN, get_identifier(self._data, self._equipment.equipment.device_id))},
1725+
"manufacturer": MANUFACTURER_SCHNEIDER,
1726+
# "model": self._device.product_type,
1727+
# "sw_version": self._device.firmware_version,
1728+
"via_device": (DOMAIN, self._data.wiserhub.system.name),
1729+
}
1730+
@property
1731+
def extra_state_attributes(self):
1732+
"""Return device state attributes."""
1733+
attrs = {}
1734+
# dev = self._equipment.equipment to facilitate the grasping of objects
1735+
dev = self._equipment.equipment
1736+
1737+
# Device identification
1738+
attrs["name"] = self._equipment.name
1739+
attrs["device_type"] = dev.device_type
1740+
attrs["device_application_instance_id"] = self._equipment.device_application_instance_id
1741+
1742+
attrs["family"] = dev.equipment_family
1743+
attrs["installation_type"] = dev.installation_type
1744+
attrs["number_of_phases"] = dev.number_of_phases
1745+
attrs["direction"] = dev.direction
1746+
1747+
attrs["application_instance_type"] = self._equipment.device_application_instance_type
1748+
attrs["equipment_id"] = dev.id
1749+
attrs["equipment_device_id"] = dev.device_id
1750+
1751+
attrs["equipment_UUID"] = dev.uuid
1752+
if dev.device_type not in ["PTE","PowerTagE",]:
1753+
attrs["functional_control_mode"] = dev.functional_control_mode
1754+
# SmartPlug attributes
1755+
if dev.device_type in ["SmartPlug"]:
1756+
attrs["functional_control_mode"] = dev.functional_control_mode
1757+
1758+
attrs["current_control_mode"] = dev.current_control_mode
1759+
1760+
# equipment capabilities
1761+
attrs["controllable"] = dev.controllable
1762+
attrs["cloud_managed"] = dev.cloud_managed
1763+
attrs["monitored"] = dev.monitored
1764+
attrs["smart_compatible"] = dev.smart_compatible
1765+
attrs["smart_supported"] = dev.smart_supported
1766+
attrs["can_be_scheduled"] = dev.can_be_scheduled
1767+
attrs["onoff_green_schedule_supported"] = dev.onoff_green_schedule_supported
1768+
attrs["onoff_cost_schedule_supported"] = dev.onoff_cost_schedule_supported
1769+
1770+
1771+
1772+
#PCM
1773+
if dev.device_type not in ["PTE","PowerTagE",]:
1774+
attrs["pcm_mode"] = dev.pcm_mode
1775+
attrs["pcm_supported"] = dev.pcm_supported
1776+
attrs["pcm_priority"] = dev.pcm_priority
1777+
1778+
1779+
# Equipment status
1780+
attrs["operating_status"] = dev.operating_status
1781+
attrs["fault_status"] = dev.fault_status
1782+
1783+
#Load and shedding
1784+
if dev.device_type not in ["PTE","PowerTagE",]:
1785+
attrs["load_state_status"] = dev.load_state_status
1786+
attrs["load_state_command_optimized"] = dev.load_state_command_optimized
1787+
attrs["load_shedding_status"] = dev.load_shedding_status
1788+
attrs["load_state_command_prio"] = dev.load_state_command_prio
1789+
attrs["load_setpoint_command_prio"] = dev.load_setpoint_command_prio
1790+
#Measures
1791+
attrs["active_power"] = dev.power.active_power
1792+
attrs["total_active_power"] = dev.power.total_active_power
1793+
attrs["energy"] = round(
1794+
dev.power.current_summation_delivered / 1000,
1795+
2,
1796+
)
1797+
1798+
# PowerTagE and SmartPlug attributes
1799+
if dev.device_type in ["PTE","PowerTagE","SmartPlug"]:
1800+
attrs["energy_delivered"] = dev.power.current_summation_delivered
1801+
attrs["pcm_mode"] = dev.pcm_mode
1802+
1803+
# PowerTagE attributes
1804+
if dev.device_type in ["PTE","PowerTagE",]:
1805+
attrs["rms_current"] = self._equipment.power.rms_current
1806+
attrs["rms_voltage"] = self._equipment.power.rms_voltage
1807+
attrs["energy_received"] = self._equipment.power.current_summation_received
1808+
1809+
attrs["grid_limit"] = self._equipment.grid_limit
1810+
attrs["grid_limit_Uom"] = self._equipment.grid_limit_uom
1811+
attrs["energy_export"] = self._equipment.energy_export
1812+
attrs["self_consumption"] = self._equipment.self_consumption
1813+
1814+
return attrs
1815+

0 commit comments

Comments
 (0)