Updates to poolsense integration (#37613)
* Created a binary sensor and corrected some review comments. * Updated the poolsense class and its interface to handle credentials better * Moved the client session to the PoolSense class. * Apply suggestions from code review * Update binary_sensor.py * Update homeassistant/components/poolsense/__init__.py * Update sensor.py * Update binary_sensor.py Co-authored-by: Chris Talkington <chris@talkingtontech.com>
This commit is contained in:
1 parent
1e9bc278e0
commit
8a2b34cc09
10 files changed
+173
-135
No files matched your search
@@ -7,13 +7,12 @@ from homeassistant.const import (
|
||||
DEVICE_CLASS_BATTERY,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
DEVICE_CLASS_TIMESTAMP,
|
||||
STATE_OK,
|
||||
STATE_PROBLEM,
|
||||
TEMP_CELSIUS,
|
||||
UNIT_PERCENTAGE,
|
||||
)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
|
||||
from . import PoolSenseEntity
|
||||
from .const import ATTRIBUTION, DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -28,7 +27,7 @@ SENSORS = {
|
||||
"pH": {"unit": None, "icon": "mdi:pool", "name": "pH", "device_class": None},
|
||||
"Battery": {
|
||||
"unit": UNIT_PERCENTAGE,
|
||||
"icon": "mdi:battery",
|
||||
"icon": None,
|
||||
"name": "Battery",
|
||||
"device_class": DEVICE_CLASS_BATTERY,
|
||||
},
|
||||
@@ -68,18 +67,6 @@ SENSORS = {
|
||||
"name": "pH Low",
|
||||
"device_class": None,
|
||||
},
|
||||
"pH Status": {
|
||||
"unit": None,
|
||||
"icon": "mdi:pool",
|
||||
"name": "pH Status",
|
||||
"device_class": None,
|
||||
},
|
||||
"Chlorine Status": {
|
||||
"unit": None,
|
||||
"icon": "mdi:pool",
|
||||
"name": "Chlorine Status",
|
||||
"device_class": None,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -87,53 +74,26 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Defer sensor setup to the shared sensor module."""
|
||||
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
||||
|
||||
async_add_entities(
|
||||
PoolSenseSensor(coordinator, config_entry.data[CONF_EMAIL], info_type)
|
||||
for info_type in SENSORS
|
||||
)
|
||||
sensors_list = []
|
||||
for sensor in SENSORS:
|
||||
sensors_list.append(
|
||||
PoolSenseSensor(coordinator, config_entry.data[CONF_EMAIL], sensor)
|
||||
)
|
||||
|
||||
async_add_entities(sensors_list, False)
|
||||
|
||||
|
||||
class PoolSenseSensor(Entity):
|
||||
class PoolSenseSensor(PoolSenseEntity, Entity):
|
||||
"""Sensor representing poolsense data."""
|
||||
|
||||
def __init__(self, coordinator, email, info_type):
|
||||
"""Initialize poolsense sensor."""
|
||||
self._email = email
|
||||
self._unique_id = f"{email}-{info_type}"
|
||||
self.coordinator = coordinator
|
||||
self.info_type = info_type
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return if sensor is available."""
|
||||
return self.coordinator.last_update_success
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return a unique ID to use for this sensor."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the particular component."""
|
||||
return f"PoolSense {SENSORS[self.info_type]['name']}"
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""Return False, updates are controlled via coordinator."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""State of the sensor."""
|
||||
if self.info_type == "pH Status":
|
||||
if self.coordinator.data[self.info_type] == "red":
|
||||
return STATE_PROBLEM
|
||||
return STATE_OK
|
||||
if self.info_type == "Chlorine Status":
|
||||
if self.coordinator.data[self.info_type] == "red":
|
||||
return STATE_PROBLEM
|
||||
return STATE_OK
|
||||
return self.coordinator.data[self.info_type]
|
||||
|
||||
@property
|
||||
@@ -144,14 +104,6 @@ class PoolSenseSensor(Entity):
|
||||
@property
|
||||
def icon(self):
|
||||
"""Return the icon."""
|
||||
if self.info_type == "pH Status":
|
||||
if self.coordinator.data[self.info_type] == "red":
|
||||
return "mdi:thumb-down"
|
||||
return "mdi:thumb-up"
|
||||
if self.info_type == "Chlorine Status":
|
||||
if self.coordinator.data[self.info_type] == "red":
|
||||
return "mdi:thumb-down"
|
||||
return "mdi:thumb-up"
|
||||
return SENSORS[self.info_type]["icon"]
|
||||
|
||||
@property
|
||||
@@ -163,13 +115,3 @@ class PoolSenseSensor(Entity):
|
||||
def device_state_attributes(self):
|
||||
"""Return device attributes."""
|
||||
return {ATTR_ATTRIBUTION: ATTRIBUTION}
|
||||
|
||||
async def async_update(self):
|
||||
"""Update status of sensor."""
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""When entity is added to hass."""
|
||||
self.async_on_remove(
|
||||
self.coordinator.async_add_listener(self.async_write_ha_state)
|
||||
)
|
||||
Reference in new issue
Block a user