1
mirror of https://github.com/home-assistant/core synced 2024-08-06 09:34:49 +02:00
ha-core/homeassistant/components/poolsense/sensor.py

173 lines
4.7 KiB
Python
Raw Normal View History

Create PoolSense integration (#35561) * Created integration for PoolSense - a device to maintain your pool * Updated poolsense integration with changes due to code review comments. * Update poolsense with lint fix (logging-not-lazy) * Update poolsense with lint fix (f string missing placeholders) * Update homeassistant/components/poolsense/config_flow.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/sensor.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/sensor.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/sensor.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/sensor.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/sensor.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/sensor.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/sensor.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/sensor.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/sensor.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/strings.json Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/strings.json Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/strings.json Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/strings.json Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Added test for poolsense component. Updated config_flow to better follow the guidelines. * Update tests/components/poolsense/test_config_flow.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update tests/components/poolsense/test_config_flow.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update tests/components/poolsense/test_config_flow.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update tests/components/poolsense/test_config_flow.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update tests/components/poolsense/test_config_flow.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update tests/components/poolsense/test_config_flow.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update tests/components/poolsense/test_config_flow.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update tests/components/poolsense/test_config_flow.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Removed uneccessary functions. * Added local venv to gitignore * Update homeassistant/components/poolsense/strings.json Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/strings.json Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/strings.json Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/strings.json Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/strings.json Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/strings.json Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update to strings to allow for translations. Also some coding convention updates. * Removed space in icon return Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Removed space in icon return Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Removed space in icon return Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/poolsense/strings.json Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Updated to include some error checks for pypi package * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review * Update tests/components/poolsense/test_config_flow.py * Update homeassistant/components/poolsense/sensor.py * Apply suggestions from code review * Update homeassistant/components/poolsense/__init__.py * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review * Update homeassistant/components/poolsense/sensor.py * Apply suggestions from code review * Update homeassistant/components/poolsense/__init__.py * Update homeassistant/components/poolsense/sensor.py Co-authored-by: Chris Talkington <chris@talkingtontech.com>
2020-07-01 07:44:10 +02:00
"""Sensor platform for the PoolSense sensor."""
import logging
from homeassistant.const import (
ATTR_ATTRIBUTION,
CONF_EMAIL,
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_TIMESTAMP,
STATE_OK,
STATE_PROBLEM,
TEMP_CELSIUS,
UNIT_PERCENTAGE,
)
from homeassistant.helpers.entity import Entity
from .const import ATTRIBUTION, DOMAIN
_LOGGER = logging.getLogger(__name__)
SENSORS = {
"Chlorine": {
"unit": "mV",
"icon": "mdi:pool",
"name": "Chlorine",
"device_class": None,
},
"pH": {"unit": None, "icon": "mdi:pool", "name": "pH", "device_class": None},
"Battery": {
"unit": UNIT_PERCENTAGE,
"icon": "mdi:battery",
"name": "Battery",
"device_class": DEVICE_CLASS_BATTERY,
},
"Water Temp": {
"unit": TEMP_CELSIUS,
"icon": "mdi:coolant-temperature",
"name": "Temperature",
"device_class": DEVICE_CLASS_TEMPERATURE,
},
"Last Seen": {
"unit": None,
"icon": "mdi:clock",
"name": "Last Seen",
"device_class": DEVICE_CLASS_TIMESTAMP,
},
"Chlorine High": {
"unit": "mV",
"icon": "mdi:pool",
"name": "Chlorine High",
"device_class": None,
},
"Chlorine Low": {
"unit": "mV",
"icon": "mdi:pool",
"name": "Chlorine Low",
"device_class": None,
},
"pH High": {
"unit": None,
"icon": "mdi:pool",
"name": "pH High",
"device_class": None,
},
"pH Low": {
"unit": None,
"icon": "mdi:pool",
"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,
},
}
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
)
class PoolSenseSensor(Entity):
"""Sensor representing poolsense data."""
unique_id = None
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 name(self):
"""Return the name of the particular component."""
return "PoolSense {}".format(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
def device_class(self):
"""Return the device class."""
return SENSORS[self.info_type]["device_class"]
@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
def unit_of_measurement(self):
"""Return unit of measurement."""
return SENSORS[self.info_type]["unit"]
@property
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)
)