Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dreed47 committed Jun 14, 2021
1 parent 61fa3f6 commit 56bcb52
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
12 changes: 8 additions & 4 deletions custom_components/redfin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ async def async_setup_entry(
hass_data[CONF_SCAN_INTERVAL] = entry.options[CONF_SCAN_INTERVAL]
hass_data[CONF_PROPERTIES] = entry.options[CONF_PROPERTIES]
# Registers update listener to update config entry when options are updated.
unsub_options_update_listener = entry.add_update_listener(options_update_listener)
unsub_options_update_listener = entry.add_update_listener(
options_update_listener)
# Store a reference to the unsubscribe function to cleanup if an entry is unloaded.
hass_data["unsub_options_update_listener"] = unsub_options_update_listener
hass.data[DOMAIN][entry.entry_id] = hass_data
Expand All @@ -42,11 +43,14 @@ async def async_setup_entry(
async def options_update_listener(
hass: core.HomeAssistant, config_entry: config_entries.ConfigEntry
):
_LOGGER.debug("%s options updated: %s", DOMAIN, config_entry.as_dict()["options"])
_LOGGER.debug("%s options updated: %s", DOMAIN,
config_entry.as_dict()["options"])
"""Handle options update."""
try:
result = await hass.config_entries.async_reload(config_entry.entry_id)
except config_entries.OperationNotAllowed:
_LOGGER.error("Entry cannot be reloaded. ID = %s Restart is required.", config_entry.entry_id)
_LOGGER.error(
"Entry cannot be reloaded. ID = %s Restart is required.", config_entry.entry_id)
except config_entries.UnknownEntry:
_LOGGER.error("Invalid entry specified. ID = %s", config_entry.entry_id)
_LOGGER.error("Invalid entry specified. ID = %s",
config_entry.entry_id)
9 changes: 6 additions & 3 deletions custom_components/redfin/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ async def async_step_property(self, user_input: Optional[Dict[str, Any]] = None)
return await self.async_step_property()

# User is done adding properties, create the config entry.
_LOGGER.debug("%s component added a new config entry: %s", DOMAIN, self.options)
_LOGGER.debug(
"%s component added a new config entry: %s", DOMAIN, self.options)
return self.async_create_entry(title=self.options["name"], data=self.data, options=self.options)

return self.async_show_form(
Expand Down Expand Up @@ -119,7 +120,8 @@ async def async_step_init(
property_map = {e.entity_id: e for e in entries}

if user_input is not None:
updated_properties = deepcopy(self.config_entry.options[CONF_PROPERTIES])
updated_properties = deepcopy(
self.config_entry.options[CONF_PROPERTIES])

# Remove any unchecked properties.
removed_entities = [
Expand All @@ -133,7 +135,8 @@ async def async_step_init(
# Remove from our configured properties.
entry = property_map[entity_id]
property_id = entry.unique_id
updated_properties = [e for e in updated_properties if e[CONF_PROPERTY_ID] != property_id]
updated_properties = [
e for e in updated_properties if e[CONF_PROPERTY_ID] != property_id]

if user_input.get(CONF_PROPERTY_ID):

Expand Down
1 change: 1 addition & 0 deletions custom_components/redfin/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
DEFAULT_NAME = "Redfin"

ATTRIBUTION = "Data provided by Redfin.com"
RESOURCE_URL = "https://www.redfin.com"
ATTR_AMOUNT = "amount"
ATTR_AMOUNT_FORMATTED = "amount_formatted"
ATTR_ADDRESS = "address"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/redfin/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"codeowners": ["@dreed47"],
"requirements": ["redfin==0.1.1"],
"iot_class": "cloud_polling",
"version": "1.1.3",
"version": "1.1.4",
"config_flow": true
}
23 changes: 9 additions & 14 deletions custom_components/redfin/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@
from redfin import Redfin
from homeassistant import config_entries, core

from .const import (DEFAULT_NAME, DOMAIN, CONF_PROPERTIES, ATTRIBUTION, DEFAULT_SCAN_INTERVAL,
CONF_PROPERTY_IDS, ICON, CONF_PROPERTY_ID, ATTR_AMOUNT, ATTR_AMOUNT_FORMATTED,
ATTR_ADDRESS, ATTR_FULL_ADDRESS, ATTR_CURRENCY, ATTR_STREET_VIEW, ATTR_REDFIN_URL,
ATTR_UNIT_OF_MEASUREMENT)
from .const import (DEFAULT_NAME, DOMAIN, CONF_PROPERTIES, ATTRIBUTION, DEFAULT_SCAN_INTERVAL, CONF_PROPERTY_IDS,
ICON, CONF_PROPERTY_ID, ATTR_AMOUNT, ATTR_AMOUNT_FORMATTED, ATTR_ADDRESS, ATTR_FULL_ADDRESS,
ATTR_CURRENCY, ATTR_STREET_VIEW, ATTR_REDFIN_URL, RESOURCE_URL, ATTR_UNIT_OF_MEASUREMENT)
from homeassistant.core import callback
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.const import ATTR_ATTRIBUTION, CONF_NAME, CONF_SCAN_INTERVAL
import homeassistant.helpers.config_validation as cv

_LOGGER = logging.getLogger(__name__)
_RESOURCE = "https://www.redfin.com"


PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
Expand Down Expand Up @@ -71,7 +69,7 @@ def name(self):
def state(self):
"""Return the state of the sensor."""
try:
#return self._state
# return self._state
return round(float(self._state), 1)
except ValueError:
return None
Expand All @@ -82,9 +80,6 @@ def extra_state_attributes(self):
attributes = {}
if self.data is not None:
attributes = self.data
attributes[ATTR_ADDRESS] = self.address
attributes[ATTR_ATTRIBUTION] = ATTRIBUTION
attributes[CONF_PROPERTY_ID] = self.params[CONF_PROPERTY_ID]
return attributes

@property
Expand Down Expand Up @@ -112,7 +107,7 @@ def update(self):
_LOGGER.error("The API returned: %s",
avm_details["errorMessage"])
except:
_LOGGER.error("Unable to retrieve data from %s", _RESOURCE)
_LOGGER.error("Unable to retrieve data from %s", RESOURCE_URL)
return
_LOGGER.debug("%s - The avm_details API returned: %s for property id: %s",
self._name, avm_details["errorMessage"], self.params[CONF_PROPERTY_ID])
Expand All @@ -124,7 +119,7 @@ def update(self):
_LOGGER.error("The API returned: %s",
above_the_fold["errorMessage"])
except:
_LOGGER.error("Unable to retrieve data from %s", _RESOURCE)
_LOGGER.error("Unable to retrieve data from %s", RESOURCE_URL)
return
_LOGGER.debug("%s - The above_the_fold API returned: %s for property id: %s",
self._name, above_the_fold["errorMessage"], self.params[CONF_PROPERTY_ID])
Expand All @@ -135,13 +130,13 @@ def update(self):
_LOGGER.error("The API returned: %s",
info_panel["errorMessage"])
except:
_LOGGER.error("Unable to retrieve data from %s", _RESOURCE)
_LOGGER.error("Unable to retrieve data from %s", RESOURCE_URL)
return
_LOGGER.debug("%s - The info_panel API returned: %s for property id: %s",
self._name, info_panel["errorMessage"], self.params[CONF_PROPERTY_ID])

if 'url' in info_panel["payload"]["mainHouseInfo"]:
redfinUrl = _RESOURCE + \
redfinUrl = RESOURCE_URL + \
info_panel["payload"]["mainHouseInfo"]['url']
else:
redfinUrl = 'Not Set'
Expand Down Expand Up @@ -181,12 +176,12 @@ def update(self):
details[ATTR_REDFIN_URL] = redfinUrl
details[ATTR_STREET_VIEW] = streetViewUrl
details[CONF_PROPERTY_ID] = self.params[CONF_PROPERTY_ID]
details[ATTR_ATTRIBUTION] = ATTRIBUTION

self.data = details

if self.data is not None:
self._state = self.data[ATTR_AMOUNT]
self.property_id = self.params[CONF_PROPERTY_ID]
else:
self._state = None
_LOGGER.error("Unable to get Redfin estimate from response")

0 comments on commit 56bcb52

Please sign in to comment.