|
| 1 | +"""Platform for sensor integration.""" |
| 2 | +from __future__ import annotations |
| 3 | +import logging |
| 4 | + |
| 5 | +from homeassistant.components.binary_sensor import ( |
| 6 | + ENTITY_ID_FORMAT, |
| 7 | + BinarySensorDeviceClass, |
| 8 | + BinarySensorEntity, |
| 9 | +) |
| 10 | +from homeassistant.core import HomeAssistant, callback |
| 11 | +from homeassistant.helpers.dispatcher import async_dispatcher_connect |
| 12 | +from homeassistant.helpers.entity_platform import AddEntitiesCallback |
| 13 | +from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType |
| 14 | +from homeassistant.util import slugify |
| 15 | + |
| 16 | +from . import UPDATE_TOPIC |
| 17 | +from .const import DOMAIN |
| 18 | + |
| 19 | +_LOGGER = logging.getLogger(__name__) |
| 20 | + |
| 21 | + |
| 22 | +class BinaryDeviceSensorConfig: |
| 23 | + """PH-803W Device Sensor configuration.""" |
| 24 | + |
| 25 | + def __init__( |
| 26 | + self, |
| 27 | + friendly_name, |
| 28 | + field, |
| 29 | + icon="mdi:gauge", |
| 30 | + unit_of_measurement=None, |
| 31 | + device_class=None, |
| 32 | + ): |
| 33 | + """Initialize configuration.""" |
| 34 | + self.device_class = device_class |
| 35 | + self.friendly_name = friendly_name |
| 36 | + self.field = field |
| 37 | + self.icon = icon |
| 38 | + self.unit_of_measurement = unit_of_measurement |
| 39 | + |
| 40 | + |
| 41 | +SENSORS = [ |
| 42 | + BinaryDeviceSensorConfig( |
| 43 | + "PH-803W In water", |
| 44 | + "in_water", |
| 45 | + "mdi:water-check", |
| 46 | + "", |
| 47 | + BinarySensorDeviceClass.CONNECTIVITY, |
| 48 | + ), |
| 49 | + BinaryDeviceSensorConfig( |
| 50 | + "PH-803W pH switch on", |
| 51 | + "ph_on", |
| 52 | + "mdi:water-plus", |
| 53 | + "", |
| 54 | + BinarySensorDeviceClass.RUNNING, |
| 55 | + ), |
| 56 | + BinaryDeviceSensorConfig( |
| 57 | + "PH-803W ORP switch on", |
| 58 | + "orp_on", |
| 59 | + "mdi:water-plus", |
| 60 | + "", |
| 61 | + BinarySensorDeviceClass.RUNNING, |
| 62 | + ), |
| 63 | +] |
| 64 | + |
| 65 | + |
| 66 | +def setup_platform( |
| 67 | + hass: HomeAssistant, |
| 68 | + config: ConfigType, |
| 69 | + add_entities: AddEntitiesCallback, |
| 70 | + discovery_info: DiscoveryInfoType | None = None, |
| 71 | +) -> None: |
| 72 | + """Set up the PH-803W sensor.""" |
| 73 | + if discovery_info is None: |
| 74 | + return |
| 75 | + |
| 76 | + sensors = [] |
| 77 | + device_data = hass.data[DOMAIN] |
| 78 | + for sconfig in SENSORS: |
| 79 | + sensors.append(DeviceSensor(device_data, sconfig)) |
| 80 | + |
| 81 | + add_entities(sensors) |
| 82 | + |
| 83 | + |
| 84 | +class DeviceSensor(BinarySensorEntity): |
| 85 | + """Implementing the Waterfurnace sensor.""" |
| 86 | + |
| 87 | + def __init__(self, device_data, config): |
| 88 | + """Initialize the sensor.""" |
| 89 | + self.device_data = device_data |
| 90 | + self._name = config.friendly_name |
| 91 | + self._attr = config.field |
| 92 | + self._state = None |
| 93 | + if self.device_data.device_client.get_latest_measurement() is not None: |
| 94 | + self._state = getattr( |
| 95 | + self.device_data.device_client.get_latest_measurement(), |
| 96 | + self._attr, |
| 97 | + None, |
| 98 | + ) |
| 99 | + self._icon = config.icon |
| 100 | + self._unit_of_measurement = config.unit_of_measurement |
| 101 | + self._attr_device_class = config.device_class |
| 102 | + |
| 103 | + # This ensures that the sensors are isolated per waterfurnace unit |
| 104 | + self.entity_id = ENTITY_ID_FORMAT.format( |
| 105 | + f"wf_{slugify(self.device_data.host)}_{slugify(self._attr)}" |
| 106 | + ) |
| 107 | + |
| 108 | + @property |
| 109 | + def name(self): |
| 110 | + """Return the name of the sensor.""" |
| 111 | + return self._name |
| 112 | + |
| 113 | + @property |
| 114 | + def device_info(self): |
| 115 | + """Return information to link this entity with the correct device.""" |
| 116 | + return { |
| 117 | + "identifiers": {(DOMAIN, self.device_data.device_client.passcode)}, |
| 118 | + "name": self.device_data.device_client.get_unique_name(), |
| 119 | + } |
| 120 | + |
| 121 | + @property |
| 122 | + def unique_id(self): |
| 123 | + """Return the sensor unique id.""" |
| 124 | + return self.device_data.device_client.passcode + self._attr |
| 125 | + |
| 126 | + @property |
| 127 | + def is_on(self): |
| 128 | + """Return the state of the sensor.""" |
| 129 | + return self._state |
| 130 | + |
| 131 | + @property |
| 132 | + def icon(self): |
| 133 | + """Return icon.""" |
| 134 | + return self._icon |
| 135 | + |
| 136 | + @property |
| 137 | + def native_unit_of_measurement(self): |
| 138 | + """Return the units of measurement.""" |
| 139 | + return self._unit_of_measurement |
| 140 | + |
| 141 | + @property |
| 142 | + def should_poll(self): |
| 143 | + """Return the polling state.""" |
| 144 | + return False |
| 145 | + |
| 146 | + async def async_added_to_hass(self): |
| 147 | + """Register callbacks.""" |
| 148 | + self.async_on_remove( |
| 149 | + async_dispatcher_connect( |
| 150 | + self.hass, UPDATE_TOPIC, self.async_update_callback |
| 151 | + ) |
| 152 | + ) |
| 153 | + |
| 154 | + @callback |
| 155 | + def async_update_callback(self): |
| 156 | + """Update state.""" |
| 157 | + if self.device_data.device_client.get_latest_measurement() is not None: |
| 158 | + self._state = getattr( |
| 159 | + self.device_data.device_client.get_latest_measurement(), |
| 160 | + self._attr, |
| 161 | + None, |
| 162 | + ) |
| 163 | + self.async_write_ha_state() |
0 commit comments