-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
Refactored to new global json saving and loading #10677
Changes from 2 commits
b69f547
69fb3ad
ecfb26c
e237739
79ba152
940a9c8
89366cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,4 +96,4 @@ docs/build | |
desktop.ini | ||
/home-assistant.pyproj | ||
/home-assistant.sln | ||
/.vs/home-assistant/v14 | ||
/.vs/* | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,14 +6,14 @@ | |
""" | ||
import json | ||
import logging | ||
import os | ||
from datetime import timedelta | ||
|
||
from homeassistant.components.fan import ( | ||
ATTR_SPEED, SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH, | ||
SUPPORT_SET_SPEED, FanEntity) | ||
from homeassistant.helpers.entity import ToggleEntity | ||
import homeassistant.util as util | ||
from homeassistant.util.json import load_json, save_json | ||
|
||
_CONFIGURING = {} | ||
_LOGGER = logging.getLogger(__name__) | ||
|
@@ -33,7 +33,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): | |
"""Set up the Insteon local fan platform.""" | ||
insteonhub = hass.data['insteon_local'] | ||
|
||
conf_fans = config_from_file(hass.config.path(INSTEON_LOCAL_FANS_CONF)) | ||
conf_fans = load_json(hass.config.path(INSTEON_LOCAL_FANS_CONF)) | ||
if conf_fans: | ||
for device_id in conf_fans: | ||
setup_fan(device_id, conf_fans[device_id], insteonhub, hass, | ||
|
@@ -88,11 +88,11 @@ def setup_fan(device_id, name, insteonhub, hass, add_devices_callback): | |
configurator.request_done(request_id) | ||
_LOGGER.info("Device configuration done!") | ||
|
||
conf_fans = config_from_file(hass.config.path(INSTEON_LOCAL_FANS_CONF)) | ||
conf_fans = load_json(hass.config.path(INSTEON_LOCAL_FANS_CONF)) | ||
if device_id not in conf_fans: | ||
conf_fans[device_id] = name | ||
|
||
if not config_from_file( | ||
if not save_json( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When will save json return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right, i looked at the new save_json and saw it returns False at the end, but didn't question the code itself. There were several components with some custom error logging if save failed, guess we can just get rid of all those occurrences. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
hass.config.path(INSTEON_LOCAL_FANS_CONF), | ||
conf_fans): | ||
_LOGGER.error("Failed to save configuration file") | ||
|
@@ -101,31 +101,6 @@ def setup_fan(device_id, name, insteonhub, hass, add_devices_callback): | |
add_devices_callback([InsteonLocalFanDevice(device, name)]) | ||
|
||
|
||
def config_from_file(filename, config=None): | ||
"""Small configuration file management function.""" | ||
if config: | ||
# We're writing configuration | ||
try: | ||
with open(filename, 'w') as fdesc: | ||
fdesc.write(json.dumps(config)) | ||
except IOError as error: | ||
_LOGGER.error('Saving config file failed: %s', error) | ||
return False | ||
return True | ||
else: | ||
# We're reading config | ||
if os.path.isfile(filename): | ||
try: | ||
with open(filename, 'r') as fdesc: | ||
return json.loads(fdesc.read()) | ||
except IOError as error: | ||
_LOGGER.error("Reading configuration file failed: %s", error) | ||
# This won't work yet | ||
return False | ||
else: | ||
return {} | ||
|
||
|
||
class InsteonLocalFanDevice(FanEntity): | ||
"""An abstract Class for an Insteon node.""" | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No newline at end of file.