use of me.retrodaredevil.solarthing.homeassistant.data.AttributeBuilder in project solarthing by wildmountainfarms.
the class HomeAssistantActionNode method createAction.
@Override
public Action createAction(ActionEnvironment actionEnvironment) {
LatestFragmentedPacketGroupEnvironment latestFragmentedPacketGroupEnvironment = actionEnvironment.getInjectEnvironment().get(LatestFragmentedPacketGroupEnvironment.class);
return Actions.createRunOnce(() -> {
FragmentedPacketGroup packetGroup = latestFragmentedPacketGroupEnvironment.getFragmentedPacketGroupProvider().getPacketGroup();
if (packetGroup == null) {
LOGGER.warn("packetGroup is null!");
return;
}
List<Call<SensorInfo>> calls = new ArrayList<>();
for (Packet packet : packetGroup.getPackets()) {
int fragmentId = packetGroup.getFragmentId(packet);
String sourceId = packetGroup.getSourceId(packet);
String nameBase = "solarthing_" + sourceId + "_" + fragmentId + "_";
if (packet instanceof FXStatusPacket) {
FXStatusPacket fx = (FXStatusPacket) packet;
nameBase += "fx_" + fx.getAddress() + "_";
calls.add(service.updateSensor(nameBase + "acmode", new SensorState(fx.getACMode().getModeName(), new AttributeBuilder().friendlyName("AC Mode").build())));
calls.add(service.updateBinarySensor(nameBase + "acpresent", new BinarySensorState(fx.getACMode() != ACMode.NO_AC, new AttributeBuilder().friendlyName("AC Present").build())));
calls.add(service.updateSensor(nameBase + "batteryvoltage", new SensorState(FORMAT.format(fx.getBatteryVoltage()), new AttributeBuilder().friendlyName("Battery Voltage").build())));
}
}
for (Call<SensorInfo> call : calls) {
try {
// TODO move into a separate thread
Response<SensorInfo> response = call.execute();
if (!response.isSuccessful()) {
LOGGER.error("Unsuccessful response! " + requireNonNull(response.errorBody()).string());
}
} catch (IOException e) {
LOGGER.error("Home assistant error", e);
}
}
});
}
Aggregations