Handle more file closing using context manager (#11942)
This commit is contained in:
parent
4cb1f93019
commit
0376cc0917
4 changed files with 12 additions and 11 deletions
|
@ -182,7 +182,8 @@ def check_pid(pid_file: str) -> None:
|
|||
"""Check that Home Assistant is not already running."""
|
||||
# Check pid file
|
||||
try:
|
||||
pid = int(open(pid_file, 'r').readline())
|
||||
with open(pid_file, 'r') as file:
|
||||
pid = int(file.readline())
|
||||
except IOError:
|
||||
# PID File does not exist
|
||||
return
|
||||
|
@ -204,7 +205,8 @@ def write_pid(pid_file: str) -> None:
|
|||
"""Create a PID File."""
|
||||
pid = os.getpid()
|
||||
try:
|
||||
open(pid_file, 'w').write(str(pid))
|
||||
with open(pid_file, 'w') as file:
|
||||
file.write(str(pid))
|
||||
except IOError:
|
||||
print('Fatal Error: Unable to write pid file {}'.format(pid_file))
|
||||
sys.exit(1)
|
||||
|
|
|
@ -38,18 +38,16 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||
tokenfile = hass.config.path('.greenwave')
|
||||
if config.get(CONF_VERSION) == 3:
|
||||
if os.path.exists(tokenfile):
|
||||
tokenfile = open(tokenfile)
|
||||
with open(tokenfile) as tokenfile:
|
||||
token = tokenfile.read()
|
||||
tokenfile.close()
|
||||
else:
|
||||
try:
|
||||
token = greenwave.grab_token(host, 'hass', 'homeassistant')
|
||||
except PermissionError:
|
||||
_LOGGER.error('The Gateway Is Not In Sync Mode')
|
||||
raise
|
||||
tokenfile = open(tokenfile, "w+")
|
||||
with open(tokenfile, "w+") as tokenfile:
|
||||
tokenfile.write(token)
|
||||
tokenfile.close()
|
||||
else:
|
||||
token = None
|
||||
bulbs = greenwave.grab_bulbs(host, token)
|
||||
|
|
|
@ -44,7 +44,8 @@ def get_service(hass, config, discovery_info=None):
|
|||
if config.get(CONF_APP_ICON) is None:
|
||||
icon_file = os.path.join(os.path.dirname(__file__), "..", "frontend",
|
||||
"www_static", "icons", "favicon-192x192.png")
|
||||
app_icon = open(icon_file, 'rb').read()
|
||||
with open(icon_file, 'rb') as file:
|
||||
app_icon = file.read()
|
||||
else:
|
||||
app_icon = config.get(CONF_APP_ICON)
|
||||
|
||||
|
|
|
@ -66,7 +66,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||
device_file, 'temperature'))
|
||||
else:
|
||||
for family_file_path in glob(os.path.join(base_dir, '*', 'family')):
|
||||
family_file = open(family_file_path, "r")
|
||||
with open(family_file_path, "r") as family_file:
|
||||
family = family_file.read()
|
||||
if family in DEVICE_SENSORS:
|
||||
for sensor_key, sensor_value in DEVICE_SENSORS[family].items():
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue