Small collection of random styling tweaks, fixes and Pythonism (#35390)

This commit is contained in:
Franck Nijhof 2020-05-09 00:10:17 +02:00 committed by GitHub
parent 3feb55a8e4
commit 4cf186a47e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 28 additions and 57 deletions

View file

@ -91,8 +91,7 @@ def update_probability(prior, prob_given_true, prob_given_false):
"""Update probability using Bayes' rule."""
numerator = prob_given_true * prior
denominator = numerator + prob_given_false * (1 - prior)
probability = numerator / denominator
return probability
return numerator / denominator
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
@ -246,7 +245,7 @@ class BayesianBinarySensor(BinarySensorEntity):
"""Return True if numeric condition is met."""
entity = entity_observation["entity_id"]
should_trigger = condition.async_numeric_state(
return condition.async_numeric_state(
self.hass,
entity,
entity_observation.get("below"),
@ -254,26 +253,18 @@ class BayesianBinarySensor(BinarySensorEntity):
None,
entity_observation,
)
return should_trigger
def _process_state(self, entity_observation):
"""Return True if state conditions are met."""
entity = entity_observation["entity_id"]
should_trigger = condition.state(
self.hass, entity, entity_observation.get("to_state")
)
return should_trigger
return condition.state(self.hass, entity, entity_observation.get("to_state"))
def _process_template(self, entity_observation):
"""Return True if template condition is True."""
template = entity_observation.get(CONF_VALUE_TEMPLATE)
template.hass = self.hass
should_trigger = condition.async_template(
self.hass, template, entity_observation
)
return should_trigger
return condition.async_template(self.hass, template, entity_observation)
@property
def name(self):
@ -299,9 +290,9 @@ class BayesianBinarySensor(BinarySensorEntity):
def device_state_attributes(self):
"""Return the state attributes of the sensor."""
attr_observations_list = list(
attr_observations_list = [
obs.copy() for obs in self.current_observations.values() if obs is not None
)
]
for item in attr_observations_list:
item.pop("value_template", None)

View file

@ -77,10 +77,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
def create_blebox_entities(product, async_add, entity_klass, entity_type):
"""Create entities from a BleBox product's features."""
entities = []
for feature in product.features[entity_type]:
entities.append(entity_klass(feature))
entities = [entity_klass(feature) for feature in product.features[entity_type]]
async_add(entities, True)

View file

@ -134,8 +134,6 @@ class DemoClimate(ClimateEntity):
self._support_flags = self._support_flags | SUPPORT_TARGET_HUMIDITY
if swing_mode is not None:
self._support_flags = self._support_flags | SUPPORT_SWING_MODE
if hvac_action is not None:
self._support_flags = self._support_flags
if aux is not None:
self._support_flags = self._support_flags | SUPPORT_AUX_HEAT
if HVAC_MODE_HEAT_COOL in hvac_modes or HVAC_MODE_AUTO in hvac_modes:

View file

@ -288,7 +288,7 @@ class Thermostat(ClimateEntity):
else:
await self.data.update()
self.thermostat = self.data.ecobee.get_thermostat(self.thermostat_index)
if self.hvac_mode is not HVAC_MODE_OFF:
if self.hvac_mode != HVAC_MODE_OFF:
self._last_active_hvac_mode = self.hvac_mode
@property

View file

@ -55,11 +55,8 @@ GOOGLE_SERVICE_ACCOUNT = vol.Schema(
def _check_report_state(data):
if data[CONF_REPORT_STATE]:
if CONF_SERVICE_ACCOUNT not in data:
raise vol.Invalid(
"If report state is enabled, a service account must exist"
)
if data[CONF_REPORT_STATE] and CONF_SERVICE_ACCOUNT not in data:
raise vol.Invalid("If report state is enabled, a service account must exist")
return data

View file

@ -116,12 +116,12 @@ class HorizonDevice(MediaPlayerEntity):
def turn_on(self):
"""Turn the device on."""
if self._state is STATE_OFF:
if self._state == STATE_OFF:
self._send_key(self._keys.POWER)
def turn_off(self):
"""Turn the device off."""
if self._state is not STATE_OFF:
if self._state != STATE_OFF:
self._send_key(self._keys.POWER)
def media_previous_track(self):

View file

@ -144,14 +144,11 @@ class MaxCubeClimate(ClimateEntity):
"""Set new target hvac mode."""
device = self._cubehandle.cube.device_by_rf(self._rf_address)
temp = device.target_temperature
mode = device.mode
mode = MAX_DEVICE_MODE_MANUAL
if hvac_mode == HVAC_MODE_OFF:
temp = OFF_TEMPERATURE
mode = MAX_DEVICE_MODE_MANUAL
elif hvac_mode == HVAC_MODE_HEAT:
mode = MAX_DEVICE_MODE_MANUAL
else:
elif hvac_mode != HVAC_MODE_HEAT:
# Reset the temperature to a sane value.
# Ideally, we should send 0 and the device will set its
# temperature according to the schedule. However, current

View file

@ -99,9 +99,11 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Modbus sensors."""
sensors = []
data_types = {DATA_TYPE_INT: {1: "h", 2: "i", 4: "q"}}
data_types[DATA_TYPE_UINT] = {1: "H", 2: "I", 4: "Q"}
data_types[DATA_TYPE_FLOAT] = {1: "e", 2: "f", 4: "d"}
data_types = {
DATA_TYPE_INT: {1: "h", 2: "i", 4: "q"},
DATA_TYPE_UINT: {1: "H", 2: "I", 4: "Q"},
DATA_TYPE_FLOAT: {1: "e", 2: "f", 4: "d"},
}
for register in config[CONF_REGISTERS]:
structure = ">i"

View file

@ -170,10 +170,8 @@ class ModbusCoilSwitch(ToggleEntity, RestoreEntity):
self._available = False
return
value = bool(result.bits[0])
self._available = True
return value
return bool(result.bits[0])
def _write_coil(self, coil, value):
"""Write coil using the Modbus hub slave."""
@ -288,10 +286,9 @@ class ModbusRegisterSwitch(ModbusCoilSwitch):
self._available = False
return
value = int(result.registers[0])
self._available = True
return value
return int(result.registers[0])
def _write_register(self, value):
"""Write holding register using the Modbus hub slave."""

View file

@ -325,9 +325,7 @@ def soco_error(errorcodes=None):
try:
return funct(*args, **kwargs)
except SoCoUPnPException as err:
if errorcodes and err.error_code in errorcodes:
pass
else:
if not errorcodes or err.error_code not in errorcodes:
_LOGGER.error("Error on %s with %s", funct.__name__, err)
except SoCoException as err:
_LOGGER.error("Error on %s with %s", funct.__name__, err)
@ -605,7 +603,6 @@ class SonosEntity(MediaPlayerEntity):
variables = event and event.variables
self.update_media_radio(variables, track_info)
else:
variables = event and event.variables
self.update_media_music(update_position, track_info)
self.schedule_update_ha_state()

View file

@ -438,9 +438,8 @@ class SpeechManager:
album = provider.name
artist = language
if options is not None:
if options.get("voice") is not None:
artist = options.get("voice")
if options is not None and options.get("voice") is not None:
artist = options.get("voice")
try:
tts_file = mutagen.File(data_bytes, easy=True)

View file

@ -144,11 +144,7 @@ class Volumio(MediaPlayerEntity):
)
return False
try:
return data
except AttributeError:
_LOGGER.error("Received invalid response: %s", data)
return False
return data
async def async_update(self):
"""Update state."""

View file

@ -87,14 +87,14 @@ class XiaomiTV(MediaPlayerEntity):
because the TV won't accept any input when turned off. Thus, the user
would be unable to turn the TV back on, unless it's done manually.
"""
if self._state is not STATE_OFF:
if self._state != STATE_OFF:
self._tv.sleep()
self._state = STATE_OFF
def turn_on(self):
"""Wake the TV back up from sleep."""
if self._state is not STATE_ON:
if self._state != STATE_ON:
self._tv.wake()
self._state = STATE_ON

View file

@ -133,7 +133,7 @@ class YamahaDevice(MediaPlayerEntity):
@property
def state(self):
"""Return the state of the device."""
if self.power == STATE_ON and self.status is not STATE_UNKNOWN:
if self.power == STATE_ON and self.status != STATE_UNKNOWN:
return self.status
return self.power