]> git.basschouten.com Git - openhab-addons.git/blob
0e7b10bf69226f49c0043314db2effcca51b842c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.imperihome.internal.model.device;
14
15 import org.openhab.core.items.Item;
16 import org.openhab.core.library.types.DecimalType;
17 import org.openhab.core.types.State;
18 import org.openhab.io.imperihome.internal.model.param.NumericValueParam;
19 import org.openhab.io.imperihome.internal.model.param.ParamType;
20 import org.openhab.io.imperihome.internal.processor.ItemProcessor;
21
22 /**
23  * Rain sensor device.
24  *
25  * @author Pepijn de Geus - Initial contribution
26  */
27 public class RainDevice extends AbstractNumericValueDevice {
28
29     private static final String LINK_ACCUMULATION = "accum";
30
31     public RainDevice(Item item) {
32         super(DeviceType.RAIN, item, "mm/h");
33     }
34
35     @Override
36     public void updateParams() {
37         super.updateParams();
38
39         if (getLinks().containsKey(LINK_ACCUMULATION)) {
40             String deviceName = getLinks().get(LINK_ACCUMULATION);
41             String deviceId = ItemProcessor.getDeviceId(deviceName);
42             AbstractDevice accumDevice = getDeviceRegistry().getDevice(deviceId);
43             if (accumDevice == null) {
44                 logger.error("Couldn't resolve linked accumulation device '{}', make sure the Item has iss tags",
45                         deviceName);
46                 return;
47             }
48
49             NumericValueParam valueParam = (NumericValueParam) accumDevice.getParams().get(ParamType.GENERIC_VALUE);
50             if (valueParam == null) {
51                 logger.warn("Linked Accumulation device has no Value parameter: {}", accumDevice);
52                 return;
53             }
54
55             NumericValueParam accumParam = new NumericValueParam(ParamType.ACCUMULATION, valueParam.getUnit(), null);
56             String unit = accumParam.getUnit();
57             if (unit == null || unit.isBlank()) {
58                 accumParam.setUnit("mm");
59             }
60
61             accumParam.setValue(valueParam.getValue());
62             addParam(accumParam);
63         }
64     }
65
66     @Override
67     public void stateUpdated(Item item, State newState) {
68         super.stateUpdated(item, newState);
69
70         DecimalType value = item.getStateAs(DecimalType.class);
71         addParam(new NumericValueParam(ParamType.RAIN_VALUE, getUnit(), value));
72     }
73 }