Search in sources :

Example 1 with SensorInfo

use of me.retrodaredevil.solarthing.homeassistant.data.SensorInfo 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);
            }
        }
    });
}
Also used : FragmentedPacketGroup(me.retrodaredevil.solarthing.packets.collection.FragmentedPacketGroup) Call(retrofit2.Call) Packet(me.retrodaredevil.solarthing.packets.Packet) FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) AttributeBuilder(me.retrodaredevil.solarthing.homeassistant.data.AttributeBuilder) BinarySensorState(me.retrodaredevil.solarthing.homeassistant.data.BinarySensorState) BinarySensorState(me.retrodaredevil.solarthing.homeassistant.data.BinarySensorState) SensorState(me.retrodaredevil.solarthing.homeassistant.data.SensorState) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LatestFragmentedPacketGroupEnvironment(me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment) FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) SensorInfo(me.retrodaredevil.solarthing.homeassistant.data.SensorInfo)

Example 2 with SensorInfo

use of me.retrodaredevil.solarthing.homeassistant.data.SensorInfo 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());
}
Also used : Retrofit(retrofit2.Retrofit) HashMap(java.util.HashMap) SensorState(me.retrodaredevil.solarthing.homeassistant.data.SensorState) SensorInfo(me.retrodaredevil.solarthing.homeassistant.data.SensorInfo)

Aggregations

SensorInfo (me.retrodaredevil.solarthing.homeassistant.data.SensorInfo)2 SensorState (me.retrodaredevil.solarthing.homeassistant.data.SensorState)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LatestFragmentedPacketGroupEnvironment (me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment)1 AttributeBuilder (me.retrodaredevil.solarthing.homeassistant.data.AttributeBuilder)1 BinarySensorState (me.retrodaredevil.solarthing.homeassistant.data.BinarySensorState)1 Packet (me.retrodaredevil.solarthing.packets.Packet)1 FragmentedPacketGroup (me.retrodaredevil.solarthing.packets.collection.FragmentedPacketGroup)1 FXStatusPacket (me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket)1 Call (retrofit2.Call)1 Retrofit (retrofit2.Retrofit)1