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.""" """Update probability using Bayes' rule."""
numerator = prob_given_true * prior numerator = prob_given_true * prior
denominator = numerator + prob_given_false * (1 - prior) denominator = numerator + prob_given_false * (1 - prior)
probability = numerator / denominator return numerator / denominator
return probability
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): 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.""" """Return True if numeric condition is met."""
entity = entity_observation["entity_id"] entity = entity_observation["entity_id"]
should_trigger = condition.async_numeric_state( return condition.async_numeric_state(
self.hass, self.hass,
entity, entity,
entity_observation.get("below"), entity_observation.get("below"),
@ -254,26 +253,18 @@ class BayesianBinarySensor(BinarySensorEntity):
None, None,
entity_observation, entity_observation,
) )
return should_trigger
def _process_state(self, entity_observation): def _process_state(self, entity_observation):
"""Return True if state conditions are met.""" """Return True if state conditions are met."""
entity = entity_observation["entity_id"] entity = entity_observation["entity_id"]
should_trigger = condition.state( return condition.state(self.hass, entity, entity_observation.get("to_state"))
self.hass, entity, entity_observation.get("to_state")
)
return should_trigger
def _process_template(self, entity_observation): def _process_template(self, entity_observation):
"""Return True if template condition is True.""" """Return True if template condition is True."""
template = entity_observation.get(CONF_VALUE_TEMPLATE) template = entity_observation.get(CONF_VALUE_TEMPLATE)
template.hass = self.hass template.hass = self.hass
should_trigger = condition.async_template( return condition.async_template(self.hass, template, entity_observation)
self.hass, template, entity_observation
)
return should_trigger
@property @property
def name(self): def name(self):
@ -299,9 +290,9 @@ class BayesianBinarySensor(BinarySensorEntity):
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the sensor.""" """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 obs.copy() for obs in self.current_observations.values() if obs is not None
) ]
for item in attr_observations_list: for item in attr_observations_list:
item.pop("value_template", None) 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): def create_blebox_entities(product, async_add, entity_klass, entity_type):
"""Create entities from a BleBox product's features.""" """Create entities from a BleBox product's features."""
entities = [] entities = [entity_klass(feature) for feature in product.features[entity_type]]
for feature in product.features[entity_type]:
entities.append(entity_klass(feature))
async_add(entities, True) async_add(entities, True)

View file

@ -134,8 +134,6 @@ class DemoClimate(ClimateEntity):
self._support_flags = self._support_flags | SUPPORT_TARGET_HUMIDITY self._support_flags = self._support_flags | SUPPORT_TARGET_HUMIDITY
if swing_mode is not None: if swing_mode is not None:
self._support_flags = self._support_flags | SUPPORT_SWING_MODE 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: if aux is not None:
self._support_flags = self._support_flags | SUPPORT_AUX_HEAT self._support_flags = self._support_flags | SUPPORT_AUX_HEAT
if HVAC_MODE_HEAT_COOL in hvac_modes or HVAC_MODE_AUTO in hvac_modes: 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: else:
await self.data.update() await self.data.update()
self.thermostat = self.data.ecobee.get_thermostat(self.thermostat_index) 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 self._last_active_hvac_mode = self.hvac_mode
@property @property

View file

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

View file

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

View file

@ -144,14 +144,11 @@ class MaxCubeClimate(ClimateEntity):
"""Set new target hvac mode.""" """Set new target hvac mode."""
device = self._cubehandle.cube.device_by_rf(self._rf_address) device = self._cubehandle.cube.device_by_rf(self._rf_address)
temp = device.target_temperature temp = device.target_temperature
mode = device.mode mode = MAX_DEVICE_MODE_MANUAL
if hvac_mode == HVAC_MODE_OFF: if hvac_mode == HVAC_MODE_OFF:
temp = OFF_TEMPERATURE temp = OFF_TEMPERATURE
mode = MAX_DEVICE_MODE_MANUAL elif hvac_mode != HVAC_MODE_HEAT:
elif hvac_mode == HVAC_MODE_HEAT:
mode = MAX_DEVICE_MODE_MANUAL
else:
# Reset the temperature to a sane value. # Reset the temperature to a sane value.
# Ideally, we should send 0 and the device will set its # Ideally, we should send 0 and the device will set its
# temperature according to the schedule. However, current # 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): def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Modbus sensors.""" """Set up the Modbus sensors."""
sensors = [] sensors = []
data_types = {DATA_TYPE_INT: {1: "h", 2: "i", 4: "q"}} data_types = {
data_types[DATA_TYPE_UINT] = {1: "H", 2: "I", 4: "Q"} DATA_TYPE_INT: {1: "h", 2: "i", 4: "q"},
data_types[DATA_TYPE_FLOAT] = {1: "e", 2: "f", 4: "d"} DATA_TYPE_UINT: {1: "H", 2: "I", 4: "Q"},
DATA_TYPE_FLOAT: {1: "e", 2: "f", 4: "d"},
}
for register in config[CONF_REGISTERS]: for register in config[CONF_REGISTERS]:
structure = ">i" structure = ">i"

View file

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

View file

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

View file

@ -438,8 +438,7 @@ class SpeechManager:
album = provider.name album = provider.name
artist = language artist = language
if options is not None: if options is not None and options.get("voice") is not None:
if options.get("voice") is not None:
artist = options.get("voice") artist = options.get("voice")
try: try:

View file

@ -144,11 +144,7 @@ class Volumio(MediaPlayerEntity):
) )
return False return False
try:
return data return data
except AttributeError:
_LOGGER.error("Received invalid response: %s", data)
return False
async def async_update(self): async def async_update(self):
"""Update state.""" """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 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. 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._tv.sleep()
self._state = STATE_OFF self._state = STATE_OFF
def turn_on(self): def turn_on(self):
"""Wake the TV back up from sleep.""" """Wake the TV back up from sleep."""
if self._state is not STATE_ON: if self._state != STATE_ON:
self._tv.wake() self._tv.wake()
self._state = STATE_ON self._state = STATE_ON

View file

@ -133,7 +133,7 @@ class YamahaDevice(MediaPlayerEntity):
@property @property
def state(self): def state(self):
"""Return the state of the device.""" """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.status
return self.power return self.power