use of me.retrodaredevil.solarthing.homeassistant.data.SensorState 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);
}
}
});
}
use of me.retrodaredevil.solarthing.homeassistant.data.SensorState in project solarthing by wildmountainfarms.
the class UploadTest method testRetrofit.
private static void testRetrofit(OkHttpClient client) throws IOException {
Retrofit retrofit = HomeAssistantRetrofitUtil.defaultBuilder(new Retrofit.Builder(), "http://192.168.1.210:8123/api/").client(client).build();
HomeAssistantService service = retrofit.create(HomeAssistantService.class);
Map<String, String> attributes = new HashMap<>();
// this is required
attributes.put("friendly_name", "Cool Sensor");
Call<SensorInfo> call = service.updateSensor("solarthing_coolsensor", new SensorState("Charging", attributes));
Response<SensorInfo> result = call.execute();
System.out.println(result);
System.out.println(result.body());
}
Aggregations