Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PyFlume version, support for multiple state attributes #38138

Merged
merged 12 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions homeassistant/components/flume/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
DEFAULT_NAME = "Flume Sensor"

FLUME_TYPE_SENSOR = 2
FLUME_QUERIES_SENSOR = {
"current_interval": {"friendly_name": "Current", "unit_of_measurement": "gal/m"},
"month_to_date": {"friendly_name": "Current Month", "unit_of_measurement": "gal"},
"week_to_date": {"friendly_name": "Current Week", "unit_of_measurement": "gal"},
"today": {"friendly_name": "Current Day", "unit_of_measurement": "gal"},
"last_60_min": {"friendly_name": "60 Minutes", "unit_of_measurement": "gal/h"},
"last_24_hrs": {"friendly_name": "24 Hours", "unit_of_measurement": "gal/d"},
"last_30_days": {"friendly_name": "30 Days", "unit_of_measurement": "gal/mo"},
}

FLUME_AUTH = "flume_auth"
FLUME_HTTP_SESSION = "http_session"
Expand All @@ -20,3 +29,4 @@
KEY_DEVICE_ID = "id"
KEY_DEVICE_LOCATION = "location"
KEY_DEVICE_LOCATION_NAME = "name"
KEY_DEVICE_LOCATION_TIMEZONE = "tz"
2 changes: 1 addition & 1 deletion homeassistant/components/flume/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"domain": "flume",
"name": "Flume",
"documentation": "https://www.home-assistant.io/integrations/flume/",
"requirements": ["pyflume==0.4.0"],
"requirements": ["pyflume==0.5.5"],
"dependencies": [],
"codeowners": ["@ChrisMandich", "@bdraco"],
"config_flow": true
Expand Down
90 changes: 67 additions & 23 deletions homeassistant/components/flume/sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Sensor for displaying the number of result from Flume."""
from datetime import timedelta
import logging
from numbers import Number

from pyflume import FlumeData
import voluptuous as vol
Expand All @@ -24,10 +25,12 @@
FLUME_AUTH,
FLUME_DEVICES,
FLUME_HTTP_SESSION,
FLUME_QUERIES_SENSOR,
FLUME_TYPE_SENSOR,
KEY_DEVICE_ID,
KEY_DEVICE_LOCATION,
KEY_DEVICE_LOCATION_NAME,
KEY_DEVICE_LOCATION_TIMEZONE,
KEY_DEVICE_TYPE,
)

Expand All @@ -49,7 +52,6 @@

async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Import the platform into a config entry."""

hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=config
Expand All @@ -59,7 +61,6 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=

async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the Flume sensor."""

flume_domain_data = hass.data[DOMAIN][config_entry.entry_id]

flume_auth = flume_domain_data[FLUME_AUTH]
Expand All @@ -76,17 +77,28 @@ async def async_setup_entry(hass, config_entry, async_add_entities):

device_id = device[KEY_DEVICE_ID]
device_name = device[KEY_DEVICE_LOCATION][KEY_DEVICE_LOCATION_NAME]
device_timezone = device[KEY_DEVICE_LOCATION][KEY_DEVICE_LOCATION_TIMEZONE]
device_friendly_name = f"{name} {device_name}"
flume_device = FlumeData(
flume_auth,
device_id,
device_timezone,
SCAN_INTERVAL,
update_on_init=False,
http_session=http_session,
)
flume_entity_list.append(
FlumeSensor(flume_device, device_friendly_name, device_id)
)

flume_data = FlumeSensorData(flume_device)

for flume_query_sensor in FLUME_QUERIES_SENSOR.items():
flume_entity_list.append(
FlumeSensor(
flume_data,
flume_query_sensor,
f"{device_friendly_name} {flume_query_sensor[1]['friendly_name']}",
device_id,
)
)

if flume_entity_list:
async_add_entities(flume_entity_list)
Expand All @@ -95,13 +107,14 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class FlumeSensor(Entity):
"""Representation of the Flume sensor."""

def __init__(self, flume_device, name, device_id):
def __init__(self, flume_data, flume_query_sensor, name, device_id):
"""Initialize the Flume sensor."""
self._flume_device = flume_device
self._flume_data = flume_data
self._flume_query_sensor = flume_query_sensor
self._name = name
self._device_id = device_id
self._undo_track_sensor = None
self._available = False
self._available = self._flume_data.available
self._state = None

@property
Expand All @@ -128,7 +141,7 @@ def state(self):
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
# This is in gallons per SCAN_INTERVAL
return "gal/m"
return self._flume_query_sensor[1]["unit_of_measurement"]

@property
def available(self):
Expand All @@ -137,26 +150,57 @@ def available(self):

@property
def unique_id(self):
"""Device unique ID."""
return self._device_id
"""Flume query and Device unique ID."""
return f"{self._flume_query_sensor[0]}_{self._device_id}"

@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
"""Get the latest data and updates the states."""
_LOGGER.debug("Updating flume sensor: %s", self._name)
try:
self._flume_device.update_force()
except Exception as ex: # pylint: disable=broad-except
if self._available:
_LOGGER.error("Update of flume sensor %s failed: %s", self._name, ex)
self._available = False
return
_LOGGER.debug("Successful update of flume sensor: %s", self._name)
self._state = self._flume_device.value
self._available = True

def format_state_value(value):
return round(value, 1) if isinstance(value, Number) else None

self._flume_data.update()
self._state = format_state_value(
self._flume_data.flume_device.values[self._flume_query_sensor[0]]
)
_LOGGER.debug(
"Updating sensor: '%s', value: '%s'",
self._name,
self._flume_data.flume_device.values[self._flume_query_sensor[0]],
)
self._available = self._flume_data.available

async def async_added_to_hass(self):
"""Request an update when added."""
# We do ask for an update with async_add_entities()
# because it will update disabled entities
self.async_schedule_update_ha_state()


class FlumeSensorData:
"""Get the latest data and update the states."""

def __init__(self, flume_device):
"""Initialize the data object."""
self.flume_device = flume_device
self.available = True

@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
"""Get the latest data from the Flume."""
_LOGGER.debug("Updating Flume data")
try:
self.flume_device.update_force()
ChrisMandich marked this conversation as resolved.
Show resolved Hide resolved
except Exception as ex: # pylint: disable=broad-except
if self.available:
_LOGGER.error("Update of Flume data failed: %s", ex)
self.available = False
return
self.available = True
_LOGGER.debug(
"Flume update details: %s",
{
"values": self.flume_device.values,
"query_payload": self.flume_device.query_payload,
},
)
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1331,7 +1331,7 @@ pyflexit==0.3
pyflic-homeassistant==0.4.dev0

# homeassistant.components.flume
pyflume==0.4.0
pyflume==0.5.5

# homeassistant.components.flunearyou
pyflunearyou==1.0.7
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ pyeverlights==0.1.0
pyfido==2.1.1

# homeassistant.components.flume
pyflume==0.4.0
pyflume==0.5.5

# homeassistant.components.flunearyou
pyflunearyou==1.0.7
Expand Down